feat: function CRUD operation is ready

This commit is contained in:
RockYang 2023-12-24 22:12:12 +08:00
parent 4936896ff7
commit 7ca89d8b54
9 changed files with 71 additions and 30 deletions

View File

@ -14,13 +14,12 @@ var logger = logger2.GetLogger()
func NewDefaultConfig() *types.AppConfig { func NewDefaultConfig() *types.AppConfig {
return &types.AppConfig{ return &types.AppConfig{
Listen: "0.0.0.0:5678", Listen: "0.0.0.0:5678",
ProxyURL: "", ProxyURL: "",
Manager: types.Manager{Username: "admin", Password: "admin123"}, Manager: types.Manager{Username: "admin", Password: "admin123"},
StaticDir: "./static", StaticDir: "./static",
StaticUrl: "http://localhost/5678/static", StaticUrl: "http://localhost/5678/static",
Redis: types.RedisConfig{Host: "localhost", Port: 6379, Password: ""}, Redis: types.RedisConfig{Host: "localhost", Port: 6379, Password: ""},
AesEncryptKey: utils.RandString(24),
Session: types.Session{ Session: types.Session{
SecretKey: utils.RandString(64), SecretKey: utils.RandString(64),
MaxAge: 86400, MaxAge: 86400,

View File

@ -5,22 +5,21 @@ import (
) )
type AppConfig struct { type AppConfig struct {
Path string `toml:"-"` Path string `toml:"-"`
Listen string Listen string
Session Session Session Session
ProxyURL string ProxyURL string
MysqlDns string // mysql 连接地址 MysqlDns string // mysql 连接地址
Manager Manager // 后台管理员账户信息 Manager Manager // 后台管理员账户信息
StaticDir string // 静态资源目录 StaticDir string // 静态资源目录
StaticUrl string // 静态资源 URL StaticUrl string // 静态资源 URL
Redis RedisConfig // redis 连接信息 Redis RedisConfig // redis 连接信息
ApiConfig ChatPlusApiConfig // ChatPlus API authorization configs ApiConfig ChatPlusApiConfig // ChatPlus API authorization configs
AesEncryptKey string SmsConfig AliYunSmsConfig // AliYun send message service config
SmsConfig AliYunSmsConfig // AliYun send message service config OSS OSSConfig // OSS config
OSS OSSConfig // OSS config MjConfigs []MidJourneyConfig // mj AI draw service pool
MjConfigs []MidJourneyConfig // mj AI draw service pool WeChatBot bool // 是否启用微信机器人
WeChatBot bool // 是否启用微信机器人 SdConfigs []StableDiffusionConfig // sd AI draw service pool
SdConfigs []StableDiffusionConfig // sd AI draw service pool
XXLConfig XXLConfig XXLConfig XXLConfig
AlipayConfig AlipayConfig AlipayConfig AlipayConfig

View File

@ -9,6 +9,8 @@ import (
"chatplus/utils" "chatplus/utils"
"chatplus/utils/resp" "chatplus/utils/resp"
"github.com/golang-jwt/jwt/v5"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -104,3 +106,20 @@ func (h *FunctionHandler) Remove(c *gin.Context) {
} }
resp.SUCCESS(c) resp.SUCCESS(c)
} }
// GenToken generate function api access token
func (h *FunctionHandler) GenToken(c *gin.Context) {
// 创建 token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user_id": 0,
"expired": 0,
})
tokenString, err := token.SignedString([]byte(h.App.Config.Session.SecretKey))
if err != nil {
logger.Error("error with generate token", err)
resp.ERROR(c)
return
}
resp.SUCCESS(c, tokenString)
}

View File

@ -355,6 +355,7 @@ func main() {
group.POST("set", h.Set) group.POST("set", h.Set)
group.GET("list", h.List) group.GET("list", h.List)
group.GET("remove", h.Remove) group.GET("remove", h.Remove)
group.GET("token", h.GenToken)
}), }),
fx.Provide(handler.NewTestHandler), fx.Provide(handler.NewTestHandler),

View File

@ -32,7 +32,7 @@
<el-table-column label="操作" width="180"> <el-table-column label="操作" width="180">
<template #default="scope"> <template #default="scope">
<el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button> <el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button>
<el-popconfirm title="确定要删除当前记录吗?" @confirm="remove(scope.row)"> <el-popconfirm title="确定要删除当前记录吗?" @confirm="remove(scope.row)" :width="200">
<template #reference> <template #reference>
<el-button size="small" type="danger">删除</el-button> <el-button size="small" type="danger">删除</el-button>
</template> </template>

View File

@ -35,7 +35,7 @@
<el-table-column label="操作" width="180"> <el-table-column label="操作" width="180">
<template #default="scope"> <template #default="scope">
<el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button> <el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button>
<el-popconfirm title="确定要删除当前记录吗?" @confirm="remove(scope.row)"> <el-popconfirm title="确定要删除当前记录吗?" @confirm="remove(scope.row)" :width="200">
<template #reference> <template #reference>
<el-button size="small" type="danger">删除</el-button> <el-button size="small" type="danger">删除</el-button>
</template> </template>

View File

@ -21,7 +21,7 @@
<el-table-column label="操作" width="150" align="right"> <el-table-column label="操作" width="150" align="right">
<template #default="scope"> <template #default="scope">
<el-button size="small" type="primary" @click="rowEdit(scope.$index, scope.row)">编辑</el-button> <el-button size="small" type="primary" @click="rowEdit(scope.$index, scope.row)">编辑</el-button>
<el-popconfirm title="确定要删除当前函数吗?" @confirm="remove(scope.row)"> <el-popconfirm title="确定要删除当前函数吗?" @confirm="remove(scope.row)" :width="200">
<template #reference> <template #reference>
<el-button size="small" type="danger">删除</el-button> <el-button size="small" type="danger">删除</el-button>
</template> </template>
@ -119,6 +119,7 @@
<el-input <el-input
v-model="item.action" v-model="item.action"
autocomplete="off" autocomplete="off"
placeholder="该函数实现的API地址可以是第三方服务API"
/> />
</el-form-item> </el-form-item>
@ -126,7 +127,20 @@
<el-input <el-input
v-model="item.token" v-model="item.token"
autocomplete="off" autocomplete="off"
/> placeholder="API授权Token"
>
<template #append>
<el-tooltip
class="box-item"
effect="dark"
content="只有本地服务才可以使用自动生成Token<br/>第三方服务请填写第三方服务API Token"
placement="top-end"
raw-content
>
<el-button @click="generateToken">生成Token</el-button>
</el-tooltip>
</template>
</el-input>
</el-form-item> </el-form-item>
<el-form-item label="启用状态"> <el-form-item label="启用状态">
<el-switch v-model="item.enabled"/> <el-switch v-model="item.enabled"/>
@ -207,6 +221,7 @@ const rowEdit = function (index, row) {
const addRow = function () { const addRow = function () {
item.value = {enabled:true} item.value = {enabled:true}
params.value = []
showDialog.value = true showDialog.value = true
} }
@ -240,7 +255,7 @@ const save = function () {
} }
const remove = function (row) { const remove = function (row) {
httpGet('/api/admin/role/remove?id=' + row.id).then(() => { httpGet('/api/admin/function/remove?id=' + row.id).then(() => {
ElMessage.success("删除成功!") ElMessage.success("删除成功!")
fetch() fetch()
}).catch(() => { }).catch(() => {
@ -267,6 +282,14 @@ const functionSet = (filed,row) => {
}) })
} }
const generateToken = () => {
httpGet('/api/admin/function/token').then(res => {
item.value.token = res.data
}).catch(() => {
ElMessage.error("生成 Token 失败")
})
}
</script> </script>
<style lang="stylus" scoped> <style lang="stylus" scoped>

View File

@ -38,7 +38,7 @@
<el-table-column label="操作" width="180"> <el-table-column label="操作" width="180">
<template #default="scope"> <template #default="scope">
<el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button> <el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button>
<el-popconfirm title="确定要删除当前记录吗?" @confirm="remove(scope.row)"> <el-popconfirm title="确定要删除当前记录吗?" @confirm="remove(scope.row)" :width="200">
<template #reference> <template #reference>
<el-button size="small" type="danger">删除</el-button> <el-button size="small" type="danger">删除</el-button>
</template> </template>

View File

@ -36,7 +36,7 @@
<el-table-column label="操作" width="150" align="right"> <el-table-column label="操作" width="150" align="right">
<template #default="scope"> <template #default="scope">
<el-button size="small" type="primary" @click="rowEdit(scope.$index, scope.row)">编辑</el-button> <el-button size="small" type="primary" @click="rowEdit(scope.$index, scope.row)">编辑</el-button>
<el-popconfirm title="确定要删除当前角色吗?" @confirm="removeRole(scope.row)"> <el-popconfirm title="确定要删除当前角色吗?" @confirm="removeRole(scope.row)" :width="200">
<template #reference> <template #reference>
<el-button size="small" type="danger">删除</el-button> <el-button size="small" type="danger">删除</el-button>
</template> </template>