1
0

userLogin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. var api = require('../../../config/api.js');
  2. var util = require('../../../utils/util.js');
  3. var app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. phone: '',
  10. smscode: '',
  11. second: '发送验证码',
  12. disabled: false,
  13. disabledUpdate: false,
  14. paramView: '',
  15. navUrl: '',
  16. isRegist: true
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. var that = this;
  23. if(options.view){
  24. that.setData({
  25. paramView: options.view
  26. });
  27. }
  28. console.log(that.data.paramView)
  29. if (wx.getStorageSync("navUrl")) {
  30. that.setData({
  31. navUrl: wx.getStorageSync("navUrl")
  32. })
  33. } else {
  34. that.setData({
  35. navUrl: '/pages/index/index'
  36. })
  37. }
  38. },
  39. /**
  40. * 页面渲染完成
  41. */
  42. onReady: function () {
  43. },
  44. /**
  45. * 页面显示
  46. */
  47. onShow: function () {
  48. },
  49. /**
  50. * 页面隐藏
  51. */
  52. onHide: function () {
  53. },
  54. /**
  55. * 页面关闭
  56. */
  57. onUnload: function () {
  58. console.log('登陆页面返回2,进入登陆页参数:' + this.data.paramView)
  59. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  60. if (this.data.paramView == 'hotGoods' || this.data.paramView == 'indexGoods' || this.data.paramView == 'cart') {
  61. wx.setStorageSync("isLocationIndex", "true");//购物车是否已跳转登录页,跳转成功设置为true已跳转
  62. wx.switchTab({
  63. url: '/pages/index/index'
  64. });
  65. }
  66. if (this.data.paramView == 'cateGoods') {
  67. wx.switchTab({
  68. url: '/pages/catalog/catalog'
  69. });
  70. }
  71. if (this.data.paramView == 'hotGoodsView') {
  72. wx.navigateBack({
  73. delta: 1
  74. })
  75. }
  76. },
  77. clearPhone() {
  78. this.setData({
  79. phone: ''
  80. });
  81. },
  82. clearCode() {
  83. this.setData({
  84. smscode: ''
  85. });
  86. },
  87. bindPhoneInput: function (e) {
  88. console.log(e.detail.value)
  89. this.setData({
  90. phone: e.detail.value
  91. });
  92. },
  93. bindSmscodeInput: function (e) {
  94. this.setData({
  95. smscode: e.detail.value
  96. });
  97. },
  98. /**
  99. * 用户登录,获取授权用户信息,查询授权用户openid是否与登录用户手机号绑定一致,
  100. * 不一致的提示是否更新手机绑定信息,一致则登录成功
  101. */
  102. loginUser: function () {
  103. let regular = /^1[3|4|5|7|8]\d{9}$/;
  104. if (!regular.test(this.data.phone)) {
  105. util.showErrorToast('手机格式不正确')
  106. return false;
  107. }
  108. if (this.data.smscode.length == 0) {
  109. util.showErrorToast('验证码不能为空')
  110. return false;
  111. }
  112. var that = this;
  113. wx.login({
  114. success: function (res) {
  115. if (res.code) {
  116. wx.getUserInfo({
  117. withCredentials: true,
  118. success: function (succRes) {
  119. util.request(api.getOpenId, {
  120. code: res.code
  121. }).then((openIdRes) => {
  122. if (openIdRes.errno === 0){
  123. //授权成功登录用户信息
  124. util.request(api.userLogin, {
  125. smscode: that.data.smscode,
  126. phone: that.data.phone,
  127. storeId: wx.getStorageSync('storeId'),
  128. merchSn: wx.getStorageSync('merchSn'),
  129. userInfo: succRes,
  130. openId: openIdRes.data
  131. }, 'POST').then(function (userLoginRes) {
  132. if (userLoginRes.errno === 0) {
  133. //存储用户信息
  134. that.setLoginUserInfo(userLoginRes);
  135. console.log("登录成功");
  136. util.showSuccessToast('登录成功');
  137. }else if (userLoginRes.errno === 2) {//openid的绑定的手机号与当前用户登录的手机号不一致
  138. wx.showModal({
  139. title: '提示',
  140. content: userLoginRes.errmsg,
  141. success: function (conRes) {
  142. if (conRes.confirm) {
  143. util.request(api.userLoginMobileUpdate, {
  144. phone: that.data.phone,
  145. openId: openIdRes.data,
  146. storeId: wx.getStorageSync('storeId'),
  147. merchSn: wx.getStorageSync('merchSn'),
  148. userInfo: succRes,
  149. }, 'POST').then(function (loginMobUpdRes) {
  150. if (loginMobUpdRes.errno === 0) {
  151. //存储用户信息
  152. that.setLoginUserInfo(loginMobUpdRes);
  153. console.log("登录成功");
  154. util.showSuccessToast('登录成功');
  155. } else {
  156. util.showErrorToast(loginMobUpdRes.errmsg);
  157. }
  158. })
  159. } else {
  160. console.log('弹框后点取消')
  161. }
  162. }
  163. })
  164. } else {
  165. if (userLoginRes.errmsg.length>8){
  166. wx.showModal({
  167. title: '提示',
  168. content: userLoginRes.errmsg,
  169. showCancel: false
  170. })
  171. } else {
  172. util.showErrorToast(userLoginRes.errmsg);
  173. }
  174. }
  175. });
  176. }
  177. });
  178. },
  179. fail: function (err) {
  180. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  181. // wx.switchTab({
  182. // url: '/pages/index/index'
  183. // });
  184. console.log("登录页重新认证");
  185. }
  186. });
  187. } else {
  188. console.log("failed");
  189. }
  190. },
  191. fail: function (err) {
  192. console.log("failed");
  193. }
  194. });
  195. },
  196. /**
  197. * 存储用户信息
  198. */
  199. setLoginUserInfo: function (loginMobUpdRes){
  200. var that = this;
  201. if (loginMobUpdRes.data.userInfo) {
  202. wx.setStorageSync('userInfo', loginMobUpdRes.data.userInfo);
  203. }
  204. if (loginMobUpdRes.data.token) {
  205. wx.setStorageSync('token', loginMobUpdRes.data.token);
  206. }
  207. wx.setStorageSync('userId', loginMobUpdRes.data.userId);
  208. if (that.data.paramView == 'goodsView') {
  209. wx.navigateBack({
  210. delta: 1
  211. });
  212. } else {
  213. if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  214. console.log(that.data.navUrl)
  215. wx.switchTab({
  216. url: that.data.navUrl,
  217. });
  218. } else if (that.data.navUrl) {
  219. wx.redirectTo({
  220. url: that.data.navUrl,
  221. });
  222. }
  223. }
  224. wx.setStorageSync('isRefusedLogin', 'false');//允许授权
  225. },
  226. /**
  227. * 发送短信
  228. */
  229. smscode: function () {
  230. var that = this;
  231. let regular = /^1[3|4|5|7|8]\d{9}$/;
  232. if (!regular.test(that.data.phone)) {
  233. util.showErrorToast('手机格式不正确')
  234. return false;
  235. }
  236. util.request(api.smscodeSend, {
  237. phone: that.data.phone
  238. }, 'POST').then(function (res) {
  239. console.log("发送验证码 => ", res);
  240. let n = 59;
  241. var timer = setInterval(function () {
  242. if (n == 0) {
  243. clearInterval(timer);
  244. that.setData({
  245. second: '发送验证码',
  246. disabled: false
  247. });
  248. } else {
  249. that.setData({
  250. second: n-- + 's后重新获取',
  251. disabled: true
  252. });
  253. }
  254. }, 1000);
  255. if (res.errno == 0) {
  256. wx.showToast({
  257. title: '短信发送成功'
  258. })
  259. } else {
  260. util.showErrorToast('发送失败');
  261. clearInterval(timer);
  262. that.setData({
  263. second: '发送验证码',
  264. disabled: false
  265. });
  266. return false;
  267. }
  268. });
  269. },
  270. backHome: function () {
  271. wx.reLaunch({
  272. url: '../index/index',
  273. })
  274. },
  275. /**
  276. * 页面相关事件处理函数--监听用户下拉动作
  277. */
  278. onPullDownRefresh: function () {
  279. },
  280. /**
  281. * 页面上拉触底事件的处理函数
  282. */
  283. onReachBottom: function () {
  284. },
  285. // /**
  286. // * 用户点击右上角分享
  287. // */
  288. // onShareAppMessage: function () {
  289. // },
  290. bindCancelLogin: function () {
  291. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  292. wx.switchTab({
  293. url: '/pages/index/index'
  294. });
  295. },
  296. /**
  297. * 登录页直接触发微信授权,授权成功保存用户信息
  298. */
  299. bindGetUserInfo: function () {
  300. let that = this;
  301. wx.login({
  302. success: function (res) {
  303. if (res.code) {
  304. wx.getUserInfo({
  305. withCredentials: true,
  306. success: function (succRes) {
  307. //保存授权用户信息
  308. wx.request({
  309. url: api.AuthLoginByWeixin,
  310. data: {
  311. code: res.code, userInfo: succRes, storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  312. },
  313. method: 'POST',
  314. header: {
  315. 'Content-Type': 'application/json'
  316. },
  317. success: function (wxRes) {
  318. if (wxRes.data.errno === 0) {
  319. //存储用户信息到缓存
  320. if (wxRes.data.data.userInfo) {
  321. wx.setStorageSync('userInfo', wxRes.data.data.userInfo);
  322. }
  323. wx.setStorageSync('token', wxRes.data.data.token);
  324. wx.setStorageSync('userId', wxRes.data.data.userId);
  325. if (that.data.paramView == 'goodsView') {
  326. wx.navigateBack({
  327. delta: 1
  328. });
  329. } else {
  330. if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  331. console.log(that.data.navUrl)
  332. wx.switchTab({
  333. url: that.data.navUrl,
  334. });
  335. } else if (that.data.navUrl) {
  336. wx.redirectTo({
  337. url: that.data.navUrl,
  338. });
  339. }
  340. }
  341. wx.setStorageSync('isRefusedLogin', 'false');//允许授权
  342. // console.log("登录成功");
  343. }
  344. },
  345. fail: function (err) {
  346. console.log("failed");
  347. }
  348. });
  349. },
  350. fail: function (err) {
  351. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  352. // wx.switchTab({
  353. // url: '/pages/index/index'
  354. // });
  355. console.log("登录页重新认证");
  356. }
  357. });
  358. } else {
  359. console.log("failed");
  360. }
  361. },
  362. fail: function (err) {
  363. console.log("failed");
  364. }
  365. });
  366. },
  367. /*registUser: function () {
  368. var that = this;
  369. let regular = /^1[3|4|5|7|8]\d{9}$/;
  370. if (!regular.test(that.data.phone)) {
  371. util.showErrorToast('手机格式不正确')
  372. return false;
  373. }
  374. if (that.data.smscode.length == 0) {
  375. util.showErrorToast('验证码不能为空')
  376. return false;
  377. }
  378. wx.login({
  379. success: function (res) {
  380. if (res.code) {
  381. wx.getUserInfo({
  382. withCredentials: true,
  383. success: function (succRes) {
  384. util.request(api.getOpenId, {
  385. code: res.code
  386. }).then((openIdRes) => {
  387. if (openIdRes.errno === 0) {
  388. that.registUserUtil();
  389. }
  390. });
  391. },
  392. fail: function (err) {
  393. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  394. console.log("注册前进行授权重新认证");
  395. }
  396. });
  397. } else {
  398. console.log("faild");
  399. }
  400. },
  401. fail: function (err) {
  402. console.log("failed");
  403. }
  404. })
  405. },
  406. registUserUtil: function () {
  407. var that = this;
  408. util.request(api.registUser, {
  409. smscode: that.data.smscode,
  410. phone: that.data.phone
  411. }, 'POST').then(function (res) {
  412. if (res.errno === 0) {
  413. //存储用户信息
  414. wx.setStorageSync('userInfo', res.data.data.userInfo);
  415. wx.setStorageSync('token', res.data.data.token);
  416. wx.setStorageSync('userId', res.data.data.userId);
  417. if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  418. wx.switchTab({
  419. url: that.data.navUrl,
  420. });
  421. } else if (that.data.navUrl) {
  422. wx.redirectTo({
  423. url: that.data.navUrl,
  424. });
  425. }
  426. wx.setStorageSync('isRefusedLogin', 'false');//允许授权
  427. // console.log("登录成功");
  428. } else {
  429. util.showErrorToast(res.errmsg);
  430. }
  431. });
  432. },
  433. showRegist: function(){
  434. var that = this;
  435. that.setData({
  436. isRegist: false
  437. });
  438. wx.setNavigationBarTitle({
  439. title: '注册'
  440. })
  441. }*/
  442. })