Skip to content

Commit

Permalink
🥑fix:修改服务端状态码
Browse files Browse the repository at this point in the history
  • Loading branch information
durunsong committed Nov 25, 2024
1 parent c334e5a commit 57398c4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions server/controllers/deleteUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const permanentDeleteUser = (req, res) => {
return;
}
if (results.affectedRows === 0) {
res.status(404).json({ status: 404, message: "用户不存在" });
res.status(409).json({ status: 409, message: "用户不存在" });
} else {
res.status(200).json({ status: 200, message: "用户删除成功" });
}
Expand All @@ -96,8 +96,8 @@ const restoreUserApi = (req, res) => {
}
if (results.affectedRows === 0) {
res
.status(404)
.json({ status: 404, message: "用户不存在或已被永久删除" });
.status(409)
.json({ status: 409, message: "用户不存在或已被永久删除" });
} else {
res.status(200).json({ status: 200, message: "用户还原成功" });
}
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const loginUser = (req, res) => {
}

if (results.length === 0) {
return res.status(404).json({ status: 404, message: "用户不存在" });
return res.status(409).json({ status: 409, message: "用户不存在" });
}

const user = results[0];
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/userInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const getUserDetails = (req, res) => {
return res.status(500).json({ message: "查询用户失败" });
}
if (results.length === 0) {
return res.status(404).json({ message: "用户不存在" });
return res.status(409).json({ message: "用户不存在" });
}

const user = results[0];
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const updateUser = async (req, res) => {
.json({ status: 500, message: "更新失败", error: err });
}
if (results.affectedRows === 0) {
return res.status(404).json({ status: 404, message: "用户不存在" });
return res.status(409).json({ status: 409, message: "用户不存在" });
}
res.status(200).json({ status: 200, message: "更新成功", data: results });
});
Expand All @@ -192,7 +192,7 @@ const deleteUser = (req, res) => {
.json({ status: 500, message: "删除用户失败", error: err });
}
if (results.affectedRows === 0) {
return res.status(404).json({ status: 404, message: "用户不存在" });
return res.status(409).json({ status: 409, message: "用户不存在" });
}
res.status(200).json({ status: 200, message: "用户删除成功" });
});
Expand Down
8 changes: 6 additions & 2 deletions server/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const formatTime = (date = new Date(), format = "YYYY-MM-DD HH:mm:ss") => {
* @returns {string} - 生成的 JWT
*/
const generateToken = (payload, expiresIn = "1h") => {
const secretKey = process.env.JWT_SECRET || "default_secret_key";
const secretKey =
process.env.JWT_SECRET ||
"f7d623cd21149c493d7304960edaf2e10ad147528dbaf183520184fc0a0f64cb";
return jwt.sign(payload, secretKey, { expiresIn });
};

Expand All @@ -44,7 +46,9 @@ const generateToken = (payload, expiresIn = "1h") => {
* @throws {Error} - 如果验证失败
*/
const verifyToken = (token) => {
const secretKey = process.env.JWT_SECRET || "default_secret_key"; // 确保有密钥
const secretKey =
process.env.JWT_SECRET ||
"f7d623cd21149c493d7304960edaf2e10ad147528dbaf183520184fc0a0f64cb"; // 确保有密钥
return jwt.verify(token, secretKey); // 解码并验证
};

Expand Down

0 comments on commit 57398c4

Please sign in to comment.