import request from '@/helpers/request' import { ElButton, ElForm, ElFormItem, ElInput, ElLink, ElMessage } from 'element-plus' import { defineComponent, nextTick } from 'vue' import Cookies from 'js-cookie' import ImgCode from '../img-code' import styles from './index.module.less' import { setAuth } from '@/helpers/utils' import { checkPhone } from '@/helpers/validate' import iconSuccess from '@/common/images/icon_success.png' // const validatePassword = ( // rule: any, // value: string | any[], // callback: () => void // ) => { // if (value.length < 6) { // // @ts-ignore // callback(new Error('密码必须大于六位')) // } else { // callback() // } // } export default defineComponent({ name: 'loginForm', props: { type: { type: String, default: 'login' as 'login' | 'register' }, loginType: { type: String, default: 'TEACHER' as 'TEACHER' | 'STUDENT' }, onClose: { type: Function, default: () => {} }, onChange: { type: Function, default: (type: string) => {} }, onLoginTypeChange: { type: Function, default: (type: string) => {} } }, data() { return { loading: false, codeDsiable: false, codeStatus: false, // 是否显示图形验证码 codeTimer: 120, // 发短信时长 codeInverval: null as any, form: { username: '', code: '' // password: '' }, formRules: { username: [ { required: true, message: '请输入手机号', trigger: 'blur' }, { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' } ], code: [{ required: true, message: '请输入验证码', trigger: 'blur' }] // password: [ // { required: true, trigger: 'blur', validator: validatePassword } // ] } } }, unmounted() { console.log('form unmounted') clearInterval(this.codeInverval) }, methods: { onSubmit() { ;(this as any).$refs.loginForm.validate(async (valid: boolean) => { if (valid) { if (this.type === 'login') { const params = { isSurportRegister: false, loginUserType: this.loginType } await this.onLogin(params) } else if (this.type === 'register') { const params = { isSurportRegister: true, loginUserType: this.loginType } await this.onLogin(params) } } }) }, async onLogin(params: any) { this.loading = true try { const form = this.form const res = await request.post('/api-auth/smsLogin', { requestType: 'form', data: { clientId: 'website', clientSecret: 'website', phone: form.username, smsCode: form.code, ...params } }) const { authentication } = res.data const token = authentication.token_type + ' ' + authentication.access_token setAuth( JSON.stringify({ token, loginUserType: params.loginUserType }) ) // console.log(res, 'res') if (this.type === 'login') { window.location.reload() this.onClose() } else if (this.type === 'register') { this.onChange('register-success') } } catch (e: any) { console.log(e) } this.loading = false }, onResetFields() { ;(this as any).$refs.loginForm.resetFields() } }, render() { return ( (
{ if (!checkPhone(this.form.username)) { return ElMessage.error('请输入正确的手机号码') } this.codeStatus = true }} > {!this.codeDsiable ? '获取验证码' : this.codeTimer + 's'}
) }} >
{/* {(this.type === 'teacher-register' || this.type === 'student-register') && ( )} */} {this.type === 'login' ? (
{ this.onLoginTypeChange('TEACHER') }} > {this.loginType === 'TEACHER' ? ( ) : (
)} 老师登录
{ this.onLoginTypeChange('STUDENT') }} > {this.loginType === 'STUDENT' ? ( ) : (
)} 学生登录
) : (
)} {this.type === 'teacher-login' || this.type === 'student-login' ? '登 录' : '注 册'} {/* 图形验证码 */} {this.codeStatus && ( { this.codeStatus = false }} onSendCode={async () => { this.codeDsiable = true this.codeInverval = setInterval(() => { this.codeTimer-- if (this.codeTimer === 0) { this.codeDsiable = false clearInterval(this.codeInverval) this.codeTimer = 120 } }, 1000) }} /> )}
) } })