|
@@ -8,12 +8,8 @@
|
|
</route>
|
|
</route>
|
|
|
|
|
|
<script lang="ts" setup>
|
|
<script lang="ts" setup>
|
|
-import type { ILoginForm } from '@/api/login'
|
|
|
|
-// 必须导入需要用到的页面生命周期(即使在当前页面上没有直接使用到)
|
|
|
|
-// eslint-disable-next-line unused-imports/no-unused-imports
|
|
|
|
-import { onPageScroll, onReachBottom } from '@dcloudio/uni-app'
|
|
|
|
-import { login } from '@/api/login'
|
|
|
|
import { useUserStore } from '@/store/user'
|
|
import { useUserStore } from '@/store/user'
|
|
|
|
+import { goBack, toPage } from '@/utils/page'
|
|
import { toast } from '@/utils/toast'
|
|
import { toast } from '@/utils/toast'
|
|
|
|
|
|
defineOptions({
|
|
defineOptions({
|
|
@@ -33,74 +29,26 @@ const formData = ref({
|
|
password: '',
|
|
password: '',
|
|
})
|
|
})
|
|
|
|
|
|
-// 登录处理
|
|
|
|
-async function handleLogin() {
|
|
|
|
- try {
|
|
|
|
- // 表单验证
|
|
|
|
- const isValid = await validateForm()
|
|
|
|
- if (!isValid)
|
|
|
|
- return
|
|
|
|
-
|
|
|
|
- // 显示加载状态
|
|
|
|
- uni.showLoading({
|
|
|
|
- title: 'Logging in...',
|
|
|
|
- mask: true,
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
- // 准备登录数据,根据 ILoginForm 接口要求
|
|
|
|
- const loginData: ILoginForm = {
|
|
|
|
- account: formData.value.username,
|
|
|
|
- pwd: formData.value.password,
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 调用登录接口
|
|
|
|
- const res = await login(loginData)
|
|
|
|
- uni.hideLoading()
|
|
|
|
- // 登录成功,使用 userStore 管理用户状态和 token
|
|
|
|
- uni.setStorageSync('token', res.data.token)
|
|
|
|
- // 使用状态管理工具设置用户信息,会自动处理 token 存储
|
|
|
|
- userStore.getUserInfo()
|
|
|
|
- // 登录成功提示
|
|
|
|
- toast.success('Login successful!')
|
|
|
|
- }
|
|
|
|
- catch (error) {
|
|
|
|
- uni.hideLoading()
|
|
|
|
- uni.showToast({
|
|
|
|
- title: error.message || 'Login failed',
|
|
|
|
- icon: 'none',
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// 表单验证
|
|
// 表单验证
|
|
function validateForm() {
|
|
function validateForm() {
|
|
return new Promise((resolve) => {
|
|
return new Promise((resolve) => {
|
|
// 验证用户名
|
|
// 验证用户名
|
|
if (!formData.value.username.trim()) {
|
|
if (!formData.value.username.trim()) {
|
|
- uni.showToast({
|
|
|
|
- title: 'Please enter username or phone number',
|
|
|
|
- icon: 'none',
|
|
|
|
- })
|
|
|
|
|
|
+ toast.error('Please enter username or phone number')
|
|
resolve(false)
|
|
resolve(false)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
// 验证密码
|
|
// 验证密码
|
|
if (!formData.value.password.trim()) {
|
|
if (!formData.value.password.trim()) {
|
|
- uni.showToast({
|
|
|
|
- title: 'Please enter password',
|
|
|
|
- icon: 'none',
|
|
|
|
- })
|
|
|
|
|
|
+ toast.error('Please enter password')
|
|
resolve(false)
|
|
resolve(false)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
// 验证密码长度
|
|
// 验证密码长度
|
|
if (formData.value.password.length < 6 || formData.value.password.length > 20) {
|
|
if (formData.value.password.length < 6 || formData.value.password.length > 20) {
|
|
- uni.showToast({
|
|
|
|
- title: 'Password should be 6-20 characters',
|
|
|
|
- icon: 'none',
|
|
|
|
- })
|
|
|
|
|
|
+ toast.error('Password should be 6-20 characters')
|
|
resolve(false)
|
|
resolve(false)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -109,23 +57,32 @@ function validateForm() {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
-// 跳转注册页
|
|
|
|
-function toRegister() {
|
|
|
|
- uni.navigateTo({
|
|
|
|
- url: '/pages/register/register',
|
|
|
|
- })
|
|
|
|
-}
|
|
|
|
|
|
+// 登录处理
|
|
|
|
+async function handleLogin() {
|
|
|
|
+ try {
|
|
|
|
+ // 表单验证
|
|
|
|
+ const isValid = await validateForm()
|
|
|
|
+ if (!isValid)
|
|
|
|
+ return
|
|
|
|
|
|
-// 跳转忘记密码页
|
|
|
|
-function toForgotPassword() {
|
|
|
|
- uni.navigateTo({
|
|
|
|
- url: '/pages/forgotPassword/forgotPassword',
|
|
|
|
- })
|
|
|
|
-}
|
|
|
|
|
|
+ // 准备登录数据
|
|
|
|
+ const loginData = {
|
|
|
|
+ account: formData.value.username,
|
|
|
|
+ pwd: formData.value.password,
|
|
|
|
+ }
|
|
|
|
|
|
-// 返回上一页
|
|
|
|
-function goBack() {
|
|
|
|
- uni.navigateBack()
|
|
|
|
|
|
+ // 获取页面参数中的跳转地址(如果有的话)
|
|
|
|
+ const pages = getCurrentPages()
|
|
|
|
+ const currentPage = pages[pages.length - 1] as any
|
|
|
|
+ const redirectUrl = currentPage?.options?.redirect
|
|
|
|
+
|
|
|
|
+ // 调用 userStore 中的登录方法,传入跳转地址
|
|
|
|
+ await userStore.login(loginData, redirectUrl)
|
|
|
|
+ }
|
|
|
|
+ catch (error) {
|
|
|
|
+ // 错误处理已在 userStore.login 中处理
|
|
|
|
+ console.error('Login error:', error)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
</script>
|
|
</script>
|
|
|
|
|
|
@@ -193,7 +150,7 @@ function goBack() {
|
|
type="error"
|
|
type="error"
|
|
plain
|
|
plain
|
|
custom-style="width: 200rpx; height: 72rpx; border-radius: 36rpx; font-size: 28rpx;"
|
|
custom-style="width: 200rpx; height: 72rpx; border-radius: 36rpx; font-size: 28rpx;"
|
|
- @click="toRegister"
|
|
|
|
|
|
+ @click="toPage('/pages/register/register')"
|
|
>
|
|
>
|
|
Register
|
|
Register
|
|
</wd-button>
|
|
</wd-button>
|
|
@@ -202,7 +159,7 @@ function goBack() {
|
|
|
|
|
|
<!-- 忘记密码 -->
|
|
<!-- 忘记密码 -->
|
|
<view class="text-center">
|
|
<view class="text-center">
|
|
- <text class="text-28rpx text-#5C5C5C" @click="toForgotPassword">
|
|
|
|
|
|
+ <text class="text-28rpx text-#5C5C5C" @click="toPage('/pages/forgotPassword/forgotPassword')">
|
|
Forgot Password?
|
|
Forgot Password?
|
|
</text>
|
|
</text>
|
|
</view>
|
|
</view>
|