kefu/controller/ent.go

173 lines
4.5 KiB
Go
Raw Permalink Normal View History

2024-12-10 02:50:12 +00:00
package controller
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"kefu/lib"
"kefu/models"
"kefu/tools"
"kefu/types"
"log"
"time"
)
// 获取企业信息
func PostEntInfo(c *gin.Context) {
entId := c.PostForm("ent_id")
if entId == "" {
HandleError(c, types.ApiCode.ENT_ERROR, types.ApiCode.GetMessage(types.ApiCode.ENT_ERROR), errors.New("商户ID不存在"))
return
}
entInfo := models.FindUserByUid(entId)
if entInfo.Name == "" {
HandleError(c, types.ApiCode.ENT_ERROR, types.ApiCode.GetMessage(types.ApiCode.ENT_ERROR), errors.New("商户ID不存在"))
return
}
//查看过期时间
nowSecond := time.Now().Unix()
expireSecond := entInfo.ExpiredAt.Unix()
if models.FindConfig("KefuExpired") == "on" && expireSecond < nowSecond {
HandleError(c, types.ApiCode.ACCOUNT_EXPIRED, types.ApiCode.GetMessage(types.ApiCode.ACCOUNT_EXPIRED), errors.New("账号过期"))
return
}
//查看状态
if entInfo.Status == 1 {
HandleError(c, types.ApiCode.ACCOUNT_FORBIDDEN, types.ApiCode.GetMessage(types.ApiCode.ACCOUNT_FORBIDDEN), errors.New("账号未开通"))
return
}
configs := models.GetEntConfigsMap(entId, "PreVisitorForm", "KefuIntroduce",
"RobotStatus", "TurnToMan", "RobotNoAnswer", "RobotName", "RobotAvator", "RobotCard",
"VisitorNotice", "AutoWelcome", "VisitorCookie", "VisitorMaxNum", "VisitorMaxNumNotice", "ScanWechatQrcode",
"VisitorAuthApi", "CloseNotice", "QdrantAIStatus", "Marketing")
configs["RobotName"] = tools.Ifelse(configs["RobotName"] != "", configs["RobotName"], entInfo.Nickname).(string)
configs["RobotAvator"] = tools.Ifelse(configs["RobotAvator"] != "", configs["RobotAvator"], entInfo.Avator).(string)
c.JSON(200, gin.H{
"code": types.ApiCode.SUCCESS,
"msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS),
"result": configs,
})
}
func SendEntEmail(c *gin.Context) {
ent_id := c.PostForm("ent_id")
email := c.PostForm("email")
weixin := c.PostForm("weixin")
msg := c.PostForm("msg")
name := c.PostForm("name")
if msg == "" || name == "" || ent_id == "" {
c.JSON(200, gin.H{
"code": 400,
"msg": "内容/姓名不能为空",
})
return
}
content := fmt.Sprintf("[%s] [%s] [%s] [%s]", name, weixin, email, msg)
go SendEntSmtpEmail("[留言]"+name, content, ent_id)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
})
}
// 翻译
func GetTranslate(c *gin.Context) {
from := c.Query("from")
if from == "" {
from = "en"
}
to := c.Query("to")
if to == "" {
to = "zh"
}
words := c.Query("words")
baidu := &lib.BaiduFanyi{
AppId: models.FindConfig("BaiduFanyiAppId"),
AppSec: models.FindConfig("BaiduFanyiAppSec"),
}
res, err := baidu.Translate(words, from, to)
if err != nil {
}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"from": from,
"to": to,
"src": words,
"dst": res,
},
})
}
// 翻译
func GetKefuTranslate(c *gin.Context) {
entId, _ := c.Get("ent_id")
from := c.Query("from")
if from == "" {
from = "auto"
}
to := c.Query("to")
if to == "" {
to = "zh"
}
lang := c.Query("lang")
if lang == "" {
lang = "中文"
}
words := c.Query("words")
configs := models.GetEntConfigsMap(entId.(string), "BaiduFanyiAppId", "BaiduFanyiAppSec", "FanyiSource", "chatGPTUrl", "chatGPTSecret", "BigModelName")
if configs["FanyiSource"] == "gpt" {
openaiUrl := configs["chatGPTUrl"]
openaiKey := configs["chatGPTSecret"]
gpt := lib.NewChatGptTool(openaiUrl, openaiKey)
gptMessages := make([]lib.Gpt3Dot5Message, 0)
gptMessages = append(gptMessages, lib.Gpt3Dot5Message{
Role: "system",
Content: "假设你是一个专业的翻译工具,请使用地道语言翻译,不用解释仅提供结果",
})
gptMessages = append(gptMessages, lib.Gpt3Dot5Message{
Role: "user",
Content: fmt.Sprintf("请将这句话翻译成%s%s", lang, words),
})
log.Println(gptMessages)
content, _ := gpt.ChatGPT3Dot5Turbo(gptMessages, configs["BigModelName"])
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"from": from,
"to": to,
"src": words,
"dst": content,
},
})
return
}
appId := configs["BaiduFanyiAppId"]
appSec := configs["BaiduFanyiAppSec"]
if appId == "" || appSec == "" {
appId = models.FindConfig("BaiduFanyiAppId")
appSec = models.FindConfig("BaiduFanyiAppSec")
}
baidu := &lib.BaiduFanyi{
AppId: appId,
AppSec: appSec,
}
res, err := baidu.Translate(words, from, to)
if err != nil {
}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"from": from,
"to": to,
"src": words,
"dst": res,
},
})
}