619 lines
17 KiB
Vue
619 lines
17 KiB
Vue
<script setup>
|
||
import { ref } from 'vue';
|
||
import { login, register, loginByEmail, sendCodeToEmail, registerByEmail } from './api';
|
||
import { useUserStore } from '@/store/user.js';
|
||
import { showLoadingToast, closeToast } from '@/common/common.js';
|
||
|
||
const email = ref(''); // 邮箱
|
||
const password = ref(''); // 密码
|
||
const checked = ref(true); // 同意条款,默认勾选
|
||
const isLoading = ref(false); // 加载状态
|
||
|
||
// 注册相关变量
|
||
const showRegister = ref(false); // 显示注册弹窗
|
||
const registerEmail = ref(''); // 注册邮箱
|
||
const registerPassword = ref(''); // 注册密码
|
||
const confirmPassword = ref(''); // 确认密码
|
||
const verificationCode = ref(''); // 验证码
|
||
const codeBtnText = ref('获取验证码');
|
||
const codeBtnDisabled = ref(false);
|
||
let countdownTimer = null;
|
||
|
||
const userStore = useUserStore();
|
||
|
||
// 邮箱格式验证
|
||
const validateEmail = (email) => {
|
||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||
return emailRegex.test(email);
|
||
};
|
||
|
||
// 登录函数
|
||
const onLogin = async() => {
|
||
if (!checked.value) {
|
||
uni.showToast({
|
||
title: '请先同意用户协议',
|
||
icon: 'none',
|
||
duration: 2000,
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (!email.value) {
|
||
uni.showToast({
|
||
title: '请输入邮箱',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (!password.value) {
|
||
uni.showToast({
|
||
title: '请输入密码',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 邮箱格式验证
|
||
if (!validateEmail(email.value)) {
|
||
uni.showToast({
|
||
title: '请输入正确的邮箱',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 密码长度验证
|
||
if (password.value.length < 6) {
|
||
uni.showToast({
|
||
title: '密码至少6位',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 防止重复提交
|
||
if (isLoading.value) return;
|
||
|
||
isLoading.value = true;
|
||
|
||
try {
|
||
uni.showLoading({
|
||
title: '登录中...',
|
||
mask: true,
|
||
});
|
||
|
||
const params = {
|
||
email: email.value,
|
||
password: password.value
|
||
};
|
||
|
||
const res = await loginByEmail(params);
|
||
|
||
if (res && res.code === 0) { // 修改为code===0判断
|
||
// 存储用户信息到全局状态
|
||
userStore.setUserInfo(res.data);
|
||
userStore.setToken(res.data.accessToken);
|
||
userStore.setLoginStatus(true);
|
||
|
||
uni.showToast({
|
||
title: '登录成功',
|
||
icon: 'success',
|
||
});
|
||
|
||
uni.switchTab({
|
||
url:'/pages/mine/index'
|
||
})
|
||
// 延迟一点时间再关闭弹窗,让用户看到成功提示
|
||
setTimeout(() => {
|
||
// 触发父组件的登录成功事件
|
||
emit('success');
|
||
emit('close');
|
||
}, 1000);
|
||
} else {
|
||
uni.showToast({
|
||
title: res.msg || '登录失败',
|
||
icon: 'error',
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.error('登录错误:', error);
|
||
uni.showToast({
|
||
title: error.message || '登录失败,请稍后重试',
|
||
icon: 'error',
|
||
});
|
||
} finally {
|
||
uni.hideLoading();
|
||
isLoading.value = false;
|
||
}
|
||
};
|
||
|
||
// 同意条款切换
|
||
const onChange = (e) => {
|
||
checked.value = e.detail.value; // 更新checked值
|
||
};
|
||
|
||
// 显示用户协议
|
||
const showAgreement = () => {
|
||
uni.showModal({
|
||
title: '用户协议',
|
||
content: '欢迎使用我们的服务!\n\n本协议规定了您使用我们服务的条款和条件,请仔细阅读。\n\n1. 服务使用条款\n2. 隐私政策\n3. 账户安全\n4. 免责声明\n\n继续使用即表示您同意遵守这些条款。',
|
||
showCancel: true,
|
||
confirmText: '同意',
|
||
cancelText: '取消',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
checked.value = true;
|
||
}
|
||
}
|
||
});
|
||
};
|
||
|
||
// 忘记密码
|
||
const forgetPassword = () => {
|
||
uni.navigateTo({
|
||
url: '/pages/auth/forget-password'
|
||
});
|
||
};
|
||
|
||
// 显示注册弹窗
|
||
const showRegisterPopup = () => {
|
||
showRegister.value = true;
|
||
};
|
||
|
||
// 隐藏注册弹窗
|
||
const hideRegisterPopup = () => {
|
||
showRegister.value = false;
|
||
// 清空注册表单
|
||
registerEmail.value = '';
|
||
registerPassword.value = '';
|
||
confirmPassword.value = '';
|
||
verificationCode.value = '';
|
||
};
|
||
|
||
// 获取验证码
|
||
const getVerificationCode = async () => {
|
||
if (!registerEmail.value) {
|
||
uni.showToast({
|
||
title: '请输入邮箱',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (!validateEmail(registerEmail.value)) {
|
||
uni.showToast({
|
||
title: '请输入正确的邮箱',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 发送验证码
|
||
try {
|
||
uni.showLoading({
|
||
title: '发送中...',
|
||
mask: true,
|
||
});
|
||
|
||
const params = {
|
||
email: registerEmail.value,
|
||
register: true
|
||
};
|
||
|
||
const res = await sendCodeToEmail(params);
|
||
|
||
if (res && res.code === 0) {
|
||
uni.hideLoading();
|
||
uni.showToast({
|
||
title: '验证码已发送',
|
||
icon: 'success',
|
||
});
|
||
|
||
// 开始倒计时
|
||
let timeLeft = 60;
|
||
codeBtnDisabled.value = true;
|
||
codeBtnText.value = `${timeLeft}秒`;
|
||
|
||
countdownTimer = setInterval(() => {
|
||
timeLeft--;
|
||
codeBtnText.value = `${timeLeft}秒`;
|
||
|
||
if (timeLeft <= 0) {
|
||
clearInterval(countdownTimer);
|
||
codeBtnText.value = '重新获取';
|
||
codeBtnDisabled.value = false;
|
||
}
|
||
}, 1000);
|
||
} else {
|
||
uni.hideLoading();
|
||
uni.showToast({
|
||
title: res.msg || '发送失败',
|
||
icon: 'error',
|
||
});
|
||
}
|
||
} catch (error) {
|
||
uni.hideLoading();
|
||
uni.showToast({
|
||
title: error.message || '发送失败,请重试',
|
||
icon: 'error',
|
||
});
|
||
}
|
||
};
|
||
|
||
// 注册提交
|
||
const onRegister = async () => {
|
||
if (!registerEmail.value) {
|
||
uni.showToast({
|
||
title: '请输入邮箱',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (!validateEmail(registerEmail.value)) {
|
||
uni.showToast({
|
||
title: '请输入正确的邮箱',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (!verificationCode.value) {
|
||
uni.showToast({
|
||
title: '请输入验证码',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (!registerPassword.value) {
|
||
uni.showToast({
|
||
title: '请输入密码',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (registerPassword.value !== confirmPassword.value) {
|
||
uni.showToast({
|
||
title: '两次输入密码不一致',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (registerPassword.value.length < 6) {
|
||
uni.showToast({
|
||
title: '密码至少6位',
|
||
icon: 'none',
|
||
});
|
||
return;
|
||
}
|
||
|
||
try {
|
||
uni.showLoading({
|
||
title: '注册中...',
|
||
mask: true,
|
||
});
|
||
|
||
// 调用注册API
|
||
const registerParams = {
|
||
email: registerEmail.value,
|
||
verificationCode: verificationCode.value,
|
||
password: registerPassword.value,
|
||
confirmPassword: confirmPassword.value
|
||
};
|
||
|
||
const res = await registerByEmail(registerParams);
|
||
|
||
if (res && res.code === 0) {
|
||
uni.hideLoading();
|
||
uni.showToast({
|
||
title: '注册成功',
|
||
icon: 'success',
|
||
});
|
||
|
||
// 注册成功后直接调用登录接口
|
||
setTimeout(async () => {
|
||
try {
|
||
const loginRes = await loginByEmail({
|
||
email: registerEmail.value,
|
||
password: registerPassword.value
|
||
});
|
||
|
||
if (loginRes && loginRes.code === 0) {
|
||
// 存储用户信息到全局状态
|
||
userStore.setUserInfo(loginRes.data);
|
||
userStore.setToken(loginRes.data.accessToken);
|
||
userStore.setLoginStatus(true);
|
||
|
||
uni.showToast({
|
||
title: '登录成功',
|
||
icon: 'success',
|
||
});
|
||
|
||
// 关闭注册弹窗和登录弹窗
|
||
setTimeout(() => {
|
||
emit('success');
|
||
emit('close');
|
||
hideRegisterPopup();
|
||
}, 1000);
|
||
} else {
|
||
uni.showToast({
|
||
title: loginRes.msg || '自动登录失败',
|
||
icon: 'none',
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.error('自动登录失败:', error);
|
||
uni.showToast({
|
||
title: '自动登录失败,请手动登录',
|
||
icon: 'none',
|
||
});
|
||
}
|
||
}, 1000);
|
||
} else {
|
||
uni.hideLoading();
|
||
uni.showToast({
|
||
title: res.msg || '注册失败',
|
||
icon: 'error',
|
||
});
|
||
}
|
||
} catch (error) {
|
||
uni.hideLoading();
|
||
uni.showToast({
|
||
title: error.message || '注册失败,请稍后重试',
|
||
icon: 'error',
|
||
});
|
||
}
|
||
};
|
||
|
||
// 清空输入框
|
||
const clearInput = (type) => {
|
||
if (type === 'email') {
|
||
email.value = '';
|
||
} else if (type === 'password') {
|
||
password.value = '';
|
||
} else if (type === 'registerEmail') {
|
||
registerEmail.value = '';
|
||
} else if (type === 'registerPassword') {
|
||
registerPassword.value = '';
|
||
} else if (type === 'confirmPassword') {
|
||
confirmPassword.value = '';
|
||
} else if (type === 'verificationCode') {
|
||
verificationCode.value = '';
|
||
}
|
||
};
|
||
|
||
// 监听输入变化,清除错误状态
|
||
const onInputChange = (type) => {
|
||
if (type === 'email') {
|
||
// 移除邮箱错误状态
|
||
} else if (type === 'password') {
|
||
// 移除密码错误状态
|
||
}
|
||
};
|
||
|
||
// 暴露事件给父组件
|
||
const emit = defineEmits(['close', 'success']);
|
||
</script>
|
||
|
||
<template>
|
||
<view class="h-[120vh] p-4 min-h-screen bg-white flex flex-col justify-center">
|
||
<!-- 标题 -->
|
||
<view class="mb-12 mt-20 text-center">
|
||
<image src="/static/tabbar/find.png" class="w-20 h-20 mb-8" mode="aspectFit"></image>
|
||
<text class="block text-3xl font-bold text-gray-800 mb-4">WELCOME BACK</text>
|
||
<text class="text-sm text-gray-500">Sign in to connect with Sweekar</text>
|
||
</view>
|
||
|
||
<!-- 邮箱输入 -->
|
||
<view class="mb-8">
|
||
<view class=" flex items-center h-12 bg-gray-100 rounded-lg px-6 gap-1 border border-solid border-round-theme">
|
||
<u-icon name="email" class="h-10" color="black" bold="true"></u-icon>
|
||
<input
|
||
class="w-full text-base text-gray-800 bg-transparent"
|
||
type="email"
|
||
placeholder="Email Address"
|
||
v-model="email"
|
||
@input="onInputChange('email')"
|
||
/>
|
||
<view
|
||
v-if="email"
|
||
class="absolute right-6 w-8 h-8 flex items-center justify-center bg-gray-400 rounded-full text-black font-bold"
|
||
@tap="clearInput('email')"
|
||
>
|
||
×
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 密码输入 -->
|
||
<view class="mb-8">
|
||
|
||
<view class=" flex items-center h-12 bg-gray-100 rounded-lg px-6 gap-1 border border-solid border-round-theme">
|
||
<u-icon name="lock" class="h-10" color="black" bold="true"></u-icon>
|
||
<input
|
||
class="w-full text-base text-gray-800 bg-transparent"
|
||
type="password"
|
||
placeholder="Password"
|
||
v-model="password"
|
||
@input="onInputChange('password')"
|
||
/>
|
||
<view
|
||
v-if="password"
|
||
class="absolute right-6 w-8 h-8 flex items-center justify-center bg-gray-400 rounded-full text-white font-bold"
|
||
@tap="clearInput('password')"
|
||
>
|
||
×
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 忘记密码链接 -->
|
||
<view class="flex justify-end mb-8">
|
||
<text class="text-blue-500 text-sm" @tap="forgetPassword">Forget Password?</text>
|
||
</view>
|
||
|
||
<!-- 登录按钮 -->
|
||
<view
|
||
class="w-full h-12 bg-theme rounded-lg text-white text-xl flex items-center justify-center "
|
||
:disabled="isLoading"
|
||
@click="onLogin"
|
||
>
|
||
<text v-if="!isLoading">SIGN IN</text>
|
||
<view v-else class="w-8 h-8 border-2 border-white border-t-transparent rounded-full animate-spin"></view>
|
||
</view>
|
||
|
||
<!-- 注册链接 -->
|
||
<view class="flex justify-center items-center my-8 text-gray-500 text-sm">
|
||
<text>Don't have an account?</text>
|
||
<text class="text-green-500 ml-2" @tap="showRegisterPopup">立即注册</text>
|
||
</view>
|
||
|
||
<!-- 同意条款 -->
|
||
<view class="mt-auto text-center">
|
||
<label class="flex items-center justify-center text-xs text-gray-500 leading-loose">
|
||
<checkbox
|
||
class="scale-75 mr-2"
|
||
:checked="checked"
|
||
@change="onChange"
|
||
/>
|
||
我已阅读并同意
|
||
<text class="text-green-500 underline" @tap="showAgreement">《用户协议》</text>
|
||
和
|
||
<text class="text-green-500 underline" @tap="showAgreement">《隐私政策》</text>
|
||
</label>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 注册弹窗 -->
|
||
<u-popup
|
||
v-model:show="showRegister"
|
||
mode="center"
|
||
:border-radius="20"
|
||
:safe-area-inset-bottom="false"
|
||
width="80%"
|
||
:closeable="true"
|
||
@close="hideRegisterPopup"
|
||
>
|
||
<view class="p-4 min-h-screen bg-white">
|
||
<view class="mb-12 text-center">
|
||
<text class="block text-2xl font-bold text-gray-800 mb-4">用户注册</text>
|
||
<text class="text-lg text-gray-500">请填写注册信息</text>
|
||
</view>
|
||
|
||
<!-- 注册邮箱输入 -->
|
||
<view class="mb-8">
|
||
<view class="text-sm text-gray-700 mb-4 ml-2">邮箱</view>
|
||
<view class="relative flex items-center h-12 bg-gray-100 rounded-full px-6">
|
||
<input
|
||
class="w-full text-base text-gray-800 bg-transparent"
|
||
type="text"
|
||
placeholder="请输入邮箱"
|
||
v-model="registerEmail"
|
||
/>
|
||
<view
|
||
v-if="registerEmail"
|
||
class="absolute right-6 w-8 h-8 flex items-center justify-center bg-gray-400 rounded-full text-white font-bold"
|
||
@tap="clearInput('registerEmail')"
|
||
>
|
||
×
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 验证码输入 -->
|
||
<view class="mb-8">
|
||
<view class="text-sm text-gray-700 mb-4 ml-2">验证码</view>
|
||
<view class="relative flex items-center h-12 bg-gray-100 rounded-full px-6">
|
||
<input
|
||
class="w-full text-base text-gray-800 bg-transparent pr-32"
|
||
type="text"
|
||
placeholder="请输入验证码"
|
||
v-model="verificationCode"
|
||
/>
|
||
<view
|
||
v-if="verificationCode"
|
||
class="absolute right-36 w-8 h-8 flex items-center justify-center bg-gray-400 rounded-full text-white font-bold"
|
||
@tap="clearInput('verificationCode')"
|
||
>
|
||
×
|
||
</view>
|
||
</view>
|
||
<button
|
||
class="absolute right-2 top-1/2 transform -translate-y-1/2 h-10 px-4 bg-gradient-to-r from-green-500 to-green-400 text-white text-xs rounded-full disabled:bg-gray-400 disabled:text-gray-300"
|
||
:disabled="codeBtnDisabled"
|
||
@tap="getVerificationCode"
|
||
>
|
||
{{ codeBtnText }}
|
||
</button>
|
||
</view>
|
||
|
||
<!-- 注册密码输入 -->
|
||
<view class="mb-8">
|
||
<view class="text-sm text-gray-700 mb-4 ml-2">密码</view>
|
||
<view class="relative flex items-center h-12 bg-gray-100 rounded-full px-6">
|
||
<input
|
||
class="w-full text-base text-gray-800 bg-transparent"
|
||
type="password"
|
||
placeholder="请输入密码"
|
||
v-model="registerPassword"
|
||
/>
|
||
<view
|
||
v-if="registerPassword"
|
||
class="absolute right-6 w-8 h-8 flex items-center justify-center bg-gray-400 rounded-full text-white font-bold"
|
||
@tap="clearInput('registerPassword')"
|
||
>
|
||
×
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 确认密码输入 -->
|
||
<view class="mb-8">
|
||
<view class="text-sm text-gray-700 mb-4 ml-2">确认密码</view>
|
||
<view class="relative flex items-center h-12 bg-gray-100 rounded-full px-6">
|
||
<input
|
||
class="w-full text-base text-gray-800 bg-transparent"
|
||
type="password"
|
||
placeholder="请再次输入密码"
|
||
v-model="confirmPassword"
|
||
/>
|
||
<view
|
||
v-if="confirmPassword"
|
||
class="absolute right-6 w-8 h-8 flex items-center justify-center bg-gray-400 rounded-full text-white font-bold"
|
||
@tap="clearInput('confirmPassword')"
|
||
>
|
||
×
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<!-- 注册按钮 -->
|
||
<button
|
||
class="w-full h-12 bg-gradient-to-r from-green-500 to-green-400 rounded-full text-white text-xl font-bold flex items-center justify-center mt-5"
|
||
@tap="onRegister"
|
||
>
|
||
注册
|
||
</button>
|
||
|
||
<!-- 同意条款 -->
|
||
<view class="mt-auto text-center">
|
||
<label class="flex items-center justify-center text-xs text-gray-500 leading-loose">
|
||
<checkbox
|
||
class="scale-75 mr-2"
|
||
:checked="checked"
|
||
@change="onChange"
|
||
/>
|
||
我已阅读并同意
|
||
<text class="text-green-500 underline" @tap="showAgreement">《用户协议》</text>
|
||
和
|
||
<text class="text-green-500 underline" @tap="showAgreement">《隐私政策》</text>
|
||
</label>
|
||
</view>
|
||
</view>
|
||
</u-popup>
|
||
</template>
|