mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-22 11:16:39 +08:00
用户列表功能完成
This commit is contained in:
parent
fcfe0c7913
commit
7a67af642d
@ -2,20 +2,25 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"openai/types"
|
"openai/types"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetChatRoleListHandle 获取聊天角色列表
|
// GetChatRoleListHandle 获取聊天角色列表
|
||||||
func (s *Server) GetChatRoleListHandle(c *gin.Context) {
|
func (s *Server) GetChatRoleListHandle(c *gin.Context) {
|
||||||
sessionId := c.GetHeader(types.TokenName)
|
var user *types.User
|
||||||
session := s.ChatSession[sessionId]
|
username := c.Query("username")
|
||||||
user, err := GetUser(session.Username)
|
if username != "" {
|
||||||
if err != nil {
|
u, err := GetUser(username)
|
||||||
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Hacker Access!!!"})
|
if err != nil {
|
||||||
return
|
c.JSON(http.StatusOK, types.BizVo{Code: types.Failed, Message: "Hacker Access!!!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
user = u
|
||||||
}
|
}
|
||||||
|
|
||||||
var rolesOrder = []string{"gpt", "teacher", "translator", "english_trainer", "weekly_report", "girl_friend",
|
var rolesOrder = []string{"gpt", "teacher", "translator", "english_trainer", "weekly_report", "girl_friend",
|
||||||
"kong_zi", "lu_xun", "steve_jobs", "elon_musk", "red_book", "dou_yin", "programmer",
|
"kong_zi", "lu_xun", "steve_jobs", "elon_musk", "red_book", "dou_yin", "programmer",
|
||||||
"seller", "good_comment", "psychiatrist", "artist"}
|
"seller", "good_comment", "psychiatrist", "artist"}
|
||||||
@ -23,10 +28,11 @@ func (s *Server) GetChatRoleListHandle(c *gin.Context) {
|
|||||||
var roles = GetChatRoles()
|
var roles = GetChatRoles()
|
||||||
for _, k := range rolesOrder {
|
for _, k := range rolesOrder {
|
||||||
// 确认当前用户是否订阅了当前角色
|
// 确认当前用户是否订阅了当前角色
|
||||||
if v, ok := user.ChatRoles[k]; !ok || v != 1 {
|
if user != nil {
|
||||||
continue
|
if v, ok := user.ChatRoles[k]; !ok || v != 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := roles[k]; ok && v.Enable {
|
if v, ok := roles[k]; ok && v.Enable {
|
||||||
res = append(res, struct {
|
res = append(res, struct {
|
||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
|
@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
"github.com/syndtr/goleveldb/leveldb"
|
||||||
"github.com/syndtr/goleveldb/leveldb/util"
|
"github.com/syndtr/goleveldb/leveldb/util"
|
||||||
)
|
)
|
||||||
@ -40,13 +41,59 @@ func (db *LevelDB) Get(key string) ([]byte, error) {
|
|||||||
func (db *LevelDB) Search(prefix string) []string {
|
func (db *LevelDB) Search(prefix string) []string {
|
||||||
var items = make([]string, 0)
|
var items = make([]string, 0)
|
||||||
iter := db.driver.NewIterator(util.BytesPrefix([]byte(prefix)), nil)
|
iter := db.driver.NewIterator(util.BytesPrefix([]byte(prefix)), nil)
|
||||||
|
defer iter.Release()
|
||||||
|
|
||||||
for iter.Next() {
|
for iter.Next() {
|
||||||
items = append(items, string(iter.Value()))
|
items = append(items, string(iter.Value()))
|
||||||
}
|
}
|
||||||
iter.Release()
|
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PageVo struct {
|
||||||
|
Items []string
|
||||||
|
Page int
|
||||||
|
PageSize int
|
||||||
|
Total int
|
||||||
|
TotalPage int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *LevelDB) SearchPage(prefix string, page int, pageSize int) *PageVo {
|
||||||
|
var items = make([]string, 0)
|
||||||
|
iter := db.driver.NewIterator(util.BytesPrefix([]byte(prefix)), nil)
|
||||||
|
defer iter.Release()
|
||||||
|
|
||||||
|
res := &PageVo{Page: page, PageSize: pageSize}
|
||||||
|
// 计算数据总数和总页数
|
||||||
|
total := 0
|
||||||
|
for iter.Next() {
|
||||||
|
total++
|
||||||
|
}
|
||||||
|
res.TotalPage = (total + pageSize - 1) / pageSize
|
||||||
|
res.Total = total
|
||||||
|
|
||||||
|
// 计算目标页码的起始和结束位置
|
||||||
|
start := (page - 1) * pageSize
|
||||||
|
if start > total {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
end := start + pageSize
|
||||||
|
if end > total {
|
||||||
|
end = total
|
||||||
|
}
|
||||||
|
|
||||||
|
// 跳转到目标页码的起始位置
|
||||||
|
count := 0
|
||||||
|
for iter.Next() {
|
||||||
|
if count >= start {
|
||||||
|
items = append(items, string(iter.Value()))
|
||||||
|
}
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
iter.Release()
|
||||||
|
res.Items = items
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
func (db *LevelDB) Delete(key string) error {
|
func (db *LevelDB) Delete(key string) error {
|
||||||
return db.driver.Delete([]byte(key), nil)
|
return db.driver.Delete([]byte(key), nil)
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,242 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="system-config">
|
<div class="system-config" v-loading="loading">
|
||||||
{{ title }}
|
<el-row class="opt-box">
|
||||||
|
<el-button type="primary" @click="showUserDialog = true">
|
||||||
|
<el-icon><Plus /></el-icon> 新增
|
||||||
|
</el-button>
|
||||||
|
|
||||||
|
<el-button type="success">
|
||||||
|
<el-icon><Plus /></el-icon> 批量新增
|
||||||
|
</el-button>
|
||||||
|
</el-row>
|
||||||
|
|
||||||
|
<el-row>
|
||||||
|
<el-table :data="users">
|
||||||
|
<el-table-column prop="name" label="用户名" />
|
||||||
|
<el-table-column prop="max_calls" label="最大调用次数" />
|
||||||
|
<el-table-column prop="remaining_calls" label="剩余调用次数" />
|
||||||
|
<el-table-column label="激活时间">
|
||||||
|
<template #default="scope">
|
||||||
|
<el-tag type="info" v-if="scope.row.active_time === 0">未激活</el-tag>
|
||||||
|
<span v-else>{{dateFormat(scope.row.active_time)}}</span>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column label="过期时间">
|
||||||
|
<template #default="scope">
|
||||||
|
<el-tag type="info" v-if="scope.row.active_time === 0">未激活</el-tag>
|
||||||
|
<span v-else>{{dateFormat(scope.row.expired_time)}}</span>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column label="状态" width="180">
|
||||||
|
<template #default="scope">
|
||||||
|
<el-tag v-if="scope.row.status" type="success">正常</el-tag>
|
||||||
|
<el-tag type="danger" v-else>停用</el-tag>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
|
||||||
|
<el-table-column label="操作" width="180">
|
||||||
|
<template #default="scope">
|
||||||
|
<el-button size="small" type="primary" @click="removeApiKey(scope.row.value)">编辑</el-button>
|
||||||
|
<el-button size="small" type="danger" @click="removeApiKey(scope.row.value)">删除</el-button>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
</el-table>
|
||||||
|
</el-row>
|
||||||
|
|
||||||
|
<el-dialog
|
||||||
|
v-model="showUserDialog"
|
||||||
|
title="新增用户"
|
||||||
|
width="30%"
|
||||||
|
:destroy-on-close="true"
|
||||||
|
>
|
||||||
|
<el-form :model="user" label-width="100px" ref="userAddFormRef" :rules="rules">
|
||||||
|
<el-form-item label="用户名:" prop="name">
|
||||||
|
<el-input
|
||||||
|
v-model="user.name"
|
||||||
|
autocomplete="off"
|
||||||
|
placeholder="请输入用户名"
|
||||||
|
/>
|
||||||
|
</el-form-item>
|
||||||
|
|
||||||
|
<el-form-item label="调用次数:">
|
||||||
|
<el-input
|
||||||
|
v-model="user.max_calls"
|
||||||
|
autocomplete="off"
|
||||||
|
placeholder="0 表示不限制调用次数"
|
||||||
|
/>
|
||||||
|
</el-form-item>
|
||||||
|
|
||||||
|
<el-form-item label="有效期:">
|
||||||
|
<el-input
|
||||||
|
v-model="user.term"
|
||||||
|
autocomplete="off"
|
||||||
|
placeholder="单位:天"
|
||||||
|
/>
|
||||||
|
</el-form-item>
|
||||||
|
|
||||||
|
<el-form-item label="聊天角色">
|
||||||
|
<el-select
|
||||||
|
v-model="user.chat_roles"
|
||||||
|
multiple
|
||||||
|
:filterable="true"
|
||||||
|
placeholder="选择聊天角色,多选"
|
||||||
|
@change="selectRole"
|
||||||
|
>
|
||||||
|
<el-option
|
||||||
|
v-for="item in roles"
|
||||||
|
:key="item.key"
|
||||||
|
:label="item.name"
|
||||||
|
:value="item.key"
|
||||||
|
:disabled="item.disabled"
|
||||||
|
/>
|
||||||
|
</el-select>
|
||||||
|
</el-form-item>
|
||||||
|
|
||||||
|
<el-form-item label="聊天记录">
|
||||||
|
<el-switch v-model="user.enable_history" />
|
||||||
|
</el-form-item>
|
||||||
|
</el-form>
|
||||||
|
|
||||||
|
<template #footer>
|
||||||
|
<span class="dialog-footer">
|
||||||
|
<el-button @click="showUserDialog = false">取消</el-button>
|
||||||
|
<el-button type="primary" @click="saveUser">提交</el-button>
|
||||||
|
</span>
|
||||||
|
</template>
|
||||||
|
</el-dialog>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import {onMounted} from "vue";
|
||||||
|
|
||||||
|
const userAddFormRef = ref(null);
|
||||||
|
onMounted(() => {
|
||||||
|
alert('xxxx')
|
||||||
|
})
|
||||||
|
|
||||||
|
const saveUser = ()=> {
|
||||||
|
userAddFormRef.value.validate((valid) => {
|
||||||
|
if (valid) {
|
||||||
|
|
||||||
|
this.showUserDialog = false
|
||||||
|
this.user.term = parseInt(this.user.term)
|
||||||
|
this.user.max_calls = parseInt(this.user.max_calls)
|
||||||
|
httpPost('/api/admin/user/add', this.user).then((res) => {
|
||||||
|
ElMessage.success('添加用户成功')
|
||||||
|
this.user = {}
|
||||||
|
this.users.push(res.data)
|
||||||
|
}).catch((e) => {
|
||||||
|
ElMessage.error('添加用户失败,'+e.message)
|
||||||
|
})
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ElMessage.error('error submit !')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import {defineComponent} from "vue";
|
import {defineComponent, nextTick, reactive, ref} from "vue";
|
||||||
|
import {Plus} from "@element-plus/icons-vue";
|
||||||
|
import {httpPost} from "@/utils/http";
|
||||||
|
import {ElMessage} from "element-plus";
|
||||||
|
import {arrayContains, dateFormat} from "@/utils/libs";
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: 'UserList',
|
name: 'UserList',
|
||||||
|
components: {Plus},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
title: "用户管理",
|
title: "用户管理",
|
||||||
|
users: [],
|
||||||
|
user: {
|
||||||
|
term: 30,
|
||||||
|
enable_history: true
|
||||||
|
},
|
||||||
|
roles: [],
|
||||||
|
showUserDialog: false,
|
||||||
|
rules: reactive({
|
||||||
|
name: [
|
||||||
|
{
|
||||||
|
required: true,
|
||||||
|
message: 'Please select Activity zone',
|
||||||
|
trigger: 'change',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}),
|
||||||
|
loading: true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
mounted() {
|
||||||
|
// 获取用户列表
|
||||||
|
httpPost('/api/admin/user/list').then((res) => {
|
||||||
|
this.users = res.data;
|
||||||
|
}).catch(() => {
|
||||||
|
ElMessage.error('获取系统配置失败')
|
||||||
|
})
|
||||||
|
|
||||||
|
// 获取角色列表
|
||||||
|
httpPost("/api/admin/chat-roles/get").then((res) => {
|
||||||
|
this.roles = res.data;
|
||||||
|
this.roles.unshift({name:'全部',key:'all'})
|
||||||
|
}).catch(() => {
|
||||||
|
ElMessage.error("获取聊天角色失败");
|
||||||
|
})
|
||||||
|
|
||||||
|
nextTick(() => {
|
||||||
|
this.loading = false
|
||||||
|
})
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
dateFormat() {
|
||||||
|
return dateFormat
|
||||||
|
},
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
selectRole: function (items) {
|
||||||
|
if (arrayContains(items, 'all')) {
|
||||||
|
for (let i =0;i < this.roles.length;i++) {
|
||||||
|
if (this.roles[i].key === 'all') {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
this.roles[i].disabled = true
|
||||||
|
this.user.chat_roles = ['all']
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (let i =0;i < this.roles.length;i++) {
|
||||||
|
if (this.roles[i].key === 'all') {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
this.roles[i].disabled = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
removeUser: function (user) {
|
||||||
|
console.log(user)
|
||||||
|
},
|
||||||
|
|
||||||
|
updateUser:function (user) {
|
||||||
|
console.log(user)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="stylus" scoped>
|
<style lang="stylus" scoped>
|
||||||
.system-config {
|
.system-config {
|
||||||
|
|
||||||
|
.opt-box {
|
||||||
|
padding-bottom: 10px;
|
||||||
|
|
||||||
|
.el-icon {
|
||||||
|
margin-right 5px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
Loading…
Reference in New Issue
Block a user