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.

194 lines
5.3KB

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