77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package client
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
type WeGetTokenRes struct {
|
||
AccessToken string `json:"access_token"`
|
||
ExpiresIn int `json:"expires_in"`
|
||
}
|
||
|
||
func WeGetToken() (token string, e error) {
|
||
gotRes, err := Get(GetAccessTokenURL, nil, WeChatGetAccessTokenReq(1))
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
var body WeGetTokenRes
|
||
if e = json.Unmarshal(gotRes, &body); e != nil {
|
||
return
|
||
}
|
||
token = body.AccessToken
|
||
return
|
||
}
|
||
|
||
type WeSendMessageReq map[string]WeSendMessageItemReq
|
||
|
||
type WeSendMessageItemReq struct {
|
||
Value string `json:"value"`
|
||
}
|
||
|
||
func WeSendMessage(openID, templateID string, message WeSendMessageReq, token string) (e error) {
|
||
if len(token) == 0 {
|
||
if token, e = WeGetToken(); e != nil {
|
||
return
|
||
}
|
||
}
|
||
gotRes, err := Post(strings.ReplaceAll(SendMessageURL, "ACCESS_TOKEN", token), nil, map[string]interface{}{
|
||
"template_id": templateID,
|
||
"touser": openID,
|
||
"data": message,
|
||
"miniprogram_state": "formal", // developer为开发版;trial为体验版;formal为正式版;默认为正式版
|
||
"lang": "zh_CN", // 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为 zh_CN
|
||
})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
fmt.Printf("%s\n", string(gotRes))
|
||
return
|
||
}
|
||
|
||
func WeSendNoticeTakeOrder(openID, company, driver, vehicel string, date time.Time) error {
|
||
const tid = "Ff5tDsIlBy-t51Usxh_oBvge9WbMbBFVKI020hIpWcA"
|
||
return WeSendMessage(openID, tid, messageNoticeTakeOrder(company, driver, vehicel, date), "")
|
||
}
|
||
|
||
// messageBookReminder 预约提醒消息
|
||
func messageNoticeTakeOrder(company, driver, vehicel string, date time.Time) WeSendMessageReq {
|
||
return WeSendMessageReq{
|
||
"thing1": WeSendMessageItemReq{Value: company}, // 公司名称
|
||
"thing2": WeSendMessageItemReq{Value: driver}, // 接单司机
|
||
"car_number3": WeSendMessageItemReq{Value: vehicel}, // 车牌号
|
||
"time4": WeSendMessageItemReq{Value: StrFromTime(date)}, // 接单时间
|
||
}
|
||
}
|
||
|
||
const (
|
||
TimeFormat = "2006-01-02 15:04:05"
|
||
DateFormat = "2006-01-02"
|
||
)
|
||
|
||
func StrFromTime(t time.Time) string {
|
||
return t.Format(TimeFormat)
|
||
}
|