Login.js 4.22 KB
const phoneRex = /^[1]([3-9])[0-9]{9}$/
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loginType: 0,
    isAgree: false,
    mobile: '',
    captcha: '',
    count: 0
  },
  changeLoginType() {
    const {
      loginType
    } = this.data
    this.setData({
      loginType: loginType == 0 ? 1 : 0
    })
  },
  // 同意隐私政策
  handleChangeAgree() {
    this.setData({
      isAgree: !this.data.isAgree
    })
  },

  // 跳转隐私政策页面
  handleAgree(e) {
    const {
      name
    } = e.currentTarget.dataset
    wx.p.router.jumpH5(`https://img02.glsx.com.cn/weapp/resource/saas/scrm/html/hmd/${name}.html`, true)
  },
  async onLoad() {
    wx.p.wxApi.showLoading()
    const {
      data,
      code
    } = await wx.p.api.userApi.login()
    if (code == '0') this.customerLoginCallBack(data)
    if (data) this.sessionId = data.sessionId
    wx.hideLoading()
  },
  input(e) {
    const {
      value
    } = e.detail, {
      name
    } = e.target.dataset
    this.setData({
      [name]: value
    })
  },
  /**获取验证码 */
  async getSmsCode() {
    const {
      mobile,
      count
    } = this.data
    try {
      if (!phoneRex.test(mobile)) throw '请输入正确的手机号'
      if (count) throw '验证码发送过于频繁,请稍后在试'
    } catch (error) {
      return wx.p.wxApi.showToast(error)
    }
    wx.p.wxApi.showLoading()
    try {
      const {
        code,
        message
      } = await wx.p.api.userApi.getSmsCode(mobile)
      if (code == '0') {
        wx.p.wxApi.showToast('验证码已发送')
        this.countDown(60)
      } else wx.p.wxApi.showToast(message || '发送验证码失败')
    } catch (error) {
      wx.p.wxApi.showToast(error)
    }
  },
  /**验证码倒计时 */
  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)
  },
  showTips(message) {
    wx.p.wxApi.showToast(typeof message == 'string' ? message : '请同意以下用户协议')
  },
  /**手机号短信登录 */
  async phoneLogin() {
    const {
      mobile,
      captcha,
      isAgree
    } = this.data
    try {
      if (!mobile) throw '请输入手机号'
      if (!captcha) throw '请输入验证码'
      if (!phoneRex.test(mobile)) throw '请输入正确的手机号'
      if (!isAgree) throw '请同意以下用户协议'
      wx.p.wxApi.showLoading()
      const {
        code,
        data,
        message
      } = await wx.p.api.userApi.phoneLogin({
        mobile,
        captcha,
        sessionId: this.sessionId
      })
      if (code == '0') this.customerLoginCallBack(data)
      else throw message
      wx.hideLoading()
    } catch (error) {
      this.showTips(error || '服务不可用')
      setTimeout(() => {
        this.onLoad()
      }, 1500)
    }
  },
  /**微信登录 */
  async getPhoneNumber({
    detail
  }) {
    const {
      isAgree
    } = this.data
    try {
      if (detail.errMsg && ['authorized', 'deny'].findIndex(e => detail.errMsg.toLowerCase().includes(e)) != -1) return
      if (!detail?.code) throw '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
      if (!isAgree) throw '请同意以下用户协议'
      wx.p.wxApi.showLoading()
      const {
        code,
        data,
        message
      } = await wx.p.api.userApi.authUserPhone(detail, this.sessionId)
      wx.hideLoading()
      if (code == '0') this.customerLoginCallBack(data)
      else throw message
    } catch (error) {
      console.log(error)
      this.showTips(error || '服务不可用')
      setTimeout(() => {
        this.onLoad()
      }, 1500)
    }
  },
  /**C端登录回调 */
  customerLoginCallBack(data) {
    wx.$LoginStatus = false
    wx.p.storage.set('userInfo', data)
    wx.p.storage.set('sessionId', data.sessionId)
    // 返回上一页
    wx.p.router.back(thisPage => {
      thisPage && thisPage.setData({
        userInfo: data
      })
      thisPage.onLoginSuccess && thisPage.onLoginSuccess() //登陆成功后页面回调
    })
  },

  back() {
    wx.p.router.back()
  }
})