login.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. wx.setStorageSync('userInfo', res.data.userInfo);
  99. wx.setStorageSync('token', res.data.token);
  100. that.getdefalutUserAddress();
  101. wx.showToast({
  102. title: '授权注册成功',
  103. icon: 'success',
  104. duration: 2000,
  105. complete: function () {
  106. setTimeout(function () {
  107. wx.navigateBack({
  108. delta: 1
  109. })
  110. }, 2000);
  111. }
  112. })
  113. } else {
  114. console.info(res);
  115. wx.showToast({
  116. title: '授权失败请重试',
  117. icon: 'error',
  118. duration: 3000
  119. });
  120. }
  121. });
  122. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  123. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
  124. wx.showModal({
  125. title: '警告',
  126. content: '您点击了拒绝授权,部分功能无法使用!!!',
  127. showCancel: false,
  128. confirmText: '确定',
  129. success: function (res) {
  130. // 用户没有授权成功,不需要改变 isHide 的值
  131. if (res.confirm) {
  132. wx.switchTab({
  133. url: '/pages/index/index',
  134. })
  135. };
  136. }
  137. })
  138. }
  139. },
  140. wxLogin: function (e) {
  141. //console.log(e);
  142. if (e.detail.userInfo == null) {
  143. app.globalData.hasLogin = false;
  144. return;
  145. }
  146. app.globalData.hasLogin = false;
  147. wx.setStorageSync('userInfo', null);
  148. user.checkLogin().catch(() => {
  149. user.loginByWeixin(e.detail.userInfo).then(res => {
  150. console.log(res);
  151. app.globalData.hasLogin = true;
  152. wx.navigateBack({
  153. delta: 1
  154. })
  155. }).catch((err) => {
  156. app.globalData.hasLogin = false;
  157. console.log(err);
  158. wx.switchTab({
  159. url: '/pages/index/index',
  160. })
  161. });
  162. });
  163. }
  164. })