123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- import type { FormInstance, FormRules } from 'element-plus'
- import { getEmailCodeApi, validateEmailIsExistApi, validateEmailIsWorkApi } from '@/api/model/user'
- import { useUserStore } from '@/stores/modules/user'
- const { closeLoginModal } = useLoginModal()
- const { loading, onGoogleLogin } = useGoogleLogin()
- const loginForm = ref<any>({
- email: '',
- captcha: '',
- })
- const emailStep = ref<number>(0)
- const selectedMethod = ref<string>('google')
- const seconds = ref<number>(0)
- const errorCodeTxt = ref<string>('')
- const isEmailGoogle = ref<boolean>(false)
- export default function useRegister() {
- const errorTxt = ref<string>('')
- const showCode = ref<boolean>(true)
- const nextStep = () => {
- emailStep.value++
- }
- const backStep = () => {
- if (emailStep.value > 0)
- emailStep.value--
- }
- /**
- * 二选一选择邮箱--下一步
- */
- const onSelectEmail = () => {
- selectedMethod.value = 'email'
- loginForm.value.email = ''
- nextStep()
- }
- /**
- * 选择曾经登陆过的账号--下一步
- */
- 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') {
- selectedMethod.value = 'email'
- loginForm.value.email = item.email
- emailStep.value = 1
- }
- }
- /**
- * dao: 倒计时60秒
- */
- const countSeconds = () => {
- seconds.value = 60
- const timer = setInterval(() => {
- seconds.value--
- if (seconds.value === 0)
- clearInterval(timer)
- }, 1000)
- }
- /**
- * 邮箱--发送验证码
- */
- const getMailCode = async () => {
- try {
- if (seconds.value > 0)
- return
- // 获取验证码
- await getEmailCodeApi({ account: loginForm.value.email, type: 's003_login' })
- showCode.value = false
- countSeconds()
- }
- catch (error) {
- console.error('Error sending email code:', error)
- }
- }
- /**
- * 邮箱--验证是否存在--是否与已存在google的邮箱--下一步
- * @returns
- */
- async function sendEmail(formEl: FormInstance | undefined) {
- if (!formEl)
- return
- await formEl.validate(async (valid, fields) => {
- if (valid) {
- try {
- // 先验证邮箱是否有效
- const result: any = await validateEmailIsWorkApi({ email: loginForm.value.email })
- if (result === '2') {
- // 触发form的错误提示
- errorTxt.value = 'The email is invalid.'
- formEl.validateField('email')
- return
- }
- // 验证已存在google的邮箱
- const res: any = await validateEmailIsExistApi({ email: loginForm.value.email })
- if (!res || res !== 'google')
- // 如果没有google的邮箱,直接下一步,或者没有注册过邮箱
- isEmailGoogle.value = false
- if (res === 'google') {
- // 显示google登陆按钮
- isEmailGoogle.value = true
- loginForm.value.email = ''
- }
- nextStep()
- }
- catch (error) {
- console.log(error)
- }
- }
- else { console.log('error submit!', fields) }
- })
- }
- /**
- * 邮箱--验证码--完成登陆 (后台默认为注册)
- * @returns
- */
- async function finishCode(formEl: FormInstance | undefined) {
- errorCodeTxt.value = ''
- if (!formEl)
- return
- await formEl.validate(async (valid, fields) => {
- if (valid) {
- await handleLogin({
- email: loginForm.value.email,
- captcha: loginForm.value.captcha,
- type: 'email',
- })
- }
- else { console.log('error submit!', fields) }
- })
- }
- /**
- * 登录--提交
- */
- async function handleLogin(params: any) {
- try {
- const { login } = useUserStore()
- await login(params)
- console.log('登陆成功回调------1111')
- closeLoginModal({ status: true, isFirstLogin: true })
- }
- 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.')
- }
- }
- /**
- * google登陆成功
- */
- async function onGoogleLoginSuccess(info: any) {
- await handleLogin({
- googleKey: info.token,
- email: info.userInfo.email,
- type: 'google',
- })
- }
- return { emailStep, loginForm, errorTxt, onSelectAccount, showCode, errorCodeTxt, seconds, isEmailGoogle, sendEmail, nextStep, onSelectEmail, finishCode, onGoogleLoginSuccess, backStep, getMailCode }
- }
|