Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

208 lines
6.0KB

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