1
0

userLogin.js 13 KB

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