gosdk/weixin/oa_sdk.go

205 lines
4.7 KiB
Go
Raw Normal View History

2024-10-21 17:27:46 +08:00
package weixin
import (
"bytes"
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"io"
2024-12-11 18:52:08 +08:00
"mime/multipart"
2024-10-21 17:27:46 +08:00
"net/http"
2024-12-11 18:52:08 +08:00
"time"
2024-10-21 17:27:46 +08:00
)
type OaSdk struct {
BaseSdk
}
func NewOaSdk(appid, secret string) *OaSdk {
return &OaSdk{
BaseSdk{
appid: appid,
secret: secret,
},
}
}
func (o *OaSdk) GetQrCode(sceneStr string) (string, error) {
params := make(map[string]interface{})
params["expire_seconds"] = 1728000 //20天
params["action_name"] = "QR_STR_SCENE"
params["action_info"] = map[string]interface{}{
"scene": map[string]interface{}{
"scene_str": sceneStr, // sceneStr不超过64个字符
},
}
marshal, _ := json.Marshal(params)
accessToken, err := o.getAccessToken()
if err != nil {
return "", err
}
2024-10-29 16:25:34 +08:00
url := fmt.Sprintf("%s?access_token=%s", oaQrCodeCreateUrl, accessToken)
2024-10-21 18:05:01 +08:00
res, err := http.Post(url, "application/json", bytes.NewBuffer(marshal))
if err != nil {
return "", err
}
2024-10-21 17:27:46 +08:00
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
g := gjson.ParseBytes(body)
qrcodeUrl := g.Get("url").String()
if qrcodeUrl == "" {
return "", fmt.Errorf("%d:%s", g.Get("errcode").Int(), g.Get("errmsg").String())
}
return qrcodeUrl, nil
}
2024-12-05 11:28:49 +08:00
func (o *OaSdk) GetUserInfoByCode(code string) (*UserInfo, error) {
return o.getUserInfoByCode(code, true)
}
func (o *OaSdk) GetUserInfoByCodeNoAuth(code string) (*UserInfo, error) {
return o.getUserInfoByCode(code, false)
2024-10-21 17:27:46 +08:00
}
func (o *OaSdk) GetUserInfoByOpenid(openid string) (*UserInfo, error) {
accessToken, err := o.getAccessToken()
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s?access_token=%s&openid=%s&lang=zh_CN",
userInfoByOpenid, accessToken, openid)
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
g := gjson.ParseBytes(body)
errcode := g.Get("errcode").Int()
if errcode != 0 {
return nil, fmt.Errorf("%d:%s", errcode, g.Get("errmsg").String())
}
user := new(UserInfo)
user.Unionid = g.Get("unionid").String()
user.Nickname = g.Get("nickname").String()
user.Province = g.Get("province").String()
user.City = g.Get("city").String()
user.Country = g.Get("country").String()
user.HeadUrl = g.Get("headimgurl").String()
user.Sex = int(g.Get("sex").Int())
user.Openid = openid
return user, nil
}
2024-12-10 19:19:12 +08:00
2024-12-11 18:52:08 +08:00
func (o *OaSdk) UploadFileByByte(file []byte, ext string) (string, string, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
formFile, err := writer.CreateFormFile("file", fmt.Sprintf("%d%s", time.Now().Unix(), ext))
if err != nil {
return "", "", err
}
_, err = formFile.Write(file)
if err != nil {
return "", "", err
}
err = writer.Close()
if err != nil {
return "", "", err
}
2024-12-11 19:23:29 +08:00
token, err := o.getAccessToken()
if err != nil {
return "", "", err
}
postType := ""
if ext == ".jpg" || ext == ".png" || ext == ".gif" || ext == ".jpeg" {
postType = "image"
} else if ext == ".mp4" {
postType = "video"
} else if ext == ".mp3" {
postType = "voice"
}
if postType == "" {
return "", "", fmt.Errorf("不支持的文件类型")
}
res, err := http.Post(
fmt.Sprintf("%s?access_token=%s&type=%s", UploadOaMedia, token, postType),
writer.FormDataContentType(),
body,
)
2024-12-11 18:52:08 +08:00
if err != nil {
return "", "", err
}
resBody, err := io.ReadAll(res.Body)
if err != nil {
return "", "", err
}
g := gjson.ParseBytes(resBody)
errCode := g.Get("errcode").Int()
if errCode != 0 {
return "", "", fmt.Errorf("%d:%s", errCode, g.Get("errmsg").String())
}
fileType := g.Get("type").String()
fileId := g.Get("media_id").String()
return fileType, fileId, nil
}
2024-12-10 19:19:12 +08:00
func (o *OaSdk) CreateMenu(buttons []*OaMenuButton) error {
accessToken, err := o.getAccessToken()
if err != nil {
return err
}
bodyMp := make(map[string]interface{})
bodyMp["button"] = buttons
bodyBytes, _ := json.Marshal(bodyMp)
url := fmt.Sprintf("%s?access_token=%s", CreateOaMenu, accessToken)
res, err := http.Post(url, "application/json", bytes.NewBuffer(bodyBytes))
if err != nil {
return err
}
all, err := io.ReadAll(res.Body)
if err != nil {
return err
}
g := gjson.ParseBytes(all)
errCode := g.Get("errcode").Int()
if errCode != 0 {
return fmt.Errorf("%d:%s", errCode, g.Get("errmsg").String())
}
return nil
}
func (o *OaSdk) DeleteMenu() error {
accessToken, err := o.getAccessToken()
if err != nil {
return err
}
url := fmt.Sprintf("%s?access_token=%s", DeleteOaMenu, accessToken)
res, err := http.Get(url)
if err != nil {
return err
}
all, err := io.ReadAll(res.Body)
if err != nil {
return err
}
g := gjson.ParseBytes(all)
errCode := g.Get("errcode").Int()
if errCode != 0 {
return fmt.Errorf("%d:%s", errCode, g.Get("errmsg").String())
}
return nil
}