kefu/lib/wechat.go

109 lines
3.2 KiB
Go

package lib
import (
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"kefu/models"
"kefu/tools"
"sort"
)
type Wechat struct {
AppId, AppSecret, Token, WechatVisitorTemplateId,
WechatMessageTemplateId, WechatKefuTemplateId,
WechatKefu, WechatHost, WorkWechatCorpid, WorkWechatSecret,
WorkWechatAppSecret, WorkWechatAppAgentId, WorkWechatAppToken, WorkWechatAppEncodingAESKey,
WechatMiniAppId, WechatMiniAppSecret, WechatMiniToken,
WechatCustomerTemplateId string
}
//AppId,AppSecret,Token
func NewWechatLib(entId string) (*Wechat, error) {
configs := models.FindEntConfigs(entId)
wechat := &Wechat{}
for _, config := range configs {
if config.ConfKey == "WechatAppId" {
wechat.AppId = config.ConfValue
}
if config.ConfKey == "WechatAppSecret" {
wechat.AppSecret = config.ConfValue
}
if config.ConfKey == "WechatAppToken" {
wechat.Token = config.ConfValue
}
if config.ConfKey == "WechatVisitorTemplateId" {
wechat.WechatVisitorTemplateId = config.ConfValue
}
if config.ConfKey == "WechatKefuTemplateId" {
wechat.WechatKefuTemplateId = config.ConfValue
}
if config.ConfKey == "WechatMessageTemplateId" {
wechat.WechatMessageTemplateId = config.ConfValue
}
if config.ConfKey == "WechatHost" {
wechat.WechatHost = config.ConfValue
}
if config.ConfKey == "WechatKefu" {
wechat.WechatKefu = config.ConfValue
}
if config.ConfKey == "WorkWechatCorpid" {
wechat.WorkWechatCorpid = config.ConfValue
}
if config.ConfKey == "WorkWechatSecret" {
wechat.WorkWechatSecret = config.ConfValue
}
if config.ConfKey == "WorkWechatAppSecret" {
wechat.WorkWechatAppSecret = config.ConfValue
}
if config.ConfKey == "WorkWechatAppAgentId" {
wechat.WorkWechatAppAgentId = config.ConfValue
}
if config.ConfKey == "WorkWechatAppToken" {
wechat.WorkWechatAppToken = config.ConfValue
}
if config.ConfKey == "WorkWechatAppEncodingAESKey" {
wechat.WorkWechatAppEncodingAESKey = config.ConfValue
}
if config.ConfKey == "WechatMiniAppId" {
wechat.WechatMiniAppId = config.ConfValue
}
if config.ConfKey == "WechatMiniAppSecret" {
wechat.WechatMiniAppSecret = config.ConfValue
}
if config.ConfKey == "WechatMiniToken" {
wechat.WechatMiniToken = config.ConfValue
}
if config.ConfKey == "WechatCustomerTemplateId" {
wechat.WechatCustomerTemplateId = config.ConfValue
}
}
return wechat, nil
}
func (this *Wechat) GetAccess() (string, error) {
url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", this.AppId, this.AppSecret)
tools.Get(url)
return "", errors.New("获取access_token失败")
}
func (this *Wechat) CheckWechatSign(signature, timestamp, nonce, echostr string) (string, error) {
token := this.Token
//将token、timestamp、nonce三个参数进行字典序排序
var tempArray = []string{token, timestamp, nonce}
sort.Strings(tempArray)
//将三个参数字符串拼接成一个字符串进行sha1加密
var sha1String string = ""
for _, v := range tempArray {
sha1String += v
}
h := sha1.New()
h.Write([]byte(sha1String))
sha1String = hex.EncodeToString(h.Sum([]byte("")))
//获得加密后的字符串可与signature对比
if sha1String == signature {
return echostr, nil
}
return "", errors.New("微信API验证失")
}