1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

Merge branch 'v2' of github.com:silenceper/wechat into feature/remove-redis

This commit is contained in:
houseme
2023-09-26 13:13:51 +08:00
21 changed files with 1503 additions and 71 deletions

View File

@@ -1,29 +0,0 @@
name: goreleaser
on:
push:
tags:
- '*'
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.16
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -55,7 +55,7 @@ issues:
linters-settings:
funlen:
lines: 66
statements: 40
statements: 50
#issues:
# include:

View File

@@ -1,29 +0,0 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod download
# you may remove this if you don't need go generate
- go generate ./...
builds:
- skip: true
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

View File

@@ -66,8 +66,9 @@ func (ak *DefaultAccessToken) GetAccessToken() (accessToken string, err error) {
func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (accessToken string, err error) {
// 先从cache中取
accessTokenCacheKey := fmt.Sprintf("%s_access_token_%s", ak.cacheKeyPrefix, ak.appID)
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
return val.(string), nil
val := ak.cache.Get(accessTokenCacheKey)
if accessToken = val.(string); accessToken != "" {
return
}
// 加上lock是为了防止在并发获取token时cache刚好失效导致从微信服务器上获取到不同token
@@ -75,8 +76,9 @@ func (ak *DefaultAccessToken) GetAccessTokenContext(ctx context.Context) (access
defer ak.accessTokenLock.Unlock()
// 双检,防止重复从微信服务器获取
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
return val.(string), nil
val = ak.cache.Get(accessTokenCacheKey)
if accessToken = val.(string); accessToken != "" {
return
}
// cache失效从微信服务器获取

View File

@@ -7,9 +7,11 @@ import (
// Config .config for 小程序
type Config struct {
AppID string `json:"app_id"` // appid
AppSecret string `json:"app_secret"` // appSecret
AppKey string `json:"app_key"` // appKey
OfferID string `json:"offer_id"` // offerId
Cache cache.Cache
AppID string `json:"app_id"` // appid
AppSecret string `json:"app_secret"` // appSecret
AppKey string `json:"app_key"` // appKey
OfferID string `json:"offer_id"` // offerId
Token string `json:"token"` // token
EncodingAESKey string `json:"encoding_aes_key"` // EncodingAESKey
Cache cache.Cache
}

View File

@@ -20,6 +20,12 @@ const (
MsgTypeLink = "link"
// MsgTypeMiniProgramPage 小程序卡片
MsgTypeMiniProgramPage = "miniprogrampage"
// MsgTypeEvent 事件
MsgTypeEvent MsgType = "event"
// DataTypeXML XML格式数据
DataTypeXML = "xml"
// DataTypeJSON JSON格式数据
DataTypeJSON = "json"
)
// CommonToken 消息中通用的结构

View File

@@ -0,0 +1,375 @@
package message
import (
"encoding/json"
"encoding/xml"
"errors"
"io"
"net/http"
"sort"
"strings"
"github.com/silenceper/wechat/v2/miniprogram/context"
"github.com/silenceper/wechat/v2/miniprogram/security"
"github.com/silenceper/wechat/v2/util"
)
// ConfirmReceiveMethod 确认收货方式
type ConfirmReceiveMethod int8
const (
// EventTypeTradeManageRemindAccessAPI 提醒接入发货信息管理服务API
// 小程序完成账期授权时/小程序产生第一笔交易时/已产生交易但从未发货的小程序,每天一次
EventTypeTradeManageRemindAccessAPI EventType = "trade_manage_remind_access_api"
// EventTypeTradeManageRemindShipping 提醒需要上传发货信息
// 曾经发过货的小程序订单超过48小时未发货时
EventTypeTradeManageRemindShipping EventType = "trade_manage_remind_shipping"
// EventTypeTradeManageOrderSettlement 订单将要结算或已经结算
// 订单完成发货时/订单结算时
EventTypeTradeManageOrderSettlement EventType = "trade_manage_order_settlement"
// EventTypeAddExpressPath 运单轨迹更新事件
EventTypeAddExpressPath EventType = "add_express_path"
// EventTypeSecvodUpload 短剧媒资上传完成事件
EventTypeSecvodUpload EventType = "secvod_upload_event"
// EventTypeSecvodAudit 短剧媒资审核状态事件
EventTypeSecvodAudit EventType = "secvod_audit_event"
// EventTypeWxaMediaCheck 媒体内容安全异步审查结果通知
EventTypeWxaMediaCheck EventType = "wxa_media_check"
// EventTypeXpayGoodsDeliverNotify 道具发货推送事件
EventTypeXpayGoodsDeliverNotify EventType = "xpay_goods_deliver_notify"
// EventTypeXpayCoinPayNotify 代币支付推送事件
EventTypeXpayCoinPayNotify EventType = "xpay_coin_pay_notify"
// ConfirmReceiveMethodAuto 自动确认收货
ConfirmReceiveMethodAuto ConfirmReceiveMethod = 1
// ConfirmReceiveMethodManual 手动确认收货
ConfirmReceiveMethodManual ConfirmReceiveMethod = 2
)
// PushReceiver 接收消息推送
// 暂仅支付Aes加密方式
type PushReceiver struct {
*context.Context
}
// NewPushReceiver 实例化
func NewPushReceiver(ctx *context.Context) *PushReceiver {
return &PushReceiver{
Context: ctx,
}
}
// GetMsg 获取接收到的消息(如果是加密的返回解密数据)
func (receiver *PushReceiver) GetMsg(r *http.Request) (string, []byte, error) {
// 判断请求格式
var dataType string
contentType := r.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "text/xml") {
// xml格式
dataType = DataTypeXML
} else {
// json格式
dataType = DataTypeJSON
}
// 读取参数,验证签名
signature := r.FormValue("signature")
timestamp := r.FormValue("timestamp")
nonce := r.FormValue("nonce")
encryptType := r.FormValue("encrypt_type")
// 验证签名
tmpArr := []string{
receiver.Token,
timestamp,
nonce,
}
sort.Strings(tmpArr)
tmpSignature := util.Signature(tmpArr...)
if tmpSignature != signature {
return dataType, nil, errors.New("signature error")
}
if encryptType == "aes" {
// 解密
var reqData DataReceived
if dataType == DataTypeXML {
if err := xml.NewDecoder(r.Body).Decode(&reqData); err != nil {
return dataType, nil, err
}
} else {
if err := json.NewDecoder(r.Body).Decode(&reqData); err != nil {
return dataType, nil, err
}
}
_, rawMsgBytes, err := util.DecryptMsg(receiver.AppID, reqData.Encrypt, receiver.EncodingAESKey)
return dataType, rawMsgBytes, err
}
// 不加密
byteData, err := io.ReadAll(r.Body)
return dataType, byteData, err
}
// GetMsgData 获取接收到的消息(解密数据)
func (receiver *PushReceiver) GetMsgData(r *http.Request) (MsgType, EventType, PushData, error) {
dataType, decryptMsg, err := receiver.GetMsg(r)
if err != nil {
return "", "", nil, err
}
var (
msgType MsgType
eventType EventType
)
if dataType == DataTypeXML {
var commonToken CommonPushData
if err := xml.Unmarshal(decryptMsg, &commonToken); err != nil {
return "", "", nil, err
}
msgType, eventType = commonToken.MsgType, commonToken.Event
} else {
var commonToken CommonPushData
if err := json.Unmarshal(decryptMsg, &commonToken); err != nil {
return "", "", nil, err
}
msgType, eventType = commonToken.MsgType, commonToken.Event
}
if msgType == MsgTypeEvent {
pushData, err := receiver.getEvent(dataType, eventType, decryptMsg)
// 暂不支持其他事件类型
return msgType, eventType, pushData, err
}
// 暂不支持其他消息类型
return msgType, eventType, decryptMsg, nil
}
// getEvent 获取事件推送的数据
func (receiver *PushReceiver) getEvent(dataType string, eventType EventType, decryptMsg []byte) (PushData, error) {
switch eventType {
case EventTypeTradeManageRemindAccessAPI:
// 提醒接入发货信息管理服务API
var pushData PushDataRemindAccessAPI
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
return &pushData, err
case EventTypeTradeManageRemindShipping:
// 提醒需要上传发货信息
var pushData PushDataRemindShipping
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
return &pushData, err
case EventTypeTradeManageOrderSettlement:
// 订单将要结算或已经结算
var pushData PushDataOrderSettlement
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
return &pushData, err
case EventTypeWxaMediaCheck:
// 媒体内容安全异步审查结果通知
var pushData MediaCheckAsyncData
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
return &pushData, err
case EventTypeAddExpressPath:
// 运单轨迹更新
var pushData PushDataAddExpressPath
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
return &pushData, err
case EventTypeSecvodUpload:
// 短剧媒资上传完成
var pushData PushDataSecVodUpload
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
return &pushData, err
case EventTypeSecvodAudit:
// 短剧媒资审核状态
var pushData PushDataSecVodAudit
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
return &pushData, err
case EventTypeXpayGoodsDeliverNotify:
// 道具发货推送事件
var pushData PushDataXpayGoodsDeliverNotify
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
return &pushData, err
case EventTypeXpayCoinPayNotify:
// 代币支付推送事件
var pushData PushDataXpayCoinPayNotify
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
return &pushData, err
}
// 暂不支持其他事件类型,直接返回解密后的数据,由调用方处理
return decryptMsg, nil
}
// unmarshal 解析推送的数据
func (receiver *PushReceiver) unmarshal(dateType string, decryptMsg []byte, pushData interface{}) error {
if dateType == DataTypeXML {
return xml.Unmarshal(decryptMsg, pushData)
}
return json.Unmarshal(decryptMsg, pushData)
}
// DataReceived 接收到的数据
type DataReceived struct {
Encrypt string `json:"Encrypt" xml:"Encrypt"` // 加密的消息体
}
// PushData 推送的数据(已转对应的结构体)
type PushData interface{}
// CommonPushData 推送数据通用部分
type CommonPushData struct {
XMLName xml.Name `json:"-" xml:"xml"`
MsgType MsgType `json:"MsgType" xml:"MsgType"` // 消息类型,为固定值 "event"
Event EventType `json:"Event" xml:"Event"` // 事件类型
ToUserName string `json:"ToUserName" xml:"ToUserName"` // 小程序的原始 ID
FromUserName string `json:"FromUserName" xml:"FromUserName"` // 发送方账号(一个 OpenID此时发送方是系统账号
CreateTime int64 `json:"CreateTime" xml:"CreateTime"` // 消息创建时间 (整型),时间戳
}
// MediaCheckAsyncData 媒体内容安全异步审查结果通知
type MediaCheckAsyncData struct {
CommonPushData
Appid string `json:"appid" xml:"appid"`
TraceID string `json:"trace_id" xml:"trace_id"`
Version int `json:"version" xml:"version"`
Detail []*MediaCheckDetail `json:"detail" xml:"detail"`
Errcode int `json:"errcode" xml:"errcode"`
Errmsg string `json:"errmsg" xml:"errmsg"`
Result MediaCheckAsyncResult `json:"result" xml:"result"`
}
// MediaCheckDetail 检测结果详情
type MediaCheckDetail struct {
Strategy string `json:"strategy" xml:"strategy"`
Errcode int `json:"errcode" xml:"errcode"`
Suggest security.CheckSuggest `json:"suggest" xml:"suggest"`
Label int `json:"label" xml:"label"`
Prob int `json:"prob" xml:"prob"`
}
// MediaCheckAsyncResult 检测结果
type MediaCheckAsyncResult struct {
Suggest security.CheckSuggest `json:"suggest" xml:"suggest"`
Label security.CheckLabel `json:"label" xml:"label"`
}
// PushDataOrderSettlement 订单将要结算或已经结算通知
type PushDataOrderSettlement struct {
CommonPushData
TransactionID string `json:"transaction_id" xml:"transaction_id"` // 支付订单号
MerchantID string `json:"merchant_id" xml:"merchant_id"` // 商户号
SubMerchantID string `json:"sub_merchant_id" xml:"sub_merchant_id"` // 子商户号
MerchantTradeNo string `json:"merchant_trade_no" xml:"merchant_trade_no"` // 商户订单号
PayTime int64 `json:"pay_time" xml:"pay_time"` // 支付成功时间,秒级时间戳
ShippedTime int64 `json:"shipped_time" xml:"shipped_time"` // 发货时间,秒级时间戳
EstimatedSettlementTime int64 `json:"estimated_settlement_time" xml:"estimated_settlement_time"` // 预计结算时间,秒级时间戳。发货时推送才有该字段
ConfirmReceiveMethod ConfirmReceiveMethod `json:"confirm_receive_method" xml:"confirm_receive_method"` // 确认收货方式1. 自动确认收货2. 手动确认收货。结算时推送才有该字段
ConfirmReceiveTime int64 `json:"confirm_receive_time" xml:"confirm_receive_time"` // 确认收货时间,秒级时间戳。结算时推送才有该字段
SettlementTime int64 `json:"settlement_time" xml:"settlement_time"` // 订单结算时间,秒级时间戳。结算时推送才有该字段
}
// PushDataRemindShipping 提醒需要上传发货信息
type PushDataRemindShipping struct {
CommonPushData
TransactionID string `json:"transaction_id" xml:"transaction_id"` // 微信支付订单号
MerchantID string `json:"merchant_id" xml:"merchant_id"` // 商户号
SubMerchantID string `json:"sub_merchant_id" xml:"sub_merchant_id"` // 子商户号
MerchantTradeNo string `json:"merchant_trade_no" xml:"merchant_trade_no"` // 商户订单号
PayTime int64 `json:"pay_time" xml:"pay_time"` // 支付成功时间,秒级时间戳
Msg string `json:"msg" xml:"msg"` // 消息文本内容
}
// PushDataRemindAccessAPI 提醒接入发货信息管理服务API信息
type PushDataRemindAccessAPI struct {
CommonPushData
Msg string `json:"msg" xml:"msg"` // 消息文本内容
}
// PushDataAddExpressPath 运单轨迹更新信息
type PushDataAddExpressPath struct {
CommonPushData
DeliveryID string `json:"DeliveryID" xml:"DeliveryID"` // 快递公司ID
WayBillID string `json:"WaybillId" xml:"WaybillId"` // 运单ID
OrderID string `json:"OrderId" xml:"OrderId"` // 订单ID
Version int `json:"Version" xml:"Version"` // 轨迹版本号(整型)
Count int `json:"Count" xml:"Count"` // 轨迹节点数(整型)
Actions []*PushDataAddExpressPathAction `json:"Actions" xml:"Actions"` // 轨迹节点列表
}
// PushDataAddExpressPathAction 轨迹节点
type PushDataAddExpressPathAction struct {
ActionTime int64 `json:"ActionTime" xml:"ActionTime"` // 轨迹节点 Unix 时间戳
ActionType int `json:"ActionType" xml:"ActionType"` // 轨迹节点类型
ActionMsg string `json:"ActionMsg" xml:"ActionMsg"` // 轨迹节点详情
}
// PushDataSecVodUpload 短剧媒资上传完成
type PushDataSecVodUpload struct {
CommonPushData
UploadEvent SecVodUploadEvent `json:"upload_event" xml:"upload_event"` // 上传完成事件
}
// SecVodUploadEvent 短剧媒资上传完成事件
type SecVodUploadEvent struct {
MediaID string `json:"media_id" xml:"media_id"` // 媒资id
SourceContext string `json:"source_context" xml:"source_context"` // 透传上传接口中开发者设置的值。
Errcode int `json:"errcode" xml:"errcode"` // 错误码,上传失败时该值非
Errmsg string `json:"errmsg" xml:"errmsg"` // 错误提示
}
// PushDataSecVodAudit 短剧媒资审核状态
type PushDataSecVodAudit struct {
CommonPushData
AuditEvent SecVodAuditEvent `json:"audit_event" xml:"audit_event"` // 审核状态事件
}
// SecVodAuditEvent 短剧媒资审核状态事件
type SecVodAuditEvent struct {
DramaID string `json:"drama_id" xml:"drama_id"` // 剧目id
SourceContext string `json:"source_context" xml:"source_context"` // 透传上传接口中开发者设置的值
AuditDetail DramaAuditDetail `json:"audit_detail" xml:"audit_detail"` // 剧目审核结果单独每一集的审核结果可以根据drama_id查询剧集详情得到
}
// DramaAuditDetail 剧目审核结果
type DramaAuditDetail struct {
Status int `json:"status" xml:"status"` // 审核状态0为无效值1为审核中2为最终失败3为审核通过4为驳回重填
CreateTime int64 `json:"create_time" xml:"create_time"` // 提审时间戳
AuditTime int64 `json:"audit_time" xml:"audit_time"` // 审核时间戳
}
// PushDataXpayGoodsDeliverNotify 道具发货推送
type PushDataXpayGoodsDeliverNotify struct {
CommonPushData
OpenID string `json:"OpenId" xml:"OpenId"` // 用户openid
OutTradeNo string `json:"OutTradeNo" xml:"OutTradeNo"` // 业务订单号
Env int `json:"Env" xml:"Env"` //,环境配置 0现网环境也叫正式环境1沙箱环境
WeChatPayInfo WeChatPayInfo `json:"WeChatPayInfo" xml:"WeChatPayInfo"` // 微信支付信息 非微信支付渠道可能没有
GoodsInfo GoodsInfo `json:"GoodsInfo" xml:"GoodsInfo"` // 道具参数信息
}
// WeChatPayInfo 微信支付信息
type WeChatPayInfo struct {
MchOrderNo string `json:"MchOrderNo" xml:"MchOrderNo"` // 微信支付商户单号
TransactionID string `json:"TransactionId" xml:"TransactionId"` // 交易单号(微信支付订单号)
PaidTime int64 `json:"PaidTime" xml:"PaidTime"` // 用户支付时间Linux秒级时间戳
}
// GoodsInfo 道具参数信息
type GoodsInfo struct {
ProductID string `json:"ProductId" xml:"ProductId"` // 道具ID
Quantity int `json:"Quantity" xml:"Quantity"` // 数量
OrigPrice int64 `json:"OrigPrice" xml:"OrigPrice"` // 物品原始价格 (单位:分)
ActualPrice int64 `json:"ActualPrice" xml:"ActualPrice"` // 物品实际支付价格(单位:分)
Attach string `json:"Attach" xml:"Attach"` // 透传信息
}
// PushDataXpayCoinPayNotify 代币支付推送
type PushDataXpayCoinPayNotify struct {
CommonPushData
OpenID string `json:"OpenId" xml:"OpenId"` // 用户openid
OutTradeNo string `json:"OutTradeNo" xml:"OutTradeNo"` // 业务订单号
Env int `json:"Env" xml:"Env"` //,环境配置 0现网环境也叫正式环境1沙箱环境
WeChatPayInfo WeChatPayInfo `json:"WeChatPayInfo" xml:"WeChatPayInfo"` // 微信支付信息 非微信支付渠道可能没有
CoinInfo CoinInfo `json:"CoinInfo" xml:"CoinInfo"` // 代币参数信息
}
// CoinInfo 代币参数信息
type CoinInfo struct {
Quantity int `json:"Quantity" xml:"Quantity"` // 数量
OrigPrice int64 `json:"OrigPrice" xml:"OrigPrice"` // 物品原始价格 (单位:分)
ActualPrice int64 `json:"ActualPrice" xml:"ActualPrice"` // 物品实际支付价格(单位:分)
Attach string `json:"Attach" xml:"Attach"` // 透传信息
}

View File

@@ -12,6 +12,7 @@ import (
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
"github.com/silenceper/wechat/v2/miniprogram/message"
"github.com/silenceper/wechat/v2/miniprogram/minidrama"
"github.com/silenceper/wechat/v2/miniprogram/order"
"github.com/silenceper/wechat/v2/miniprogram/privacy"
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
"github.com/silenceper/wechat/v2/miniprogram/riskcontrol"
@@ -140,6 +141,16 @@ func (miniProgram *MiniProgram) GetVirtualPayment() *virtualpayment.VirtualPayme
return virtualpayment.NewVirtualPayment(miniProgram.ctx)
}
// GetMessageReceiver 获取消息推送接收器
func (miniProgram *MiniProgram) GetMessageReceiver() *message.PushReceiver {
return message.NewPushReceiver(miniProgram.ctx)
}
// GetShipping 小程序发货信息管理服务
func (miniProgram *MiniProgram) GetShipping() *order.Shipping {
return order.NewShipping(miniProgram.ctx)
}
// GetMiniDrama 小程序娱乐微短剧
func (miniProgram *MiniProgram) GetMiniDrama() *minidrama.MiniDrama {
return minidrama.NewMiniDrama(miniProgram.ctx)

View File

@@ -0,0 +1,269 @@
package order
import (
"fmt"
"time"
"github.com/silenceper/wechat/v2/miniprogram/context"
"github.com/silenceper/wechat/v2/util"
)
const (
// 发货信息录入
uploadShippingInfoURL = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=%s"
// 查询订单发货状态
getShippingOrderURL = "https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=%s"
// 查询订单列表
getShippingOrderListURL = "https://api.weixin.qq.com/wxa/sec/order/get_order_list?access_token=%s"
// 确认收货提醒接口
notifyConfirmReceiveURL = "https://api.weixin.qq.com/wxa/sec/order/notify_confirm_receive?access_token=%s"
)
// Shipping 发货信息管理
type Shipping struct {
*context.Context
}
// NewShipping init
func NewShipping(ctx *context.Context) *Shipping {
return &Shipping{ctx}
}
// UploadShippingInfo 发货信息录入
// see https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/order-shipping/order-shipping.html
func (shipping *Shipping) UploadShippingInfo(in *UploadShippingInfoRequest) (err error) {
accessToken, err := shipping.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf(uploadShippingInfoURL, accessToken)
response, err := util.PostJSON(uri, in)
if err != nil {
return
}
// 使用通用方法返回错误
return util.DecodeWithCommonError(response, "UploadShippingInfo")
}
// GetShippingOrder 查询订单发货状态
func (shipping *Shipping) GetShippingOrder(in *GetShippingOrderRequest) (res ShippingOrderResponse, err error) {
accessToken, err := shipping.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf(getShippingOrderURL, accessToken)
response, err := util.PostJSON(uri, in)
if err != nil {
return
}
err = util.DecodeWithError(response, &res, "GetShippingOrder")
return
}
// GetShippingOrderList 查询订单列表
func (shipping *Shipping) GetShippingOrderList(in *GetShippingOrderListRequest) (res GetShippingOrderListResponse, err error) {
accessToken, err := shipping.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf(getShippingOrderListURL, accessToken)
response, err := util.PostJSON(uri, in)
if err != nil {
return
}
err = util.DecodeWithError(response, &res, "GetShippingOrderList")
return
}
// NotifyConfirmReceive 确认收货提醒接口
func (shipping *Shipping) NotifyConfirmReceive(in *NotifyConfirmReceiveRequest) (err error) {
accessToken, err := shipping.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf(notifyConfirmReceiveURL, accessToken)
response, err := util.PostJSON(uri, in)
if err != nil {
return
}
// 使用通用方法返回错误
return util.DecodeWithCommonError(response, "NotifyConfirmReceive")
}
// UploadShippingInfoRequest 发货信息录入请求参数
type UploadShippingInfoRequest struct {
OrderKey *ShippingOrderKey `json:"order_key"` // 订单,需要上传物流信息的订单
LogisticsType LogisticsType `json:"logistics_type"` // 物流模式
DeliveryMode DeliveryMode `json:"delivery_mode"` // 发货模式
IsAllDelivered bool `json:"is_all_delivered"` // 分拆发货模式时必填,用于标识分拆发货模式下是否已全部发货完成
ShippingList []*ShippingInfo `json:"shipping_list"` // 物流信息列表,发货物流单列表,支持统一发货(单个物流单)和分拆发货(多个物流单)两种模式
UploadTime *time.Time `json:"upload_time"` // 上传时间,用于标识请求的先后顺序
Payer *ShippingPayer `json:"payer"` // 支付人信息
}
// ShippingOrderKey 订单
type ShippingOrderKey struct {
OrderNumberType NumberType `json:"order_number_type"` // 订单单号类型用于确认需要上传详情的订单。枚举值1使用下单商户号和商户侧单号枚举值2使用微信支付单号。
TransactionID string `json:"transaction_id"` // 原支付交易对应的微信订单号
Mchid string `json:"mchid"` // 支付下单商户的商户号,由微信支付生成并下发
OutTradeNo string `json:"out_trade_no"` // 商户系统内部订单号,只能是数字、大小写字母`_-*`且在同一个商户号下唯一
}
// ShippingPayer 支付者信息
type ShippingPayer struct {
Openid string `json:"openid"` // 用户标识用户在小程序appid下的唯一标识
}
// ShippingInfo 物流信息
type ShippingInfo struct {
TrackingNo string `json:"tracking_no"` // 物流单号,物流快递发货时必填
ExpressCompany string `json:"express_company"` // 物流公司编码快递公司ID物流快递发货时必填参见「查询物流公司编码列表」
ItemDesc string `json:"item_desc"` // 商品信息,例如:微信红包抱枕*1个限120个字以内
Contact ShippingContact `json:"contact"` // 联系方式,当发货的物流公司为顺丰时,联系方式为必填,收件人或寄件人联系方式二选一
}
// ShippingContact 联系方式
type ShippingContact struct {
ConsignorContact string `json:"consignor_contact"` // 寄件人联系方式寄件人联系方式采用掩码传输最后4位数字不能打掩码
ReceiverContact string `json:"receiver_contact"` // 收件人联系方式收件人联系方式采用掩码传输最后4位数字不能打掩码
}
// DeliveryMode 发货模式
type DeliveryMode uint8
const (
// DeliveryModeUnifiedDelivery 统一发货
DeliveryModeUnifiedDelivery DeliveryMode = 1
// DeliveryModeSplitDelivery 分拆发货
DeliveryModeSplitDelivery DeliveryMode = 2
)
// LogisticsType 物流模式
type LogisticsType uint8
const (
// LogisticsTypeExpress 实体物流配送采用快递公司进行实体物流配送形式
LogisticsTypeExpress LogisticsType = 1
// LogisticsTypeSameCity 同城配送
LogisticsTypeSameCity LogisticsType = 2
// LogisticsTypeVirtual 虚拟商品,虚拟商品,例如话费充值,点卡等,无实体配送形式
LogisticsTypeVirtual LogisticsType = 3
// LogisticsTypeSelfPickup 用户自提
LogisticsTypeSelfPickup LogisticsType = 4
)
// NumberType 订单单号类型
type NumberType uint8
const (
// NumberTypeOutTradeNo 使用下单商户号和商户侧单号
NumberTypeOutTradeNo NumberType = 1
// NumberTypeTransactionID 使用微信支付单号
NumberTypeTransactionID NumberType = 2
)
// GetShippingOrderRequest 查询订单发货状态参数
type GetShippingOrderRequest struct {
TransactionID string `json:"transaction_id"` // 原支付交易对应的微信订单号
MerchantID string `json:"merchant_id"` // 支付下单商户的商户号,由微信支付生成并下发
SubMerchantID string `json:"sub_merchant_id"` //二级商户号
MerchantTradeNo string `json:"merchant_trade_no"` //商户系统内部订单号,只能是数字、大小写字母`_-*`且在同一个商户号下唯一。
}
// ShippingItem 物流信息
type ShippingItem struct {
TrackingNo string `json:"tracking_no"` // 物流单号,示例值: "323244567777
ExpressCompany string `json:"express_company"` // 物流公司编码快递公司ID物流快递发货时必填参见「查询物流公司编码列表」
UploadTime int64 `json:"upload_time"` // 上传物流信息时间,时间戳形式
}
// ShippingDetail 发货信息
type ShippingDetail struct {
DeliveryMode DeliveryMode `json:"delivery_mode"` // 发货模式
LogisticsType LogisticsType `json:"logistics_type"` // 物流模式
FinishShipping bool `json:"finish_shipping"` // 是否已全部发货
FinishShippingCount int `json:"finish_shipping_count"` // 已完成全部发货的次数
GoodsDesc string `json:"goods_desc"` // 在小程序后台发货信息录入页录入的商品描述
ShippingList []*ShippingItem `json:"shipping_list"` // 物流信息列表
}
// ShippingOrder 订单发货状态
type ShippingOrder struct {
TransactionID string `json:"transaction_id"` // 原支付交易对应的微信订单号
MerchantTradeNo string `json:"merchant_trade_no"` // 商户系统内部订单号,只能是数字、大小写字母`_-*`且在同一个商户号下唯一
MerchantID string `json:"merchant_id"` // 支付下单商户的商户号,由微信支付生成并下发
SubMerchantID string `json:"sub_merchant_id"` // 二级商户号
Description string `json:"description"` // 以分号连接的该支付单的所有商品描述当超过120字时自动截断并以 “...” 结尾
PaidAmount int64 `json:"paid_amount"` // 支付单实际支付金额,整型,单位:分钱
Openid string `json:"openid"` // 支付者openid
TradeCreateTime int64 `json:"trade_create_time"` // 交易创建时间,时间戳形式
PayTime int64 `json:"pay_time"` // 支付时间,时间戳形式
InComplaint bool `json:"in_complaint"` // 是否处在交易纠纷中
OrderState State `json:"order_state"` // 订单状态枚举:(1) 待发货;(2) 已发货;(3) 确认收货;(4) 交易完成;(5) 已退款
Shipping *ShippingDetail `json:"shipping"` // 订单发货信息
}
// ShippingOrderResponse 查询订单发货状态返回参数
type ShippingOrderResponse struct {
util.CommonError
Order ShippingOrder `json:"order"` // 订单发货信息
}
// State 订单状态
type State uint8
const (
// StateWaitShipment 待发货
StateWaitShipment State = 1
// StateShipped 已发货
StateShipped State = 2
// StateConfirm 确认收货
StateConfirm State = 3
// StateComplete 交易完成
StateComplete State = 4
// StateRefund 已退款
StateRefund State = 5
)
// GetShippingOrderListRequest 查询订单列表请求参数
type GetShippingOrderListRequest struct {
PayTimeRange *TimeRange `json:"pay_time_range"` // 支付时间范围
OrderState State `json:"order_state,omitempty"` // 订单状态
Openid string `json:"openid,omitempty"` // 支付者openid
LastIndex string `json:"last_index,omitempty"` // 翻页时使用,获取第一页时不用传入,如果查询结果中 has_more 字段为 true则传入该次查询结果中返回的 last_index 字段可获取下一页
PageSize int64 `json:"page_size"` // 每页数量最多50条
}
// TimeRange 时间范围
type TimeRange struct {
BeginTime int64 `json:"begin_time,omitempty"` // 查询开始时间,时间戳形式
EndTime int64 `json:"end_time,omitempty"` // 查询结束时间,时间戳形式
}
// GetShippingOrderListResponse 查询订单列表返回参数
type GetShippingOrderListResponse struct {
util.CommonError
OrderList []*ShippingOrder `json:"order_list"`
LastIndex string `json:"last_index"`
HasMore bool `json:"has_more"`
}
// NotifyConfirmReceiveRequest 确认收货提醒接口请求参数
type NotifyConfirmReceiveRequest struct {
TransactionID string `json:"transaction_id"` // 原支付交易对应的微信订单号
MerchantID string `json:"merchant_id"` // 支付下单商户的商户号,由微信支付生成并下发
SubMerchantID string `json:"sub_merchant_id"` // 二级商户号
MerchantTradeNo string `json:"merchant_trade_no"` // 商户系统内部订单号,只能是数字、大小写字母`_-*`且在同一个商户号下唯一
ReceivedTime int64 `json:"received_time"` // 收货时间,时间戳形式
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/silenceper/wechat/v2/pay/config"
"github.com/silenceper/wechat/v2/pay/notify"
"github.com/silenceper/wechat/v2/pay/order"
"github.com/silenceper/wechat/v2/pay/redpacket"
"github.com/silenceper/wechat/v2/pay/refund"
"github.com/silenceper/wechat/v2/pay/transfer"
)
@@ -37,3 +38,8 @@ func (pay *Pay) GetRefund() *refund.Refund {
func (pay *Pay) GetTransfer() *transfer.Transfer {
return transfer.NewTransfer(pay.cfg)
}
// GetRedpacket 红包
func (pay *Pay) GetRedpacket() *redpacket.Redpacket {
return redpacket.NewRedpacket(pay.cfg)
}

131
pay/redpacket/redpacket.go Normal file
View File

@@ -0,0 +1,131 @@
package redpacket
import (
"encoding/xml"
"fmt"
"strconv"
"github.com/silenceper/wechat/v2/pay/config"
"github.com/silenceper/wechat/v2/util"
)
// redpacketGateway 发放红包接口
// https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
var redpacketGateway = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"
// Redpacket struct extends context
type Redpacket struct {
*config.Config
}
// NewRedpacket return an instance of Redpacket package
func NewRedpacket(cfg *config.Config) *Redpacket {
return &Redpacket{cfg}
}
// Params 调用参数
type Params struct {
MchBillno string // 商户订单号
SendName string // 商户名称
ReOpenID string
TotalAmount int
TotalNum int
Wishing string
ClientIP string
ActName string
Remark string
RootCa string // ca证书
}
// request 接口请求参数
type request struct {
NonceStr string `xml:"nonce_str"`
Sign string `xml:"sign"`
MchID string `xml:"mch_id"`
MchBillno string `xml:"mch_billno"`
Wxappid string `xml:"wxappid"`
SendName string `xml:"send_name"`
ReOpenID string `xml:"re_openid"`
TotalAmount int `xml:"total_amount"`
TotalNum int `xml:"total_num"`
Wishing string `xml:"wishing"`
ClientIP string `xml:"client_ip"`
ActName string `xml:"act_name"`
Remark string `xml:"remark"`
}
// Response 接口返回
type Response struct {
ReturnCode string `xml:"return_code"`
ReturnMsg string `xml:"return_msg"`
ResultCode string `xml:"result_code,omitempty"`
ErrCode string `xml:"err_code,omitempty"`
ErrCodeDes string `xml:"err_code_des,omitempty"`
MchBillno string `xml:"mch_billno,omitempty"`
MchID string `xml:"mch_id,omitempty"`
Wxappid string `xml:"wxappid"`
ReOpenID string `xml:"re_openid"`
TotalAmount int `xml:"total_amount"`
SendListid string `xml:"send_listid"`
}
// SendRedpacket 发放红包
func (redpacket *Redpacket) SendRedpacket(p *Params) (rsp *Response, err error) {
nonceStr := util.RandomStr(32)
param := make(map[string]string)
param["nonce_str"] = nonceStr
param["mch_id"] = redpacket.MchID
param["wxappid"] = redpacket.AppID
param["mch_billno"] = p.MchBillno
param["send_name"] = p.SendName
param["re_openid"] = p.ReOpenID
param["total_amount"] = strconv.Itoa(p.TotalAmount)
param["total_num"] = strconv.Itoa(p.TotalNum)
param["wishing"] = p.Wishing
param["client_ip"] = p.ClientIP
param["act_name"] = p.ActName
param["remark"] = p.Remark
//param["scene_id"] = "PRODUCT_2"
sign, err := util.ParamSign(param, redpacket.Key)
if err != nil {
return
}
req := request{
NonceStr: nonceStr,
Sign: sign,
MchID: redpacket.MchID,
Wxappid: redpacket.AppID,
MchBillno: p.MchBillno,
SendName: p.SendName,
ReOpenID: p.ReOpenID,
TotalAmount: p.TotalAmount,
TotalNum: p.TotalNum,
Wishing: p.Wishing,
ClientIP: p.ClientIP,
ActName: p.ActName,
Remark: p.Remark,
}
rawRet, err := util.PostXMLWithTLS(redpacketGateway, req, p.RootCa, redpacket.MchID)
if err != nil {
return
}
err = xml.Unmarshal(rawRet, &rsp)
if err != nil {
return
}
if rsp.ReturnCode == "SUCCESS" {
if rsp.ResultCode == "SUCCESS" {
err = nil
return
}
err = fmt.Errorf("send redpacket error, errcode=%s,errmsg=%s", rsp.ErrCode, rsp.ErrCodeDes)
return
}
err = fmt.Errorf("[msg : xmlUnmarshalError] [rawReturn : %s] [sign : %s]", string(rawRet), sign)
return
}

View File

@@ -13,6 +13,8 @@ const (
departmentSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token=%s&id=%d"
// departmentListURL 获取部门列表
departmentListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s"
// departmentGetURL 获取单个部门详情 https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=ACCESS_TOKEN&id=ID
departmentGetURL = "https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=%s&id=%d"
)
type (
@@ -56,6 +58,11 @@ type (
ParentID int `json:"parentid"` // 父部门id。根部门为1
Order int `json:"order"` // 在父部门中的次序值。order值大的排序靠前
}
// DepartmentGetResponse 获取单个部门详情
DepartmentGetResponse struct {
util.CommonError
Department Department `json:"department"`
}
)
// DepartmentCreate 创建部门
@@ -121,3 +128,24 @@ func (r *Client) DepartmentList() ([]*Department, error) {
// 返回数据
return result.Department, err
}
// DepartmentGet 获取单个部门详情
// see https://developer.work.weixin.qq.com/document/path/95351
func (r *Client) DepartmentGet(departmentID int) (*Department, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.HTTPGet(fmt.Sprintf(departmentGetURL, accessToken, departmentID)); err != nil {
return nil, err
}
result := &DepartmentGetResponse{}
if err = util.DecodeWithError(response, result, "DepartmentGet"); err != nil {
return nil, err
}
return &result.Department, nil
}

View File

@@ -221,7 +221,7 @@ type UserGetResponse struct {
} `json:"external_profile"` // 成员对外属性,字段详情见对外属性;代开发自建应用需要管理员授权才返回;第三方仅通讯录应用可获取;对于非第三方创建的成员,第三方通讯录应用也不可获取;上游企业不可获取下游企业成员该字段
}
// UserGet 获取部门成员
// UserGet 读取成员
// @see https://developer.work.weixin.qq.com/document/path/90196
func (r *Client) UserGet(UserID string) (*UserGetResponse, error) {
var (
@@ -237,8 +237,8 @@ func (r *Client) UserGet(UserID string) (*UserGetResponse, error) {
strings.Join([]string{
userGetURL,
util.Query(map[string]interface{}{
"access_token": accessToken,
"department_id": UserID,
"access_token": accessToken,
"userid": UserID,
}),
}, "?")); err != nil {
return nil, err

69
work/checkin/checkin.go Normal file
View File

@@ -0,0 +1,69 @@
package checkin
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// getCheckinDataURL 获取打卡记录数据
getCheckinDataURL = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=%s"
)
type (
// GetCheckinDataRequest 获取打卡记录数据请求
GetCheckinDataRequest struct {
OpenCheckinDataType int64 `json:"opencheckindatatype"`
StartTime int64 `json:"starttime"`
EndTime int64 `json:"endtime"`
UserIDList []string `json:"useridlist"`
}
// GetCheckinDataResponse 获取打卡记录数据响应
GetCheckinDataResponse struct {
util.CommonError
CheckinData []*GetCheckinDataItem `json:"checkindata"`
}
// GetCheckinDataItem 打卡记录数据
GetCheckinDataItem struct {
UserID string `json:"userid"`
GroupName string `json:"groupname"`
CheckinType string `json:"checkin_type"`
ExceptionType string `json:"exception_type"`
CheckinTime int64 `json:"checkin_time"`
LocationTitle string `json:"location_title"`
LocationDetail string `json:"location_detail"`
WifiName string `json:"wifiname"`
Notes string `json:"notes"`
WifiMac string `json:"wifimac"`
MediaIDs []string `json:"mediaids"`
SchCheckinTime int64 `json:"sch_checkin_time"`
GroupID int64 `json:"groupid"`
ScheduleID int64 `json:"schedule_id"`
TimelineID int64 `json:"timeline_id"`
Lat int64 `json:"lat,omitempty"`
Lng int64 `json:"lng,omitempty"`
DeviceID string `json:"deviceid,omitempty"`
}
)
// GetCheckinData 获取打卡记录数据
// @see https://developer.work.weixin.qq.com/document/path/90262
func (r *Client) GetCheckinData(req *GetCheckinDataRequest) (*GetCheckinDataResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getCheckinDataURL, accessToken), req); err != nil {
return nil, err
}
result := &GetCheckinDataResponse{}
if err = util.DecodeWithError(response, result, "GetCheckinData"); err != nil {
return nil, err
}
return result, nil
}

17
work/checkin/client.go Normal file
View File

@@ -0,0 +1,17 @@
package checkin
import (
"github.com/silenceper/wechat/v2/work/context"
)
// Client 打卡接口实例
type Client struct {
*context.Context
}
// NewClient 初始化实例
func NewClient(ctx *context.Context) *Client {
return &Client{
ctx,
}
}

View File

@@ -70,6 +70,7 @@ type (
GroupNickname string `json:"group_nickname"` //在群里的昵称
Name string `json:"name"` //名字。仅当 need_name = 1 时返回 如果是微信用户,则返回其在微信中设置的名字 如果是企业微信联系人,则返回其设置对外展示的别名或实名
UnionID string `json:"unionid,omitempty"` //外部联系人在微信开放平台的唯一身份标识微信unionid通过此字段企业可将外部联系人与公众号/小程序用户关联起来。仅当群成员类型是微信用户包括企业成员未添加好友且企业绑定了微信开发者ID有此字段查看绑定方法。第三方不可获取上游企业不可获取下游企业客户的unionid字段
State string `json:"state,omitempty"` //如果在配置入群方式时配置了state参数那么在获取客户群详情时通过该方式入群的成员会额外获取到相应的state参数
}
//GroupChatAdmin 群管理员
GroupChatAdmin struct {

View File

@@ -25,6 +25,10 @@ const (
getGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/get?access_token=%s"
// delGroupWelcomeTemplateURL 删除入群欢迎语素材
delGroupWelcomeTemplateURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/group_welcome_template/del?access_token=%s"
// remindGroupMsgSendURL 提醒成员群发
remindGroupMsgSendURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/remind_groupmsg_send?access_token=%s"
// cancelGroupMsgSendURL 停止企业群发
cancelGroupMsgSendURL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/cancel_groupmsg_send?access_token=%s"
)
// AddMsgTemplateRequest 创建企业群发请求
@@ -422,3 +426,47 @@ func (r *Client) DelGroupWelcomeTemplate(req *DelGroupWelcomeTemplateRequest) er
}
return nil
}
// RemindGroupMsgSendRequest 提醒成员群发请求
type RemindGroupMsgSendRequest struct {
MsgID string `json:"msgid"`
}
// RemindGroupMsgSend 提醒成员群发
// see https://developer.work.weixin.qq.com/document/path/97610
func (r *Client) RemindGroupMsgSend(req *RemindGroupMsgSendRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(remindGroupMsgSendURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "RemindGroupMsgSend")
}
// CancelGroupMsgSendRequest 停止企业群发请求
type CancelGroupMsgSendRequest struct {
MsgID string `json:"msgid"`
}
// CancelGroupMsgSend 提醒成员群发
// see https://developer.work.weixin.qq.com/document/path/97611
func (r *Client) CancelGroupMsgSend(req *CancelGroupMsgSendRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(cancelGroupMsgSendURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "CancelGroupMsgSend")
}

359
work/kf/knowledge.go Normal file
View File

@@ -0,0 +1,359 @@
package kf
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// addKnowledgeGroupURL 知识库分组添加
addKnowledgeGroupURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/add_group?access_token=%s"
// delKnowledgeGroupURL 知识库分组删除
delKnowledgeGroupURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/del_group?access_token=%s"
// modKnowledgeGroupURL 知识库分组修改
modKnowledgeGroupURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/mod_group?access_token=%s"
// listKnowledgeGroupURL 知识库分组列表
listKnowledgeGroupURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/list_group?access_token=%s"
// addKnowledgeIntentURL 知识库问答添加
addKnowledgeIntentURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/add_intent?access_token=%s"
// delKnowledgeIntentURL 知识库问答删除
delKnowledgeIntentURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/del_intent?access_token=%s"
// modKnowledgeIntentURL 知识库问答修改
modKnowledgeIntentURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/mod_intent?access_token=%s"
// listKnowledgeIntentURL 知识库问答列表
listKnowledgeIntentURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/knowledge/list_intent?access_token=%s"
)
// AddKnowledgeGroupRequest 知识库分组添加请求
type AddKnowledgeGroupRequest struct {
Name string `json:"name"`
}
// AddKnowledgeGroupResponse 知识库分组添加响应
type AddKnowledgeGroupResponse struct {
util.CommonError
GroupID string `json:"group_id"`
}
// AddKnowledgeGroup 知识库分组添加
// see https://developer.work.weixin.qq.com/document/path/95971#%E6%B7%BB%E5%8A%A0%E5%88%86%E7%BB%84
func (r *Client) AddKnowledgeGroup(req *AddKnowledgeGroupRequest) (*AddKnowledgeGroupResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(addKnowledgeGroupURL, accessToken), req); err != nil {
return nil, err
}
result := &AddKnowledgeGroupResponse{}
err = util.DecodeWithError(response, result, "AddKnowledgeGroup")
return result, err
}
// DelKnowledgeGroupRequest 知识库分组删除请求
type DelKnowledgeGroupRequest struct {
GroupID string `json:"group_id"`
}
// DelKnowledgeGroup 知识库分组删除
// see https://developer.work.weixin.qq.com/document/path/95971#%E5%88%A0%E9%99%A4%E5%88%86%E7%BB%84
func (r *Client) DelKnowledgeGroup(req *DelKnowledgeGroupRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(delKnowledgeGroupURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "DelKnowledgeGroup")
}
// ModKnowledgeGroupRequest 知识库分组修改请求
type ModKnowledgeGroupRequest struct {
GroupID string `json:"group_id"`
Name string `json:"name"`
}
// ModKnowledgeGroup 知识库分组修改
// see https://developer.work.weixin.qq.com/document/path/95971#%E4%BF%AE%E6%94%B9%E5%88%86%E7%BB%84
func (r *Client) ModKnowledgeGroup(req *ModKnowledgeGroupRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(modKnowledgeGroupURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "ModKnowledgeGroup")
}
// ListKnowledgeGroupRequest 知识库分组列表请求
type ListKnowledgeGroupRequest struct {
Cursor string `json:"cursor"`
Limit int `json:"limit"`
GroupID string `json:"group_id"`
}
// ListKnowledgeGroupResponse 知识库分组列表响应
type ListKnowledgeGroupResponse struct {
util.CommonError
NextCursor string `json:"next_cursor"`
HasMore int `json:"has_more"`
GroupList []KnowledgeGroup `json:"group_list"`
}
// KnowledgeGroup 知识库分组
type KnowledgeGroup struct {
GroupID string `json:"group_id"`
Name string `json:"name"`
IsDefault int `json:"is_default"`
}
// ListKnowledgeGroup 知识库分组列表
// see https://developer.work.weixin.qq.com/document/path/95971#%E8%8E%B7%E5%8F%96%E5%88%86%E7%BB%84%E5%88%97%E8%A1%A8
func (r *Client) ListKnowledgeGroup(req *ListKnowledgeGroupRequest) (*ListKnowledgeGroupResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(listKnowledgeGroupURL, accessToken), req); err != nil {
return nil, err
}
result := &ListKnowledgeGroupResponse{}
err = util.DecodeWithError(response, result, "ListKnowledgeGroup")
return result, err
}
// AddKnowledgeIntentRequest 知识库问答添加请求
type AddKnowledgeIntentRequest struct {
GroupID string `json:"group_id"`
Question IntentQuestion `json:"question"`
SimilarQuestions IntentSimilarQuestions `json:"similar_questions"`
Answers []IntentAnswerReq `json:"answers"`
}
// IntentQuestion 主问题
type IntentQuestion struct {
Text IntentQuestionText `json:"text"`
}
// IntentQuestionText 问题文本
type IntentQuestionText struct {
Content string `json:"content"`
}
// IntentSimilarQuestions 相似问题
type IntentSimilarQuestions struct {
Items []IntentQuestion `json:"items"`
}
// IntentAnswerReq 回答请求
type IntentAnswerReq struct {
Text IntentAnswerText `json:"text"`
Attachments []IntentAnswerAttachmentReq `json:"attachments"`
}
// IntentAnswerText 回答文本
type IntentAnswerText struct {
Content string `json:"content"`
}
// IntentAnswerAttachmentReq 回答附件请求
type IntentAnswerAttachmentReq struct {
MsgType string `json:"msgtype"`
Image IntentAnswerAttachmentImgReq `json:"image,omitempty"`
Video IntentAnswerAttachmentVideoReq `json:"video,omitempty"`
Link IntentAnswerAttachmentLink `json:"link,omitempty"`
MiniProgram IntentAnswerAttachmentMiniProgramReq `json:"miniprogram,omitempty"`
}
// IntentAnswerAttachmentImgReq 图片类型回答附件请求
type IntentAnswerAttachmentImgReq struct {
MediaID string `json:"media_id"`
}
// IntentAnswerAttachmentVideoReq 视频类型回答附件请求
type IntentAnswerAttachmentVideoReq struct {
MediaID string `json:"media_id"`
}
// IntentAnswerAttachmentLink 链接类型回答附件
type IntentAnswerAttachmentLink struct {
Title string `json:"title"`
PicURL string `json:"picurl"`
Desc string `json:"desc"`
URL string `json:"url"`
}
// IntentAnswerAttachmentMiniProgramReq 小程序类型回答附件请求
type IntentAnswerAttachmentMiniProgramReq struct {
Title string `json:"title"`
ThumbMediaID string `json:"thumb_media_id"`
AppID string `json:"appid"`
PagePath string `json:"pagepath"`
}
// AddKnowledgeIntentResponse 知识库问答添加响应
type AddKnowledgeIntentResponse struct {
util.CommonError
IntentID string `json:"intent_id"`
}
// AddKnowledgeIntent 知识库问答添加
// see https://developer.work.weixin.qq.com/document/path/95972#%E6%B7%BB%E5%8A%A0%E9%97%AE%E7%AD%94
func (r *Client) AddKnowledgeIntent(req *AddKnowledgeIntentRequest) (*AddKnowledgeIntentResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(addKnowledgeIntentURL, accessToken), req); err != nil {
return nil, err
}
result := &AddKnowledgeIntentResponse{}
err = util.DecodeWithError(response, result, "AddKnowledgeIntent")
return result, err
}
// DelKnowledgeIntentRequest 知识库问答删除请求
type DelKnowledgeIntentRequest struct {
IntentID string `json:"intent_id"`
}
// DelKnowledgeIntent 知识库问答删除
// see https://developer.work.weixin.qq.com/document/path/95972#%E5%88%A0%E9%99%A4%E9%97%AE%E7%AD%94
func (r *Client) DelKnowledgeIntent(req *DelKnowledgeIntentRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(delKnowledgeIntentURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "DelKnowledgeIntent")
}
// ModKnowledgeIntentRequest 知识库问答修改请求
type ModKnowledgeIntentRequest struct {
IntentID string `json:"intent_id"`
Question IntentQuestion `json:"question"`
SimilarQuestions IntentSimilarQuestions `json:"similar_questions"`
Answers []IntentAnswerReq `json:"answers"`
}
// ModKnowledgeIntent 知识库问答修改
// see https://developer.work.weixin.qq.com/document/path/95972#%E4%BF%AE%E6%94%B9%E9%97%AE%E7%AD%94
func (r *Client) ModKnowledgeIntent(req *ModKnowledgeIntentRequest) error {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(modKnowledgeIntentURL, accessToken), req); err != nil {
return err
}
return util.DecodeWithCommonError(response, "ModKnowledgeIntent")
}
// ListKnowledgeIntentRequest 知识库问答列表请求
type ListKnowledgeIntentRequest struct {
Cursor string `json:"cursor"`
Limit int `json:"limit"`
GroupID string `json:"group_id"`
IntentID string `json:"intent_id"`
}
// ListKnowledgeIntentResponse 知识库问答列表响应
type ListKnowledgeIntentResponse struct {
util.CommonError
NextCursor string `json:"next_cursor"`
HasMore int `json:"has_more"`
IntentList []KnowledgeIntent `json:"intent_list"`
}
// KnowledgeIntent 问答摘要
type KnowledgeIntent struct {
GroupID string `json:"group_id"`
IntentID string `json:"intent_id"`
Question IntentQuestion `json:"question"`
SimilarQuestions IntentSimilarQuestions `json:"similar_questions"`
Answers []IntentAnswerRes `json:"answers"`
}
// IntentAnswerRes 回答返回
type IntentAnswerRes struct {
Text IntentAnswerText `json:"text"`
Attachments []IntentAnswerAttachmentRes `json:"attachments"`
}
// IntentAnswerAttachmentRes 回答附件返回
type IntentAnswerAttachmentRes struct {
MsgType string `json:"msgtype"`
Image IntentAnswerAttachmentImgRes `json:"image,omitempty"`
Video IntentAnswerAttachmentVideoRes `json:"video,omitempty"`
Link IntentAnswerAttachmentLink `json:"link,omitempty"`
MiniProgram IntentAnswerAttachmentMiniProgramRes `json:"miniprogram,omitempty"`
}
// IntentAnswerAttachmentImgRes 图片类型回答附件返回
type IntentAnswerAttachmentImgRes struct {
Name string `json:"name"`
}
// IntentAnswerAttachmentVideoRes 视频类型回答附件返回
type IntentAnswerAttachmentVideoRes struct {
Name string `json:"name"`
}
// IntentAnswerAttachmentMiniProgramRes 小程序类型回答附件返回
type IntentAnswerAttachmentMiniProgramRes struct {
Title string `json:"title"`
AppID string `json:"appid"`
PagePath string `json:"pagepath"`
}
// ListKnowledgeIntent 知识库问答列表
// see https://developer.work.weixin.qq.com/document/path/95972#%E8%8E%B7%E5%8F%96%E9%97%AE%E7%AD%94%E5%88%97%E8%A1%A8
func (r *Client) ListKnowledgeIntent(req *ListKnowledgeIntentRequest) (*ListKnowledgeIntentResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(listKnowledgeIntentURL, accessToken), req); err != nil {
return nil, err
}
result := &ListKnowledgeIntentResponse{}
err = util.DecodeWithError(response, result, "ListKnowledgeIntent")
return result, err
}

127
work/kf/statistic.go Normal file
View File

@@ -0,0 +1,127 @@
package kf
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// getCorpStatisticURL 获取「客户数据统计」企业汇总数据
getCorpStatisticURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/get_corp_statistic?access_token=%s"
// getServicerStatisticURL 获取「客户数据统计」接待人员明细数据
getServicerStatisticURL = "https://qyapi.weixin.qq.com/cgi-bin/kf/get_servicer_statistic?access_token=%s"
)
// GetCorpStatisticRequest 获取「客户数据统计」企业汇总数据请求
type GetCorpStatisticRequest struct {
OpenKfID string `json:"open_kfid"`
StartTime int64 `json:"start_time"`
EndTime int64 `json:"end_time"`
}
// GetCorpStatisticResponse 获取「客户数据统计」企业汇总数据响应
type GetCorpStatisticResponse struct {
util.CommonError
StatisticList []CorpStatisticList `json:"statistic_list"`
}
// CorpStatisticList 企业汇总统计数据列表
type CorpStatisticList struct {
StatTime int64 `json:"stat_time"`
Statistic CorpStatistic `json:"statistic"`
}
// CorpStatistic 企业汇总统计一天的统计数据
type CorpStatistic struct {
SessionCnt int64 `json:"session_cnt"`
CustomerCnt int64 `json:"customer_cnt"`
CustomerMsgCnt int64 `json:"customer_msg_cnt"`
UpgradeServiceCustomerCnt int64 `json:"upgrade_service_customer_cnt"`
AiSessionReplyCnt int64 `json:"ai_session_reply_cnt"`
AiTransferRate float64 `json:"ai_transfer_rate"`
AiKnowledgeHitRate float64 `json:"ai_knowledge_hit_rate"`
MsgRejectedCustomerCnt int64 `json:"msg_rejected_customer_cnt"`
}
// GetCorpStatistic 获取「客户数据统计」企业汇总数据
// see https://developer.work.weixin.qq.com/document/path/95489
func (r *Client) GetCorpStatistic(req *GetCorpStatisticRequest) (*GetCorpStatisticResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getCorpStatisticURL, accessToken), req); err != nil {
return nil, err
}
result := &GetCorpStatisticResponse{}
if err = util.DecodeWithError(response, result, "GetCorpStatistic"); err != nil {
return nil, err
}
return result, nil
}
// GetServicerStatisticRequest 获取「客户数据统计」接待人员明细数据请求
type GetServicerStatisticRequest struct {
OpenKfID string `json:"open_kfid"`
ServicerUserID string `json:"servicer_userid"`
StartTime int64 `json:"start_time"`
EndTime int64 `json:"end_time"`
}
// GetServicerStatisticResponse 获取「客户数据统计」接待人员明细数据响应
type GetServicerStatisticResponse struct {
util.CommonError
StatisticList []ServicerStatisticList `json:"statistic_list"`
}
// ServicerStatisticList 接待人员明细统计数据列表
type ServicerStatisticList struct {
StatTime int64 `json:"stat_time"`
Statistic ServicerStatistic `json:"statistic"`
}
// ServicerStatistic 接待人员明细统计一天的统计数据
type ServicerStatistic struct {
SessionCnt int64 `json:"session_cnt"`
CustomerCnt int64 `json:"customer_cnt"`
CustomerMsgCnt int64 `json:"customer_msg_cnt"`
ReplyRate float64 `json:"reply_rate"`
FirstReplyAverageSec float64 `json:"first_reply_average_sec"`
SatisfactionInvestgateCnt int64 `json:"satisfaction_investgate_cnt"`
SatisfactionParticipationRate float64 `json:"satisfaction_participation_rate"`
SatisfiedRate float64 `json:"satisfied_rate"`
MiddlingRate float64 `json:"middling_rate"`
DissatisfiedRate float64 `json:"dissatisfied_rate"`
UpgradeServiceCustomerCnt int64 `json:"upgrade_service_customer_cnt"`
UpgradeServiceMemberInviteCnt int64 `json:"upgrade_service_member_invite_cnt"`
UpgradeServiceMemberCustomerCnt int64 `json:"upgrade_service_member_customer_cnt"`
UpgradeServiceGroupChatInviteCnt int64 `json:"upgrade_service_groupchat_invite_cnt"`
UpgradeServiceGroupChatCustomerCnt int64 `json:"upgrade_service_groupchat_customer_cnt"`
MsgRejectedCustomerCnt int64 `json:"msg_rejected_customer_cnt"`
}
// GetServicerStatistic 获取「客户数据统计」接待人员明细数据
// see https://developer.work.weixin.qq.com/document/path/95490
func (r *Client) GetServicerStatistic(req *GetServicerStatisticRequest) (*GetServicerStatisticResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.ctx.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostJSON(fmt.Sprintf(getServicerStatisticURL, accessToken), req); err != nil {
return nil, err
}
result := &GetServicerStatisticResponse{}
if err = util.DecodeWithError(response, result, "GetServicerStatistic"); err != nil {
return nil, err
}
return result, nil
}

View File

@@ -11,6 +11,8 @@ const (
uploadImgURL = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s"
// uploadTempFile 上传临时素材
uploadTempFile = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s"
// uploadAttachment 上传附件资源
uploadAttachment = "https://qyapi.weixin.qq.com/cgi-bin/media/upload_attachment?access_token=%s&media_type=%s&attachment_type=%d"
)
// UploadImgResponse 上传图片响应
@@ -27,6 +29,14 @@ type UploadTempFileResponse struct {
Type string `json:"type"`
}
// UploadAttachmentResponse 上传资源附件响应
type UploadAttachmentResponse struct {
util.CommonError
MediaID string `json:"media_id"`
CreateAt int64 `json:"created_at"`
Type string `json:"type"`
}
// UploadImg 上传图片
// @see https://developer.work.weixin.qq.com/document/path/90256
func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) {
@@ -69,3 +79,26 @@ func (r *Client) UploadTempFile(filename string, mediaType string) (*UploadTempF
}
return result, nil
}
// UploadAttachment 上传附件资源
// @see https://developer.work.weixin.qq.com/document/path/95098
// @mediaType 媒体文件类型分别有图片image、视频video、普通文件file
// @attachment_type 附件类型不同的附件类型用于不同的场景。1朋友圈2:商品图册
func (r *Client) UploadAttachment(filename string, mediaType string, attachmentType int) (*UploadAttachmentResponse, error) {
var (
accessToken string
err error
)
if accessToken, err = r.GetAccessToken(); err != nil {
return nil, err
}
var response []byte
if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType)); err != nil {
return nil, err
}
result := &UploadAttachmentResponse{}
if err = util.DecodeWithError(response, result, "UploadAttachment"); err != nil {
return nil, err
}
return result, nil
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/silenceper/wechat/v2/credential"
"github.com/silenceper/wechat/v2/work/addresslist"
"github.com/silenceper/wechat/v2/work/appchat"
"github.com/silenceper/wechat/v2/work/checkin"
"github.com/silenceper/wechat/v2/work/config"
"github.com/silenceper/wechat/v2/work/context"
"github.com/silenceper/wechat/v2/work/externalcontact"
@@ -85,3 +86,8 @@ func (wk *Work) GetAppChat() *appchat.Client {
func (wk *Work) GetInvoice() *invoice.Client {
return invoice.NewClient(wk.ctx)
}
// GetCheckin 获取打卡接口实例
func (wk *Work) GetCheckin() *checkin.Client {
return checkin.NewClient(wk.ctx)
}