login.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. var api = require('../../../config/api.js');
  2. var util = require('../../../utils/util.js');
  3. var user = require('../../../utils/user.js');
  4. var app = getApp();
  5. Page({
  6. data: {
  7. username: '',
  8. mobile: '',
  9. code: ""
  10. },
  11. onLoad: function (options) {
  12. // 页面初始化 options为页面跳转所带来的参数
  13. // 页面渲染完成
  14. if (options.reg != null) {
  15. wx.showModal({
  16. title: '提示',
  17. content: '授权过期,请重新授权',
  18. showCancel: false
  19. });
  20. }
  21. },
  22. onReady: function () {
  23. },
  24. onShow: function () {
  25. // 页面显示
  26. let that = this;
  27. user.login().then(res => {
  28. console.info(res)
  29. that.setData({
  30. code: res.code
  31. })
  32. })
  33. },
  34. onHide: function () {
  35. // 页面隐藏
  36. },
  37. onUnload: function () {
  38. // 页面关闭
  39. },
  40. getdefalutUserAddress() {
  41. //登陆拿用户默认地址,如果没有就拿系统默认地址
  42. //如果有默认地址,但是不在门店服务区域里
  43. util.request(api.OrderBookDefaultAddress, {}, 'get').then(res => {
  44. if (res.errno === 0) {
  45. let address = res.data;
  46. var city = address.city.indexOf("市辖") != -1 ? address.province : address.city;
  47. //通过jz_shop的shop_no关联服务力分配的门店id,拿实际的jz_shop的门店id
  48. var addresInfo = {
  49. city: city,
  50. lng: address.lng,
  51. lat: address.lat,
  52. shopId: address.shopId,
  53. address: address.addressDetail
  54. }
  55. // that.setData({
  56. // addressInfo: res.data
  57. // })
  58. wx.setStorageSync('addressInfo', addresInfo);
  59. // that.getIndexData(); //加载商品
  60. } else {
  61. //无默认地址时 拿系统的
  62. this.getdefaultCityShop();
  63. }
  64. })
  65. },
  66. getdefaultCityShop() {
  67. let that = this;
  68. util.request(api.getDefaultCityShop, {}, 'get').then(res => {
  69. if (res.errno === 0) {
  70. // that.setData({
  71. // addressInfo: res.data
  72. // })
  73. wx.setStorageSync('addressInfo', res.data);
  74. // that.getIndexData();
  75. }
  76. // else {
  77. // util.showErrorToast(res.errmsg);
  78. // }
  79. })
  80. },
  81. getPhoneNumber: function (e) {
  82. var ivObj = e.detail.iv
  83. var telObj = e.detail.encryptedData
  84. var that = this;
  85. let r_userid = wx.getStorageSync('r_userid');
  86. if (r_userid == null || r_userid == "") r_userid = 0;
  87. console.log("请求授权登录:code:"+that.data.code+"encryptedData,"+telObj+"iv,"+ivObj+'r_userid,'+r_userid);
  88. //------执行Login---------
  89. util.request(api.AuthRegister, {
  90. code: that.data.code,
  91. encryptedData: telObj,
  92. iv: ivObj,
  93. r_userid: r_userid
  94. }).then(function (res) {
  95. if (res.errno === 0) {
  96. //存储用户信息
  97. app.globalData.hasLogin = true;
  98. app.globalData.userInfo=res.data.userInfo;
  99. wx.setStorageSync('userInfo', res.data.userInfo);
  100. wx.setStorageSync('token', res.data.token);
  101. that.getdefalutUserAddress();
  102. wx.showToast({
  103. title: '授权注册成功',
  104. icon: 'success',
  105. duration: 2000,
  106. complete: function () {
  107. setTimeout(function () {
  108. wx.navigateBack({
  109. delta: 1
  110. })
  111. }, 2000);
  112. }
  113. })
  114. } else {
  115. console.info(res);
  116. wx.showToast({
  117. title: '授权失败请重试',
  118. icon: 'error',
  119. duration: 3000
  120. });
  121. }
  122. });
  123. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  124. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
  125. wx.showModal({
  126. title: '警告',
  127. content: '您点击了拒绝授权,部分功能无法使用!!!',
  128. showCancel: false,
  129. confirmText: '确定',
  130. success: function (res) {
  131. // 用户没有授权成功,不需要改变 isHide 的值
  132. if (res.confirm) {
  133. wx.switchTab({
  134. url: '/pages/index/index',
  135. })
  136. };
  137. }
  138. })
  139. }
  140. },
  141. wxLogin: function (e) {
  142. //console.log(e);
  143. if (e.detail.userInfo == null) {
  144. app.globalData.hasLogin = false;
  145. return;
  146. }
  147. app.globalData.hasLogin = false;
  148. wx.setStorageSync('userInfo', null);
  149. user.checkLogin().catch(() => {
  150. user.loginByWeixin(e.detail.userInfo).then(res => {
  151. console.log(res);
  152. app.globalData.hasLogin = true;
  153. wx.navigateBack({
  154. delta: 1
  155. })
  156. }).catch((err) => {
  157. app.globalData.hasLogin = false;
  158. console.log(err);
  159. wx.switchTab({
  160. url: '/pages/index/index',
  161. })
  162. });
  163. });
  164. }
  165. })