wui-components-v2 1.2.48 → 1.2.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { defineEmits, defineOptions, defineProps, reactive, ref } from 'vue'
|
|
3
3
|
import { useRouter } from 'uni-mini-router'
|
|
4
4
|
import { useGlobalToast } from '../../composables/useGlobalToast'
|
|
5
|
-
import { getUserConfig, login } from '../../api/login'
|
|
5
|
+
import { getKaptchaToken, getUserConfig, login } from '../../api/login'
|
|
6
6
|
import { useUser } from '../../composables/useUser'
|
|
7
7
|
import { useTabbar } from '../../composables/useTabbar'
|
|
8
8
|
import type { TabbarItem } from '../../composables/useTabbar'
|
|
@@ -30,11 +30,46 @@ const model = reactive<{
|
|
|
30
30
|
name: string
|
|
31
31
|
password: string
|
|
32
32
|
remember: boolean
|
|
33
|
+
kaptchaText: string
|
|
33
34
|
}>({
|
|
34
35
|
name: uni.getStorageSync('USER_NAME') || '',
|
|
35
36
|
password: uni.getStorageSync('USER_PASSWORD') || '',
|
|
36
37
|
remember: uni.getStorageSync('REMEMBER') || false,
|
|
38
|
+
kaptchaText: '',
|
|
37
39
|
})
|
|
40
|
+
// 是否显示验证码
|
|
41
|
+
const showCaptcha = ref(false)
|
|
42
|
+
// 验证码token
|
|
43
|
+
const kaptchaToken = ref('')
|
|
44
|
+
// 验证码图片
|
|
45
|
+
const captchaImg = ref('')
|
|
46
|
+
// 是否正在加载验证码
|
|
47
|
+
const captchaLoading = ref(false)
|
|
48
|
+
|
|
49
|
+
// 获取验证码
|
|
50
|
+
async function handleGetCaptcha() {
|
|
51
|
+
try {
|
|
52
|
+
captchaLoading.value = true
|
|
53
|
+
// 调用 /v3/auth/kaptcha 接口获取验证码
|
|
54
|
+
const res = await getKaptchaToken()
|
|
55
|
+
// 接口返回:{ img: base64图片, kaptchaToken: 验证码token, status: 'success' }
|
|
56
|
+
const data = res?.data || res
|
|
57
|
+
if (data.status === 'success' || data.kaptchaToken) {
|
|
58
|
+
kaptchaToken.value = data.kaptchaToken
|
|
59
|
+
captchaImg.value = data.img
|
|
60
|
+
showCaptcha.value = true
|
|
61
|
+
// 清空之前输入的验证码
|
|
62
|
+
model.kaptchaText = ''
|
|
63
|
+
} else {
|
|
64
|
+
toast.error({ msg: data.message || '验证码获取失败,请重试' })
|
|
65
|
+
}
|
|
66
|
+
} catch (error: any) {
|
|
67
|
+
console.log(error, '获取验证码失败')
|
|
68
|
+
toast.error({ msg: '验证码获取失败,请重试' })
|
|
69
|
+
} finally {
|
|
70
|
+
captchaLoading.value = false
|
|
71
|
+
}
|
|
72
|
+
}
|
|
38
73
|
const { setTabbarItemActive } = useTabbar(props.tabbarItems)
|
|
39
74
|
const form = ref()
|
|
40
75
|
|
|
@@ -46,8 +81,13 @@ function handleSubmit() {
|
|
|
46
81
|
if (valid) {
|
|
47
82
|
try {
|
|
48
83
|
subloading.value = true
|
|
49
|
-
//
|
|
50
|
-
const res = await login(
|
|
84
|
+
// 登录(如果已显示验证码则携带验证码信息)
|
|
85
|
+
const res = await login(
|
|
86
|
+
model.name,
|
|
87
|
+
model.password,
|
|
88
|
+
showCaptcha.value ? kaptchaToken.value : '',
|
|
89
|
+
showCaptcha.value ? model.kaptchaText : ''
|
|
90
|
+
)
|
|
51
91
|
|
|
52
92
|
if (res.token) {
|
|
53
93
|
// 保存token
|
|
@@ -79,10 +119,14 @@ function handleSubmit() {
|
|
|
79
119
|
toast.success({ msg: '登录成功!' })
|
|
80
120
|
}
|
|
81
121
|
} else {
|
|
82
|
-
toast.error({ msg: res.message })
|
|
122
|
+
toast.error({ msg: res.message || res.msg || '账号或密码错误' })
|
|
123
|
+
// 账号或密码错误时,显示并获取验证码
|
|
124
|
+
handleGetCaptcha()
|
|
83
125
|
}
|
|
84
126
|
} catch (error: any) {
|
|
85
|
-
|
|
127
|
+
// 登录接口返回 status: 'error' 时,core/index.ts 会 reject 整个 res
|
|
128
|
+
// 该函数内部已通过 uni.showToast 提示错误,这里仅触发验证码显示
|
|
129
|
+
handleGetCaptcha()
|
|
86
130
|
}
|
|
87
131
|
} else {
|
|
88
132
|
toast.error({ msg: '请填写登录信息' })
|
|
@@ -122,10 +166,37 @@ function handleSubmit() {
|
|
|
122
166
|
placeholder="请输入密码"
|
|
123
167
|
:rules="[{ required: true, message: '请填写密码' }]"
|
|
124
168
|
/>
|
|
125
|
-
<view class="p-4 pb-0 pt-0 dark:bg-[#1b1b1e]">
|
|
126
|
-
<wd-checkbox v-model="model.remember" shape="square">记住密码</wd-checkbox>
|
|
127
|
-
</view>
|
|
128
169
|
</wd-cell-group>
|
|
170
|
+
<!-- 验证码(账号或密码错误时显示) -->
|
|
171
|
+
<wd-cell-group v-if="showCaptcha" border>
|
|
172
|
+
<wd-input
|
|
173
|
+
v-model="model.kaptchaText"
|
|
174
|
+
label="验证码"
|
|
175
|
+
label-width="100px"
|
|
176
|
+
prop="kaptchaText"
|
|
177
|
+
clearable
|
|
178
|
+
placeholder="请输入验证码"
|
|
179
|
+
:rules="[{ required: true, message: '请填写验证码' }]"
|
|
180
|
+
>
|
|
181
|
+
<template #suffix>
|
|
182
|
+
<view class="flex items-center justify-center" @click="handleGetCaptcha">
|
|
183
|
+
<image
|
|
184
|
+
v-if="captchaImg"
|
|
185
|
+
:src="captchaImg"
|
|
186
|
+
class="h-32px w-90px"
|
|
187
|
+
mode="aspectFit"
|
|
188
|
+
/>
|
|
189
|
+
<wd-button v-else size="small" :round="false" :loading="captchaLoading">
|
|
190
|
+
获取验证码
|
|
191
|
+
</wd-button>
|
|
192
|
+
</view>
|
|
193
|
+
</template>
|
|
194
|
+
</wd-input>
|
|
195
|
+
</wd-cell-group>
|
|
196
|
+
<!-- 记住密码 -->
|
|
197
|
+
<view class="p-4 pb-0 pt-0 dark:bg-[#1b1b1e]">
|
|
198
|
+
<wd-checkbox v-model="model.remember" shape="square">记住密码</wd-checkbox>
|
|
199
|
+
</view>
|
|
129
200
|
</view>
|
|
130
201
|
<view class="p-4">
|
|
131
202
|
<wd-button block :loading="subloading" :round="false" @click="handleSubmit">登录</wd-button>
|