userLogin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. let n = 59;
  240. var timer = setInterval(function () {
  241. if (n == 0) {
  242. clearInterval(timer);
  243. that.setData({
  244. second: '发送验证码',
  245. disabled: false
  246. });
  247. } else {
  248. that.setData({
  249. second: n-- + 's后重新获取',
  250. disabled: true
  251. });
  252. }
  253. }, 1000);
  254. if (res.errno == 0) {
  255. wx.showToast({
  256. title: '短信发送成功'
  257. })
  258. } else {
  259. util.showErrorToast('发送失败');
  260. clearInterval(timer);
  261. that.setData({
  262. second: '发送验证码',
  263. disabled: false
  264. });
  265. return false;
  266. }
  267. });
  268. },
  269. backHome: function () {
  270. wx.reLaunch({
  271. url: '../index/index',
  272. })
  273. },
  274. /**
  275. * 页面相关事件处理函数--监听用户下拉动作
  276. */
  277. onPullDownRefresh: function () {
  278. },
  279. /**
  280. * 页面上拉触底事件的处理函数
  281. */
  282. onReachBottom: function () {
  283. },
  284. // /**
  285. // * 用户点击右上角分享
  286. // */
  287. // onShareAppMessage: function () {
  288. // },
  289. bindCancelLogin: function () {
  290. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  291. wx.switchTab({
  292. url: '/pages/index/index'
  293. });
  294. },
  295. /**
  296. * 登录页直接触发微信授权,授权成功保存用户信息
  297. */
  298. bindGetUserInfo: function () {
  299. let that = this;
  300. wx.login({
  301. success: function (res) {
  302. if (res.code) {
  303. wx.getUserInfo({
  304. withCredentials: true,
  305. success: function (succRes) {
  306. //保存授权用户信息
  307. wx.request({
  308. url: api.AuthLoginByWeixin,
  309. data: {
  310. code: res.code, userInfo: succRes, storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  311. },
  312. method: 'POST',
  313. header: {
  314. 'Content-Type': 'application/json'
  315. },
  316. success: function (wxRes) {
  317. if (wxRes.data.errno === 0) {
  318. //存储用户信息到缓存
  319. if (wxRes.data.data.userInfo) {
  320. wx.setStorageSync('userInfo', wxRes.data.data.userInfo);
  321. }
  322. wx.setStorageSync('token', wxRes.data.data.token);
  323. wx.setStorageSync('userId', wxRes.data.data.userId);
  324. if (that.data.paramView == 'goodsView') {
  325. wx.navigateBack({
  326. delta: 1
  327. });
  328. } else {
  329. if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  330. console.log(that.data.navUrl)
  331. wx.switchTab({
  332. url: that.data.navUrl,
  333. });
  334. } else if (that.data.navUrl) {
  335. wx.redirectTo({
  336. url: that.data.navUrl,
  337. });
  338. }
  339. }
  340. wx.setStorageSync('isRefusedLogin', 'false');//允许授权
  341. // console.log("登录成功");
  342. }
  343. },
  344. fail: function (err) {
  345. console.log("failed");
  346. }
  347. });
  348. },
  349. fail: function (err) {
  350. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  351. // wx.switchTab({
  352. // url: '/pages/index/index'
  353. // });
  354. console.log("登录页重新认证");
  355. }
  356. });
  357. } else {
  358. console.log("failed");
  359. }
  360. },
  361. fail: function (err) {
  362. console.log("failed");
  363. }
  364. });
  365. },
  366. /*registUser: function () {
  367. var that = this;
  368. let regular = /^1[3|4|5|7|8]\d{9}$/;
  369. if (!regular.test(that.data.phone)) {
  370. util.showErrorToast('手机格式不正确')
  371. return false;
  372. }
  373. if (that.data.smscode.length == 0) {
  374. util.showErrorToast('验证码不能为空')
  375. return false;
  376. }
  377. wx.login({
  378. success: function (res) {
  379. if (res.code) {
  380. wx.getUserInfo({
  381. withCredentials: true,
  382. success: function (succRes) {
  383. util.request(api.getOpenId, {
  384. code: res.code
  385. }).then((openIdRes) => {
  386. if (openIdRes.errno === 0) {
  387. that.registUserUtil();
  388. }
  389. });
  390. },
  391. fail: function (err) {
  392. wx.setStorageSync('isRefusedLogin', 'true');//拒绝授权
  393. console.log("注册前进行授权重新认证");
  394. }
  395. });
  396. } else {
  397. console.log("faild");
  398. }
  399. },
  400. fail: function (err) {
  401. console.log("failed");
  402. }
  403. })
  404. },
  405. registUserUtil: function () {
  406. var that = this;
  407. util.request(api.registUser, {
  408. smscode: that.data.smscode,
  409. phone: that.data.phone
  410. }, 'POST').then(function (res) {
  411. if (res.errno === 0) {
  412. //存储用户信息
  413. wx.setStorageSync('userInfo', res.data.data.userInfo);
  414. wx.setStorageSync('token', res.data.data.token);
  415. wx.setStorageSync('userId', res.data.data.userId);
  416. if (that.data.navUrl && that.data.navUrl == '/pages/index/index') {
  417. wx.switchTab({
  418. url: that.data.navUrl,
  419. });
  420. } else if (that.data.navUrl) {
  421. wx.redirectTo({
  422. url: that.data.navUrl,
  423. });
  424. }
  425. wx.setStorageSync('isRefusedLogin', 'false');//允许授权
  426. // console.log("登录成功");
  427. } else {
  428. util.showErrorToast(res.errmsg);
  429. }
  430. });
  431. },
  432. showRegist: function(){
  433. var that = this;
  434. that.setData({
  435. isRegist: false
  436. });
  437. wx.setNavigationBarTitle({
  438. title: '注册'
  439. })
  440. }*/
  441. })