mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-28 17:29:44 +00:00
feat: admin add user
This commit is contained in:
@@ -46,63 +46,6 @@ export const useSystemRoute = (app) => {
|
||||
res.status(401).end('username or password incorrect');
|
||||
}
|
||||
});
|
||||
app.get('/system', auth(), async (req, res) => {
|
||||
try {
|
||||
const data = await System.find();
|
||||
const totalCount = await System.countDocuments();
|
||||
|
||||
res.header('Access-Control-Expose-Headers', 'X-Total-Count');
|
||||
res.header('X-Total-Count', totalCount);
|
||||
res.json(
|
||||
data.map((item) => {
|
||||
const obj = item.toObject();
|
||||
return {
|
||||
...obj,
|
||||
id: obj._id
|
||||
};
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
res.status(500).json({ error: 'Error creating system env' });
|
||||
}
|
||||
});
|
||||
app.post('/system', auth(), async (req, res) => {
|
||||
try {
|
||||
await System.create({
|
||||
...req.body,
|
||||
sensitiveCheck: req.body.sensitiveCheck === 'true'
|
||||
});
|
||||
postParent();
|
||||
res.json({});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Error creating system env' });
|
||||
}
|
||||
});
|
||||
app.put('/system/:id', auth(), async (req, res) => {
|
||||
try {
|
||||
const _id = req.params.id;
|
||||
await System.findByIdAndUpdate(_id, {
|
||||
...req.body,
|
||||
sensitiveCheck: req.body.sensitiveCheck === 'true'
|
||||
});
|
||||
postParent();
|
||||
res.json({});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Error updating system env' });
|
||||
}
|
||||
});
|
||||
app.delete('/system/:id', auth(), async (req, res) => {
|
||||
try {
|
||||
const _id = req.params.id;
|
||||
await System.findByIdAndDelete(_id);
|
||||
|
||||
res.json({});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Error updating system env' });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const auth = () => {
|
||||
@@ -110,7 +53,7 @@ export const auth = () => {
|
||||
try {
|
||||
const authorization = req.headers.authorization;
|
||||
if (!authorization) {
|
||||
return next(new Error("unAuthorization"))
|
||||
return next(new Error('unAuthorization'));
|
||||
}
|
||||
|
||||
const token = authorization.slice('Bearer '.length);
|
||||
|
@@ -2,6 +2,11 @@ import { User, Pay } from '../schema.js';
|
||||
import dayjs from 'dayjs';
|
||||
import { auth } from './system.js';
|
||||
import crypto from 'crypto';
|
||||
export const PRICE_SCALE = 100000;
|
||||
|
||||
export const formatPrice = (val = 0, multiple = 1) => {
|
||||
return Number(((val / PRICE_SCALE) * multiple).toFixed(10));
|
||||
};
|
||||
|
||||
// 加密
|
||||
const hashPassword = (psw) => {
|
||||
@@ -78,6 +83,7 @@ export const useUserRoute = (app) => {
|
||||
return {
|
||||
...obj,
|
||||
id: obj._id,
|
||||
balance: formatPrice(obj.balance),
|
||||
createTime: dayjs(obj.createTime).format('YYYY/MM/DD HH:mm'),
|
||||
password: ''
|
||||
};
|
||||
@@ -107,8 +113,8 @@ export const useUserRoute = (app) => {
|
||||
|
||||
const result = await User.create({
|
||||
username,
|
||||
password,
|
||||
balance
|
||||
password: hashPassword(hashPassword(password)),
|
||||
balance: balance * PRICE_SCALE
|
||||
});
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
@@ -116,17 +122,17 @@ export const useUserRoute = (app) => {
|
||||
res.status(500).json({ error: 'Error creating user' });
|
||||
}
|
||||
});
|
||||
|
||||
// 修改用户信息
|
||||
app.put('/users/:id', auth(), async (req, res) => {
|
||||
try {
|
||||
const _id = req.params.id;
|
||||
|
||||
let { password, balance = 0 } = req.body;
|
||||
let { username, password, balance = 0 } = req.body;
|
||||
|
||||
const result = await User.findByIdAndUpdate(_id, {
|
||||
...(username && { username }),
|
||||
...(password && { password: hashPassword(hashPassword(password)) }),
|
||||
...(balance && { balance })
|
||||
...(balance && { balance: balance * PRICE_SCALE })
|
||||
});
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
@@ -134,6 +140,7 @@ export const useUserRoute = (app) => {
|
||||
res.status(500).json({ error: 'Error updating user' });
|
||||
}
|
||||
});
|
||||
|
||||
// 新增: 获取 pays 列表
|
||||
app.get('/pays', auth(), async (req, res) => {
|
||||
try {
|
||||
|
Reference in New Issue
Block a user