import { defineComponent } from 'vue' import { CellGroup, Field, Button, CountDown, Row, Col, Toast } from 'vant' import ImgCode from '@/components/o-img-code' import { checkPhone } from '@/helpers/validate' import request from '@/helpers/request' import { setLogin, state } from '@/state' import { removeAuth, setAuth } from '@/helpers/utils' import styles from './login.module.less' type loginType = 'PWD' | 'SMS' export default defineComponent({ name: 'login', data() { return { loginType: 'SMS' as loginType, username: '', password: '', smsCode: '', countDownStatus: true, // 是否发送验证码 countDownTime: 1000 * 120, // 倒计时时间 // countDownRef: null as any, // 倒计时实例 imgCodeStatus: false } }, computed: { codeDisable() { let status = true if (this.loginType === 'PWD') { this.username && this.password && (status = false) } else { this.username && this.smsCode && (status = false) } return status }, appName() { const template = { STUDENT: '学生端', TEACHER: '老师端', SCHOOL: '管理端' } return template[state.platformType] } }, mounted() { removeAuth() this.directNext() }, methods: { directNext() { if (state.user.status === 'login' || state.user.status === 'error') { const { returnUrl, isRegister, ...rest } = this.$route.query this.$router.replace({ path: returnUrl as any, query: { ...rest } }) } }, async onLogin() { try { // let res: any const forms: any = { username: this.username, client_id: state.clientId[state.platformType], client_secret: state.clientId[state.platformType] } if (this.loginType === 'PWD') { forms.password = this.password forms.loginType = 'PASSWORD' forms.grant_type = 'PASSWORD' } else { forms.password = this.smsCode forms.loginType = 'SMS' forms.grant_type = 'SMS' } const { data } = await request.post('/api-oauth/userlogin', { requestType: 'form', data: { ...forms } }) setAuth(data.token_type + ' ' + data.access_token) const userCash = await request.get(state.platformApi + '/appLoginUser/getUserInfo', { initRequest: true // 初始化接口 }) setLogin(userCash.data) this.directNext() } catch { // } }, async onSendCode() { // 发送验证码 if (!checkPhone(this.username)) { return Toast('请输入正确的手机号码') } this.imgCodeStatus = true }, onCodeSend() { this.countDownStatus = false this.$nextTick(() => { ;(this.$refs.countDownRef as any).start() }) }, onFinished() { this.countDownStatus = true ;(this.$refs.countDownRef as any).reset() }, onChange() { if (this.loginType === 'PWD') { this.loginType = 'SMS' } else if (this.loginType === 'SMS') { this.loginType = 'PWD' } } }, render() { return (
您好,
欢迎使用管乐团{this.appName}
手机号 {this.loginType === 'PWD' ? ( 密码 ) : ( 验证码 this.countDownStatus ? ( 获取验证码 ) : ( ) }} /> )}
{this.imgCodeStatus ? ( { this.imgCodeStatus = false }} onSendCode={this.onCodeSend} /> ) : null}
) } })