methods.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import Apis from '@/common/apis/index'
  2. let codeStatus = false
  3. const mobileReg = /^1[3456789]\d{9}$/
  4. export default {
  5. inputMobile(event) {
  6. this.form.mobile = event.detail.value
  7. },
  8. inputCode(event) {
  9. this.form.sms_code = event.detail.value
  10. },
  11. async sendCode() {
  12. if (codeStatus) return
  13. if (!mobileReg.test(this.form.mobile)) {
  14. return uni.showToast({
  15. title: '请输入正确手机号',
  16. icon: 'error'
  17. })
  18. }
  19. try {
  20. const that = this
  21. uni.showLoading({
  22. title: '发送中...'
  23. });
  24. await Apis.GetVerificationCode({
  25. mobile: this.form.mobile
  26. })
  27. uni.hideLoading();
  28. codeStatus = true
  29. let num = 60
  30. loop()
  31. const time = setInterval(() => {
  32. loop()
  33. }, 1000)
  34. function loop() {
  35. num--
  36. if (num <= 0) {
  37. num = '重新获取'
  38. clearInterval(time)
  39. codeStatus = false
  40. }
  41. that.timeNum = num
  42. }
  43. } catch (error) {
  44. console.log(error)
  45. }
  46. },
  47. async login() {
  48. try {
  49. for (let key in this.form) {
  50. if (!this.form[key]) {
  51. let keyname = {
  52. mobile: '手机号',
  53. sms_code: '验证码'
  54. } [key]
  55. uni.showToast({
  56. title: `请输入${keyname}`,
  57. duration: 2000,
  58. icon: 'error'
  59. });
  60. return
  61. }
  62. }
  63. uni.showLoading({
  64. title: '登录中...'
  65. });
  66. const res = await Apis.PostSmsLogin(this.form)
  67. uni.hideLoading();
  68. if (res.data.token) {
  69. uni.setStorageSync('token', res.data.token)
  70. uni.setStorageSync('userInfo', res.data.user)
  71. uni.switchTab({
  72. url: '../index/index'
  73. })
  74. }
  75. } catch (error) {
  76. console.log(error)
  77. }
  78. },
  79. // 手机号授权登录
  80. async getPhoneNumber(event) {
  81. try {
  82. let me = this
  83. console.log(event)
  84. if (event.detail.errMsg === 'getPhoneNumber:ok') {
  85. uni.showLoading({
  86. title: '正在登录...',
  87. mask: true
  88. });
  89. let params = {
  90. code: event.detail.code
  91. }
  92. const res = await Apis.PostWechatLogin(params)
  93. uni.hideLoading()
  94. if (res.data.token) {
  95. uni.setStorageSync('token', res.data.token)
  96. uni.setStorageSync('userInfo', res.data.user)
  97. uni.switchTab({
  98. url: '../index/index'
  99. })
  100. }
  101. }
  102. } catch (error) {
  103. console.error(error)
  104. }
  105. },
  106. }