package controller import ( "github.com/gin-gonic/gin" "github.com/silenceper/wechat/v2" offConfig "github.com/silenceper/wechat/v2/officialaccount/config" "kefu/lib" "kefu/models" "kefu/types" "path" "strconv" "strings" ) func GetConfigs(c *gin.Context) { configs := models.FindConfigs() c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": configs, }) } func GetEntConfigs(c *gin.Context) { entId, _ := c.Get("ent_id") configs := models.FindEntConfigs(entId) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": configs, }) } func GetConfig(c *gin.Context) { key := c.Query("key") config := models.FindConfig(key) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": config, }) } func GetEntConfig(c *gin.Context) { key := c.Query("key") entId, _ := c.Get("ent_id") config := models.FindEntConfig(entId, key) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": config.ConfValue, }) } func PostEntConfigs(c *gin.Context) { entId, _ := c.Get("ent_id") name := c.PostForm("name") key := c.PostForm("key") value := c.PostForm("value") if key == "" { c.JSON(200, gin.H{ "code": 400, "msg": "error", }) return } config := models.FindEntConfig(entId, key) if config.ID == 0 { models.CreateEntConfig(entId, name, key, value) } else { models.UpdateEntConfig(entId, name, key, value) } c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": "", }) } func PostEntConfigsByAdmin(c *gin.Context) { entId := c.PostForm("ent_id") name := c.PostForm("name") key := c.PostForm("key") value := c.PostForm("value") if key == "" { c.JSON(200, gin.H{ "code": 400, "msg": "error", }) return } config := models.FindEntConfig(entId, key) if config.ID == 0 { models.CreateEntConfig(entId, name, key, value) } else { models.UpdateEntConfig(entId, name, key, value) } c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": "", }) } // 保存微信菜单数据 func PostWechatMenu(c *gin.Context) { kefuId, _ := c.Get("kefu_id") menu := c.PostForm("menu") name := c.PostForm("name") if menu == "" { c.JSON(200, gin.H{ "code": 400, "msg": "error", }) return } config := models.FindEntConfig(kefuId, "WechatMenu") if config.ID == 0 { models.CreateEntConfig(kefuId, name, "WechatMenu", menu) } else { models.UpdateEntConfig(kefuId, name, "WechatMenu", menu) } c.JSON(200, gin.H{ "code": 200, "msg": "ok", }) } // 生成微信菜单 func GetWechatMenu(c *gin.Context) { entId, _ := c.Get("ent_id") config := models.FindEntConfig(entId, "WechatMenu") if config.ID == 0 || config.ConfValue == "" { c.JSON(200, gin.H{ "code": 400, "msg": "没有菜单数据", }) return } wechatConfig, err := lib.NewWechatLib(entId.(string)) if wechatConfig == nil { c.JSON(200, gin.H{ "code": 400, "msg": err.Error(), }) return } wc := wechat.NewWechat() cfg := &offConfig.Config{ AppID: wechatConfig.AppId, AppSecret: wechatConfig.AppSecret, Token: wechatConfig.Token, //EncodingAESKey: "xxxx", Cache: memory, } officialAccount := wc.GetOfficialAccount(cfg) menu := officialAccount.GetMenu() err = menu.SetMenuByJSON(config.ConfValue) if err != nil { c.JSON(200, gin.H{ "code": 400, "msg": err.Error(), }) return } c.JSON(200, gin.H{ "code": 200, "msg": "ok", }) } // 删除菜单 func GetDelWechatMenu(c *gin.Context) { entId, _ := c.Get("ent_id") wechatConfig, err := lib.NewWechatLib(entId.(string)) if wechatConfig == nil { c.JSON(200, gin.H{ "code": 400, "msg": err.Error(), }) return } wc := wechat.NewWechat() cfg := &offConfig.Config{ AppID: wechatConfig.AppId, AppSecret: wechatConfig.AppSecret, Token: wechatConfig.Token, //EncodingAESKey: "xxxx", Cache: memory, } officialAccount := wc.GetOfficialAccount(cfg) menu := officialAccount.GetMenu() err = menu.DeleteMenu() if err != nil { c.JSON(200, gin.H{ "code": 400, "msg": err.Error(), }) return } c.JSON(200, gin.H{ "code": 200, "msg": "ok", }) } func PostConfig(c *gin.Context) { key := c.PostForm("key") value := c.PostForm("value") if key == "" { c.JSON(200, gin.H{ "code": 400, "msg": "error", }) return } models.UpdateConfig(key, value) c.JSON(200, gin.H{ "code": 200, "msg": "ok", }) } // 设置机器人信息 func GetRobotInfo(c *gin.Context) { entId, _ := c.Get("ent_id") c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": gin.H{ "name": models.FindEntConfig(entId, "RobotName").ConfValue, "avator": models.FindEntConfig(entId, "RobotAvator").ConfValue, "status": models.FindEntConfig(entId, "RobotStatus").ConfValue, "noAnswer": models.FindEntConfig(entId, "RobotNoAnswer").ConfValue, "transferKefu": models.FindEntConfig(entId, "TurnToMan").ConfValue, "chatGPTSecret": models.FindEntConfig(entId, "chatGPTSecret").ConfValue, }, }) } // 设置机器人信息 func PostSetRobotInfo(c *gin.Context) { entId, _ := c.Get("ent_id") models.DelEntConfig("ent_id = ? and (conf_key='chatGPTSecret' or conf_key='RobotAvator' or conf_key='RobotStatus' or conf_key='RobotName' or conf_key='RobotNoAnswer' or conf_key='TurnToMan')", entId) configs := []models.EntConfig{ { ConfName: "机器人名称", ConfKey: "RobotName", ConfValue: c.PostForm("name"), EntId: entId.(string), }, { ConfName: "机器人头像", ConfKey: "RobotAvator", ConfValue: c.PostForm("avator"), EntId: entId.(string), }, { ConfName: "机器人接待方式", ConfKey: "RobotStatus", ConfValue: c.PostForm("status"), EntId: entId.(string), }, { ConfName: "机器人无法解答", ConfKey: "RobotNoAnswer", ConfValue: c.PostForm("noAnswer"), EntId: entId.(string), }, { ConfName: "转接人工", ConfKey: "TurnToMan", ConfValue: c.PostForm("transferKefu"), EntId: entId.(string), }, { ConfName: "chatGPT OpenAI接口密钥", ConfKey: "chatGPTSecret", ConfValue: c.PostForm("chatGPTSecret"), EntId: entId.(string), }, } models.CreateMany(entId, configs) c.JSON(200, gin.H{ "code": 200, "msg": "ok", }) } // 上传微信认证文件 func PostUploadWechatFile(c *gin.Context) { SendAttachment, err := strconv.ParseBool(models.FindConfig("SendAttachment")) if !SendAttachment || err != nil { c.JSON(200, gin.H{ "code": 400, "msg": "禁止上传附件!", }) return } f, err := c.FormFile("file") if err != nil { c.JSON(200, gin.H{ "code": 400, "msg": "上传失败!", }) return } else { fileExt := strings.ToLower(path.Ext(f.Filename)) if f.Size >= 1*1024*1024 { c.JSON(200, gin.H{ "code": 400, "msg": "上传失败!不允许超过1M", }) return } if fileExt != ".txt" { c.JSON(200, gin.H{ "code": 400, "msg": "上传失败!只允许txt文件", }) return } c.SaveUploadedFile(f, f.Filename) c.JSON(200, gin.H{ "code": 200, "msg": "上传成功!", "result": gin.H{ "path": "/" + f.Filename, }, }) } } // 设置工作时间 func PostWorkTime(c *gin.Context) { entId, _ := c.Get("ent_id") models.DelEntConfig("ent_id = ? and conf_key in ('afterWorkMessage','workTimeStatus','workDay','morningWorkTime','afternoonWorkTime','otherWorkTime')", entId) configs := []models.EntConfig{ { ConfName: "是否开启上班时间", ConfKey: "workTimeStatus", ConfValue: c.PostForm("workTimeStatus"), EntId: entId.(string), }, { ConfName: "工作日选择", ConfKey: "workDay", ConfValue: c.PostForm("workDay"), EntId: entId.(string), }, { ConfName: "上午时间段", ConfKey: "morningWorkTime", ConfValue: c.PostForm("morningWorkTime"), EntId: entId.(string), }, { ConfName: "下午时间段", ConfKey: "afternoonWorkTime", ConfValue: c.PostForm("afternoonWorkTime"), EntId: entId.(string), }, { ConfName: "其他时间段", ConfKey: "otherWorkTime", ConfValue: c.PostForm("otherWorkTime"), EntId: entId.(string), }, { ConfName: "下班后消息提示", ConfKey: "afterWorkMessage", ConfValue: c.PostForm("afterWorkMessage"), EntId: entId.(string), }, } models.CreateMany(entId, configs) c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": "ok", }) }