1 Star 1 Fork 0

Ostaer/go-workwx

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
models.go 6.15 KB
Copy Edit Raw Blame History
liran authored 2020-06-08 21:55 +08:00 . 增加扫码登陆
package workwx
import (
"encoding/json"
"net/url"
"strconv"
"strings"
)
type reqAccessToken struct {
CorpID string
CorpSecret string
}
var _ urlValuer = reqAccessToken{}
func (x reqAccessToken) intoURLValues() url.Values {
return url.Values{
"corpid": {x.CorpID},
"corpsecret": {x.CorpSecret},
}
}
type respCommon struct {
ErrCode int64 `json:"errcode"`
ErrMsg string `json:"errmsg"`
}
// IsOK 响应体是否为一次成功请求的响应
//
// 实现依据: https://work.weixin.qq.com/api/doc#10013
//
// > 企业微信所有接口,返回包里都有errcode、errmsg。
// > 开发者需根据errcode是否为0判断是否调用成功(errcode意义请见全局错误码)。
// > 而errmsg仅作参考,后续可能会有变动,因此不可作为是否调用成功的判据。
func (x *respCommon) IsOK() bool {
return x.ErrCode == 0
}
type respAccessToken struct {
respCommon
AccessToken string `json:"access_token"`
ExpiresInSecs int64 `json:"expires_in"`
}
// reqMessage 消息发送请求
type reqMessage struct {
ToUser []string
ToParty []string
ToTag []string
ChatID string
AgentID int64
MsgType string
Content map[string]interface{}
IsSafe bool
}
var _ bodyer = reqMessage{}
func (x reqMessage) intoBody() ([]byte, error) {
// fuck
safeInt := 0
if x.IsSafe {
safeInt = 1
}
obj := map[string]interface{}{
"msgtype": x.MsgType,
"agentid": x.AgentID,
"safe": safeInt,
}
// msgtype polymorphism
obj[x.MsgType] = x.Content
// 复用这个结构体,因为是 package-private 的所以这么做没风险
if x.ChatID != "" {
obj["chatid"] = x.ChatID
} else {
obj["touser"] = strings.Join(x.ToUser, "|")
obj["toparty"] = strings.Join(x.ToParty, "|")
obj["totag"] = strings.Join(x.ToTag, "|")
}
result, err := json.Marshal(obj)
if err != nil {
// should never happen unless OOM or similar bad things
// TODO: error_chain
return nil, err
}
return result, nil
}
// respMessageSend 消息发送响应
type respMessageSend struct {
respCommon
InvalidUsers string `json:"invaliduser"`
InvalidParties string `json:"invalidparty"`
InvalidTags string `json:"invalidtag"`
}
type reqUserInfoGet struct {
Code string
}
var _ urlValuer = reqUserInfoGet{}
func (x reqUserInfoGet) intoURLValues() url.Values {
return url.Values{
"code": {x.Code},
}
}
type respUserInfoGet struct {
respCommon
UserId string `json:"UserId"`
OpenId string `json:"OpenId"`
}
type reqUserGet struct {
UserID string
}
var _ urlValuer = reqUserGet{}
func (x reqUserGet) intoURLValues() url.Values {
return url.Values{
"userid": {x.UserID},
}
}
// respUserDetail 成员详细信息的公共字段
type respUserDetail struct {
UserID string `json:"userid"`
Name string `json:"name"`
DeptIDs []int64 `json:"department"`
DeptOrder []uint32 `json:"order"`
Position string `json:"position"`
Mobile string `json:"mobile"`
Gender string `json:"gender"`
Email string `json:"email"`
IsLeaderInDept []int `json:"is_leader_in_dept"`
AvatarURL string `json:"avatar"`
Telephone string `json:"telephone"`
IsEnabled int `json:"enable"`
Alias string `json:"alias"`
Status int `json:"status"`
QRCodeURL string `json:"qr_code"`
// TODO: extattr external_profile external_position
}
// respUserGet 读取成员响应
type respUserGet struct {
respCommon
respUserDetail
}
// reqUserList 部门成员请求
type reqUserList struct {
DeptID int64
FetchChild bool
}
var _ urlValuer = reqUserList{}
func (x reqUserList) intoURLValues() url.Values {
var fetchChild int64
if x.FetchChild {
fetchChild = 1
}
return url.Values{
"department_id": {strconv.FormatInt(x.DeptID, 10)},
"fetch_child": {strconv.FormatInt(fetchChild, 10)},
}
}
// respUsersByDeptID 部门成员详情响应
type respUserList struct {
respCommon
Users []*respUserDetail `json:"userlist"`
}
type reqDeptList struct {
HaveID bool
ID int64
}
var _ urlValuer = reqDeptList{}
func (x reqDeptList) intoURLValues() url.Values {
if !x.HaveID {
return url.Values{}
}
return url.Values{
"id": {strconv.FormatInt(x.ID, 10)},
}
}
// respDeptList 部门列表响应
type respDeptList struct {
respCommon
// TODO: 不要懒惰,把 API 层的类型写好
Department []*DeptInfo `json:"department"`
}
// reqAppchatGet 获取群聊会话请求
type reqAppchatGet struct {
ChatID string
}
var _ urlValuer = reqAppchatGet{}
func (x reqAppchatGet) intoURLValues() url.Values {
return url.Values{
"chatid": {x.ChatID},
}
}
// respAppchatGet 获取群聊会话响应
type respAppchatGet struct {
respCommon
ChatInfo *ChatInfo `json:"chat_info"`
}
// reqAppchatCreate 创建群聊会话请求
type reqAppchatCreate struct {
ChatInfo *ChatInfo
}
var _ bodyer = reqAppchatCreate{}
func (x reqAppchatCreate) intoBody() ([]byte, error) {
result, err := json.Marshal(x.ChatInfo)
if err != nil {
// should never happen unless OOM or similar bad things
// TODO: error_chain
return nil, err
}
return result, nil
}
// respAppchatCreate 创建群聊会话响应
type respAppchatCreate struct {
respCommon
ChatID string `json:"chatid"`
}
// reqMediaUpload 临时素材上传请求
type reqMediaUpload struct {
Type string
Media *Media
}
var _ urlValuer = reqMediaUpload{}
var _ mediaUploader = reqMediaUpload{}
func (x reqMediaUpload) intoURLValues() url.Values {
return url.Values{
"type": {x.Type},
}
}
func (x reqMediaUpload) getMedia() *Media {
return x.Media
}
// respMediaUpload 临时素材上传响应
type respMediaUpload struct {
respCommon
Type string `json:"type"`
MediaID string `json:"media_id"`
CreatedAt string `json:"created_at"`
}
// reqMediaUploadImg 永久图片素材上传请求
type reqMediaUploadImg struct {
Media *Media
}
var _ urlValuer = reqMediaUploadImg{}
var _ mediaUploader = reqMediaUploadImg{}
func (x reqMediaUploadImg) intoURLValues() url.Values {
return url.Values{}
}
func (x reqMediaUploadImg) getMedia() *Media {
return x.Media
}
// respMediaUploadImg 永久图片素材上传响应
type respMediaUploadImg struct {
respCommon
URL string `json:"url"`
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ostaer/go-workwx.git
git@gitee.com:ostaer/go-workwx.git
ostaer
go-workwx
go-workwx
b16c285aee72

Search