Skip to content

Commit 089f1d3

Browse files
author
Kerwin
committed
feat: allow edit user remark (Close #343)
1 parent 42be798 commit 089f1d3

File tree

7 files changed

+32
-13
lines changed

7 files changed

+32
-13
lines changed

service/src/chatgpt/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt'
55
import { SocksProxyAgent } from 'socks-proxy-agent'
66
import httpsProxyAgent from 'https-proxy-agent'
77
import fetch from 'node-fetch'
8-
import type { AuditConfig, CHATMODEL, KeyConfig, UserInfo } from '../storage/model'
9-
import { Status } from '../storage/model'
108
import jwt_decode from 'jwt-decode'
119
import dayjs from 'dayjs'
10+
import type { AuditConfig, CHATMODEL, KeyConfig, UserInfo } from '../storage/model'
11+
import { Status } from '../storage/model'
1212
import type { TextAuditService } from '../utils/textAudit'
1313
import { textAuditServices } from '../utils/textAudit'
1414
import { getCacheApiKeys, getCacheConfig, getOriginConfig } from '../storage/config'
@@ -355,12 +355,13 @@ async function getMessageById(id: string): Promise<ChatMessage | undefined> {
355355
return parentMessageId
356356
? getMessageById(parentMessageId)
357357
: undefined
358-
} else {
358+
}
359+
else {
359360
if (isPrompt) { // prompt
360361
return {
361362
id,
362363
conversationId: chatInfo.options.conversationId,
363-
parentMessageId: parentMessageId,
364+
parentMessageId,
364365
role: 'user',
365366
text: chatInfo.prompt,
366367
}
@@ -369,7 +370,7 @@ async function getMessageById(id: string): Promise<ChatMessage | undefined> {
369370
return { // completion
370371
id,
371372
conversationId: chatInfo.options.conversationId,
372-
parentMessageId: parentMessageId,
373+
parentMessageId,
373374
role: 'assistant',
374375
text: chatInfo.response,
375376
}

service/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,13 +763,13 @@ router.post('/user-status', rootAuth, async (req, res) => {
763763

764764
router.post('/user-edit', rootAuth, async (req, res) => {
765765
try {
766-
const { userId, email, password, roles } = req.body as { userId?: string; email: string; password: string; roles: UserRole[] }
766+
const { userId, email, password, roles, remark } = req.body as { userId?: string; email: string; password: string; roles: UserRole[]; remark?: string }
767767
if (userId) {
768-
await updateUser(userId, roles, password)
768+
await updateUser(userId, roles, password, remark)
769769
}
770770
else {
771771
const newPassword = md5(password)
772-
const user = await createUser(email, newPassword, roles)
772+
const user = await createUser(email, newPassword, roles, remark)
773773
await updateUserStatus(user._id.toString(), Status.Normal)
774774
}
775775
res.send({ status: 'Success', message: '更新成功 | Update successfully' })

service/src/storage/model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class UserInfo {
3636
updateTime?: string
3737
config?: UserConfig
3838
roles?: UserRole[]
39+
remark?: string
3940
constructor(email: string, password: string) {
4041
this.name = email
4142
this.email = email
@@ -45,6 +46,7 @@ export class UserInfo {
4546
this.verifyTime = null
4647
this.updateTime = new Date().toLocaleString()
4748
this.roles = [UserRole.User]
49+
this.remark = null
4850
}
4951
}
5052

service/src/storage/mongo.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,13 @@ export async function deleteChat(roomId: number, uuid: number, inversion: boolea
201201
await chatCol.updateOne(query, update)
202202
}
203203

204-
export async function createUser(email: string, password: string, roles?: UserRole[]): Promise<UserInfo> {
204+
export async function createUser(email: string, password: string, roles?: UserRole[], remark?: string): Promise<UserInfo> {
205205
email = email.toLowerCase()
206206
const userInfo = new UserInfo(email, password)
207207
if (roles && roles.includes(UserRole.Admin))
208208
userInfo.status = Status.Normal
209209
userInfo.roles = roles
210+
userInfo.remark = remark
210211
await userCol.insertOne(userInfo)
211212
return userInfo
212213
}
@@ -277,15 +278,15 @@ export async function updateUserStatus(userId: string, status: Status) {
277278
return await userCol.updateOne({ _id: new ObjectId(userId) }, { $set: { status, verifyTime: new Date().toLocaleString() } })
278279
}
279280

280-
export async function updateUser(userId: string, roles: UserRole[], password: string) {
281+
export async function updateUser(userId: string, roles: UserRole[], password: string, remark?: string) {
281282
const user = await getUserById(userId)
282283
const query = { _id: new ObjectId(userId) }
283284
if (user.password !== password && user.password) {
284285
const newPassword = md5(password)
285-
return await userCol.updateOne(query, { $set: { roles, verifyTime: new Date().toLocaleString(), password: newPassword } })
286+
return await userCol.updateOne(query, { $set: { roles, verifyTime: new Date().toLocaleString(), password: newPassword, remark } })
286287
}
287288
else {
288-
return await userCol.updateOne(query, { $set: { roles, verifyTime: new Date().toLocaleString() } })
289+
return await userCol.updateOne(query, { $set: { roles, verifyTime: new Date().toLocaleString(), remark } })
289290
}
290291
}
291292

src/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export function fetchUpdateUserStatus<T = any>(userId: string, status: Status) {
140140
export function fetchUpdateUser<T = any>(userInfo: UserInfo) {
141141
return post<T>({
142142
url: '/user-edit',
143-
data: { userId: userInfo._id, roles: userInfo.roles, email: userInfo.email, password: userInfo.password },
143+
data: { userId: userInfo._id, roles: userInfo.roles, email: userInfo.email, password: userInfo.password, remark: userInfo.remark },
144144
})
145145
}
146146

src/components/common/Setting/User.vue

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ const columns = [
6565
return Status[row.status]
6666
},
6767
},
68+
{
69+
title: 'Remark',
70+
key: 'remark',
71+
width: 220,
72+
},
6873
{
6974
title: 'Action',
7075
key: '_id',
@@ -259,6 +264,15 @@ onMounted(async () => {
259264
/>
260265
</div>
261266
</div>
267+
<div class="flex items-center space-x-4">
268+
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.remark') }}</span>
269+
<div class="flex-1">
270+
<NInput
271+
v-model:value="userRef.remark" type="textarea"
272+
:autosize="{ minRows: 1, maxRows: 2 }" placeholder=""
273+
/>
274+
</div>
275+
</div>
262276
<div class="flex items-center space-x-4">
263277
<span class="flex-shrink-0 w-[100px]" />
264278
<NButton type="primary" :loading="handleSaving" @click="handleUpdateUser()">

src/components/common/Setting/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export class UserInfo {
125125
email?: string
126126
password?: string
127127
roles: UserRole[]
128+
remark?: string
128129
constructor(roles: UserRole[]) {
129130
this.roles = roles
130131
}

0 commit comments

Comments
 (0)