login.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const REG = /^((13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8})|(8){11}$/
  2. const api = require('../../config/api')
  3. const util = require('../../utils/util.js');
  4. const uutil = require('../../utils/index')
  5. // pages/login/login.js
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. phone: '',
  12. psw: '',
  13. showPsw: false,
  14. tg: false
  15. },
  16. updatePhone(e) {
  17. this.setData({phone: e.detail.value})
  18. this.check(false)
  19. },
  20. updatePsw(e) {
  21. this.setData({psw: e.detail.value})
  22. this.check(false)
  23. },
  24. check(tip) {
  25. if (!this.data.phone) {
  26. tip && wx.showToast({title: '请输入手机号', icon: 'none'})
  27. } else if (!REG.test(this.data.phone)) {
  28. tip && wx.showToast({title: '手机号格式不正确!', icon: 'none'})
  29. } else if (!this.data.psw) {
  30. tip && wx.showToast({title: '请输入密码', icon: 'none'})
  31. } else {
  32. this.setData({ tg: true })
  33. return true
  34. }
  35. },
  36. showPsw() {
  37. this.setData({showPsw: true})
  38. },
  39. hidePsw() {
  40. this.setData({showPsw: false})
  41. },
  42. async login() {
  43. if (!this.check(true)) return;
  44. //登录远程服务器
  45. util.request(api.userLogin, {
  46. userName: this.data.phone,
  47. password: uutil.encryption(this.data.psw)
  48. }, 'POST').then(res => {
  49. if (res.code === 0) {
  50. //存储用户信息
  51. wx.setStorageSync('userInfo', res.data.user);
  52. wx.setStorageSync('token', res.data.token);
  53. wx.setStorageSync('userId', res.data.user.userId);
  54. wx.setStorageSync('department', res.data.department);
  55. wx.setStorageSync('isLogin', true);
  56. getApp().setLoginProps(true)
  57. wx.redirectTo({
  58. url: '/pages/index/index',
  59. })
  60. } else {
  61. wx.showToast({title: res.msg, icon: 'none'})
  62. }
  63. });
  64. },
  65. onLoad() {
  66. if (wx.getStorageSync('token')) {
  67. }
  68. }
  69. })