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.

136 satır
4.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. 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. wx.request({
  99. url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址
  100. data: data,
  101. header: {
  102. 'openid': this.globalData.baseInfo.openid,
  103. 'token': this.globalData.couponToken,
  104. 'content-type': 'application/json' // 默认值
  105. },
  106. success(res) {
  107. callBack(res.data)
  108. }
  109. })
  110. },
  111. requestPost: function(url, data, callBack) {
  112. wx.request({
  113. url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址
  114. data: data,
  115. header: {
  116. 'openid': this.globalData.baseInfo.openid,
  117. 'token': this.globalData.couponToken,
  118. 'content-type': 'application/x-www-form-urlencoded' // 默认值
  119. },
  120. method: "POST",
  121. success(res) {
  122. callBack(res.data)
  123. }
  124. })
  125. },
  126. globalData: {
  127. userInfo: null,
  128. userState: -1, //-1未获取 0未注册 1已注册
  129. storeData:null,//店铺信息
  130. apiUrl: 'https://laomenkuang.jiyou-tech.com/apiWxAdmin/',
  131. baseInfo: null,
  132. couponData:null,//优惠券信息
  133. couponToken:null,//优惠券Token
  134. }
  135. })