340 lines
10 KiB
Go
340 lines
10 KiB
Go
package tmpl
|
||
|
||
import (
|
||
"fmt"
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/silenceper/wechat/v2"
|
||
"github.com/silenceper/wechat/v2/cache"
|
||
offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
|
||
"github.com/silenceper/wechat/v2/officialaccount/oauth"
|
||
"github.com/tidwall/gjson"
|
||
"kefu/common"
|
||
"kefu/lib"
|
||
"kefu/models"
|
||
"kefu/tools"
|
||
"log"
|
||
"net/http"
|
||
"time"
|
||
)
|
||
|
||
var memory = cache.NewMemory()
|
||
|
||
// 聊天室界面
|
||
func PageChatRoom(c *gin.Context) {
|
||
c.HTML(http.StatusOK, "chat_room.html", gin.H{})
|
||
}
|
||
|
||
// 咨询界面
|
||
func PageChat(c *gin.Context) {
|
||
kefuId := c.Query("kefu_id")
|
||
lang, _ := c.Get("lang")
|
||
refer := c.Query("refer")
|
||
referUrl := c.Query("url")
|
||
entId := c.Query("ent_id")
|
||
if refer == "" {
|
||
refer = c.Request.Referer()
|
||
}
|
||
if refer == "" {
|
||
refer = ""
|
||
}
|
||
|
||
visitorId := c.Query("visitor_id")
|
||
visitorName := c.Query("visitor_name")
|
||
avator := c.Query("avator")
|
||
extra := c.Query("extra")
|
||
errMsg := ""
|
||
|
||
SystemNotice := models.FindConfig("SystemNotice")
|
||
SystemTitle := models.FindConfig("SystemTitle")
|
||
SystemKeywords := models.FindConfig("SystemKeywords")
|
||
SystemDesc := models.FindConfig("SystemDesc")
|
||
CopyrightTxt := models.FindConfig("CopyrightTxt")
|
||
CopyrightUrl := models.FindConfig("CopyrightUrl")
|
||
ShowKefuName := models.FindConfig("ShowKefuName")
|
||
//界面相关判断
|
||
VisitorVoiceBtn := models.FindConfig("VisitorVoiceBtn")
|
||
VisitorMapBtn := models.FindConfig("VisitorMapBtn")
|
||
VisitorFaceBtn := models.FindConfig("VisitorFaceBtn")
|
||
VisitorCommentBtn := models.FindConfig("VisitorCommentBtn")
|
||
VisitorReadStatus := models.FindConfig("VisitorReadStatus")
|
||
VisitorPlusBtn := models.FindConfig("VisitorPlusBtn")
|
||
VisitorUploadImgBtn := models.FindConfig("VisitorUploadImgBtn")
|
||
VisitorUploadFileBtn := models.FindConfig("VisitorUploadFileBtn")
|
||
VisitorMaxLength := models.FindConfig("VisitorMaxLength")
|
||
VisitorShowAvator := models.FindConfig("VisitorShowAvator")
|
||
VisitorWechatQrcodeUrl := models.FindConfig("VisitorWechatQrcodeUrl")
|
||
|
||
//获取微信用户消息
|
||
weixinCode := c.Query("code")
|
||
if weixinCode != "" && entId != "" {
|
||
userInfo, err := GetWechatUserInfo(weixinCode, entId)
|
||
if err != nil {
|
||
errMsg = err.Error()
|
||
log.Println("公众号网页授权错误:" + errMsg + ",ent_id:" + entId)
|
||
} else {
|
||
visitorId = "wx|" + entId + "|" + userInfo.OpenID
|
||
avator = userInfo.HeadImgURL
|
||
visitorName = userInfo.Nickname
|
||
ShowKefuName = ""
|
||
}
|
||
}
|
||
//商户设置的落地域名
|
||
entConfig := models.FindEntConfig(entId, "VisitorLandHost")
|
||
if entConfig.ConfValue != "" {
|
||
if entConfig.ConfValue != c.Request.Host {
|
||
c.Redirect(302, fmt.Sprintf("//%s/chatIndex?kefu_id=%s&ent_id=%s&lang=%s&visitor_id=%s&visitor_name=%s&avator=%s", entConfig.ConfValue,
|
||
kefuId, entId, lang, visitorId, visitorName, avator))
|
||
return
|
||
}
|
||
} else {
|
||
//落地域名跳转
|
||
landHost := models.FindConfig("LandHost")
|
||
if landHost != "" && landHost != c.Request.Host {
|
||
c.Redirect(302, fmt.Sprintf("//%s/chatIndex?kefu_id=%s&ent_id=%s&lang=%s&visitor_id=%s&visitor_name=%s&avator=%s", landHost,
|
||
kefuId, entId, lang, visitorId, visitorName, avator))
|
||
return
|
||
}
|
||
}
|
||
|
||
entInfo := models.FindUserById(entId)
|
||
title := "智能在线客服系统"
|
||
if entInfo.Nickname != "" {
|
||
title = entInfo.Nickname
|
||
}
|
||
c.HTML(http.StatusOK, "chat_page.html", gin.H{
|
||
"KEFU_ID": kefuId,
|
||
"Lang": lang.(string),
|
||
"Refer": refer,
|
||
"ReferUrl": referUrl,
|
||
"Extra": extra,
|
||
"ENT_ID": entId,
|
||
"visitorId": visitorId,
|
||
"visitorName": visitorName,
|
||
"avator": avator,
|
||
"errMsg": errMsg,
|
||
"IS_TRY": common.IsTry,
|
||
"SystemTitle": SystemTitle,
|
||
"SystemKeywords": SystemKeywords,
|
||
"SystemDesc": SystemDesc,
|
||
"CopyrightTxt": CopyrightTxt,
|
||
"CopyrightUrl": CopyrightUrl,
|
||
"SystemNotice": SystemNotice,
|
||
"Title": title,
|
||
"ShowKefuName": ShowKefuName,
|
||
"VisitorVoiceBtn": VisitorVoiceBtn,
|
||
"VisitorMapBtn": VisitorMapBtn,
|
||
"VisitorCommentBtn": VisitorCommentBtn,
|
||
"VisitorFaceBtn": VisitorFaceBtn,
|
||
"VisitorReadStatus": VisitorReadStatus,
|
||
"VisitorPlusBtn": VisitorPlusBtn,
|
||
"VisitorUploadImgBtn": VisitorUploadImgBtn,
|
||
"VisitorUploadFileBtn": VisitorUploadFileBtn,
|
||
"VisitorMaxLength": VisitorMaxLength,
|
||
"VisitorShowAvator": VisitorShowAvator,
|
||
"VisitorWechatQrcodeUrl": VisitorWechatQrcodeUrl,
|
||
})
|
||
}
|
||
|
||
// 微信跳转界面
|
||
func PageWechatChat(c *gin.Context) {
|
||
kefuId := c.Query("kefu_id")
|
||
lang, _ := c.Get("lang")
|
||
refer := c.Query("refer")
|
||
entId := c.Query("ent_id")
|
||
if refer == "" {
|
||
refer = c.Request.Referer()
|
||
}
|
||
if refer == "" {
|
||
refer = ""
|
||
}
|
||
appId := ""
|
||
host := ""
|
||
wechatConfig, _ := lib.NewWechatLib(entId)
|
||
if wechatConfig != nil {
|
||
appId = wechatConfig.AppId
|
||
host = wechatConfig.WechatHost
|
||
}
|
||
if appId == "" {
|
||
wechatConfig, _ := lib.NewWechatLib(models.FindConfig("SystemBussinesId"))
|
||
appId = wechatConfig.AppId
|
||
host = wechatConfig.WechatHost
|
||
}
|
||
entInfo := models.FindUserById(entId)
|
||
c.HTML(http.StatusOK, "chat_wechat_page.html", gin.H{
|
||
"Title": entInfo.Nickname,
|
||
"kefuId": kefuId,
|
||
"Lang": lang.(string),
|
||
"Refer": refer,
|
||
"EntId": entId,
|
||
"AppId": appId,
|
||
"Host": host,
|
||
})
|
||
}
|
||
|
||
// 企业微信跳转界面
|
||
func PageEntWechatChat(c *gin.Context) {
|
||
kefuId := c.Query("kefu_id")
|
||
entId := c.Query("ent_id")
|
||
configs := models.GetEntConfigsMap(entId, "WorkWechatCorpid", "WorkWechatAppAgentId")
|
||
c.HTML(http.StatusOK, "ent_wechat_page.html", gin.H{
|
||
"WorkWechatCorpid": configs["WorkWechatCorpid"],
|
||
"WorkWechatAppAgentId": configs["WorkWechatAppAgentId"],
|
||
"EntId": entId,
|
||
"KefuName": kefuId,
|
||
"kefuId": kefuId,
|
||
})
|
||
}
|
||
|
||
// 企业微信oauth获取信息
|
||
func PageEntWechatAuth(c *gin.Context) {
|
||
|
||
entId := c.Query("ent_id")
|
||
kefuName := c.Query("kefu_name")
|
||
code := c.Query("code")
|
||
|
||
configs := models.GetEntConfigsMap(entId, "WorkWechatCorpid", "WorkWechatAppSecret")
|
||
accessToken := ""
|
||
accessTokenCache, ok := pageCache.Get("ent_wechat_access_token_" + entId)
|
||
if !ok || accessTokenCache == nil {
|
||
result := tools.Get(fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", configs["WorkWechatCorpid"], configs["WorkWechatAppSecret"]))
|
||
accessToken = gjson.Get(result, "access_token").String()
|
||
if accessToken != "" {
|
||
pageCache.Set("ent_wechat_access_token_"+entId, accessToken, 0)
|
||
}
|
||
} else {
|
||
accessToken = accessTokenCache.(string)
|
||
}
|
||
|
||
//获取成员信息
|
||
result := tools.Get(fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s", accessToken, code))
|
||
userid := gjson.Get(result, "userid").String()
|
||
name := userid
|
||
avatar := ""
|
||
if userid != "" {
|
||
result = tools.Get(fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=%s&userid=%s", accessToken, userid))
|
||
name = gjson.Get(result, "name").String()
|
||
avatar = gjson.Get(result, "avatar").String()
|
||
}
|
||
c.Redirect(301, fmt.Sprintf("/chatIndex?ent_id=%s&kefu_id=%s&visitor_id=%s&visitor_name=%s&avator=%s", entId, kefuName, userid, name, avatar))
|
||
c.JSON(200, gin.H{
|
||
"code": code,
|
||
"accessToken": accessToken,
|
||
"userid": userid,
|
||
})
|
||
}
|
||
|
||
// 获取微信用户信息
|
||
func GetWechatUserInfo(weixinCode, entId string) (oauth.UserInfo, error) {
|
||
var userinfo oauth.UserInfo
|
||
wechatConfig, err := lib.NewWechatLib(entId)
|
||
if wechatConfig.AppId == "" {
|
||
wechatConfig, err = lib.NewWechatLib(models.FindConfig("SystemBussinesId"))
|
||
if wechatConfig.AppId == "" {
|
||
return userinfo, err
|
||
}
|
||
}
|
||
|
||
cfg := &offConfig.Config{
|
||
AppID: wechatConfig.AppId,
|
||
AppSecret: wechatConfig.AppSecret,
|
||
Token: wechatConfig.Token,
|
||
//EncodingAESKey: "xxxx",
|
||
Cache: memory,
|
||
}
|
||
wc := wechat.NewWechat()
|
||
officialAccount := wc.GetOfficialAccount(cfg)
|
||
oauth := officialAccount.GetOauth()
|
||
accessToken, err := oauth.GetUserAccessToken(weixinCode)
|
||
if err != nil {
|
||
return userinfo, err
|
||
}
|
||
userinfo, err = oauth.GetUserInfo(accessToken.AccessToken, accessToken.OpenID, "")
|
||
if err != nil {
|
||
return userinfo, err
|
||
}
|
||
return userinfo, nil
|
||
}
|
||
|
||
// 客服微信授权中专页
|
||
func PageWechatKefuTransfer(c *gin.Context) {
|
||
entId := c.Query("ent_id")
|
||
KefuName := c.Query("kefu_name")
|
||
|
||
configs := models.GetEntConfigsMap(entId, "WechatAppId",
|
||
"WechatHost",
|
||
)
|
||
if configs["WechatAppId"] == "" || configs["WechatHost"] == "" {
|
||
configs = models.GetEntConfigsMap(models.FindConfig("SystemBussinesId"), "WechatAppId",
|
||
"WechatHost",
|
||
)
|
||
}
|
||
entInfo := models.FindUserById(entId)
|
||
c.HTML(http.StatusOK, "wechat_kefu_transfer.html", gin.H{
|
||
"Title": entInfo.Nickname,
|
||
"EntId": entId,
|
||
"KefuName": KefuName,
|
||
"AppId": configs["WechatAppId"],
|
||
"Host": configs["WechatHost"],
|
||
})
|
||
}
|
||
|
||
// 客服微信授权
|
||
func GetWechatKefu(c *gin.Context) {
|
||
entId := c.Query("ent_id")
|
||
kefuName := c.Query("kefu_name")
|
||
//获取微信用户消息
|
||
weixinCode := c.Query("code")
|
||
errMsg := ""
|
||
openId := ""
|
||
|
||
if weixinCode != "" && entId != "" {
|
||
userInfo, err := GetWechatUserInfo(weixinCode, entId)
|
||
if err != nil {
|
||
systemId := models.FindConfig("SystemBussinesId")
|
||
if systemId != "" {
|
||
entId = systemId
|
||
}
|
||
userInfo, err = GetWechatUserInfo(weixinCode, entId)
|
||
if err != nil {
|
||
errMsg = err.Error()
|
||
log.Println("公众号网页授权错误:" + errMsg + ",ent_id:" + entId)
|
||
c.JSON(200, gin.H{
|
||
"code": 400,
|
||
"msg": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
}
|
||
openId = userInfo.OpenID
|
||
}
|
||
oauth := models.FindOauth(kefuName, openId)
|
||
user := models.FindUser(oauth.UserId)
|
||
if user.Name == "" {
|
||
//c.JSON(200, gin.H{
|
||
// "code": 400,
|
||
// "msg": "获取客服账号错误!微信ID: " + openId,
|
||
//})
|
||
c.Redirect(302, "/ironMan/signInx")
|
||
return
|
||
}
|
||
token := GenUserToken(user)
|
||
c.Redirect(301, "/sso?token="+token+"&redirect=/h5")
|
||
}
|
||
func GenUserToken(info models.User) string {
|
||
userinfo := make(map[string]interface{})
|
||
userinfo["name"] = info.Name
|
||
userinfo["kefu_id"] = info.ID
|
||
userinfo["type"] = "kefu"
|
||
uRole := models.FindRoleByUserId(info.ID)
|
||
if uRole.RoleId != 0 {
|
||
userinfo["role_id"] = uRole.RoleId
|
||
} else {
|
||
userinfo["role_id"] = 2
|
||
}
|
||
userinfo["create_time"] = time.Now().Unix()
|
||
userinfo["pid"] = info.Pid
|
||
|
||
token, _ := tools.MakeToken(userinfo)
|
||
return token
|
||
}
|