|
@@ -1,21 +1,19 @@
|
|
|
import type { FormInstance, FormRules } from 'element-plus'
|
|
|
-import { registerApi } from '@/api/model/user'
|
|
|
+import { getEmailCodeApi, validateEmailIsExistApi } from '@/api/model/user'
|
|
|
import { useUserStore } from '@/stores/modules/user'
|
|
|
|
|
|
-const { isLoginModalOpen, closeLoginModal } = useLoginModal()
|
|
|
-
|
|
|
-// const userStore = useUserStore()
|
|
|
-// const { login } = userStore
|
|
|
+const { closeLoginModal } = useLoginModal()
|
|
|
+const { loading, onGoogleLogin } = useGoogleLogin()
|
|
|
|
|
|
const loginForm = ref<any>({
|
|
|
- mail: '',
|
|
|
- code: '',
|
|
|
+ email: '',
|
|
|
+ captcha: '',
|
|
|
})
|
|
|
const emailStep = ref<number>(0)
|
|
|
const seconds = ref<number>(0)
|
|
|
-const selectedAccount = ref<string>('')
|
|
|
+const selectedMethod = ref<string>('google')
|
|
|
const errorCodeTxt = ref<string>('')
|
|
|
-const isEmailGoogle = ref<string>('')
|
|
|
+const isEmailGoogle = ref<boolean>(false)
|
|
|
export default function useRegister() {
|
|
|
const nextStep = () => {
|
|
|
emailStep.value++
|
|
@@ -42,17 +40,24 @@ export default function useRegister() {
|
|
|
* 二选一选择邮箱--下一步
|
|
|
*/
|
|
|
const onSelectEmail = () => {
|
|
|
- selectedAccount.value = ''
|
|
|
- loginForm.value.mail = ''
|
|
|
+ selectedMethod.value = 'email'
|
|
|
+ loginForm.value.email = ''
|
|
|
nextStep()
|
|
|
}
|
|
|
/**
|
|
|
* 选择曾经登陆过的账号--下一步
|
|
|
*/
|
|
|
- const onSelectAccount = (item: any) => {
|
|
|
- selectedAccount.value = item.email
|
|
|
- loginForm.value.mail = item.email
|
|
|
- emailStep.value = 1
|
|
|
+ const onSelectAccount = async (item: any) => {
|
|
|
+ console.log('onSelectAccount', item, loading.value)
|
|
|
+ // type: 'google' | 'email'
|
|
|
+ if (item.type === 'google') {
|
|
|
+ const result = await onGoogleLogin()
|
|
|
+ await onGoogleLoginSuccess(result)
|
|
|
+ }
|
|
|
+ if (item.type === 'email') {
|
|
|
+ loginForm.value.email = item.email
|
|
|
+ emailStep.value = 1
|
|
|
+ }
|
|
|
}
|
|
|
/**
|
|
|
* dao: 倒计时60秒
|
|
@@ -73,7 +78,7 @@ export default function useRegister() {
|
|
|
if (seconds.value > 0)
|
|
|
return
|
|
|
// 获取验证码
|
|
|
- // const res = await registerApi(loginForm.value.mail)
|
|
|
+ await getEmailCodeApi({ account: loginForm.value.email, type: 's003_login' })
|
|
|
countSeconds()
|
|
|
}
|
|
|
catch (error) {
|
|
@@ -90,15 +95,24 @@ export default function useRegister() {
|
|
|
await formEl.validate(async (valid, fields) => {
|
|
|
if (valid) {
|
|
|
// 验证已存在google的邮箱
|
|
|
+ const res: any = await validateEmailIsExistApi({ email: loginForm.value.email })
|
|
|
+ console.log('loginedAccountList', res)
|
|
|
|
|
|
- isEmailGoogle.value = 'google' // 模拟google邮箱验证
|
|
|
+ if (!res || res.last_login_type !== 'google')
|
|
|
+ // 如果没有google的邮箱,直接下一步,或者没有注册过邮箱
|
|
|
+ isEmailGoogle.value = false
|
|
|
+ if (res.last_login_type === 'google') {
|
|
|
+ // 显示google登陆按钮
|
|
|
+ isEmailGoogle.value = true
|
|
|
+ loginForm.value.email = ''
|
|
|
+ }
|
|
|
nextStep()
|
|
|
}
|
|
|
else { console.log('error submit!', fields) }
|
|
|
})
|
|
|
}
|
|
|
/**
|
|
|
- * 邮箱--验证码--完成注册
|
|
|
+ * 邮箱--验证码--完成登陆 (后台默认为注册)
|
|
|
* @returns
|
|
|
*/
|
|
|
async function finishCode(formEl: FormInstance | undefined) {
|
|
@@ -106,24 +120,46 @@ export default function useRegister() {
|
|
|
if (!formEl)
|
|
|
return
|
|
|
await formEl.validate(async (valid, fields) => {
|
|
|
- if (valid)
|
|
|
- await handleLogin()
|
|
|
+ if (valid) {
|
|
|
+ await handleLogin({
|
|
|
+ email: loginForm.value.email,
|
|
|
+ captcha: loginForm.value.captcha,
|
|
|
+ type: 'email',
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
- else console.log('error submit!', fields)
|
|
|
+ else { console.log('error submit!', fields) }
|
|
|
})
|
|
|
}
|
|
|
/**
|
|
|
* 登录--提交
|
|
|
*/
|
|
|
- async function handleLogin() {
|
|
|
+ async function handleLogin(params: any) {
|
|
|
try {
|
|
|
- // await login(loginForm.value)
|
|
|
- // closeLoginModal({ status: true, isFirstLogin: true })
|
|
|
- isLoginModalOpen.value = false
|
|
|
+ console.log('登陆成功后------000')
|
|
|
+ const { login } = useUserStore()
|
|
|
+ await login(params)
|
|
|
+ console.log('登陆成功后------1111')
|
|
|
+ closeLoginModal({ status: true, isFirstLogin: true })
|
|
|
}
|
|
|
- catch (err) {
|
|
|
- errorCodeTxt.value = 'The code is wrong or invalid.'
|
|
|
+ catch (err: any) {
|
|
|
+ console.log('Login error:', err, selectedMethod.value)
|
|
|
+ if (selectedMethod.value === 'email')
|
|
|
+ errorCodeTxt.value = 'The code is wrong or invalid.'
|
|
|
+ else
|
|
|
+ ElMessage.error(err.message || 'Login failed, please try again later.')
|
|
|
}
|
|
|
}
|
|
|
- return { emailStep, loginForm, selectedAccount, onSelectAccount, errorCodeTxt, seconds, isEmailGoogle, sendEmail, nextStep, onSelectEmail, finishCode, backStep, getMailCode }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * google登陆成功
|
|
|
+ */
|
|
|
+ async function onGoogleLoginSuccess(info: any) {
|
|
|
+ await handleLogin({
|
|
|
+ googleKey: info.token,
|
|
|
+ email: info.userInfo.email,
|
|
|
+ type: 'google',
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return { emailStep, loginForm, onSelectAccount, errorCodeTxt, seconds, isEmailGoogle, sendEmail, nextStep, onSelectEmail, finishCode, onGoogleLoginSuccess, backStep, getMailCode }
|
|
|
}
|