Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

app.js 5.2KB

5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
5 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. wx.setStorageSync('state', -1);
  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. // onShow:function(){
  50. // this.getUserType();
  51. // },
  52. getUserType: function () {//获取用户注册状态
  53. console.log(this.globalData.openId)
  54. this.wxRequest(this.globalData.httpUrl + 'getinfo', {},e=>{
  55. console.log(e)
  56. if(e.code==201){//未注册跳到注册页面
  57. wx.setStorageSync('state', 0);
  58. wx.setStorageSync("noUser", 0)
  59. } else if (e.code == 202) {//已注册
  60. wx.setStorageSync('state', 1);
  61. this.globalData.userInfo = e.data;
  62. e.data.avatarUrl = e.data.headimgurl;
  63. e.data.nickName = e.data.nickname;
  64. wx.setStorageSync('user_phone', e.data.user_phone);
  65. wx.setStorageSync("userInfo", e.data)
  66. if (e.data.item_state == 1) {
  67. wx.setStorageSync("noUser", 0)
  68. } else {
  69. wx.setStorageSync("noUser", 1)
  70. }
  71. }
  72. if(this.globalData.userInfoBackFn){
  73. this.globalData.userInfoBackFn(wx.getStorageSync('state'));
  74. }
  75. },this)
  76. },
  77. globalData: {
  78. openidSuccessFuc: null,//方法回调
  79. userInfoSuccessFuc: null,//方法回调
  80. present_id:"",//判断用户是通过领取优惠券进来的还是直接进的
  81. number:"",//赠送优惠券的数量
  82. openId:"",
  83. session_key:"",
  84. token:"",
  85. userInfo:"",
  86. httpUrl:"https://laomenkuang.jiyou-tech.com/apiWx/",
  87. user_phone:"",//用户手机号
  88. },
  89. wxRequest: function (url, params, callback, thisArg, methods, openid) {
  90. let that = this;
  91. var httpUrl = url;
  92. var str = "";
  93. var count = 0;
  94. for (let key in params) {
  95. if (count) {
  96. str += "&" + key + "=" + params[key];
  97. } else {
  98. str += key + "=" + params[key];
  99. }
  100. count++;
  101. }
  102. if (str) {
  103. httpUrl += "?" + str;
  104. }
  105. if (!methods) {
  106. methods = "GET";
  107. }
  108. if (methods == "POST") {
  109. wx.request({
  110. url: url,
  111. data: params,
  112. method: methods,
  113. header: {
  114. "content-type": "application/x-www-form-urlencoded",
  115. "openid": this.globalData.openId
  116. },
  117. success: function (re) {
  118. // if (re.data.code == -1002) {
  119. // wx.removeStorageSync('openid');
  120. // that.wxLogin();
  121. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  122. // }
  123. if (callback && thisArg) {
  124. callback.call(thisArg, re.data);
  125. }
  126. },
  127. fail: function (re) {
  128. wx.hideLoading();
  129. }
  130. })
  131. } else {
  132. wx.request({
  133. url: httpUrl,
  134. method: methods,
  135. header: {
  136. "openid": this.globalData.openId,
  137. },
  138. success: function (re) {
  139. // if (re.data.code == -1002) {
  140. // wx.removeStorageSync('openid');
  141. // that.wxLogin();
  142. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  143. // }
  144. if (callback && thisArg) {
  145. callback.call(thisArg, re.data);
  146. }
  147. },
  148. fail: function (res) {
  149. wx.hideLoading();
  150. }
  151. })
  152. }
  153. },
  154. // requestGet:function(url,data,callback){//get接口调用
  155. // wx.request({
  156. // url: url,
  157. // header: {
  158. // "openid": this.globalData.openId
  159. // },
  160. // data: data,
  161. // success:res=>{
  162. // if (callback) {
  163. // }
  164. // }
  165. // })
  166. // },
  167. // requestPost: function (url, data) {//post接口调用
  168. // wx.request({
  169. // url: url,
  170. // header: {
  171. // "Content-Type":"application/x-www-form-urlencoded",
  172. // openid: this.globalData.openId
  173. // },
  174. // data: data,
  175. // success(res) {
  176. // console.log(res.data)
  177. // }
  178. // })
  179. // }
  180. })