login.js 5.7 KB

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