feat: 登录功能

This commit is contained in:
2026-01-14 13:59:15 +08:00
parent 121d762413
commit 0953c1b6cb
7 changed files with 101 additions and 55 deletions

View File

@@ -2,7 +2,7 @@ import { get, post } from "@/utils/http";
// 用户登录
export const login = (data) => {
return post("/user/login", data);
return post("/mobile/auth/login", data);
};
// 用户注册

View File

@@ -10,7 +10,7 @@
{
"path": "pages/index/index",
"style": {
"navigationBarTitleText": "uni-app"
"navigationBarTitleText": "首页"
}
}
],

View File

@@ -76,6 +76,7 @@
import { ref, onMounted } from "vue";
import { setBaseURL } from "@/utils/http";
import { login } from "@/api/user";
import { encrypt } from "@/utils/rsaEncrypt";
// 表单数据
const formData = ref({
@@ -132,32 +133,44 @@ const handleLogin = async () => {
// 设置IP地址到http.js
setBaseURL(fullUrl);
// 预留登录接口调用
// const res = await login({
// username: formData.value.username,
// password: formData.value.password,
// });
// 模拟登录成功(实际使用时需要替换为真实接口调用)
uni.showToast({
title: "登录成功",
icon: "success",
// 加密密码
const encryptedPassword = encrypt(formData.value.password);
// 调用登录接口
const res = await login({
username: formData.value.username,
password: encryptedPassword,
});
// 保存登录信息(实际使用时需要根据接口返回数据处理)
// uni.setStorageSync("token", res.data.token);
// uni.setStorageSync("userInfo", res.data.userInfo);
// 处理返回数据
if (res && res.user && res.token) {
console.log(res);
// 保存token去掉Bearer前缀因为http.js会自动添加
const token = res.token.replace(/^Bearer\s+/i, "");
uni.setStorageSync("token", token);
// 保存用户信息
uni.setStorageSync("userInfo", res.user.user || res.user);
// 跳转到首页
setTimeout(() => {
uni.reLaunch({
url: "/pages/index/index",
uni.showToast({
title: "登录成功",
icon: "success",
});
}, 1500);
// 跳转到首页
setTimeout(() => {
uni.navigateTo({
url: "/pages/index/index",
});
}, 1500);
} else {
throw new Error("登录失败:返回数据格式错误");
}
} catch (error) {
console.error("登录失败:", error);
uni.showToast({
title: error.message || "登录失败,请重试",
title: error.message || error.response?.data?.message || "登录失败,请重试",
icon: "none",
});
} finally {

View File

@@ -59,19 +59,21 @@ http.interceptors.response.use(
uni.hideLoading();
const { data } = response;
// console.log(data);
// 根据业务状态码处理(可根据实际接口调整)
if (data.code === 200 || data.code === 0) {
return data;
}
// if (data.code === 200 || data.code === 0) {
// return data;
// }
// 业务错误提示
uni.showToast({
title: data.message || "请求失败",
icon: "none",
});
// // 业务错误提示
// uni.showToast({
// title: data.message || "请求失败",
// icon: "none",
// });
return Promise.reject(data);
// return Promise.reject(data);
return data;
},
(error) => {
uni.hideLoading();

30
src/utils/rsaEncrypt.js Normal file
View File

@@ -0,0 +1,30 @@
import JSEncrypt from 'jsencrypt'
// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANL378k3RiZHWx5AfJqdH9xRNBmD9wGD\n' +
'2iRe41HdTNF8RUhNnHit5NpMNtGL0NPTSSpPjjI1kJfVorRvaQerUgkCAwEAAQ=='
const privateKey = 'MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEA0vfvyTdGJkdbHkB8\n' +
'mp0f3FE0GYP3AYPaJF7jUd1M0XxFSE2ceK3k2kw20YvQ09NJKk+OMjWQl9WitG9p\n' +
'B6tSCQIDAQABAkA2SimBrWC2/wvauBuYqjCFwLvYiRYqZKThUS3MZlebXJiLB+Ue\n' +
'/gUifAAKIg1avttUZsHBHrop4qfJCwAI0+YRAiEA+W3NK/RaXtnRqmoUUkb59zsZ\n' +
'UBLpvZgQPfj1MhyHDz0CIQDYhsAhPJ3mgS64NbUZmGWuuNKp5coY2GIj/zYDMJp6\n' +
'vQIgUueLFXv/eZ1ekgz2Oi67MNCk5jeTF2BurZqNLR3MSmUCIFT3Q6uHMtsB9Eha\n' +
'4u7hS31tj1UWE+D+ADzp59MGnoftAiBeHT7gDMuqeJHPL4b+kC+gzV4FGTfhR9q3\n' +
'tTbklZkD2A=='
// 加密
export function encrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPublicKey(publicKey) // 设置公钥
return encryptor.encrypt(txt) // 对需要加密的数据进行加密
}
// 解密
export function decrypt(txt) {
const encryptor = new JSEncrypt()
encryptor.setPrivateKey(privateKey)
return encryptor.decrypt(txt)
}