login.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. shopName:address.shopName
  55. }
  56. // that.setData({
  57. // addressInfo: res.data
  58. // })
  59. wx.setStorageSync('addressInfo', addresInfo);
  60. // that.getIndexData(); //加载商品
  61. } else {
  62. //无默认地址时 拿系统的
  63. this.getdefaultCityShop();
  64. }
  65. })
  66. },
  67. getdefaultCityShop() {
  68. let that = this;
  69. util.request(api.getDefaultCityShop, {}, 'get').then(res => {
  70. if (res.errno === 0) {
  71. // that.setData({
  72. // addressInfo: res.data
  73. // })
  74. wx.setStorageSync('addressInfo', res.data);
  75. // that.getIndexData();
  76. }
  77. // else {
  78. // util.showErrorToast(res.errmsg);
  79. // }
  80. })
  81. },
  82. getPhoneNumber: function (e) {
  83. var ivObj = e.detail.iv
  84. var telObj = e.detail.encryptedData
  85. var that = this;
  86. let r_userid = wx.getStorageSync('r_userid');
  87. if (r_userid == null || r_userid == "") r_userid = 0;
  88. console.log("请求授权登录:code:"+that.data.code+"encryptedData,"+telObj+"iv,"+ivObj+'r_userid,'+r_userid);
  89. //------执行Login---------
  90. util.request(api.AuthRegister, {
  91. code: that.data.code,
  92. encryptedData: telObj,
  93. iv: ivObj,
  94. r_userid: r_userid
  95. }).then(function (res) {
  96. if (res.errno === 0) {
  97. //存储用户信息
  98. app.globalData.hasLogin = true;
  99. app.globalData.userInfo=res.data.userInfo;
  100. wx.setStorageSync('userInfo', res.data.userInfo);
  101. wx.setStorageSync('token', res.data.token);
  102. that.getdefalutUserAddress();
  103. wx.showToast({
  104. title: '授权注册成功',
  105. icon: 'success',
  106. duration: 2000,
  107. complete: function () {
  108. setTimeout(function () {
  109. wx.navigateBack({
  110. delta: 1
  111. })
  112. }, 2000);
  113. }
  114. })
  115. } else {
  116. console.info(res);
  117. wx.showToast({
  118. title: '授权失败请重试',
  119. icon: 'error',
  120. duration: 3000
  121. });
  122. }
  123. });
  124. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  125. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
  126. wx.showModal({
  127. title: '警告',
  128. content: '您点击了拒绝授权,部分功能无法使用!!!',
  129. showCancel: false,
  130. confirmText: '确定',
  131. success: function (res) {
  132. // 用户没有授权成功,不需要改变 isHide 的值
  133. if (res.confirm) {
  134. wx.switchTab({
  135. url: '/pages/index/index',
  136. })
  137. };
  138. }
  139. })
  140. }
  141. },
  142. wxLogin: function (e) {
  143. //console.log(e);
  144. if (e.detail.userInfo == null) {
  145. app.globalData.hasLogin = false;
  146. return;
  147. }
  148. app.globalData.hasLogin = false;
  149. wx.setStorageSync('userInfo', null);
  150. user.checkLogin().catch(() => {
  151. user.loginByWeixin(e.detail.userInfo).then(res => {
  152. console.log(res);
  153. app.globalData.hasLogin = true;
  154. wx.navigateBack({
  155. delta: 1
  156. })
  157. }).catch((err) => {
  158. app.globalData.hasLogin = false;
  159. console.log(err);
  160. wx.switchTab({
  161. url: '/pages/index/index',
  162. })
  163. });
  164. });
  165. }
  166. })