methods.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 || !this.form.mobile) return
  13. try {
  14. const that = this
  15. uni.showLoading({
  16. title: '发送中...'
  17. });
  18. await Apis.GetVerificationCode({
  19. mobile: this.form.mobile
  20. })
  21. uni.hideLoading();
  22. codeStatus = true
  23. let num = 60
  24. loop()
  25. const time = setInterval(() => {
  26. loop()
  27. }, 1000)
  28. function loop() {
  29. num--
  30. if (num <= 0) {
  31. num = '重新获取'
  32. clearInterval(time)
  33. codeStatus = false
  34. }
  35. that.timeNum = num
  36. }
  37. } catch (error) {
  38. console.log(error)
  39. }
  40. },
  41. async login() {
  42. try {
  43. for (let key in this.form) {
  44. if (!this.form[key]) {
  45. let keyname = {
  46. mobile: '手机号',
  47. sms_code: '验证码'
  48. } [key]
  49. uni.showToast({
  50. title: `请输入${keyname}`,
  51. duration: 2000,
  52. icon: 'error'
  53. });
  54. return
  55. }
  56. }
  57. uni.showLoading({
  58. title: '登录中...'
  59. });
  60. const res = await Apis.PostSmsLogin(this.form)
  61. uni.hideLoading();
  62. if (res.data.token) {
  63. uni.setStorageSync('token', res.data.token)
  64. uni.setStorageSync('userInfo', res.data.user)
  65. uni.switchTab({
  66. url: '../index/index'
  67. })
  68. }
  69. } catch (error) {
  70. console.log(error)
  71. }
  72. },
  73. }