kefu/controller/notice.go

200 lines
5.7 KiB
Go

package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"kefu/models"
"kefu/tools"
"kefu/types"
"strconv"
"time"
)
type KefuNoticeForm struct {
EntId string `form:"ent_id" json:"ent_id" uri:"ent_id" xml:"ent_id" binding:"required"`
KefuName string `form:"kefu_name" json:"kefu_name" uri:"kefu_name" xml:"kefu_name"`
VisitorId string `form:"visitor_id" json:"visitor_id" uri:"visitor_id" xml:"visitor_id"`
IsRecord string `form:"is_record" json:"is_record" uri:"is_record" xml:"is_record"`
}
func GetNotice(c *gin.Context) {
var form KefuNoticeForm
err := c.Bind(&form)
if err != nil {
c.JSON(200, gin.H{
"code": types.ApiCode.FAILED,
"msg": types.ApiCode.GetMessage(types.ApiCode.INVALID),
"result": err.Error(),
})
return
}
if form.VisitorId != "" {
vistorInfo := models.FindVisitorByVistorId(form.VisitorId)
if vistorInfo.ID == 0 {
c.JSON(200, gin.H{
"code": types.ApiCode.ACCOUNT_NO_EXIST,
"msg": types.ApiCode.GetMessage(types.ApiCode.ACCOUNT_NO_EXIST),
})
return
}
}
entId := form.EntId
kefuName := form.KefuName
var welcomes []models.Welcome
var kefu models.User
if kefuName != "" {
kefu = models.FindUser(kefuName)
} else {
kefu = models.FindUserByUid(entId)
}
if kefu.ID == 0 {
c.JSON(200, gin.H{
"code": types.ApiCode.ACCOUNT_NO_EXIST,
"msg": types.ApiCode.GetMessage(types.ApiCode.ACCOUNT_NO_EXIST),
})
return
}
//查询企业配置项
configs := models.GetEntConfigsMap(form.EntId, "KefuIntroduce",
"RobotName", "RobotAvator", "RobotSwitch", "TurnToMan", "RobotNoAnswer",
"VisitorNotice", "AutoWelcome", "VisitorCookie", "VisitorMaxNum", "VisitorMaxNumNotice", "ScanWechatQrcode", "AutoOpenDialogTime",
"KefuOfflineNotice")
allOffline := false
if kefu.OnlineStatus != 1 {
allOffline = true
}
result := make([]gin.H, 0)
//如果离线并且离线提示不为空
if allOffline && configs["KefuOfflineNotice"] != "" {
h := gin.H{
"name": tools.Ifelse(configs["RobotName"] != "", configs["RobotName"], kefu.Nickname),
"avator": tools.Ifelse(configs["RobotAvator"] != "", configs["RobotAvator"], kefu.Avator),
"is_kefu": false,
"content": configs["KefuOfflineNotice"],
"delay_second": 1,
"time": time.Now().Format("2006-01-02 15:04:05"),
}
result = append(result, h)
} else {
welcomes = models.FindWelcomesByKeyword(kefu.Name, "welcome")
for _, welcome := range welcomes {
h := gin.H{
"name": tools.Ifelse(configs["RobotName"] != "", configs["RobotName"], kefu.Nickname),
"avator": tools.Ifelse(configs["RobotAvator"] != "", configs["RobotAvator"], kefu.Avator),
"is_kefu": false,
"content": welcome.Content,
"delay_second": welcome.DelaySecond,
"time": time.Now().Format("2006-01-02 15:04:05"),
}
result = append(result, h)
if form.IsRecord != "" {
models.CreateMessage(kefu.Name, form.VisitorId, welcome.Content, "kefu", entId, "unread")
}
}
}
//configs := GetEntConfigsMap(entId, "AutoOpenDialogTime", "KefuIntroduce")
//delay_second := configs["AutoOpenDialogTime"]
//ent_introduce := configs["KefuIntroduce"]
//访客Cookie有效期
if configs["VisitorCookie"] == "" {
configs["VisitorCookie"] = fmt.Sprintf("%d", 24*3600*365*100)
}
//同时接待人数
if configs["RobotName"] == "" {
configs["RobotName"] = kefu.Nickname
}
entConfig := gin.H{
"robotNoAnswer": configs["RobotNoAnswer"],
"turnToMan": configs["TurnToMan"],
"visitorMaxNumNotice": configs["VisitorMaxNumNotice"],
"entIntroduce": configs["KefuIntroduce"],
"robotSwitch": configs["RobotSwitch"],
"visitorNotice": configs["VisitorNotice"],
"autoWelcome": configs["AutoWelcome"],
"visitorCookie": configs["VisitorCookie"],
"scanWechatQrcode": configs["ScanWechatQrcode"],
"robotName": configs["RobotName"],
"autoOpenDialogTime": configs["AutoOpenDialogTime"],
"kefuOfflineNotice": configs["KefuOfflineNotice"],
}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"welcome": result,
"username": kefu.Nickname,
"avatar": kefu.Avator,
"all_offline": allOffline,
"ent_config": entConfig,
},
})
}
func GetOpenConfigs(c *gin.Context) {
entId := c.Query("ent_id")
configs := models.FindEntConfigs(entId)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": configs,
})
}
func GetNotices(c *gin.Context) {
kefuId, _ := c.Get("kefu_name")
welcomes := models.FindWelcomesByUserId(kefuId)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": welcomes,
})
}
//添加welcome表
func PostNotice(c *gin.Context) {
kefuId, _ := c.Get("kefu_name")
content := c.PostForm("content")
keyword := c.PostForm("keyword")
delaySecond, _ := strconv.Atoi(c.PostForm("delay_second"))
id := c.PostForm("id")
if id != "" {
id := c.PostForm("id")
models.UpdateWelcome(fmt.Sprintf("%s", kefuId), id, content, uint(delaySecond))
} else {
models.CreateWelcome(fmt.Sprintf("%s", kefuId), content, keyword, uint(delaySecond))
}
c.JSON(200, gin.H{
"code": types.ApiCode.SUCCESS,
"msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS),
})
}
func PostNoticeSave(c *gin.Context) {
kefuId, _ := c.Get("kefu_name")
content := c.PostForm("content")
delaySecond, _ := strconv.Atoi(c.PostForm("delay_second"))
id := c.PostForm("id")
models.UpdateWelcome(fmt.Sprintf("%s", kefuId), id, content, uint(delaySecond))
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": "",
})
}
func DelNotice(c *gin.Context) {
kefuId, _ := c.Get("kefu_name")
id := c.Query("id")
models.DeleteWelcome(kefuId, id)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": "",
})
}