customer info

This commit is contained in:
jiangyong27 2024-01-24 18:36:07 +08:00
parent 940a627ade
commit e04e84cb8a
2 changed files with 114 additions and 0 deletions

View File

@ -2,10 +2,13 @@ package qyweixin
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"gitlab.batiao8.com/open/gosdk/util"
"gitlab.batiao8.com/open/gosdk/wechat"
@ -51,6 +54,14 @@ type MessageRequest struct {
} `json:"video"`
}
type JsapiConfig struct {
Corpid string `json:"corpid"`
Agent string `json:"agent"`
Signature string `json:"signature"`
Noncestr string `json:"noncestr"`
Timestamp string `json:"timestamp"`
}
type BaseResponse struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
@ -301,6 +312,26 @@ func (a *App) Upload(path, kind string) (string, error) {
return cast.ToString(result["media_id"]), nil
}
func (q *App) GetJsapiConfig(url string) (*JsapiConfig, error) {
ticket, err := q.getJsapiTicket()
if err != nil {
return nil, err
}
cfg := new(JsapiConfig)
noncestr := goutil.RandomStr(32)
timstamp := cast.ToString(time.Now().Unix())
str := "jsapi_ticket=" + cast.ToString(ticket) + "&noncestr=" + noncestr + "&timestamp=" + timstamp + "&url=" + url
h := sha1.New()
h.Write([]byte(str))
signature := hex.EncodeToString(h.Sum(nil))
cfg.Signature = signature
cfg.Timestamp = timstamp
cfg.Noncestr = noncestr
cfg.Agent = q.config.Agent
cfg.Corpid = q.config.Corpid
return cfg, nil
}
func (q *App) Callback(ctx *gin.Context) {
//配置微信参数
@ -350,6 +381,22 @@ func (q *App) Callback(ctx *gin.Context) {
}
}
func (q *App) getJsapiTicket() (string, error) {
resp, err := http.Get("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=" + q.GetToken() + "&type=agent_config")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
jsonData := make(map[string]interface{})
json.Unmarshal(body, &jsonData)
return cast.ToString(jsonData["ticket"]), nil
}
func (q *App) refreshToken() error {
if time.Now().Unix() <= q.tokenExpire-600 {
return nil

View File

@ -2,6 +2,7 @@ package qyweixin
import (
"encoding/json"
"errors"
"fmt"
"github.com/spf13/cast"
"gitlab.batiao8.com/open/gosdk/util"
@ -76,6 +77,14 @@ type CustomerEvent struct {
NewServicerUserid string `json:"new_servicer_userid"`
}
type CustomerInfo struct {
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Gender int `json:"gender"`
Unionid string `json:"unionid"`
Context interface{} `json:"enter_Session_Context"`
}
type AppCustomer struct {
App
}
@ -228,6 +237,64 @@ func (a *AppCustomer) SyncMessage(openKfId, cursor, token string) ([]*CustomerMe
return message, cursor, nil
}
func (q *AppCustomer) GetServiceLink(openKfId, sence string) (string, error) {
params := make(map[string]string)
params["open_kfid"] = openKfId
params["scene"] = sence
reqBody, _ := json.Marshal(params)
reqUrl := "https://qyapi.weixin.qq.com/cgi-bin/kf/add_contact_way?access_token=" + q.GetToken()
rspBody, err := util.HttpPostJson(reqUrl, nil, reqBody)
if err != nil {
return "", err
}
result, err := q.GetResult(rspBody)
if err != nil {
return "", err
}
customerServcieUrl := cast.ToString(result["url"])
if customerServcieUrl == "" {
return "", fmt.Errorf("%d:%s", cast.ToInt64(result["errcode"]), cast.ToString(result["errmsg"]))
}
return customerServcieUrl, nil
}
func (q *AppCustomer) GetCustomerInfo(externalUserid string) (*CustomerInfo, error) {
postdata := map[string]interface{}{
"external_userid_list": []string{
externalUserid,
},
"need_enter_session_context": 1,
}
dataByte, err := json.Marshal(postdata)
if err != nil {
return nil, err
}
rspBody, err := util.HttpPostJson("https://qyapi.weixin.qq.com/cgi-bin/kf/customer/batchget?access_token="+q.GetToken(), nil, dataByte)
if err != nil {
return nil, err
}
result, err := q.GetResult(rspBody)
if err != nil {
return nil, err
}
users := cast.ToSlice(result["customer_list"])
if len(users) == 0 {
return nil, errors.New("no user info")
}
user := cast.ToStringMap(users[0])
u := new(CustomerInfo)
u.Nickname = cast.ToString(user["nickname"])
u.Avatar = cast.ToString(user["avatar"])
u.Unionid = cast.ToString(user["unionid"])
u.Gender = cast.ToInt(user["gender"])
u.Context = user["enter_session_context"]
return u, nil
}
func (m *CustomerMessage) From(data map[string]interface{}) {
m.SyncTime = time.Now().Unix()
m.SendTime = cast.ToInt64(data["send_time"])