login.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import * as requestAPI from '../../models/dataModel'
  2. import {
  3. isPoneAvailable
  4. } from '../../utils/util'
  5. Page({
  6. data: {
  7. phone: '',
  8. success: false,
  9. code: '',
  10. ifUser: true
  11. },
  12. onLoad() {
  13. if(wx.getStorageSync('token')) {
  14. this.getIfExpired()
  15. wx.switchTab({
  16. url: '/pages/home/home',
  17. })
  18. }
  19. },
  20. // 获取手机号号
  21. phoneInput: function (e) {
  22. this.setData({
  23. phone: e.detail.value
  24. })
  25. },
  26. // 获取验证码
  27. codeInput: function (e) {
  28. this.setData({
  29. code: e.detail.value
  30. })
  31. },
  32. // 登录
  33. login: function () {
  34. console.log('s')
  35. var self = this
  36. wx.login({
  37. success(wxres) {
  38. requestAPI.userLogin(wxres.code).then(res => {
  39. if (res.statusCode === 401) {
  40. self.setData({
  41. ifUser: false
  42. })
  43. } else {
  44. if (res.data.access_token) {
  45. wx.setStorageSync('token', res.data.access_token);
  46. wx.switchTab({
  47. url: '/pages/home/home',
  48. })
  49. }
  50. }
  51. })
  52. }
  53. })
  54. },
  55. register: function () {
  56. let self = this
  57. if (this.data.phone.length == 0) {
  58. wx.showToast({
  59. title: '手机不能为空',
  60. icon: 'none',
  61. duration: 3000
  62. })
  63. } else if (!isPoneAvailable(this.data.phone)) {
  64. wx.showToast({
  65. title: '请输入正确的手机号',
  66. icon: 'none',
  67. duration: 3000
  68. })
  69. } else {
  70. requestAPI.getSmsCode(this.data.phone).then(res => {
  71. if (isNaN(res.data.msg)) {
  72. wx.showToast({
  73. title: res.data.msg,
  74. icon: 'none',
  75. duration: 3000
  76. })
  77. } else {
  78. self.setData({
  79. code: res.data.msg
  80. })
  81. requestAPI.getSmsToken(self.data.phone, self.data.code).then(res => {
  82. wx.setStorageSync('token', res.data.access_token)
  83. wx.login({
  84. success(wxres) {
  85. requestAPI.bindOpenId(wxres.code).then(res => {
  86. if (res.data.data === true) {
  87. self.setData({
  88. ifUser: true
  89. })
  90. }
  91. })
  92. },fail(res) {
  93. wx.showToast({
  94. title: '验证失败',
  95. duration: 3000
  96. })
  97. }
  98. })
  99. })
  100. }
  101. })
  102. }
  103. },
  104. getIfExpired() {
  105. requestAPI.getUserInfo().then(res => {
  106. })
  107. }
  108. })