|
|
@@ -5,6 +5,7 @@ import { cn } from '@/utils/cn'
|
|
|
import Navbar from '@/components/ui/navbar'
|
|
|
import { isWeapp } from '@/utils/platform'
|
|
|
import { Button } from '@/components/ui/button'
|
|
|
+import { authClient } from '@/api'
|
|
|
|
|
|
export default function WechatLogin() {
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
@@ -62,27 +63,22 @@ export default function WechatLogin() {
|
|
|
throw new Error('获取登录凭证失败')
|
|
|
}
|
|
|
|
|
|
- // 3. 调用后端小程序登录API
|
|
|
- const response = await Taro.request({
|
|
|
- url: `${process.env.API_BASE_URL || ''}/api/v1/auth/mini-login`,
|
|
|
- method: 'POST',
|
|
|
- data: {
|
|
|
+ // 3. 使用RPC client调用后端小程序登录API
|
|
|
+ const response = await authClient['mini-login'].$post({
|
|
|
+ json: {
|
|
|
code: loginRes.code,
|
|
|
userInfo: userProfile.userInfo
|
|
|
- },
|
|
|
- header: {
|
|
|
- 'Content-Type': 'application/json'
|
|
|
}
|
|
|
})
|
|
|
|
|
|
Taro.hideLoading()
|
|
|
|
|
|
- if (response.statusCode === 200) {
|
|
|
- const { token, user, isNewUser } = response.data
|
|
|
+ if (response.status === 200) {
|
|
|
+ const { token, user, isNewUser } = await response.json()
|
|
|
|
|
|
// 4. 保存token和用户信息
|
|
|
- Taro.setStorageSync('token', token)
|
|
|
Taro.setStorageSync('userInfo', user)
|
|
|
+ Taro.setStorageSync('mini_token', token) // 兼容RPC client的token存储
|
|
|
|
|
|
Taro.showToast({
|
|
|
title: isNewUser ? '注册成功' : '登录成功',
|
|
|
@@ -95,7 +91,8 @@ export default function WechatLogin() {
|
|
|
Taro.switchTab({ url: '/pages/index/index' })
|
|
|
}, 1500)
|
|
|
} else {
|
|
|
- throw new Error(response.data.message || '登录失败')
|
|
|
+ const errorData = await response.json()
|
|
|
+ throw new Error(errorData.message || '登录失败')
|
|
|
}
|
|
|
} catch (error: any) {
|
|
|
Taro.hideLoading()
|
|
|
@@ -109,7 +106,7 @@ export default function WechatLogin() {
|
|
|
showCancel: false,
|
|
|
confirmText: '知道了'
|
|
|
})
|
|
|
- } else if (errorMessage.includes('网络')) {
|
|
|
+ } else if (errorMessage.includes('网络') || errorMessage.includes('Network')) {
|
|
|
Taro.showModal({
|
|
|
title: '网络错误',
|
|
|
content: '请检查网络连接后重试',
|