You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.7KB

  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. if (res.code == 200) {
  20. that.globalData.baseInfo = res.data;
  21. wx.setStorageSync('laomenkuangBaseInfo', res.data)
  22. that.getInfo()
  23. } else {
  24. wx.showToast({
  25. title: res.message,
  26. icon: 'none',
  27. duration: 2000
  28. })
  29. }
  30. }
  31. })
  32. }
  33. })
  34. } else {
  35. this.globalData.baseInfo = baseInfo
  36. this.getInfo()
  37. }
  38. // 获取用户信息
  39. wx.getSetting({
  40. success: res => {
  41. if (res.authSetting['scope.userInfo']) {
  42. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  43. wx.getUserInfo({
  44. success: res => {
  45. // 可以将 res 发送给后台解码出 unionId
  46. // console.log(res.userInfo)
  47. this.globalData.userInfo = res.userInfo
  48. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  49. // 所以此处加入 callback 以防止这种情况
  50. if (this.userInfoReadyCallback) {
  51. this.userInfoReadyCallback(res)
  52. }
  53. }
  54. })
  55. }
  56. }
  57. })
  58. },
  59. setUserInfo(userInfo){
  60. userInfo.nickname = userInfo.nickName
  61. userInfo.headimgurl = userInfo.avatarUrl
  62. return userInfo
  63. },
  64. getInfo: function() {
  65. var accountInfo = wx.getStorageSync('laomenkuangAccountInfo');
  66. if (accountInfo){//已有账户信息-已登录
  67. this.globalData.userState = 1;
  68. this.globalData.storeData = accountInfo;
  69. if (this.userStateReadyCallback) {
  70. this.userStateReadyCallback(1)
  71. }
  72. var userInfo = wx.getStorageSync('laomenkuangUserInfo');
  73. if (userInfo){
  74. this.globalData.userInfo = userInfo;
  75. }
  76. }else{
  77. this.requestGet('getinfo', "", res => {
  78. if (res.code == 200) {
  79. this.globalData.userState = 1;
  80. wx.setStorageSync('laomenkuangAccountInfo', res.data);//老门框账户信息-id
  81. this.globalData.storeData = res.data;
  82. if (res.data.user_info) {
  83. res.data.user_info.nickName = res.data.user_info.nickname
  84. res.data.user_info.avatarUrl = res.data.user_info.headimgurl
  85. }
  86. wx.setStorageSync('laomenkuangUserInfo', res.data.user_info);//老门框用户信息-头像昵称
  87. this.globalData.userInfo = res.data.user_info;
  88. } else {
  89. this.globalData.userState = 0;
  90. }
  91. if (this.userStateReadyCallback) {
  92. this.userStateReadyCallback(this.globalData.userState)
  93. }
  94. })
  95. }
  96. },
  97. requestGet: function(url, data, callBack) {
  98. var that=this;
  99. wx.request({
  100. url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址
  101. data: data,
  102. header: {
  103. 'openid': this.globalData.baseInfo.openid,
  104. 'token': this.globalData.couponToken,
  105. 'content-type': 'application/json' // 默认值
  106. },
  107. success(res) {
  108. if(res.data.code==-1003){
  109. that.globalData.userState=0;
  110. wx.navigateTo({
  111. url: '/pages/login/login'
  112. })
  113. }else{
  114. callBack(res.data)
  115. }
  116. }
  117. })
  118. },
  119. requestPost: function(url, data, callBack) {
  120. var that=this;
  121. wx.request({
  122. url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址
  123. data: data,
  124. header: {
  125. 'openid': this.globalData.baseInfo.openid,
  126. 'token': this.globalData.couponToken,
  127. 'content-type': 'application/x-www-form-urlencoded' // 默认值
  128. },
  129. method: "POST",
  130. success(res) {
  131. if(res.data.code==-1003){
  132. that.globalData.userState=0;
  133. wx.navigateTo({
  134. url: '/pages/login/login'
  135. })
  136. }else{
  137. callBack(res.data)
  138. }
  139. }
  140. })
  141. },
  142. globalData: {
  143. userInfo: null,
  144. userState: -1, //-1未获取 0未注册 1已注册
  145. storeData:null,//店铺信息
  146. apiUrl: 'https://laomenkuang.jiyou-tech.com/apiWxAdmin/',
  147. baseInfo: null,
  148. couponData:null,//优惠券信息
  149. couponToken:null,//优惠券Token
  150. }
  151. })