選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

app.js 5.9KB

5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // 登录
  10. if (!openid){
  11. wx.login({
  12. success: res => {
  13. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  14. // console.log(res)
  15. if (res.code) {
  16. console.log(res.code)
  17. wx.request({
  18. url: this.globalData.httpUrl+'openid',
  19. header: {
  20. code: res.code
  21. },
  22. success: e => {
  23. console.log(e)
  24. if (e.data.code == 200) {
  25. this.globalData.openId = e.data.data.openid;
  26. this.globalData.session_key = e.data.data.session_key;
  27. this.globalData.token = e.data.data.token;
  28. wx.setStorageSync('openid', e.data.data.openid)
  29. wx.setStorageSync('session_key', e.data.data.session_key)
  30. wx.setStorageSync('token', e.data.data.token)
  31. this.getUserType();
  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. this.getUserType();
  46. }
  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. onShow:function(){
  80. this.getUserType();
  81. },
  82. getUserType: function () {//获取用户注册状态
  83. console.log(this.globalData.openId)
  84. this.wxRequest(this.globalData.httpUrl + 'getinfo', {},e=>{
  85. console.log(e)
  86. if(e.code==201){//未注册跳到注册页面
  87. wx.reLaunch({
  88. url: '/pages/register/register'
  89. })
  90. } else if (e.code == 202) {//已注册
  91. this.globalData.userInfo = e.data;
  92. console.log(this.globalData.userInfo)
  93. if (this.globalData.present_id){
  94. } else {
  95. wx.switchTab({
  96. url: '/pages/index/index'
  97. })
  98. }
  99. }
  100. },this)
  101. },
  102. globalData: {
  103. openidSuccessFuc: null,//方法回调
  104. userInfoSuccessFuc: null,//方法回调
  105. userInfo: null,
  106. present_id:"",//判断用户是通过领取优惠券进来的还是直接进的
  107. number:"",//赠送优惠券的数量
  108. openId:"",
  109. session_key:"",
  110. token:"",
  111. userInfo:"",
  112. httpUrl:"https://laomenkuang.jiyou-tech.com/apiWx/",
  113. getUserArr:[],//用户信息
  114. },
  115. wxRequest: function (url, params, callback, thisArg, methods, openid) {
  116. let that = this;
  117. var httpUrl = url;
  118. var str = "";
  119. var count = 0;
  120. for (let key in params) {
  121. if (count) {
  122. str += "&" + key + "=" + params[key];
  123. } else {
  124. str += key + "=" + params[key];
  125. }
  126. count++;
  127. }
  128. if (str) {
  129. httpUrl += "?" + str;
  130. }
  131. if (!methods) {
  132. methods = "GET";
  133. }
  134. if (methods == "POST") {
  135. wx.request({
  136. url: url,
  137. data: params,
  138. method: methods,
  139. header: {
  140. "content-type": "application/x-www-form-urlencoded",
  141. "openid": this.globalData.openId
  142. },
  143. success: function (re) {
  144. // if (re.data.code == -1002) {
  145. // wx.removeStorageSync('openid');
  146. // that.wxLogin();
  147. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  148. // }
  149. if (callback && thisArg) {
  150. callback.call(thisArg, re.data);
  151. }
  152. },
  153. fail: function (re) {
  154. wx.hideLoading();
  155. }
  156. })
  157. } else {
  158. wx.request({
  159. url: httpUrl,
  160. method: methods,
  161. header: {
  162. "openid": this.globalData.openId,
  163. },
  164. success: function (re) {
  165. // if (re.data.code == -1002) {
  166. // wx.removeStorageSync('openid');
  167. // that.wxLogin();
  168. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  169. // }
  170. if (callback && thisArg) {
  171. callback.call(thisArg, re.data);
  172. }
  173. },
  174. fail: function (res) {
  175. wx.hideLoading();
  176. }
  177. })
  178. }
  179. },
  180. // requestGet:function(url,data,callback){//get接口调用
  181. // wx.request({
  182. // url: url,
  183. // header: {
  184. // "openid": this.globalData.openId
  185. // },
  186. // data: data,
  187. // success:res=>{
  188. // if (callback) {
  189. // }
  190. // }
  191. // })
  192. // },
  193. // requestPost: function (url, data) {//post接口调用
  194. // wx.request({
  195. // url: url,
  196. // header: {
  197. // "Content-Type":"application/x-www-form-urlencoded",
  198. // openid: this.globalData.openId
  199. // },
  200. // data: data,
  201. // success(res) {
  202. // console.log(res.data)
  203. // }
  204. // })
  205. // }
  206. })