Skip to content

Commit

Permalink
🍏fix:修改字段名兼容SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
durunsong committed Nov 14, 2024
1 parent 9b480f5 commit 0c4a7c7
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Kilyicms is a front-end and back-end separation/open source management system ba
7. Enable debugging in production environment - encapsulated hooks - 🎈
8. Backend Permissions, Routing Permissions, Page Permissions, Button Permissions 🎈
9. Optimization of right mouse button management in background tabs🎈
10. Login userName watermark settings, local and global watermark case demo🎈
10. Login user_name watermark settings, local and global watermark case demo🎈
11. Upload Excel sheet + parsing + printing
12. PDF preview + printing
13. Rich text dynamic content editing + image upload + video upload
Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Kilyicms 是一个前后端分离/开源的中后台管理系统基础解决方
7. 生产环境是否让用户启用调试功能----封装hooks--- 🎈
8. 后台权限,路由权限,页面权限,按钮权限 🎈
9. 后台标签页鼠标右键管理优化 🎈
10. 登陆者userName水印设置,局部和全局水印案例演示 🎈
10. 登陆者user_name水印设置,局部和全局水印案例演示 🎈
11. 上传execl表格+解析+打印
12. PDF预览++打印
13. 富文本动态编辑前台内容+上传图片+上传视频
Expand Down
61 changes: 32 additions & 29 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ const hashExistingPasswords = () => {
// 假设bcrypt的密码前缀为$2b$
bcrypt.hash(user.password, saltRounds, (err, hash) => {
if (err) {
console.error("Error hashing password for user:", user.userName);
console.error("Error hashing password for user:", user.user_name);
return;
}
const updateUserQuery = "UPDATE users SET password = ? WHERE id = ?";
connection.query(updateUserQuery, [hash, user.id], (err) => {
if (err) {
console.error("Error updating password for user:", user.userName);
console.error(
"Error updating password for user:",
user.user_name,
);
} else {
console.log("Password updated for user:", user.userName);
console.log("Password updated for user:", user.user_name);
}
});
});
Expand All @@ -38,7 +41,7 @@ const hashExistingPasswords = () => {

// 添加用户接口
const createUser = (req, res) => {
const { userName, password, description, roles } = req.body;
const { user_name, password, description, roles } = req.body;
// 增加8小时以适应中国时区
const create_time = moment().format("YYYY-MM-DD HH:mm:ss");
const update_time = moment().format("YYYY-MM-DD HH:mm:ss");
Expand All @@ -51,8 +54,8 @@ const createUser = (req, res) => {
// 生成 UUID
const uuid = uuidv4();
// 首先查重
const checkQuery = "SELECT * FROM users WHERE userName = ?";
connection.query(checkQuery, [userName], (err, results) => {
const checkQuery = "SELECT * FROM users WHERE user_name = ?";
connection.query(checkQuery, [user_name], (err, results) => {
if (err) {
res.status(500).json({ status: 500, message: "查询失败", error: err });
return;
Expand All @@ -73,7 +76,7 @@ const createUser = (req, res) => {
return;
}
const query = `INSERT INTO users
(uuid, account, create_time, is_delete, password, update_time, description, userName, nick_name, role_ids, avatar, roles)
(uuid, account, create_time, is_delete, password, update_time, description, user_name, nick_name, role_ids, avatar, roles)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
const values = [
uuid, // 在这里插入 uuid
Expand All @@ -83,7 +86,7 @@ const createUser = (req, res) => {
hashedPassword,
update_time,
description,
userName,
user_name,
nick_name,
JSON.stringify(role_ids),
avatar,
Expand Down Expand Up @@ -130,8 +133,8 @@ const getUsers = (req, res) => {
const queryParams = [];

if (keywords) {
query += " AND (userName LIKE ? OR description LIKE ?)";
countQuery += " AND (userName LIKE ? OR description LIKE ?)";
query += " AND (user_name LIKE ? OR description LIKE ?)";
countQuery += " AND (user_name LIKE ? OR description LIKE ?)";
const keywordPattern = `%${keywords}%`;
queryParams.push(keywordPattern, keywordPattern);
}
Expand Down Expand Up @@ -192,7 +195,7 @@ const getUsers = (req, res) => {
// 更新用户接口
const updateUser = (req, res) => {
const { id } = req.params;
const { userName, password, description, roles } = req.body;
const { user_name, password, description, roles } = req.body;
const update_time = moment().format("YYYY-MM-DD HH:mm:ss");
// 待处理数据权限菜单转化
const account = "testuser";
Expand All @@ -210,7 +213,7 @@ const updateUser = (req, res) => {
return;
}
const query = `UPDATE users SET
account = ?, is_delete = ?, password = ?, update_time = ?, description = ?, userName = ?,
account = ?, is_delete = ?, password = ?, update_time = ?, description = ?, user_name = ?,
nick_name = ?, role_ids = ?, avatar = ? , roles = ?
WHERE id = ?`;
const values = [
Expand All @@ -219,7 +222,7 @@ const updateUser = (req, res) => {
hashedPassword,
update_time,
description,
userName,
user_name,
nick_name,
JSON.stringify(role_ids),
avatar,
Expand Down Expand Up @@ -263,8 +266,8 @@ const getDeletedUsers = (req, res) => {
const queryParams = [];

if (keywords) {
query += " AND (userName LIKE ? OR description LIKE ?)";
countQuery += " AND (userName LIKE ? OR description LIKE ?)";
query += " AND (user_name LIKE ? OR description LIKE ?)";
countQuery += " AND (user_name LIKE ? OR description LIKE ?)";
const keywordPattern = `%${keywords}%`;
queryParams.push(keywordPattern, keywordPattern);
}
Expand Down Expand Up @@ -359,15 +362,15 @@ const restoreUserApi = (req, res) => {

// 登录接口
const loginUser = (req, res) => {
const { userName, password } = req.body;
if (!userName || !password) {
const { user_name, password } = req.body;
if (!user_name || !password) {
return res
.status(400)
.json({ status: 400, message: "账号和密码都是必需的" });
}
const findUserQuery =
"SELECT * FROM users WHERE userName = ? AND is_delete = 0";
connection.query(findUserQuery, [userName], async (err, results) => {
"SELECT * FROM users WHERE user_name = ? AND is_delete = 0";
connection.query(findUserQuery, [user_name], async (err, results) => {
if (err) {
console.error(err);
return res.status(500).json({ status: 500, message: "查询用户失败" });
Expand All @@ -384,7 +387,7 @@ const loginUser = (req, res) => {
}
// 生成 JWT
const token = jwt.sign(
{ id: user.id, userName: user.userName },
{ id: user.id, user_name: user.user_name },
"jwt_secret",
{ expiresIn: "1h" },
);
Expand Down Expand Up @@ -419,7 +422,7 @@ const getUserDetails = (req, res) => {
// 返回用户信息
const userInfo = {
id: user.id,
userName: user.userName,
user_name: user.user_name,
account: user.account,
avatar: user.avatar,
description: user.description,
Expand Down Expand Up @@ -451,7 +454,7 @@ const refreshToken = (req, res) => {
}
// 生成新的 JWT
const newToken = jwt.sign(
{ userId: user.userId, userName: user.userName },
{ userId: user.userId, user_name: user.user_name },
"jwt_secret",
{ expiresIn: "1h" },
);
Expand All @@ -461,9 +464,9 @@ const refreshToken = (req, res) => {

// 注册用户接口
const registerUser = (req, res) => {
const { userName, password, confirmPassword, description, roles } = req.body;
const { user_name, password, confirmPassword, description, roles } = req.body;
// 检查必填字段
if (!userName || !password || !confirmPassword) {
if (!user_name || !password || !confirmPassword) {
return res
.status(400)
.json({ status: 400, message: "用户名、密码和确认密码为必填项" });
Expand All @@ -477,8 +480,8 @@ const registerUser = (req, res) => {
.json({ status: 400, message: "密码和确认密码不一致" });
}
// 检查用户是否已存在
const checkUserQuery = "SELECT * FROM users WHERE userName = ?";
connection.query(checkUserQuery, [userName], (err, results) => {
const checkUserQuery = "SELECT * FROM users WHERE user_name = ?";
connection.query(checkUserQuery, [user_name], (err, results) => {
if (err) {
return res
.status(500)
Expand All @@ -493,7 +496,7 @@ const registerUser = (req, res) => {
// 增加8小时以适应中国时区
const create_time = moment().format("YYYY-MM-DD HH:mm:ss");
const update_time = moment().format("YYYY-MM-DD HH:mm:ss");
const account = userName; // 用户名作为账号
const account = user_name; // 用户名作为账号
const is_delete = 0;
const nick_name = "新用户"; // 默认昵称
const role_ids = [201]; // 普通用户角色ID
Expand All @@ -508,7 +511,7 @@ const registerUser = (req, res) => {
}
// 插入用户数据
const insertUserQuery = `INSERT INTO users
(uuid, account, create_time, is_delete, password, update_time, description, userName, nick_name, role_ids, avatar, roles)
(uuid, account, create_time, is_delete, password, update_time, description, user_name, nick_name, role_ids, avatar, roles)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
const values = [
uuid,
Expand All @@ -518,7 +521,7 @@ const registerUser = (req, res) => {
hashedPassword,
update_time,
description || "",
userName,
user_name,
nick_name,
JSON.stringify(role_ids),
avatar,
Expand Down
4 changes: 2 additions & 2 deletions server/sql/users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS users (
password VARCHAR(255) NOT NULL,
update_time DATETIME NOT NULL,
description TEXT,
userName VARCHAR(50),
user_name VARCHAR(50),
nick_name VARCHAR(50),
role_ids JSON,
avatar VARCHAR(255),
Expand All @@ -20,7 +20,7 @@ CREATE TABLE IF NOT EXISTS users (
);

-- Optional: users sql表 结构
INSERT INTO users (account, create_time, is_delete, password, update_time, description, userName, nick_name, role_ids, avatar, uuid, token,roles)
INSERT INTO users (account, create_time, is_delete, password, update_time, description, user_name, nick_name, role_ids, avatar, uuid, token,roles)
VALUES (
'testuser',
NOW(),
Expand Down
12 changes: 6 additions & 6 deletions src/hooks/useGreeting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { ElNotification } from "element-plus";
import { h } from "vue";

export function useGreeting(t: (key: string) => string) {
const getGreetingMessage = (userName: string) => {
const getGreetingMessage = (user_name: string) => {
const currentHour = new Date().getHours();
let greeting = "";

if (currentHour < 12) {
greeting = `${t("good_morning")}, ${userName}`;
greeting = `${t("good_morning")}, ${user_name}`;
} else if (currentHour < 18) {
greeting = `${t("good_afternoon")}, ${userName}`;
greeting = `${t("good_afternoon")}, ${user_name}`;
} else {
greeting = `${t("good_evening")}, ${userName}`;
greeting = `${t("good_evening")}, ${user_name}`;
}

return greeting;
};

const showGreetingNotification = (message: string, userName: string) => {
const greetingMessage = getGreetingMessage(userName);
const showGreetingNotification = (message: string, user_name: string) => {
const greetingMessage = getGreetingMessage(user_name);

ElNotification({
title: message,
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/components/NavigationBar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@mouseover="startRotate"
@mouseleave="stopRotate"
></el-avatar>
<span>{{ userInfo.userName || userStore.username }}</span>
<span>{{ userInfo.user_name || userStore.user_name }}</span>
</div>
<template #dropdown>
<el-dropdown-menu>
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ watchEffect(() => {
/** 开启或关闭系统水印 */
watchEffect(() => {
showWatermark.value
? setWatermark(userInfo.userName ?? import.meta.env.VITE_APP_TITLE)
? setWatermark(userInfo.user_name ?? import.meta.env.VITE_APP_TITLE)
: clearWatermark();
});
</script>
Expand Down
6 changes: 3 additions & 3 deletions src/service/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import request from "@/utils/request";

// 登录请求参数类型
interface LoginRequestData {
userName: string;
user_name: string;
password: string;
}

// 注册请求参数类型
interface RegisterRequestData {
userName: string;
user_name: string;
password: string;
confirmPassword?: string;
}
Expand All @@ -19,7 +19,7 @@ interface LoginResponse {
message: string;
token: string;
userInfo: {
userName: string;
user_name: string;
email?: string;
// 其他用户信息字段
};
Expand Down
2 changes: 1 addition & 1 deletion src/service/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AxiosResponse } from "axios";

interface ListItem {
id: number;
userName: string;
user_name: string;
description: string;
}

Expand Down
18 changes: 9 additions & 9 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export const useUserStore: any = defineStore("user", () => {
getLocalData(CACHE_KEY.IS_SHOW_NOTICE_TIPS) ?? false;
const token = ref<string>(getToken() || "");
const roles = ref<string[]>([]);
const userName = ref<string>("");
const user_name = ref<string>("");
const tagsViewStore = useTagsViewStore();
const settingsStore = useSettingsStore();
/** 登录 */
const login = async ({ userName, password }: LoginRequestData) => {
const res: any = await loginApi({ userName, password });
const login = async ({ user_name, password }: LoginRequestData) => {
const res: any = await loginApi({ user_name, password });
// 判断登录结果
if (res.status === 200) {
setToken(res.token);
Expand All @@ -53,18 +53,18 @@ export const useUserStore: any = defineStore("user", () => {
setLocalData(CACHE_KEY.USER_INFO, res.userInfo);
// 显示问候语
if (!is_login && !is_show_login_notice) {
showGreetingNotification(t("login_success"), res.userInfo.userName);
showGreetingNotification(t("login_success"), res.userInfo.user_name);
} else if (is_login && is_show_login_notice) {
showGreetingNotification(
t("switch_roles_Successfully"),
res.userInfo.userName,
res.userInfo.user_name,
);
if (is_show_login_notice_tips) {
setLocalData(CACHE_KEY.IS_SHOW_NOTICE, false);
}
}
// 这里模拟获取用户信息,具体逻辑看前后端约束
userName.value = res.userInfo.userName;
user_name.value = res.userInfo.user_name;
// 验证返回的 roles 是否为一个非空数组,否则塞入一个没有任何作用的默认角色,防止路由守卫逻辑进入无限循环
roles.value =
res.userInfo.roles?.length > 0
Expand All @@ -75,10 +75,10 @@ export const useUserStore: any = defineStore("user", () => {
/** 模拟角色变化 */
const changeRoles = async (role: string) => {
try {
// 这里userName和password具体是什么,需要看后端SQL表设计和接口约束,这里只是个示例
// 这里user_name和password具体是什么,需要看后端SQL表设计和接口约束,这里只是个示例
const newRole = role == "admin" ? "admin" : "user";
const params = {
userName: newRole,
user_name: newRole,
password: "123456",
};
// 登录API调用
Expand Down Expand Up @@ -140,7 +140,7 @@ export const useUserStore: any = defineStore("user", () => {
return {
token,
roles,
userName,
user_name,
login,
getInfoRoles,
changeRoles,
Expand Down
2 changes: 1 addition & 1 deletion src/types/store.user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface LoginRequestData {
/** admin 或 user */
userName: "admin" | "user";
user_name: "admin" | "user";
/** 密码 */
password: string;
}
Loading

0 comments on commit 0c4a7c7

Please sign in to comment.