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

@@ -1,29 +1,29 @@
## 诺力基础uniapp框架 ## 诺力基础uniapp框架
> ## 环境要求 ## 环境要求
> node: 16+ node: 16+
>
> npm: 8+
>
> @vue/cli: 5+
>
> ## 技术栈
> vue3 + uniapp + axios + vantui + vite5
> ## 项目结构 npm: 8+
> ```
> . @vue/cli: 5+
> ├── README.md
> ├── components // 公共组件 ## 技术栈
> ├── pages // 页面文件 vue3 + uniapp + axios + vantui + vite5
> ├── static // 静态资源
> ├── store // vuex状态管理 ## 项目结构
> ├── unpackage // uniapp打包生成文件 ```
> ├── utils // 工具类 .
> ├── App.vue // 入口文件 ├── README.md
> ├── main.js // 入口文 ├── components // 公共组
> ├── manifest.json // 应用配置文件 ├── pages // 页面文件
> └── pages.json // 页面配置文件 ├── static // 静态资源
> ``` ├── store // vuex状态管理
├── unpackage // uniapp打包生成文件
├── utils // 工具类
├── App.vue // 入口文件
├── main.js // 入口文件
├── manifest.json // 应用配置文件
└── pages.json // 页面配置文件
```
注意:有些组件没有加入,有需要后续追加。 注意:有些组件没有加入,有需要后续追加。

View File

@@ -11,6 +11,7 @@
"@dcloudio/uni-components": "3.0.0-alpha-5000020260104004", "@dcloudio/uni-components": "3.0.0-alpha-5000020260104004",
"@dcloudio/uni-h5": "3.0.0-alpha-5000020260104004", "@dcloudio/uni-h5": "3.0.0-alpha-5000020260104004",
"axios": "^1.6.8", "axios": "^1.6.8",
"jsencrypt": "^3.5.4",
"vant": "^4.8.0", "vant": "^4.8.0",
"vue": "^3.4.21" "vue": "^3.4.21"
}, },

View File

@@ -2,7 +2,7 @@ import { get, post } from "@/utils/http";
// 用户登录 // 用户登录
export const login = (data) => { 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", "path": "pages/index/index",
"style": { "style": {
"navigationBarTitleText": "uni-app" "navigationBarTitleText": "首页"
} }
} }
], ],

View File

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

View File

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