fix(profile): 修复用户信息获取路径错误并更新默认头像
修复了获取用户信息时路径错误的问题,将 `response.data.avatar` 和 `response.data.name` 更新为 `response.data.user.avatar` 和 `response.data.user.name`。同时更新了默认头像的 URL,使用 Vant 提供的默认图片。
Showing
7 changed files
with
75 additions
and
84 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-03-20 20:36:36 | 2 | * @Date: 2025-03-20 20:36:36 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-03-26 11:21:05 | 4 | + * @LastEditTime: 2025-03-26 12:34:44 |
| 5 | * @FilePath: /mlaj/src/router/guards.js | 5 | * @FilePath: /mlaj/src/router/guards.js |
| 6 | * @Description: 路由守卫逻辑 | 6 | * @Description: 路由守卫逻辑 |
| 7 | */ | 7 | */ |
| ... | @@ -33,7 +33,7 @@ export const checkWxAuth = async () => { | ... | @@ -33,7 +33,7 @@ export const checkWxAuth = async () => { |
| 33 | const { code, data } = await getAuthInfoAPI(); | 33 | const { code, data } = await getAuthInfoAPI(); |
| 34 | if (code && !data.openid_has) { | 34 | if (code && !data.openid_has) { |
| 35 | // 直接在这里处理授权跳转 | 35 | // 直接在这里处理授权跳转 |
| 36 | - let raw_url = encodeURIComponent(location.origin + location.pathname + $route.query.href); // 未授权的地址 | 36 | + let raw_url = encodeURIComponent(location.origin + location.pathname + location.href); // 未授权的地址 |
| 37 | const short_url = `/srv/?f=behalo&a=openid&res=${raw_url}`; | 37 | const short_url = `/srv/?f=behalo&a=openid&res=${raw_url}`; |
| 38 | location.href = short_url; | 38 | location.href = short_url; |
| 39 | 39 | ... | ... |
| 1 | <template> | 1 | <template> |
| 2 | - <div class="min-h-screen flex flex-col bg-gradient-to-br from-green-50 via-teal-50 to-blue-50 py-12 px-4 sm:px-6 lg:px-8"> | 2 | + <div |
| 3 | + class="min-h-screen flex flex-col bg-gradient-to-br from-green-50 via-teal-50 to-blue-50 py-12 px-4 sm:px-6 lg:px-8" | ||
| 4 | + > | ||
| 3 | <div class="sm:mx-auto sm:w-full sm:max-w-md"> | 5 | <div class="sm:mx-auto sm:w-full sm:max-w-md"> |
| 4 | <h1 class="text-center text-3xl font-bold text-gray-800 mb-2">亲子学院</h1> | 6 | <h1 class="text-center text-3xl font-bold text-gray-800 mb-2">亲子学院</h1> |
| 5 | <h2 class="text-center text-xl font-medium text-gray-600">欢迎回来</h2> | 7 | <h2 class="text-center text-xl font-medium text-gray-600">欢迎回来</h2> |
| ... | @@ -7,7 +9,10 @@ | ... | @@ -7,7 +9,10 @@ |
| 7 | 9 | ||
| 8 | <div class="mt-8 sm:mx-auto sm:w-full sm:max-w-md"> | 10 | <div class="mt-8 sm:mx-auto sm:w-full sm:max-w-md"> |
| 9 | <FrostedGlass class="py-8 px-6 rounded-lg"> | 11 | <FrostedGlass class="py-8 px-6 rounded-lg"> |
| 10 | - <div v-if="error" class="mb-4 bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-md"> | 12 | + <div |
| 13 | + v-if="error" | ||
| 14 | + class="mb-4 bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-md" | ||
| 15 | + > | ||
| 11 | {{ error }} | 16 | {{ error }} |
| 12 | </div> | 17 | </div> |
| 13 | 18 | ||
| ... | @@ -44,7 +49,10 @@ | ... | @@ -44,7 +49,10 @@ |
| 44 | 49 | ||
| 45 | <div class="flex justify-end"> | 50 | <div class="flex justify-end"> |
| 46 | <div class="text-sm"> | 51 | <div class="text-sm"> |
| 47 | - <router-link to="/forgotPwd" class="font-medium text-green-600 hover:text-green-500"> | 52 | + <router-link |
| 53 | + to="/forgotPwd" | ||
| 54 | + class="font-medium text-green-600 hover:text-green-500" | ||
| 55 | + > | ||
| 48 | 忘记密码? | 56 | 忘记密码? |
| 49 | </router-link> | 57 | </router-link> |
| 50 | </div> | 58 | </div> |
| ... | @@ -57,7 +65,7 @@ | ... | @@ -57,7 +65,7 @@ |
| 57 | class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-gradient-to-r from-green-500 to-green-600 hover:from-green-600 hover:to-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500" | 65 | class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-gradient-to-r from-green-500 to-green-600 hover:from-green-600 hover:to-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500" |
| 58 | :class="{ 'opacity-70 cursor-not-allowed': loading }" | 66 | :class="{ 'opacity-70 cursor-not-allowed': loading }" |
| 59 | > | 67 | > |
| 60 | - {{ loading ? '登录中...' : '登 录' }} | 68 | + {{ loading ? "登录中..." : "登 录" }} |
| 61 | </button> | 69 | </button> |
| 62 | </div> | 70 | </div> |
| 63 | </form> | 71 | </form> |
| ... | @@ -99,7 +107,10 @@ | ... | @@ -99,7 +107,10 @@ |
| 99 | <div class="text-center mt-6"> | 107 | <div class="text-center mt-6"> |
| 100 | <p class="text-sm text-gray-600"> | 108 | <p class="text-sm text-gray-600"> |
| 101 | 还没有账号? | 109 | 还没有账号? |
| 102 | - <router-link to="/register" class="font-medium text-green-600 hover:text-green-500"> | 110 | + <router-link |
| 111 | + to="/register" | ||
| 112 | + class="font-medium text-green-600 hover:text-green-500" | ||
| 113 | + > | ||
| 103 | 立即注册 | 114 | 立即注册 |
| 104 | </router-link> | 115 | </router-link> |
| 105 | </p> | 116 | </p> |
| ... | @@ -110,92 +121,64 @@ | ... | @@ -110,92 +121,64 @@ |
| 110 | </template> | 121 | </template> |
| 111 | 122 | ||
| 112 | <script setup> | 123 | <script setup> |
| 113 | -import { ref } from 'vue' | 124 | +import { ref } from "vue"; |
| 114 | -import { useRoute, useRouter } from 'vue-router' | 125 | +import { useRoute, useRouter } from "vue-router"; |
| 115 | -import FrostedGlass from '@/components/ui/FrostedGlass.vue' | 126 | +import FrostedGlass from "@/components/ui/FrostedGlass.vue"; |
| 116 | -import { useAuth } from '@/contexts/auth' | 127 | +import { useAuth } from "@/contexts/auth"; |
| 117 | -import { loginAPI } from '@/api/users' | 128 | +import { loginAPI, getUserInfoAPI } from "@/api/users"; |
| 118 | - | 129 | +import { useTitle } from "@vueuse/core"; |
| 119 | -import { useTitle } from '@vueuse/core' | ||
| 120 | const $route = useRoute(); | 130 | const $route = useRoute(); |
| 121 | useTitle($route.meta.title); | 131 | useTitle($route.meta.title); |
| 122 | 132 | ||
| 123 | -const router = useRouter() | 133 | +const router = useRouter(); |
| 124 | -const { login } = useAuth() | 134 | +const { login } = useAuth(); |
| 125 | 135 | ||
| 126 | -const mobile = ref('') | 136 | +const mobile = ref(""); |
| 127 | -const password = ref('') | 137 | +const password = ref(""); |
| 128 | -const error = ref('') | 138 | +const error = ref(""); |
| 129 | -const loading = ref(false) | 139 | +const loading = ref(false); |
| 130 | 140 | ||
| 131 | // 原登录逻辑 | 141 | // 原登录逻辑 |
| 132 | -// const handleSubmit = async () => { | ||
| 133 | -// if (!mobile.value || !password.value) { | ||
| 134 | -// error.value = '请填写所有字段' | ||
| 135 | -// return | ||
| 136 | -// } | ||
| 137 | - | ||
| 138 | -// try { | ||
| 139 | -// error.value = '' | ||
| 140 | -// loading.value = true | ||
| 141 | - | ||
| 142 | -// // 调用登录接口 | ||
| 143 | -// const response = await loginAPI({ | ||
| 144 | -// mobile: mobile.value, | ||
| 145 | -// password: password.value | ||
| 146 | -// }) | ||
| 147 | - | ||
| 148 | -// if (response.code !== 1) { | ||
| 149 | -// error.value = response.msg || '登录失败,请检查您的凭据' | ||
| 150 | -// return | ||
| 151 | -// } | ||
| 152 | - | ||
| 153 | -// // 登录成功,更新auth状态 | ||
| 154 | -// const success = login(response.data) | ||
| 155 | - | ||
| 156 | -// if (success) { | ||
| 157 | -// // 如果有重定向参数,登录成功后跳转到对应页面 | ||
| 158 | -// const redirect = $route.query.redirect | ||
| 159 | -// router.push(redirect || '/') | ||
| 160 | -// } else { | ||
| 161 | -// error.value = '登录失败,请检查您的凭据' | ||
| 162 | -// } | ||
| 163 | - | ||
| 164 | -// 临时登录逻辑 - 支持任意输入 | ||
| 165 | const handleSubmit = async () => { | 142 | const handleSubmit = async () => { |
| 166 | if (!mobile.value || !password.value) { | 143 | if (!mobile.value || !password.value) { |
| 167 | - error.value = '请填写所有字段' | 144 | + error.value = "请填写所有字段"; |
| 168 | - return | 145 | + return; |
| 169 | } | 146 | } |
| 170 | 147 | ||
| 171 | try { | 148 | try { |
| 172 | - error.value = '' | 149 | + error.value = ""; |
| 173 | - loading.value = true | 150 | + loading.value = true; |
| 174 | 151 | ||
| 175 | - // 构造默认用户数据 | 152 | + // 调用登录接口 |
| 176 | - const mockUserData = { | 153 | + const response = await loginAPI({ |
| 177 | - id: 1, | ||
| 178 | - name: '测试用户', | ||
| 179 | mobile: mobile.value, | 154 | mobile: mobile.value, |
| 180 | - avatar: 'https://cdn.ipadbiz.cn/mlaj/images/user-avatar-2.jpg', | 155 | + password: password.value, |
| 181 | - checkInDays: 3, | 156 | + }); |
| 182 | - completedCourses: 12 | 157 | + |
| 158 | + if (response.code !== 1) { | ||
| 159 | + error.value = response.msg || "登录失败,请检查您的凭据"; | ||
| 160 | + return; | ||
| 183 | } | 161 | } |
| 184 | 162 | ||
| 185 | - // 模拟登录成功 | 163 | + const { code, data } = await getUserInfoAPI(); |
| 186 | - const success = login(mockUserData) | 164 | + if (code) { |
| 165 | + // 登录成功,更新auth状态 | ||
| 166 | + const success = login(data.user); | ||
| 187 | 167 | ||
| 188 | if (success) { | 168 | if (success) { |
| 189 | - const redirect = $route.query.redirect | 169 | + // 如果有重定向参数,登录成功后跳转到对应页面 |
| 190 | - router.push(redirect || '/') | 170 | + const redirect = $route.query.redirect; |
| 171 | + router.push(redirect || "/"); | ||
| 191 | } else { | 172 | } else { |
| 192 | - error.value = '登录失败,请检查您的凭据' | 173 | + error.value = "登录失败,请检查您的凭据"; |
| 193 | } | 174 | } |
| 175 | + } | ||
| 176 | + | ||
| 194 | } catch (err) { | 177 | } catch (err) { |
| 195 | - console.error('Login error:', err) | 178 | + console.error("Login error:", err); |
| 196 | - error.value = '登录时发生错误' | 179 | + error.value = "登录时发生错误"; |
| 197 | } finally { | 180 | } finally { |
| 198 | - loading.value = false | 181 | + loading.value = false; |
| 199 | } | 182 | } |
| 200 | -} | 183 | +}; |
| 201 | </script> | 184 | </script> | ... | ... |
| ... | @@ -13,7 +13,7 @@ | ... | @@ -13,7 +13,7 @@ |
| 13 | class="w-24 h-24 rounded-full overflow-hidden border-4 border-white shadow-lg mb-4" | 13 | class="w-24 h-24 rounded-full overflow-hidden border-4 border-white shadow-lg mb-4" |
| 14 | > | 14 | > |
| 15 | <img | 15 | <img |
| 16 | - :src="profile.avatar || '/assets/images/user-avatar-1.jpg'" | 16 | + :src="profile.avatar || 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'" |
| 17 | :alt="profile.name" | 17 | :alt="profile.name" |
| 18 | class="w-full h-full object-cover" | 18 | class="w-full h-full object-cover" |
| 19 | @error="handleImageError" | 19 | @error="handleImageError" |
| ... | @@ -211,14 +211,22 @@ import MenuItem from "@/components/ui/MenuItem.vue"; | ... | @@ -211,14 +211,22 @@ import MenuItem from "@/components/ui/MenuItem.vue"; |
| 211 | import { useAuth } from "@/contexts/auth"; | 211 | import { useAuth } from "@/contexts/auth"; |
| 212 | import { userProfile, checkInTypes } from "@/utils/mockData"; | 212 | import { userProfile, checkInTypes } from "@/utils/mockData"; |
| 213 | import CheckInDialog from "@/components/ui/CheckInDialog.vue"; | 213 | import CheckInDialog from "@/components/ui/CheckInDialog.vue"; |
| 214 | +import { getUserInfoAPI } from "@/api/users"; | ||
| 215 | +import { showToast } from "vant"; | ||
| 214 | 216 | ||
| 215 | import { useTitle } from "@vueuse/core"; | 217 | import { useTitle } from "@vueuse/core"; |
| 216 | const router = useRouter(); | 218 | const router = useRouter(); |
| 217 | const $route = useRoute(); | 219 | const $route = useRoute(); |
| 218 | useTitle($route.meta.title); | 220 | useTitle($route.meta.title); |
| 219 | 221 | ||
| 220 | -const { currentUser, logout } = useAuth(); | 222 | +const { logout } = useAuth(); |
| 221 | -const profile = ref(userProfile); | 223 | +const profile = ref({}); |
| 224 | +onMounted(async () => { | ||
| 225 | + const { code, data } = await getUserInfoAPI(); | ||
| 226 | + if (code) { | ||
| 227 | + profile.value = data.user; | ||
| 228 | + } | ||
| 229 | +}) | ||
| 222 | const showCheckInDialog = ref(false); | 230 | const showCheckInDialog = ref(false); |
| 223 | 231 | ||
| 224 | // 处理打卡成功 | 232 | // 处理打卡成功 |
| ... | @@ -249,7 +257,7 @@ const handleMenuClick = (path) => { | ... | @@ -249,7 +257,7 @@ const handleMenuClick = (path) => { |
| 249 | // Handle image error | 257 | // Handle image error |
| 250 | const handleImageError = (e) => { | 258 | const handleImageError = (e) => { |
| 251 | e.target.onerror = null; | 259 | e.target.onerror = null; |
| 252 | - e.target.src = "/assets/images/user-avatar-1.jpg"; | 260 | + e.target.src = "https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg"; |
| 253 | }; | 261 | }; |
| 254 | 262 | ||
| 255 | // Handle check-in type click | 263 | // Handle check-in type click | ... | ... |
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2025-03-24 13:04:21 | 2 | * @Date: 2025-03-24 13:04:21 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-03-25 09:56:14 | 4 | + * @LastEditTime: 2025-03-26 13:03:11 |
| 5 | * @FilePath: /mlaj/src/views/profile/SettingsPage.vue | 5 | * @FilePath: /mlaj/src/views/profile/SettingsPage.vue |
| 6 | * @Description: 用户设置页面 | 6 | * @Description: 用户设置页面 |
| 7 | --> | 7 | --> |
| ... | @@ -95,8 +95,8 @@ onMounted(async () => { | ... | @@ -95,8 +95,8 @@ onMounted(async () => { |
| 95 | try { | 95 | try { |
| 96 | const response = await getUserInfoAPI(); | 96 | const response = await getUserInfoAPI(); |
| 97 | if (response.data) { | 97 | if (response.data) { |
| 98 | - userAvatar.value = response.data.avatar; | 98 | + userAvatar.value = response.data.user.avatar; |
| 99 | - username.value = response.data.name; | 99 | + username.value = response.data.user.name; |
| 100 | } | 100 | } |
| 101 | } catch (error) { | 101 | } catch (error) { |
| 102 | console.error('获取用户信息失败:', error); | 102 | console.error('获取用户信息失败:', error); | ... | ... |
| ... | @@ -70,7 +70,7 @@ onMounted(async () => { | ... | @@ -70,7 +70,7 @@ onMounted(async () => { |
| 70 | try { | 70 | try { |
| 71 | const response = await getUserInfoAPI(); | 71 | const response = await getUserInfoAPI(); |
| 72 | if (response.data) { | 72 | if (response.data) { |
| 73 | - userAvatar.value = response.data.avatar; | 73 | + userAvatar.value = response.data.user.avatar; |
| 74 | } | 74 | } |
| 75 | } catch (error) { | 75 | } catch (error) { |
| 76 | console.error("获取用户信息失败:", error); | 76 | console.error("获取用户信息失败:", error); | ... | ... |
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2025-03-24 13:04:21 | 2 | * @Date: 2025-03-24 13:04:21 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-03-24 13:46:15 | 4 | + * @LastEditTime: 2025-03-26 13:03:27 |
| 5 | * @FilePath: /mlaj/src/views/profile/settings/UsernameSettingPage.vue | 5 | * @FilePath: /mlaj/src/views/profile/settings/UsernameSettingPage.vue |
| 6 | * @Description: 修改用户名页面 | 6 | * @Description: 修改用户名页面 |
| 7 | --> | 7 | --> |
| ... | @@ -57,7 +57,7 @@ onMounted(async () => { | ... | @@ -57,7 +57,7 @@ onMounted(async () => { |
| 57 | try { | 57 | try { |
| 58 | const response = await getUserInfoAPI(); | 58 | const response = await getUserInfoAPI(); |
| 59 | if (response.data) { | 59 | if (response.data) { |
| 60 | - username.value = response.data.name; | 60 | + username.value = response.data.user.name; |
| 61 | } | 61 | } |
| 62 | } catch (error) { | 62 | } catch (error) { |
| 63 | console.error('获取用户信息失败:', error); | 63 | console.error('获取用户信息失败:', error); | ... | ... |
-
Please register or login to post a comment