Box.js 6.74 KB
// login/index/components/LoginBox.js
import USERAPI from '~/api/usersApi/index'
import {
  selectAccountLogin,
  setLoginInfo,
  loginModal
} from '~/login/lib/index'
import md5 from '~/utils/lib/md5'
import network from '~/utils/network'
import {
  imgUrlMixin
} from '~/mixins/index'
let thisPage
Component({
  behaviors: [imgUrlMixin],
  options: {
    styleIsolation: 'shared'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    loginType: Number,
    disabled: Boolean
  },
  /**
   * 组件的初始数据
   */
  data: {
    formData: {
      phone: '',
      code: '',
      account: '',
      password: ''
    },
    phoneReady: false,
    countDown: null
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /**隐藏验证码错误 */
    hideError(name) {
      if (name == 'code')
        this.setData({
          smsError: false
        })
    },
    /**输入回调 */
    input(e) {
      const {
        value
      } = e.detail, {
        name
      } = e.currentTarget.dataset
      this.setData({
        [`formData.${name}`]: value
      })
      if (name == 'phone')
        this.setData({
          phoneReady: thisPage.validate.methods.tel(value)
        })
      this.hideError(name)
      this.checkFormData()
    },
    /**清除input value */
    clear(e) {
      const {
        name
      } = e.currentTarget.dataset
      this.setData({
        [`formData.${name}`]: ''
      })
      this.hideError(name)
      this.checkFormData()
    },
    /**获取验证码 */
    getSmsCode() {
      const {
        phoneReady,
        formData,
        count
      } = this.data,
        countDown = 60
      if (!phoneReady || count) return
      this.setData({
        smsLoading: true
      })
      USERAPI.getSmsCode(formData.phone)
        .then(({
          code,
          message
        }) => {
          this.setData({
            smsLoading: false
          })
          if (code == '0') this.countDown(countDown)
          else wx.$api.showToast(message || '发送验证码失败')
        })
        .catch(err => {
          this.setData({
            smsLoading: false
          })
          wx.$api.showToast(err || '发送验证码失败')
        })
    },
    /**验证码倒计时 */
    countDown(countDown) {
      if (countDown)
        this.setData({
          count: countDown
        })
      setTimeout(() => {
        let {
          count
        } = this.data
        this.setData({
          count: --count
        })
        if (count > 0) this.countDown()
        else
          this.setData({
            count: null
          })
      }, 1e3)
    },
    /**检查输入框内容 */
    checkFormData() {
      const {
        loginType,
        formData
      } = this.data
      if (loginType != 0) {
        if (loginType == 1) {
          this.setData({
              phoneReady: formData.phone && thisPage.validate.methods.tel(formData.phone)
            },
            () => {
              thisPage.checkLoginBtn()
            }
          )
        } else thisPage.checkLoginBtn()
      }
    },
    /**显示隐藏密码 */
    pwdSwitch() {
      this.setData({
        showPwd: !this.data.showPwd
      })
    },
    /**手机号&帐号密码登录 */
    login() {
      const {
        formData,
        loginType
      } = this.data,
        target = {
          1: 'phoneSmsLogin',
          2: 'accountLogin'
        } [loginType],
        obj =
        loginType == 1 ? {
          mobile: formData.phone,
          code: formData.code
        } : {
          userName: formData.account,
          password: md5(formData.password)
        }
      this.setBtnLoading()
      USERAPI[target](obj)
        .then(({
          code,
          data,
          message
        }) => {
          this.setBtnLoading(false)
          if (code == '0') {
            if (loginType == 1) this.checkLoginUserInfo(data)
            else {
              const {
                autoLogin
              } = thisPage.data, {
                  sessionId,
                  loginUserInfoList
                } = data,
                validTime = 3600 * 24 * 7 * 1e3,
                autoLoginFailureTime = Date.now() + validTime
              if (autoLogin && loginUserInfoList[0]) {
                wx.$storage.set('auto_login_object', {
                  failureTime: autoLoginFailureTime,
                  userName: formData.account,
                  password: md5(formData.password)
                })
              } else wx.$storage.rm('auto_login_object')
              setLoginInfo(loginUserInfoList[0], sessionId)
            }
          } else {
            if (loginType == 1)
              this.setData({
                smsError: true
              })
            wx.$api.showToast(message || '系统异常')
          }
        })
        .catch(err => {
          wx.$api.showToast('系统异常')
          this.setBtnLoading(false)
        })
    },
    /**微信登录 */
    wxLogin({
      detail
    }) {
      if (!detail.code) return
      this.setBtnLoading()
      USERAPI.authUserPhone(detail, this.loginToken)
        .then(({
          code,
          data,
          message
        }) => {
          if (code == '0') this.checkLoginUserInfo(data)
          else wx.$api.showToast(message || '系统异常')
          this.setBtnLoading(false)
        })
        .catch(({
          errMsg
        }) => {
          wx.$api.showToast(errMsg || '系统异常')
          this.setBtnLoading(false)
        })
    },
    /**手机号&微信登录:是否跳转登录列表页 */
    checkLoginUserInfo({
      loginUserInfoList,
      sessionId
    }) {
      /**多个账号 */
      if (loginUserInfoList.length > 1)
        wx.$router.go('login.accountItems', {
          sessionId,
          ...thisPage.options
        })
      /**只有一个账号 */
      else
        selectAccountLogin({
          id: loginUserInfoList[0].uuid,
          sessionId
        })
    },
    /**设置登录按钮loading状态 */
    setBtnLoading(loading = true) {
      thisPage.setData({
        loading
      })
      this.setData({
        loading
      })
    },
    /**页面初始化 */
    async init() {
      network.getNetworkType()
      thisPage = wx.$router.getPageInstance()
      try {
        const {
          data,
          code,
          message
        } = await USERAPI.wxCodeLogin()
        if (code == '0') {
          thisPage.setData({
            disabled: false
          })
          this.loginToken = data
        } else this.reLogin(message)
        wx.hideLoading()
      } catch ({
        errMsg
      }) {
        console.error(errMsg)
        this.reLogin()
      }
    },
    /**获取loginToken */
    async reLogin(message) {
      const reload = await loginModal({
        message: message || '获取loginToken失败'
      })
      if (reload) this.init()
    }
  }
})