kefu/controller/main.go

107 lines
2.9 KiB
Go

package controller
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
"kefu/common"
"kefu/models"
"kefu/tools"
"kefu/ws"
"time"
)
func MainCheckAuth(c *gin.Context) {
id, _ := c.Get("kefu_id")
userinfo := models.FindUserRole("user.nickname,user.avator,user.name,user.id, role.name role_name", id)
c.JSON(200, gin.H{
"code": 200,
"msg": "验证成功",
"result": gin.H{
"avator": userinfo.Avator,
"name": userinfo.Name,
"role_name": userinfo.RoleName,
"nick_name": userinfo.Nickname,
},
})
}
func GetStatistics(c *gin.Context) {
kefuName, _ := c.Get("kefu_name")
//kefuId, _ := c.Get("kefu_id")
entId, _ := c.Get("ent_id")
//今日访客数
todayStart := time.Now().Format("2006-01-02")
todayEnd := fmt.Sprintf("%s 23:59:59", todayStart)
toadyVisitors := models.CountVisitors("to_id= ? and updated_at>= ? and updated_at<= ?", kefuName.(string), todayStart, todayEnd)
visitors := models.CountVisitorsByKefuId(kefuName.(string))
message := models.CountMessage("kefu_id=?", kefuName)
todayMessages := models.CountMessage("kefu_id= ? and created_at>= ? and created_at<= ?", kefuName.(string), todayStart, todayEnd)
visitorSession := 0
for _, c := range ws.ClientList {
if c.EntId == fmt.Sprintf("%v", entId) {
visitorSession++
}
}
kefuSession := 0
for _, kefus := range ws.KefuList {
for _, c := range kefus.Users {
if c.Ent_id == fmt.Sprintf("%v", entId) {
kefuSession++
}
}
}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"visitors": visitors,
"toady_visitors": toadyVisitors,
"today_messages": todayMessages,
"message": message,
"visitor_session": visitorSession,
"kefu_session": kefuSession,
},
})
}
func GetVersion(c *gin.Context) {
versionName := "商务版"
ipAuth := tools.Get(IP_SERVER_URL)
ipAddress := gjson.Get(ipAuth, "result.ip_address").String()
endTimeStr := gjson.Get(ipAuth, "result.expire_time").String()
content := gjson.Get(ipAuth, "result.content").String()
//if common.IsTry {
// versionName = "试用版"
// installTimeByte, _ := ioutil.ReadFile("./install.lock")
// installTimeByte, _ = tools.AesDecrypt(installTimeByte, []byte(common.AesKey))
// installTime := tools.ByteToInt64(installTimeByte)
// endTime := installTime + common.TryDeadline
// endTimeStr = time.Unix(endTime, 0).Format("2006-01-02")
//}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"ip_address": ipAddress,
"last_time": endTimeStr,
"version": versionName,
"content": content,
"version_code": common.Version,
},
})
}
func GetOtherVersion(c *gin.Context) {
versionCode := models.FindConfig("SystemVersion")
versionName := models.FindConfig("SystemVersionName")
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"version_name": versionName,
"version_code": versionCode,
},
})
}