gerson
2024-08-11 81a60aa4fb7ee1689518918ceb9122f5d257e2fc
miniprogram/pages/login/login.ts
@@ -5,14 +5,15 @@
// 导入本地存储 api
import { setStorage } from '@/utils/storage'
// 导入接口 API 函数
import { PostLogin } from '@/api/account';
import { loginMessageUser, loginVerifyMessage, PostLogin } from '@/api/account';
// 导入 ComponentWithStore 方法
import { ComponentWithStore } from 'mobx-miniprogram-bindings'
// 导入 store 对象
import { accountStore } from '@/stores/accountStore'
import {STATIC_FILE_BASE_URL} from '@/config/constants'
import { STATIC_FILE_BASE_URL } from '@/config/constants'
import { verifyPhone } from '@/utils/toolsValidate';
const app = getApp();
// 使用 ComponentWithStore 方法替换 Component 方法构造页面
ComponentWithStore({
  // 让页面和 Store 对象建立关联
@@ -23,23 +24,116 @@
  },
  data: {
    activeTab: 'account',
    user: 'tc',
    password: 'a',
    STATIC_FILE_BASE_URL
    user: '',
    password: '',
    STATIC_FILE_BASE_URL,
    countNum: null,
    countTimer: null,
    phoneNumber: '',
    verifyCode: '',
  },
  lifetimes: {
    attached() {
      wx.hideHomeButton();
    },
  },
  pageLifetimes: {
    hide() {
      this.resetContent();
    }
  },
  methods: {
    resetContent() {
      switch (this.data.activeTab) {
        case 'account':
          this.setData({
            user: '',
            password: '',
          })
          break;
        case 'cellPhoneNumber':
          this.setData({
            phoneNumber: '',
            verifyCode: '',
          })
          this.stopCount();
          this.setData({
            countNum: null
          })
        default:
          break;
      }
    },
    tabChange: function (event) {
      this.setData({
        activeTab: event.detail.name
      })
      this.resetContent();
    },
    async getSMSClick() {
      if (this.data.countNum !== null) {
        return;
      }
      if (!this.data.phoneNumber) {
        toast({
          title: '请输入手机号码!'
        });
        return;
      }
      console.log(this.data.phoneNumber);
      const isValid = verifyPhone(this.data.phoneNumber)
      console.log(isValid);
      if (!isValid) {
        toast({
          title: '请输入正确的手机号码!'
        });
        return;
      }
      this.setData({
        verifyCode: ''
      })
      const res = await loginVerifyMessage({
        phone: this.data.phoneNumber
      },{
        noAuth: true
      })
      if (!res?.json_ok) {
        return;
      }
      this.startCount();
    },
    stopCount() {
      clearInterval(this.data.countTimer)
    },
    startCount() {
      this.stopCount();
      this.setData({
        countNum: 60
      })
      this.data.countTimer = setInterval(() => {
        if (this.data.countNum === 0) {
          this.setData({
            countNum: null
          })
          clearInterval(this.data.countTimer);
          return;
        }
        this.setData({
          countNum: this.data.countNum - 1
        })
      }, 1000);
    },
    handlePhoneNumName(phoneStr){
     return phoneStr.substr(0,3)+"****"+phoneStr.substr(7);
    },
    // 授权登录
    login: wx.$_.debounce(async function () {
@@ -65,22 +159,54 @@
          }, {
            noAuth: true
          });
          if (!res.json_ok || !res.hswatersession) {
            toast({
              title: res.json_msg
            })
            return;
          }
          setStorage('username', this.data.user);
          this.setUsername(this.data.user);
          app.globalData.user.phoneNum = '';
          break;
        case 'cellPhoneNumber':
          if (!this.data.phoneNumber) {
            toast({
              title: '请输入手机号码!'
            });
            return;
          }
          if (!verifyPhone(this.data.phoneNumber)) {
            toast({
              title: '请输入正确的手机号码!'
            });
            return;
          }
          if (!this.data.verifyCode) {
            toast({
              title: '请输入验证码!'
            });
            return;
          }
          res = await loginMessageUser({
            phone: this.data.phoneNumber,
            code: this.data.verifyCode,
          }, {
            noAuth: true
          });
          const phoneName = this.handlePhoneNumName(this.data.phoneNumber)
          setStorage('username',phoneName);
          this.setUsername(phoneName);
          app.globalData.user.phoneNum = this.data.phoneNumber;
          break;
        default:
          break;
      }
      if (!res.json_ok || !res.hswatersession) {
        toast({
          title: res.json_msg
        })
        return;
      }
      setStorage('session', res.hswatersession);
      this.setSession(res.hswatersession)
      setStorage('username', this.data.user);
      this.setUsername(this.data.user);
      wx.switchTab({
        url: '/pages/question/question'
      });