feat: 登录功能
This commit is contained in:
48
README.md
48
README.md
@@ -1,29 +1,29 @@
|
||||
## 诺力基础uniapp框架
|
||||
|
||||
> ## 环境要求
|
||||
> node: 16+
|
||||
>
|
||||
> npm: 8+
|
||||
>
|
||||
> @vue/cli: 5+
|
||||
>
|
||||
> ## 技术栈
|
||||
> vue3 + uniapp + axios + vantui + vite5
|
||||
## 环境要求
|
||||
node: 16+
|
||||
|
||||
> ## 项目结构
|
||||
> ```
|
||||
> .
|
||||
> ├── README.md
|
||||
> ├── components // 公共组件
|
||||
> ├── pages // 页面文件
|
||||
> ├── static // 静态资源
|
||||
> ├── store // vuex状态管理
|
||||
> ├── unpackage // uniapp打包生成文件
|
||||
> ├── utils // 工具类
|
||||
> ├── App.vue // 入口文件
|
||||
> ├── main.js // 入口文件
|
||||
> ├── manifest.json // 应用配置文件
|
||||
> └── pages.json // 页面配置文件
|
||||
> ```
|
||||
npm: 8+
|
||||
|
||||
@vue/cli: 5+
|
||||
|
||||
## 技术栈
|
||||
vue3 + uniapp + axios + vantui + vite5
|
||||
|
||||
## 项目结构
|
||||
```
|
||||
.
|
||||
├── README.md
|
||||
├── components // 公共组件
|
||||
├── pages // 页面文件
|
||||
├── static // 静态资源
|
||||
├── store // vuex状态管理
|
||||
├── unpackage // uniapp打包生成文件
|
||||
├── utils // 工具类
|
||||
├── App.vue // 入口文件
|
||||
├── main.js // 入口文件
|
||||
├── manifest.json // 应用配置文件
|
||||
└── pages.json // 页面配置文件
|
||||
```
|
||||
|
||||
注意:有些组件没有加入,有需要后续追加。
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
"@dcloudio/uni-components": "3.0.0-alpha-5000020260104004",
|
||||
"@dcloudio/uni-h5": "3.0.0-alpha-5000020260104004",
|
||||
"axios": "^1.6.8",
|
||||
"jsencrypt": "^3.5.4",
|
||||
"vant": "^4.8.0",
|
||||
"vue": "^3.4.21"
|
||||
},
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
// 用户注册
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
{
|
||||
"path": "pages/index/index",
|
||||
"style": {
|
||||
"navigationBarTitleText": "uni-app"
|
||||
"navigationBarTitleText": "首页"
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
30
src/utils/rsaEncrypt.js
Normal 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user