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.

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 년 전
5 년 전
5 년 전
5 년 전
5 년 전
5 년 전
5 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. this.globalData.userInfo = e.data.user_phone
  88. if (this.globalData.present_id){
  89. wx.reLaunch({
  90. url: '/pages/receiveTicket/receiveTicket?shareId=' + this.globalData.present_id + "&number=" + this.globalData.number
  91. })
  92. } else {
  93. wx.switchTab({
  94. url: '/pages/index/index'
  95. })
  96. }
  97. }
  98. },this)
  99. },
  100. globalData: {
  101. openidSuccessFuc: null,//方法回调
  102. userInfo: null,
  103. present_id:"",//判断用户是通过领取优惠券进来的还是直接进的
  104. number:"",//赠送优惠券的数量
  105. openId:"",
  106. session_key:"",
  107. token:"",
  108. userInfo:"",
  109. httpUrl:"https://laomenkuang.jiyou-tech.com/apiWx/"
  110. },
  111. wxRequest: function (url, params, callback, thisArg, methods, openid) {
  112. let that = this;
  113. // console.log(this.globalData.openid)
  114. var httpUrl = url;
  115. var str = "";
  116. var count = 0;
  117. for (let key in params) {
  118. if (count) {
  119. str += "&" + key + "=" + params[key];
  120. } else {
  121. str += key + "=" + params[key];
  122. }
  123. count++;
  124. }
  125. if (str) {
  126. httpUrl += "?" + str;
  127. }
  128. if (!methods) {
  129. methods = "GET";
  130. }
  131. if (methods == "POST") {
  132. wx.request({
  133. url: url,
  134. data: params,
  135. method: methods,
  136. header: {
  137. "content-type": "application/x-www-form-urlencoded",
  138. "openid": this.globalData.openid
  139. },
  140. success: function (re) {
  141. // if (re.data.code == -1002) {
  142. // wx.removeStorageSync('openid');
  143. // that.wxLogin();
  144. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  145. // }
  146. if (callback && thisArg) {
  147. callback.call(thisArg, re.data);
  148. }
  149. },
  150. fail: function (re) {
  151. wx.hideLoading();
  152. }
  153. })
  154. } else {
  155. wx.request({
  156. url: httpUrl,
  157. method: methods,
  158. header: {
  159. "openid": this.globalData.openid,
  160. },
  161. success: function (re) {
  162. // if (re.data.code == -1002) {
  163. // wx.removeStorageSync('openid');
  164. // that.wxLogin();
  165. // // that.wxRequest(url, params, callback, thisArg, methods, openid);
  166. // }
  167. if (callback && thisArg) {
  168. callback.call(thisArg, re.data);
  169. }
  170. },
  171. fail: function (res) {
  172. wx.hideLoading();
  173. }
  174. })
  175. }
  176. },
  177. // requestGet:function(url,data,callback){//get接口调用
  178. // wx.request({
  179. // url: url,
  180. // header: {
  181. // "openid": this.globalData.openId
  182. // },
  183. // data: data,
  184. // success:res=>{
  185. // if (callback) {
  186. // }
  187. // }
  188. // })
  189. // },
  190. // requestPost: function (url, data) {//post接口调用
  191. // wx.request({
  192. // url: url,
  193. // header: {
  194. // "Content-Type":"application/x-www-form-urlencoded",
  195. // openid: this.globalData.openId
  196. // },
  197. // data: data,
  198. // success(res) {
  199. // console.log(res.data)
  200. // }
  201. // })
  202. // }
  203. })