Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

121 Zeilen
3.6KB

  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. this.requestGet('getinfo', "", res => {
  66. if (res.code == 200) {
  67. this.globalData.userState = 1;
  68. this.globalData.storeData=res.data;
  69. if (res.data.user_info){
  70. res.data.user_info.nickName = res.data.user_info.nickname
  71. res.data.user_info.avatarUrl = res.data.user_info.headimgurl
  72. }
  73. this.globalData.userInfo = res.data.user_info;
  74. } else {
  75. this.globalData.userState = 0;
  76. }
  77. if (this.userStateReadyCallback) {
  78. this.userStateReadyCallback(this.globalData.userState)
  79. }
  80. })
  81. },
  82. requestGet: function(url, data, callBack) {
  83. wx.request({
  84. url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址
  85. data: data,
  86. header: {
  87. 'openid': this.globalData.baseInfo.openid,
  88. 'token': this.globalData.couponToken,
  89. 'content-type': 'application/json' // 默认值
  90. },
  91. success(res) {
  92. callBack(res.data)
  93. }
  94. })
  95. },
  96. requestPost: function(url, data, callBack) {
  97. wx.request({
  98. url: this.globalData.apiUrl + url, //仅为示例,并非真实的接口地址
  99. data: data,
  100. header: {
  101. 'openid': this.globalData.baseInfo.openid,
  102. 'token': this.globalData.couponToken,
  103. 'content-type': 'application/x-www-form-urlencoded' // 默认值
  104. },
  105. method: "POST",
  106. success(res) {
  107. callBack(res.data)
  108. }
  109. })
  110. },
  111. globalData: {
  112. userInfo: null,
  113. userState: -1, //-1未获取 -0未注册 1已注册
  114. storeData:null,//店铺信息
  115. apiUrl: 'https://laomenkuang.jiyou-tech.com/apiWxAdmin/',
  116. baseInfo: null,
  117. couponData:null,//优惠券信息
  118. couponToken:null,//优惠券Token
  119. }
  120. })