micro-account/pkg/common/wechat/tempate/template.go
2025-02-20 16:18:23 +08:00

210 lines
5.5 KiB
Go

package tempate
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/fonchain_enterprise/micro-account/pkg/cache"
"github.com/fonchain_enterprise/micro-account/pkg/common/redis_key"
"github.com/fonchain_enterprise/micro-account/pkg/common/wechat"
"github.com/google/uuid"
"io/ioutil"
"net/http"
"strconv"
)
type ClockInTemplate struct {
ToUser string `json:"touser"`
TemplateID string `json:"template_id"`
ClientMsgID string `json:"client_msg_id"`
ClockInMsgData ClockInMsgData `json:"data"`
}
type StrangerClockInTemplate struct {
ToUser string `json:"touser"`
TemplateID string `json:"template_id"`
ClientMsgID string `json:"client_msg_id"`
Url string `json:"url"`
ClockInMsgData StrangerClockInMsgData `json:"data"`
}
type Thing struct {
Value string `json:"value"`
}
type ClockInMsgData struct {
Time3 Thing `json:"time3"`
Name Thing `json:"thing1"`
Const2 Thing `json:"const2"`
Thing5 Thing `json:"thing5"`
}
type StrangerClockInMsgData struct {
Thing2 Thing `json:"thing2"`
PhoneNumber10 Thing `json:"phone_number10"`
CharacterString14 Thing `json:"character_string14"`
Thing6 Thing `json:"thing6"`
Const11 Thing `json:"const11"`
}
type ClockInTemplateMsgInfo struct {
TemplateId string
OpenId string
OperatedAt string
Name string
Address string
ClockType string
VisitorSName string
VisitorSTel string
ReasonVisit string
NumberOfPeople uint64
}
func SendStrangerWechatTempMsg(wechatObj wechat.Wechat, info ClockInTemplateMsgInfo, logID uint64) error {
appID := wechatObj.Appid
appSecret := wechatObj.AppSecret
accessToken, err := wechat.GetAccessToken(appID, appSecret)
URL := fmt.Sprintf("https://material.szjixun.cn/#/pages/control-gate/index?sn=%v&id=%v", info.Address, logID)
clockInTemplate := StrangerClockInTemplate{
ToUser: info.OpenId,
TemplateID: info.TemplateId,
ClientMsgID: uuid.New().String(),
Url: URL,
ClockInMsgData: StrangerClockInMsgData{
Thing2: Thing{info.VisitorSName}, //来访人员
PhoneNumber10: Thing{info.VisitorSTel}, //联系电话
CharacterString14: Thing{strconv.FormatUint(info.NumberOfPeople, 10)}, //随行人数
Thing6: Thing{info.Name}, //拜访人
Const11: Thing{info.ReasonVisit}, //拜访事由
},
}
msg, err := json.Marshal(clockInTemplate)
openInfo, err := sendTemplateMsg(accessToken.AccessToken, msg)
fmt.Println("提示设呢么", openInfo, err)
if err != nil {
return err
}
fmt.Println("123123--123", openInfo)
fmt.Println(openInfo.Errmsg)
fmt.Println(openInfo.Errcode)
if openInfo.Errcode != 40001 {
return nil
}
fmt.Println("不顶用")
cache.RedisClient.Del(redis_key.GetWxAccessToken(appID))
return sendTemplateMsgAgain(appID, appSecret, msg)
}
func SendWechatTemplateMsg(wechatObj wechat.Wechat, info ClockInTemplateMsgInfo) error {
clockType := "上班"
appID := wechatObj.Appid
appSecret := wechatObj.AppSecret
templateId, openId, operatedAt, Name, address, _ := info.TemplateId, info.OpenId, info.OperatedAt, info.Name, info.Address, info.ClockType
accessToken, err := wechat.GetAccessToken(appID, appSecret)
if err != nil {
return err
}
if info.ClockType == "off" {
clockType = "下班"
}
clockInTemplate := ClockInTemplate{
ToUser: openId,
TemplateID: templateId,
ClientMsgID: uuid.New().String(),
ClockInMsgData: ClockInMsgData{
Time3: Thing{Value: operatedAt},
Name: Thing{Value: Name},
Const2: Thing{Value: clockType},
Thing5: Thing{Value: address},
},
}
msg, err := json.Marshal(clockInTemplate)
openInfo, err := sendTemplateMsg(accessToken.AccessToken, msg)
fmt.Println("提示设呢么", openInfo, err)
if err != nil {
return err
}
fmt.Println("123123--123", openInfo)
fmt.Println(openInfo.Errmsg)
fmt.Println(openInfo.Errcode)
if openInfo.Errcode != 40001 {
return nil
}
fmt.Println("不顶用")
cache.RedisClient.Del(redis_key.GetWxAccessToken(appID))
return sendTemplateMsgAgain(appID, appSecret, msg)
}
func sendTemplateMsgAgain(appID, appSecret string, msg []byte) error {
accessToken, err := wechat.GetAccessToken(appID, appSecret)
if err != nil {
return err
}
openInfo, err := sendTemplateMsg(accessToken.AccessToken, msg)
fmt.Println("提示设呢么", openInfo, err)
if err != nil {
return err
}
fmt.Println("123123--123", openInfo)
fmt.Println(openInfo.Errmsg)
fmt.Println(openInfo.Errcode)
if openInfo.Errcode != 40001 {
return nil
}
return errors.New(openInfo.Errmsg)
}
// "errcode": 0,
// "errmsg": "ok",
func sendTemplateMsg(accessToken string, code []byte) (res *wechat.CommonRes, err error) {
url := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
// 创建要发送的数据
// 发送POST请求
resp, err := http.Post(url, "application/json", bytes.NewBuffer(code))
if err != nil {
fmt.Println("发送请求失败:", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
fmt.Printf("请求失败%d\n", resp.StatusCode)
return nil, errors.New(fmt.Sprintf("请求失败%d", resp.StatusCode))
}
fmt.Println("2--返回数据是", string(body))
err = json.Unmarshal(body, &res)
if err != nil {
fmt.Println(err)
return nil, err
}
return res, err
}