您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

206 行
5.9KB

  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. // 展示本地存储能力
  5. // var logs = wx.getStorageSync('logs') || []
  6. // logs.unshift(Date.now())
  7. // wx.setStorageSync('logs', logs)
  8. var openid = wx.getStorageSync('openid') || "";
  9. console.log(this.globalData.present_id)
  10. // 登录
  11. if (!openid){
  12. wx.login({
  13. success: res => {
  14. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  15. // console.log(res)
  16. if (res.code) {
  17. console.log(res.code)
  18. wx.request({
  19. url: this.globalData.httpUrl+'openid',
  20. header: {
  21. code: res.code
  22. },
  23. success: e => {
  24. console.log(e)
  25. if (e.data.code == 200) {
  26. this.globalData.openId = e.data.data.openid;
  27. this.globalData.session_key = e.data.data.session_key;
  28. this.globalData.token = e.data.data.token;
  29. wx.setStorageSync('openid', e.data.data.openid)
  30. wx.setStorageSync('session_key', e.data.data.session_key)
  31. wx.setStorageSync('token', e.data.data.token)
  32. // if (this.globalData.openidSuccessFuc) {
  33. // this.globalData.openidSuccessFuc();
  34. // }
  35. }
  36. }
  37. })
  38. } else {
  39. console.log("登陆失败" + res.errMsg)
  40. }
  41. }
  42. })
  43. }else{
  44. this.globalData.openid = openid;
  45. }
  46. this.getUserType();
  47. // 获取用户信息
  48. // wx.getSetting({
  49. // success: res => {
  50. // console.log(res)
  51. // if (res.authSetting['scope.userInfo']) {
  52. // // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  53. // wx.getUserInfo({
  54. // success: e => {
  55. // // 可以将 res 发送给后台解码出 unionId
  56. // this.globalData.userInfo = e.userInfo
  57. // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  58. // // 所以此处加入 callback 以防止这种情况
  59. // if (this.userInfoReadyCallback) {
  60. // this.userInfoReadyCallback(res)
  61. // }
  62. // }
  63. // })
  64. // }else{
  65. // wx.authorize({
  66. // scope: 'scope.record',
  67. // success() {
  68. // wx.getUserInfo({
  69. // success: e => {
  70. // console.log(e)
  71. // }
  72. // })
  73. // }
  74. // })
  75. // }
  76. // }
  77. // })
  78. },
  79. getUserType:function(){//获取用户注册状态
  80. this.wxRequest(this.globalData.httpUrl + 'getinfo',{},e=>{
  81. // console.log(e)
  82. if(e.code==201){//未注册跳到注册页面
  83. wx.reLaunch({
  84. url: '/pages/register/register'
  85. })
  86. } else if (e.code == 202){//已注册
  87. if (this.globalData.present_id){
  88. wx.reLaunch({
  89. url: '/pages/receiveTicket/receiveTicket?shareId=' + this.globalData.present_id + "&number=" + this.globalData.number
  90. })
  91. } else {
  92. wx.switchTab({
  93. url: '/pages/index/index'
  94. })
  95. }
  96. }
  97. },this)
  98. },
  99. globalData: {
  100. openidSuccessFuc: null,//方法回调
  101. userInfo: null,
  102. present_id:"",//判断用户是通过领取优惠券进来的还是直接进的
  103. number:"",//赠送优惠券的数量
  104. openId:"",
  105. session_key:"",
  106. token:"",
  107. userInfo:"",
  108. httpUrl:"https://laomenkuang.jiyou-tech.com/apiWx/"
  109. },
  110. wxRequest: function (url, params, callback, thisArg, methods, openid) {
  111. let that = this;
  112. // console.log(this.globalData.openid)
  113. var httpUrl = url;
  114. var str = "";
  115. var count = 0;
  116. for (let key in params) {
  117. if (count) {
  118. str += "&" + key + "=" + params[key];
  119. } else {
  120. str += key + "=" + params[key];
  121. }
  122. count++;
  123. }
  124. if (str) {
  125. httpUrl += "?" + str;
  126. }
  127. if (!methods) {
  128. methods = "GET";
  129. }
  130. if (methods == "POST") {
  131. wx.request({
  132. url: url,
  133. data: params,
  134. method: methods,
  135. header: {
  136. "content-type": "application/x-www-form-urlencoded",
  137. "openid": this.globalData.openid
  138. },
  139. success: function (re) {
  140. // if (re.data.code == -1002) {
  141. // wx.removeStorageSync('openid');
  142. // that.wxLogin();
  143. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  144. // }
  145. if (callback && thisArg) {
  146. callback.call(thisArg, re.data);
  147. }
  148. },
  149. fail: function (re) {
  150. wx.hideLoading();
  151. }
  152. })
  153. } else {
  154. wx.request({
  155. url: httpUrl,
  156. method: methods,
  157. header: {
  158. "openid": this.globalData.openid,
  159. },
  160. success: function (re) {
  161. // if (re.data.code == -1002) {
  162. // wx.removeStorageSync('openid');
  163. // that.wxLogin();
  164. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  165. // }
  166. if (callback && thisArg) {
  167. callback.call(thisArg, re.data);
  168. }
  169. },
  170. fail: function (res) {
  171. wx.hideLoading();
  172. }
  173. })
  174. }
  175. },
  176. // requestGet:function(url,data,callback){//get接口调用
  177. // wx.request({
  178. // url: url,
  179. // header: {
  180. // "openid": this.globalData.openId
  181. // },
  182. // data: data,
  183. // success:res=>{
  184. // if (callback) {
  185. // }
  186. // }
  187. // })
  188. // },
  189. // requestPost: function (url, data) {//post接口调用
  190. // wx.request({
  191. // url: url,
  192. // header: {
  193. // "Content-Type":"application/x-www-form-urlencoded",
  194. // openid: this.globalData.openId
  195. // },
  196. // data: data,
  197. // success(res) {
  198. // console.log(res.data)
  199. // }
  200. // })
  201. // }
  202. })