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.

app.js 3.2KB

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