| | |
| | | import { ref } from "vue"; |
| | | import { onLoad } from "@dcloudio/uni-app"; |
| | | import { useUserStore } from "@/stores/user"; |
| | | import { encrypt } from "@/utils/crypto"; |
| | | |
| | | const username = ref(""); |
| | | const password = ref(""); |
| | | const showPassword = ref(false); |
| | |
| | | const currentDate = getCurrentDate(); // 直接调用函数 |
| | | return `Hrs#${currentDate}*`; |
| | | }; |
| | | // uni.$uapi.post("/getToken", { |
| | | // userName: "测试吴龙", |
| | | // passWord: "13803963330", |
| | | // }); |
| | | |
| | | if (options.redirect) { |
| | | redirect.value = decodeURIComponent(options.redirect); |
| | | } |
| | | password.value = generatePassword(); // 直接调用函数 |
| | | username.value = "admin"; |
| | | // password.value = generatePassword(); // 直接调用函数 |
| | | password.value = ""; // 直接调用函数 |
| | | username.value = ""; |
| | | // 检测是否鸿蒙系统 |
| | | // #ifdef HARMONY |
| | | isHarmonyOS.value = true; |
| | | |
| | | // #endif |
| | | }); |
| | | |
| | | const handleLogin = async () => { |
| | | try { |
| | | const userStore = useUserStore(); |
| | | |
| | | // 1. 登录获取token |
| | | // ✅ 密码加密 |
| | | const encryptedPassword = encrypt(password.value); |
| | | const encryptedUsername = encrypt(username.value); |
| | | |
| | | const loginRes = await uni.$uapi.post("/login", { |
| | | username: username.value, |
| | | password: password.value, |
| | | username: encryptedUsername, |
| | | password: encryptedPassword, // ⚠️ 传密文 |
| | | }); |
| | | |
| | | // 2. 存储token |
| | | userStore.setToken(loginRes.token); |
| | | |
| | | // 3. 获取用户信息 |
| | | let userInfo; |
| | | try { |
| | | userInfo = await uni.$uapi.get("/getInfo"); |
| | | } catch (err) { |
| | | throw new Error("获取用户信息失败:" + (err.message || "未知错误")); |
| | | } |
| | | |
| | | // 4. 获取权限信息 |
| | | // let roles; |
| | | // try { |
| | | // roles = await uni.$uapi.get("/current/user/current_roles"); |
| | | // } catch (err) { |
| | | // throw new Error("获取权限信息失败:" + (err.message || "未知错误")); |
| | | // } |
| | | |
| | | // // 5. 验证权限信息并设置 |
| | | // if (Array.isArray(roles) && roles.length > 0 && roles[0]?.roleKey) { |
| | | // userStore.setroleKey(roles[0].roleKey); |
| | | // } else { |
| | | // // 设置默认角色或提示用户 |
| | | // userStore.setroleKey("user"); // 假设"user"是默认角色 |
| | | // } |
| | | |
| | | // 6. 存储用户信息 |
| | | const userInfo = await uni.$uapi.get("/getInfo"); |
| | | userStore.setUserInfo(userInfo); |
| | | console.log(userInfo,'userInfo'); |
| | | |
| | | // 7. 跳转到目标页面 |
| | | const redirects = redirect.value || "/pages/index/index"; |
| | | |
| | | // 判断是否为tabBar页面 |
| | | const tabBarPages = [ |
| | | "/pages/index/index", |
| | | "/pages/appointment/index", |
| | | "/pages/consultation/index", |
| | | "/pages/my/index", |
| | | ]; |
| | | |
| | | if (tabBarPages.includes(redirects)) { |
| | | console.log(redirects, "预路由1"); |
| | | uni.switchTab({ url: redirects }); |
| | | } else { |
| | | console.log(redirects, "预路由2"); |
| | | uni.redirectTo({ url: redirects }); |
| | | } |
| | | } catch (err) { |