Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

112 lines
3.3KB

  1. //app.js
  2. App({
  3. onLaunch: function() {
  4. var baseInfo = wx.getStorageSync('laomenkuangBaseInfo');
  5. if (!baseInfo) {
  6. // 登录
  7. wx.login({
  8. success: res => {
  9. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  10. var that = this;
  11. wx.request({
  12. url: that.globalData.apiUrl + 'openid',
  13. header: {
  14. 'code': res.code,
  15. 'content-type': 'application/json' // 默认值
  16. },
  17. success(res) {
  18. res = res.data;
  19. console.log(res)
  20. if (res.code == 200) {
  21. that.globalData.baseInfo = res.data;
  22. wx.setStorageSync('laomenkuangBaseInfo', res.data)
  23. that.getInfo()
  24. } else {
  25. wx.showToast({
  26. title: res.message,
  27. icon: 'none',
  28. duration: 2000
  29. })
  30. }
  31. }
  32. })
  33. }
  34. })
  35. } else {
  36. this.globalData.baseInfo = baseInfo
  37. this.getInfo()
  38. }
  39. // 获取用户信息
  40. wx.getSetting({
  41. success: res => {
  42. if (res.authSetting['scope.userInfo']) {
  43. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  44. wx.getUserInfo({
  45. success: res => {
  46. // 可以将 res 发送给后台解码出 unionId
  47. // console.log(res.userInfo)
  48. this.globalData.userInfo = res.userInfo
  49. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  50. // 所以此处加入 callback 以防止这种情况
  51. if (this.userInfoReadyCallback) {
  52. this.userInfoReadyCallback(res)
  53. }
  54. }
  55. })
  56. }
  57. }
  58. })
  59. },
  60. getInfo: function() {
  61. this.requestGet('getinfo', "", res => {
  62. if (res.code == 200) {
  63. this.globalData.userState = 1;
  64. this.globalData.storeData=res.data;
  65. } else {
  66. this.globalData.userState = 0;
  67. }
  68. if (this.userStateReadyCallback) {
  69. this.userStateReadyCallback(this.globalData.userState)
  70. }
  71. })
  72. },
  73. requestGet: function(url, data, callBack) {
  74. wx.request({
  75. url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址
  76. data: data,
  77. header: {
  78. 'openid': this.globalData.baseInfo.openid,
  79. 'token': this.globalData.couponToken,
  80. 'content-type': 'application/json' // 默认值
  81. },
  82. success(res) {
  83. callBack(res.data)
  84. }
  85. })
  86. },
  87. requestPost: function(url, data, callBack) {
  88. wx.request({
  89. url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址
  90. data: data,
  91. header: {
  92. 'openid': this.globalData.baseInfo.openid,
  93. 'token': this.globalData.couponToken,
  94. 'content-type': 'application/x-www-form-urlencoded' // 默认值
  95. },
  96. method: "POST",
  97. success(res) {
  98. callBack(res.data)
  99. }
  100. })
  101. },
  102. globalData: {
  103. userInfo: null,
  104. userState: -1, //-1未获取 -0未注册 1已注册
  105. storeData:null,//店铺信息
  106. apiUrl: 'https://laomenkuang.jiyou-tech.com/apiWxAdmin/',
  107. baseInfo: null,
  108. couponData:null,//优惠券信息
  109. couponToken:null,//优惠券Token
  110. }
  111. })