import { defineComponent } from 'vue' import { CellGroup, Field, Button, CountDown, Row, Col, showToast, Popup } from 'vant' import ImgCode from '@/components/o-img-code' import { checkPhone } from '@/helpers/validate' import { setLogin, state } from '@/state' import { removeAuth, setAuth } from './utils' import styles from './index.module.less' import request from '@/helpers/request' import { browser, getUrlCode } from '@/helpers/utils' import qs from 'query-string' type loginType = 'PWD' | 'SMS' export default defineComponent({ name: 'login-music', data() { return { loginType: 'SMS' as loginType, username: '', password: '', smsCode: '', countDownStatus: true, // 是否发送验证码 countDownTime: 1000 * 120, // 倒计时时间 // countDownRef: null as any, // 倒计时实例 imgCodeStatus: false, showPopup: false, code: '' // 授权code码 } }, computed: { codeDisable() { let status = true this.username && this.smsCode && (status = false) return status } }, mounted() { removeAuth() this.directNext() // 判断是否是微信,只能微信中打开 if (browser().weixin) { // 微信公众号支付 //授权 const code = getUrlCode() console.log('login mounted code: ' + code) if (!code) { this.getAppIdAndCode() } else { this.code = code } } else { this.showPopup = true } }, methods: { async getAppIdAndCode() { try { const { data } = await request.get('/api-student/open/paramConfig/wechatAppId') // 判断是否有微信appId if (data) { this.goAuth(data) } } catch { // } }, goAuth(wxAppId: string) { // 用户授权 const urlNow = encodeURIComponent(window.location.href) const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知 const appid = wxAppId || 'wx8654c671631cfade' const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect` window.location.replace(url) }, directNext() { if (state.user.status === 'login' || state.user.status === 'error') { const { returnUrl, isRegister, ...rest } = this.$route.query console.log( { ...rest, code: this.code }, 'jump pre registration' ) // console.log(returnUrl, isRegister, { ...rest }) // this.$router.replace({ // path: returnUrl as any, // query: { // ...rest, // code: this.code // } // }) const newUrl = window.location.origin + window.location.pathname + '#' + returnUrl + '?' + qs.stringify({ ...rest, code: this.code }) this.locationReplace(newUrl) } }, locationReplace(url: any) { // 只允许同域名 console.log(history.replaceState, 'window.history.replaceState') if (history.replaceState) { history.replaceState(null, document.title, url) history.go(0) } else { location.replace(url) } }, async onLogin() { try { // let res: any const forms: any = { username: this.username, client_id: 'jmedu-student', client_secret: 'jmedu-student', autoRegister: true, password: this.smsCode, loginType: 'SMS', 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('/api-student/appLoginUser/getUserInfo', { initRequest: true // 初始化接口 }) setLogin(userCash.data) this.directNext() } catch { // } }, async onSendCode() { // 发送验证码 if (!checkPhone(this.username)) { return showToast('请输入正确的手机号码') } 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.countDownStatus ? ( 获取验证码 ) : ( ) }} />
{this.imgCodeStatus ? ( { this.imgCodeStatus = false }} onSendCode={this.onCodeSend} /> ) : null}
提示

请使用微信打开

) } })