mirror of
				https://github.com/yangjian102621/geekai.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	feat: auto translate image creating prompt
This commit is contained in:
		@@ -83,7 +83,7 @@ var InnerFunctions = []Function{
 | 
			
		||||
			Properties: map[string]Property{
 | 
			
		||||
				"prompt": {
 | 
			
		||||
					Type:        "string",
 | 
			
		||||
					Description: "提示词,如果该参数中有中文的话,则需要翻译成英文。",
 | 
			
		||||
					Description: "提示词,请自动将该参数翻译成英文。",
 | 
			
		||||
				},
 | 
			
		||||
			},
 | 
			
		||||
			Required: []string{},
 | 
			
		||||
 
 | 
			
		||||
@@ -132,11 +132,13 @@ func (h *ChatHandler) sendOpenAiMessage(
 | 
			
		||||
				utils.ReplyMessage(ws, ErrImg)
 | 
			
		||||
			} else {
 | 
			
		||||
				f := h.App.Functions[functionName]
 | 
			
		||||
				// translate prompt
 | 
			
		||||
				if functionName == types.FuncImage {
 | 
			
		||||
					params["user_id"] = userVo.Id
 | 
			
		||||
					params["role_id"] = role.Id
 | 
			
		||||
					params["chat_id"] = session.ChatId
 | 
			
		||||
					params["session_id"] = session.SessionId
 | 
			
		||||
					const translatePromptTemplate = "Translate the following painting prompt words into English keyword phrases. Without any explanation, directly output the keyword phrases separated by commas. The content to be translated is: [%s]"
 | 
			
		||||
					r, err := utils.OpenAIRequest(fmt.Sprintf(translatePromptTemplate, params["prompt"]), apiKey, h.App.Config.ProxyURL, chatConfig.OpenAI.ApiURL)
 | 
			
		||||
					if err == nil {
 | 
			
		||||
						params["prompt"] = r
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				data, err := f.Invoke(params)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -4,11 +4,10 @@ import (
 | 
			
		||||
	"chatplus/core"
 | 
			
		||||
	"chatplus/core/types"
 | 
			
		||||
	"chatplus/store/model"
 | 
			
		||||
	"chatplus/utils"
 | 
			
		||||
	"chatplus/utils/resp"
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"github.com/imroc/req/v3"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"gorm.io/gorm"
 | 
			
		||||
)
 | 
			
		||||
@@ -27,27 +26,6 @@ func NewPromptHandler(app *core.AppServer, db *gorm.DB) *PromptHandler {
 | 
			
		||||
	return h
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type apiRes struct {
 | 
			
		||||
	Model   string `json:"model"`
 | 
			
		||||
	Choices []struct {
 | 
			
		||||
		Index   int `json:"index"`
 | 
			
		||||
		Message struct {
 | 
			
		||||
			Role    string `json:"role"`
 | 
			
		||||
			Content string `json:"content"`
 | 
			
		||||
		} `json:"message"`
 | 
			
		||||
		FinishReason string `json:"finish_reason"`
 | 
			
		||||
	} `json:"choices"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type apiErrRes struct {
 | 
			
		||||
	Error struct {
 | 
			
		||||
		Code    interface{} `json:"code"`
 | 
			
		||||
		Message string      `json:"message"`
 | 
			
		||||
		Param   interface{} `json:"param"`
 | 
			
		||||
		Type    string      `json:"type"`
 | 
			
		||||
	} `json:"error"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Rewrite translate and rewrite prompt with ChatGPT
 | 
			
		||||
func (h *PromptHandler) Rewrite(c *gin.Context) {
 | 
			
		||||
	var data struct {
 | 
			
		||||
@@ -93,28 +71,5 @@ func (h *PromptHandler) request(prompt string, promptTemplate string) (string, e
 | 
			
		||||
		return "", fmt.Errorf("error with fetch OpenAI API KEY:%v", res.Error)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	messages := make([]interface{}, 1)
 | 
			
		||||
	messages[0] = types.Message{
 | 
			
		||||
		Role:    "user",
 | 
			
		||||
		Content: fmt.Sprintf(promptTemplate, prompt),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var response apiRes
 | 
			
		||||
	var errRes apiErrRes
 | 
			
		||||
	r, err := req.C().SetProxyURL(h.App.Config.ProxyURL).R().SetHeader("Content-Type", "application/json").
 | 
			
		||||
		SetHeader("Authorization", "Bearer "+apiKey.Value).
 | 
			
		||||
		SetBody(types.ApiRequest{
 | 
			
		||||
			Model:       "gpt-3.5-turbo",
 | 
			
		||||
			Temperature: 0.9,
 | 
			
		||||
			MaxTokens:   1024,
 | 
			
		||||
			Stream:      false,
 | 
			
		||||
			Messages:    messages,
 | 
			
		||||
		}).
 | 
			
		||||
		SetErrorResult(&errRes).
 | 
			
		||||
		SetSuccessResult(&response).Post(h.App.ChatConfig.OpenAI.ApiURL)
 | 
			
		||||
	if err != nil || r.IsErrorState() {
 | 
			
		||||
		return "", fmt.Errorf("error with http request: %v%v%s", err, r.Err, errRes.Error.Message)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return response.Choices[0].Message.Content, nil
 | 
			
		||||
	return utils.OpenAIRequest(fmt.Sprintf(promptTemplate, prompt), apiKey.Value, h.App.Config.ProxyURL, h.App.ChatConfig.OpenAI.ApiURL)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -105,7 +105,7 @@ func (f FuncImage) Invoke(params map[string]interface{}) (string, error) {
 | 
			
		||||
		return "", fmt.Errorf("下载图片失败: %s", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	logger.Info(imgURL)
 | 
			
		||||
	//logger.Info(imgURL)
 | 
			
		||||
	return fmt.Sprintf("\n\n\n", imgURL), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								api/store/model/function.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								api/store/model/function.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
package model
 | 
			
		||||
 | 
			
		||||
type Function struct {
 | 
			
		||||
	Id          uint `gorm:"primarykey;column:id"`
 | 
			
		||||
	Name        string
 | 
			
		||||
	Description string
 | 
			
		||||
	Parameters  string
 | 
			
		||||
	Required    string
 | 
			
		||||
	Action      string
 | 
			
		||||
	Enabled     bool
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								api/store/vo/function.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								api/store/vo/function.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
package vo
 | 
			
		||||
 | 
			
		||||
type Parameters struct {
 | 
			
		||||
	Type       string              `json:"type"`
 | 
			
		||||
	Required   []string            `json:"required"`
 | 
			
		||||
	Properties map[string]Property `json:"properties"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Property struct {
 | 
			
		||||
	Type        string `json:"type"`
 | 
			
		||||
	Description string `json:"description"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Function struct {
 | 
			
		||||
	Id          uint       `json:"id"`
 | 
			
		||||
	Name        string     `json:"name"`
 | 
			
		||||
	Description string     `json:"description"`
 | 
			
		||||
	Parameters  Parameters `json:"parameters"`
 | 
			
		||||
	Required    []string   `json:"required"`
 | 
			
		||||
	Action      string     `json:"action"`
 | 
			
		||||
	Enabled     bool       `json:"enabled"`
 | 
			
		||||
}
 | 
			
		||||
@@ -4,6 +4,8 @@ import (
 | 
			
		||||
	"chatplus/core/types"
 | 
			
		||||
	logger2 "chatplus/logger"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/imroc/req/v3"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
@@ -61,3 +63,51 @@ func DownloadImage(imageURL string, proxy string) ([]byte, error) {
 | 
			
		||||
 | 
			
		||||
	return imageBytes, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type apiRes struct {
 | 
			
		||||
	Model   string `json:"model"`
 | 
			
		||||
	Choices []struct {
 | 
			
		||||
		Index   int `json:"index"`
 | 
			
		||||
		Message struct {
 | 
			
		||||
			Role    string `json:"role"`
 | 
			
		||||
			Content string `json:"content"`
 | 
			
		||||
		} `json:"message"`
 | 
			
		||||
		FinishReason string `json:"finish_reason"`
 | 
			
		||||
	} `json:"choices"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type apiErrRes struct {
 | 
			
		||||
	Error struct {
 | 
			
		||||
		Code    interface{} `json:"code"`
 | 
			
		||||
		Message string      `json:"message"`
 | 
			
		||||
		Param   interface{} `json:"param"`
 | 
			
		||||
		Type    string      `json:"type"`
 | 
			
		||||
	} `json:"error"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func OpenAIRequest(prompt string, apiKey string, proxy string, apiURL string) (string, error) {
 | 
			
		||||
	messages := make([]interface{}, 1)
 | 
			
		||||
	messages[0] = types.Message{
 | 
			
		||||
		Role:    "user",
 | 
			
		||||
		Content: prompt,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var response apiRes
 | 
			
		||||
	var errRes apiErrRes
 | 
			
		||||
	r, err := req.C().SetProxyURL(proxy).R().SetHeader("Content-Type", "application/json").
 | 
			
		||||
		SetHeader("Authorization", "Bearer "+apiKey).
 | 
			
		||||
		SetBody(types.ApiRequest{
 | 
			
		||||
			Model:       "gpt-3.5-turbo",
 | 
			
		||||
			Temperature: 0.9,
 | 
			
		||||
			MaxTokens:   1024,
 | 
			
		||||
			Stream:      false,
 | 
			
		||||
			Messages:    messages,
 | 
			
		||||
		}).
 | 
			
		||||
		SetErrorResult(&errRes).
 | 
			
		||||
		SetSuccessResult(&response).Post(apiURL)
 | 
			
		||||
	if err != nil || r.IsErrorState() {
 | 
			
		||||
		return "", fmt.Errorf("error with http request: %v%v%s", err, r.Err, errRes.Error.Message)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return response.Choices[0].Message.Content, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,19 @@
 | 
			
		||||
ALTER TABLE `chatgpt_mj_jobs` ADD `channel_id` CHAR(40) NULL DEFAULT NULL COMMENT '频道ID' AFTER `message_id`;
 | 
			
		||||
ALTER TABLE `chatgpt_mj_jobs` DROP INDEX `task_id`;
 | 
			
		||||
ALTER TABLE `chatgpt_products` ADD `img_calls` INT(11) NOT NULL DEFAULT '0' COMMENT '绘图次数' AFTER `calls`;
 | 
			
		||||
 | 
			
		||||
CREATE TABLE `chatgpt_functions` (
 | 
			
		||||
                                     `id` int NOT NULL,
 | 
			
		||||
                                     `name` varchar(30) NOT NULL COMMENT '函数名称',
 | 
			
		||||
                                     `description` varchar(255) DEFAULT NULL COMMENT '函数描述',
 | 
			
		||||
                                     `parameters` text COMMENT '函数参数(JSON)',
 | 
			
		||||
                                     `required` varchar(255) NOT NULL COMMENT '必填参数(JSON)',
 | 
			
		||||
                                     `action` varchar(255) DEFAULT NULL COMMENT '函数处理 API'
 | 
			
		||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='函数插件表';
 | 
			
		||||
 | 
			
		||||
ALTER TABLE `chatgpt_functions` ADD PRIMARY KEY (`id`);
 | 
			
		||||
ALTER TABLE `chatgpt_functions` MODIFY `id` int NOT NULL AUTO_INCREMENT;
 | 
			
		||||
 | 
			
		||||
ALTER TABLE `chatgpt_functions` ADD UNIQUE(`name`);
 | 
			
		||||
 | 
			
		||||
ALTER TABLE `chatgpt_functions` ADD `enabled` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '是否启用' AFTER `action`;
 | 
			
		||||
@@ -111,6 +111,11 @@ const items = [
 | 
			
		||||
    index: '/admin/reward',
 | 
			
		||||
    title: '众筹管理',
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    icon: 'control',
 | 
			
		||||
    index: '/admin/functions',
 | 
			
		||||
    title: '函数管理',
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    icon: 'config',
 | 
			
		||||
    index: '/admin/system',
 | 
			
		||||
@@ -121,29 +126,29 @@ const items = [
 | 
			
		||||
    index: '/admin/loginLog',
 | 
			
		||||
    title: '用户登录日志',
 | 
			
		||||
  },
 | 
			
		||||
  {
 | 
			
		||||
    icon: 'menu',
 | 
			
		||||
    index: '1',
 | 
			
		||||
    title: '常用模板页面',
 | 
			
		||||
    subs: [
 | 
			
		||||
      {
 | 
			
		||||
        index: '/admin/demo/form',
 | 
			
		||||
        title: '表单页面',
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        index: '/admin/demo/table',
 | 
			
		||||
        title: '常用表格',
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        index: '/admin/demo/import',
 | 
			
		||||
        title: '导入Excel',
 | 
			
		||||
      },
 | 
			
		||||
      {
 | 
			
		||||
        index: '/admin/demo/editor',
 | 
			
		||||
        title: '富文本编辑器',
 | 
			
		||||
      },
 | 
			
		||||
    ],
 | 
			
		||||
  },
 | 
			
		||||
  // {
 | 
			
		||||
  //   icon: 'menu',
 | 
			
		||||
  //   index: '1',
 | 
			
		||||
  //   title: '常用模板页面',
 | 
			
		||||
  //   subs: [
 | 
			
		||||
  //     {
 | 
			
		||||
  //       index: '/admin/demo/form',
 | 
			
		||||
  //       title: '表单页面',
 | 
			
		||||
  //     },
 | 
			
		||||
  //     {
 | 
			
		||||
  //       index: '/admin/demo/table',
 | 
			
		||||
  //       title: '常用表格',
 | 
			
		||||
  //     },
 | 
			
		||||
  //     {
 | 
			
		||||
  //       index: '/admin/demo/import',
 | 
			
		||||
  //       title: '导入Excel',
 | 
			
		||||
  //     },
 | 
			
		||||
  //     {
 | 
			
		||||
  //       index: '/admin/demo/editor',
 | 
			
		||||
  //       title: '富文本编辑器',
 | 
			
		||||
  //     },
 | 
			
		||||
  //   ],
 | 
			
		||||
  // },
 | 
			
		||||
];
 | 
			
		||||
 | 
			
		||||
const route = useRoute();
 | 
			
		||||
 
 | 
			
		||||
@@ -151,28 +151,10 @@ const routes = [
 | 
			
		||||
                component: () => import('@/views/admin/LoginLog.vue'),
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
                path: '/admin/demo/form',
 | 
			
		||||
                name: 'admin-form',
 | 
			
		||||
                meta: {title: '表单页面'},
 | 
			
		||||
                component: () => import('@/views/admin/demo/Form.vue'),
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
                path: '/admin/demo/table',
 | 
			
		||||
                name: 'admin-table',
 | 
			
		||||
                meta: {title: '数据列表'},
 | 
			
		||||
                component: () => import('@/views/admin/demo/Table.vue'),
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
                path: '/admin/demo/import',
 | 
			
		||||
                name: 'admin-import',
 | 
			
		||||
                meta: {title: '导入数据'},
 | 
			
		||||
                component: () => import('@/views/admin/demo/Import.vue'),
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
                path: '/admin/demo/editor',
 | 
			
		||||
                name: 'admin-editor',
 | 
			
		||||
                meta: {title: '富文本编辑器'},
 | 
			
		||||
                component: () => import('@/views/admin/demo/Editor.vue'),
 | 
			
		||||
                path: '/admin/functions',
 | 
			
		||||
                name: 'admin-functions',
 | 
			
		||||
                meta: {title: '函数管理'},
 | 
			
		||||
                component: () => import('@/views/admin/Functions.vue'),
 | 
			
		||||
            },
 | 
			
		||||
        ]
 | 
			
		||||
    },
 | 
			
		||||
@@ -185,7 +167,7 @@ const routes = [
 | 
			
		||||
    {
 | 
			
		||||
        name: 'mobile',
 | 
			
		||||
        path: '/mobile',
 | 
			
		||||
        meta: {title: 'ChatGPT-智能助手V3'},
 | 
			
		||||
        meta: {title: process.env.VUE_APP_TITLE},
 | 
			
		||||
        component: () => import('@/views/mobile/Home.vue'),
 | 
			
		||||
        redirect: '/mobile/chat/list',
 | 
			
		||||
        children: [
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										304
									
								
								web/src/views/admin/Functions.vue
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										304
									
								
								web/src/views/admin/Functions.vue
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,304 @@
 | 
			
		||||
<template>
 | 
			
		||||
  <div class="container role-list" v-loading="loading">
 | 
			
		||||
    <div class="handle-box">
 | 
			
		||||
      <el-button type="primary" :icon="Plus" @click="addRole">新增</el-button>
 | 
			
		||||
    </div>
 | 
			
		||||
    <el-row>
 | 
			
		||||
      <el-table :data="tableData" :border="parentBorder" style="width: 100%">
 | 
			
		||||
        <el-table-column label="函数名称" prop="name">
 | 
			
		||||
          <template #default="scope">
 | 
			
		||||
            <span class="sort" :data-id="scope.row.id">{{ scope.row.name }}</span>
 | 
			
		||||
          </template>
 | 
			
		||||
        </el-table-column>
 | 
			
		||||
        <el-table-column label="功能描述" prop="key"/>
 | 
			
		||||
        <el-table-column label="">
 | 
			
		||||
          <template #default="scope">
 | 
			
		||||
            <el-tag v-if="scope.row.enable" type="success">启用</el-tag>
 | 
			
		||||
            <el-tag type="danger" v-else>禁用</el-tag>
 | 
			
		||||
          </template>
 | 
			
		||||
        </el-table-column>
 | 
			
		||||
 | 
			
		||||
        <el-table-column label="操作" width="150" align="right">
 | 
			
		||||
          <template #default="scope">
 | 
			
		||||
            <el-button size="small" type="primary" @click="rowEdit(scope.$index, scope.row)">编辑</el-button>
 | 
			
		||||
            <el-popconfirm title="确定要删除当前函数吗?" @confirm="removeRole(scope.row)">
 | 
			
		||||
              <template #reference>
 | 
			
		||||
                <el-button size="small" type="danger">删除</el-button>
 | 
			
		||||
              </template>
 | 
			
		||||
            </el-popconfirm>
 | 
			
		||||
          </template>
 | 
			
		||||
        </el-table-column>
 | 
			
		||||
      </el-table>
 | 
			
		||||
    </el-row>
 | 
			
		||||
 | 
			
		||||
    <el-dialog
 | 
			
		||||
        v-model="showDialog"
 | 
			
		||||
        title="编辑角色"
 | 
			
		||||
        width="50%"
 | 
			
		||||
    >
 | 
			
		||||
      <el-form :model="role" label-width="120px" ref="formRef" label-position="left" :rules="rules">
 | 
			
		||||
        <el-form-item label="角色名称:" prop="name">
 | 
			
		||||
          <el-input
 | 
			
		||||
              v-model="role.name"
 | 
			
		||||
              autocomplete="off"
 | 
			
		||||
          />
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
 | 
			
		||||
        <el-form-item label="角色标志:" prop="key">
 | 
			
		||||
          <el-input
 | 
			
		||||
              v-model="role.key"
 | 
			
		||||
              autocomplete="off"
 | 
			
		||||
          />
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
 | 
			
		||||
        <el-form-item label="角色图标:" prop="icon">
 | 
			
		||||
          <el-input
 | 
			
		||||
              v-model="role.icon"
 | 
			
		||||
              autocomplete="off"
 | 
			
		||||
          />
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
 | 
			
		||||
        <el-form-item label="打招呼信息:" prop="hello_msg">
 | 
			
		||||
          <el-input
 | 
			
		||||
              v-model="role.hello_msg"
 | 
			
		||||
              autocomplete="off"
 | 
			
		||||
          />
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
 | 
			
		||||
        <el-form-item label="上下文信息:" prop="context">
 | 
			
		||||
          <template #default>
 | 
			
		||||
            <el-table :data="role.context" :border="childBorder" size="small">
 | 
			
		||||
              <el-table-column label="对话角色" width="120">
 | 
			
		||||
                <template #default="scope">
 | 
			
		||||
                  <el-input
 | 
			
		||||
                      v-model="scope.row.role"
 | 
			
		||||
                      autocomplete="off"
 | 
			
		||||
                  />
 | 
			
		||||
                </template>
 | 
			
		||||
              </el-table-column>
 | 
			
		||||
              <el-table-column label="对话内容">
 | 
			
		||||
                <template #header>
 | 
			
		||||
                  <div class="context-msg-key">
 | 
			
		||||
                    <span>对话内容</span>
 | 
			
		||||
                    <span class="fr">
 | 
			
		||||
                      <el-button type="primary" @click="addContext" size="small">
 | 
			
		||||
                      <el-icon>
 | 
			
		||||
                        <Plus/>
 | 
			
		||||
                      </el-icon>
 | 
			
		||||
                      增加一行
 | 
			
		||||
                    </el-button>
 | 
			
		||||
                    </span>
 | 
			
		||||
                  </div>
 | 
			
		||||
                </template>
 | 
			
		||||
 | 
			
		||||
                <template #default="scope">
 | 
			
		||||
                  <div class="context-msg-content">
 | 
			
		||||
                    <el-input
 | 
			
		||||
                        v-model="scope.row.content"
 | 
			
		||||
                        autocomplete="off"
 | 
			
		||||
                    />
 | 
			
		||||
                    <span><el-icon @click="removeContext(scope.$index)"><RemoveFilled/></el-icon></span>
 | 
			
		||||
                  </div>
 | 
			
		||||
                </template>
 | 
			
		||||
              </el-table-column>
 | 
			
		||||
            </el-table>
 | 
			
		||||
          </template>
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
 | 
			
		||||
        <el-form-item label="启用状态">
 | 
			
		||||
          <el-switch v-model="role.enable"/>
 | 
			
		||||
        </el-form-item>
 | 
			
		||||
      </el-form>
 | 
			
		||||
 | 
			
		||||
      <template #footer>
 | 
			
		||||
            <span class="dialog-footer">
 | 
			
		||||
              <el-button @click="showDialog = false">取消</el-button>
 | 
			
		||||
              <el-button type="primary" @click="save">保存</el-button>
 | 
			
		||||
            </span>
 | 
			
		||||
      </template>
 | 
			
		||||
    </el-dialog>
 | 
			
		||||
  </div>
 | 
			
		||||
</template>
 | 
			
		||||
 | 
			
		||||
<script setup>
 | 
			
		||||
 | 
			
		||||
import {Plus, RemoveFilled} from "@element-plus/icons-vue";
 | 
			
		||||
import {onMounted, reactive, ref} from "vue";
 | 
			
		||||
import {httpGet, httpPost} from "@/utils/http";
 | 
			
		||||
import {ElMessage} from "element-plus";
 | 
			
		||||
import {copyObj, removeArrayItem} from "@/utils/libs";
 | 
			
		||||
import {Sortable} from "sortablejs"
 | 
			
		||||
 | 
			
		||||
const showDialog = ref(false)
 | 
			
		||||
const parentBorder = ref(true)
 | 
			
		||||
const childBorder = ref(true)
 | 
			
		||||
const tableData = ref([])
 | 
			
		||||
const sortedTableData = ref([])
 | 
			
		||||
const role = ref({context: []})
 | 
			
		||||
const formRef = ref(null)
 | 
			
		||||
const editRow = ref({})
 | 
			
		||||
const loading = ref(true)
 | 
			
		||||
 | 
			
		||||
const rules = reactive({
 | 
			
		||||
  name: [{required: true, message: '请输入用户名', trigger: 'blur',}],
 | 
			
		||||
  key: [{required: true, message: '请输入角色标识', trigger: 'blur',}],
 | 
			
		||||
  icon: [{required: true, message: '请输入角色图标', trigger: 'blur',}],
 | 
			
		||||
  sort: [
 | 
			
		||||
    {required: true, message: '请输入排序数字', trigger: 'blur'},
 | 
			
		||||
    {type: 'number', message: '请输入有效数字'},
 | 
			
		||||
  ],
 | 
			
		||||
  hello_msg: [{required: true, message: '请输入打招呼信息', trigger: 'change',}]
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
// 获取角色列表
 | 
			
		||||
httpGet('/api/admin/role/list').then((res) => {
 | 
			
		||||
  tableData.value = res.data
 | 
			
		||||
  sortedTableData.value = copyObj(tableData.value)
 | 
			
		||||
  loading.value = false
 | 
			
		||||
}).catch(() => {
 | 
			
		||||
  ElMessage.error("获取聊天角色失败");
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
onMounted(() => {
 | 
			
		||||
  const drawBodyWrapper = document.querySelector('.el-table__body tbody')
 | 
			
		||||
 | 
			
		||||
  // 初始化拖动排序插件
 | 
			
		||||
  Sortable.create(drawBodyWrapper, {
 | 
			
		||||
    sort: true,
 | 
			
		||||
    animation: 500,
 | 
			
		||||
    onEnd({newIndex, oldIndex, from}) {
 | 
			
		||||
      if (oldIndex === newIndex) {
 | 
			
		||||
        return
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      const sortedData = Array.from(from.children).map(row => row.querySelector('.sort').getAttribute('data-id'));
 | 
			
		||||
      const ids = []
 | 
			
		||||
      const sorts = []
 | 
			
		||||
      sortedData.forEach((id, index) => {
 | 
			
		||||
        ids.push(parseInt(id))
 | 
			
		||||
        sorts.push(index)
 | 
			
		||||
      })
 | 
			
		||||
 | 
			
		||||
      httpPost("/api/admin/role/sort", {ids: ids, sorts: sorts}).catch(e => {
 | 
			
		||||
        ElMessage.error("排序失败:" + e.message)
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
const editSort = function (event, row) {
 | 
			
		||||
  event.stopPropagation()
 | 
			
		||||
  editRow.value.id = row.id
 | 
			
		||||
  editRow.value.sort = row.sort
 | 
			
		||||
}
 | 
			
		||||
const updateSort = function (row) {
 | 
			
		||||
  if (row.sort === editRow.value.sort) {
 | 
			
		||||
    editRow.value.id = 0
 | 
			
		||||
    return
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  httpPost('/api/admin/role/sort', {"id": row.id, "sort": row.sort}).then(() => {
 | 
			
		||||
    editRow.value.id = 0
 | 
			
		||||
  }).catch(() => {
 | 
			
		||||
    ElMessage.error("更新失败!")
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 编辑
 | 
			
		||||
const curIndex = ref(0)
 | 
			
		||||
const rowEdit = function (index, row) {
 | 
			
		||||
  curIndex.value = index
 | 
			
		||||
  role.value = copyObj(row)
 | 
			
		||||
  showDialog.value = true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const addRole = function () {
 | 
			
		||||
  role.value = {context: []}
 | 
			
		||||
  showDialog.value = true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const save = function () {
 | 
			
		||||
  formRef.value.validate((valid) => {
 | 
			
		||||
    if (valid) {
 | 
			
		||||
      showDialog.value = false
 | 
			
		||||
      httpPost('/api/admin/role/save', role.value).then((res) => {
 | 
			
		||||
        ElMessage.success('操作成功')
 | 
			
		||||
        // 更新当前数据行
 | 
			
		||||
        if (role.value.id) {
 | 
			
		||||
          tableData.value[curIndex.value] = role.value
 | 
			
		||||
        } else {
 | 
			
		||||
          tableData.value.push(res.data)
 | 
			
		||||
        }
 | 
			
		||||
      }).catch((e) => {
 | 
			
		||||
        ElMessage.error('操作失败,' + e.message)
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const removeRole = function (row) {
 | 
			
		||||
  httpGet('/api/admin/role/remove?id=' + row.id).then(() => {
 | 
			
		||||
    ElMessage.success("删除成功!")
 | 
			
		||||
    tableData.value = removeArrayItem(tableData.value, row, (v1, v2) => {
 | 
			
		||||
      return v1.id === v2.id
 | 
			
		||||
    })
 | 
			
		||||
  }).catch(() => {
 | 
			
		||||
    ElMessage.error("删除失败!")
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const addContext = function () {
 | 
			
		||||
  if (!role.value.context) {
 | 
			
		||||
    role.value.context = []
 | 
			
		||||
  }
 | 
			
		||||
  role.value.context.push({role: '', content: ''})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const removeContext = function (index) {
 | 
			
		||||
  role.value.context.splice(index, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<style lang="stylus" scoped>
 | 
			
		||||
.role-list {
 | 
			
		||||
  .opt-box {
 | 
			
		||||
    padding-bottom: 10px;
 | 
			
		||||
    display flex;
 | 
			
		||||
    justify-content flex-end
 | 
			
		||||
 | 
			
		||||
    .el-icon {
 | 
			
		||||
      margin-right 5px;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .context-msg-key {
 | 
			
		||||
    .fr {
 | 
			
		||||
      float right
 | 
			
		||||
 | 
			
		||||
      .el-icon {
 | 
			
		||||
        margin-right 5px
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .context-msg-content {
 | 
			
		||||
    display flex
 | 
			
		||||
 | 
			
		||||
    .el-icon {
 | 
			
		||||
      font-size: 20px;
 | 
			
		||||
      margin-top 5px;
 | 
			
		||||
      margin-left 5px;
 | 
			
		||||
      cursor pointer
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .el-input--small {
 | 
			
		||||
    width 30px;
 | 
			
		||||
 | 
			
		||||
    .el-input__inner {
 | 
			
		||||
      text-align center
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
</style>
 | 
			
		||||
		Reference in New Issue
	
	Block a user