update service wechatoffice

master
李光春 2 years ago
parent a0ed28bf12
commit 05a9a0041d

@ -1,40 +0,0 @@
package wechatminiprogram
import (
"crypto/sha1"
"fmt"
"gopkg.in/dtapps/go-library.v3/utils/gorandom"
"io"
"time"
)
type ShareResponse struct {
AppId string `json:"app_id"`
NonceStr string `json:"nonce_str"`
Timestamp int64 `json:"timestamp"`
Url string `json:"url"`
RawString string `json:"raw_string"`
Signature string `json:"signature"`
}
type ShareResult struct {
Result ShareResponse // 结果
Err error // 错误
}
func NewShareResult(result ShareResponse, err error) *ShareResult {
return &ShareResult{Result: result, Err: err}
}
func (app *App) Share(url string) *ShareResult {
var response ShareResponse
response.AppId = app.AppId
response.NonceStr = gorandom.Alphanumeric(32)
response.Timestamp = time.Now().Unix()
response.Url = url
response.RawString = fmt.Sprintf("jsapi_ticket=%v&noncestr=%v&timestamp=%v&url=%v", app.JsapiTicket, response.NonceStr, response.Timestamp, response.Url)
t := sha1.New()
_, err := io.WriteString(t, response.RawString)
response.Signature = fmt.Sprintf("%x", t.Sum(nil))
return NewShareResult(response, err)
}

@ -3,15 +3,10 @@ package wechatoffice
import (
"encoding/json"
"fmt"
"net/http"
)
// AuthCode2Session 请求参数
type AuthCode2Session struct {
JsCode string `json:"js_code"`
}
// AuthCode2SessionResult 返回参数
type AuthCode2SessionResult struct {
type AuthCode2SessionResponse struct {
OpenId string `json:"openid"` // 用户唯一标识
SessionKey string `json:"session_key"` // 会话密钥
Unionid string `json:"unionid"` // 用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回
@ -19,14 +14,21 @@ type AuthCode2SessionResult struct {
Errmsg string `json:"errmsg"` // 错误信息
}
func (app *App) AuthCode2Session(param AuthCode2Session) (result AuthCode2SessionResult, err error) {
// request
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", app.AppId, app.AppSecret, param.JsCode), map[string]interface{}{}, "GET")
if err != nil {
return
}
if err = json.Unmarshal(body, &result); err != nil {
return
}
return
type AuthCode2SessionResult struct {
Result AuthCode2SessionResponse // 结果
Byte []byte // 内容
Err error // 错误
}
func NewAuthCode2SessionResult(result AuthCode2SessionResponse, byte []byte, err error) *AuthCode2SessionResult {
return &AuthCode2SessionResult{Result: result, Byte: byte, Err: err}
}
func (app *App) AuthCode2Session(jsCode string) *AuthCode2SessionResult {
// 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", app.AppId, app.AppSecret, jsCode), map[string]interface{}{}, http.MethodGet)
// 定义
var response AuthCode2SessionResponse
err = json.Unmarshal(body, &response)
return NewAuthCode2SessionResult(response, body, err)
}

@ -1,51 +0,0 @@
package wechatoffice
import (
"fmt"
)
type BusinessGetLiveInfoResult struct {
Errcode int `json:"errcode"` // // 错误码0代表成功1代表未创建直播间
Errmsg string `json:"errmsg"` // 错误信息
Total int `json:"total"`
RoomInfo []struct {
Name string `json:"name"` // 直播间名称
Roomid int `json:"roomid"` // 直播间ID
CoverImg string `json:"cover_img"`
ShareImg string `json:"share_img"`
LiveStatus int `json:"live_status"`
StartTime int `json:"start_time"`
EndTime int `json:"end_time"`
AnchorName string `json:"anchor_name"`
Goods []struct {
CoverImg string `json:"cover_img"`
Url string `json:"url"`
Name string `json:"name"`
Price int `json:"price"`
Price2 int `json:"price2"`
PriceType int `json:"price_type"`
GoodsId int `json:"goods_id"`
ThirdPartyAppid string `json:"third_party_appid"`
} `json:"goods"`
LiveType int `json:"live_type"`
CloseLike int `json:"close_like"`
CloseGoods int `json:"close_goods"`
CloseComment int `json:"close_comment"`
CloseKf int `json:"close_kf"`
CloseReplay int `json:"close_replay"`
IsFeedsPublic int `json:"is_feeds_public"`
CreaterOpenid string `json:"creater_openid"`
FeedsImg string `json:"feeds_img"`
} `json:"room_info"`
}
// BusinessGetLiveInfo 获取直播间列表
// 调用此接口获取直播间列表及直播间信息
// https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/liveplayer/studio-api.html
func (app *App) BusinessGetLiveInfo(notMustParams ...Params) (body []byte, err error) {
// 参数
params := app.NewParamsWith(notMustParams...)
// 请求
body, err = app.request(fmt.Sprintf("https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=%s", app.AccessToken), params, "POST")
return
}

@ -3,6 +3,7 @@ package wechatoffice
import (
"encoding/json"
"fmt"
"net/http"
)
type AuthGetAccessTokenResponse struct {
@ -27,7 +28,7 @@ func NewAuthGetAccessTokenResult(result AuthGetAccessTokenResponse, byte []byte,
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
func (app *App) AuthGetAccessToken() *AuthGetAccessTokenResult {
// request
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", app.AppId, app.AppSecret), map[string]interface{}{}, "GET")
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", app.AppId, app.AppSecret), map[string]interface{}{}, http.MethodGet)
// 定义
var response AuthGetAccessTokenResponse
err = json.Unmarshal(body, &response)

@ -3,6 +3,7 @@ package wechatoffice
import (
"encoding/json"
"fmt"
"net/http"
)
type GetCallBackIpResponse struct {
@ -24,7 +25,7 @@ func NewGetCallBackIpResult(result GetCallBackIpResponse, byte []byte, err error
// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_the_WeChat_server_IP_address.html#2.%20%E8%8E%B7%E5%8F%96%E5%BE%AE%E4%BF%A1callback%20IP%E5%9C%B0%E5%9D%80
func (app *App) GetCallBackIp() *GetCallBackIpResult {
// 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=%s", app.AccessToken), map[string]interface{}{}, "GET")
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=%s", app.AccessToken), map[string]interface{}{}, http.MethodGet)
// 定义
var response GetCallBackIpResponse
err = json.Unmarshal(body, &response)

@ -3,6 +3,7 @@ package wechatoffice
import (
"encoding/json"
"fmt"
"net/http"
)
type GetTicketRespons struct {
@ -26,7 +27,7 @@ func NewGetTicketResult(result GetTicketRespons, byte []byte, err error) *GetTic
// https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
func (app *App) GetTicket(accessToken, Type string) *GetTicketResult {
// request
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=%s", accessToken, Type), map[string]interface{}{}, "GET")
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=%s", accessToken, Type), map[string]interface{}{}, http.MethodGet)
// 定义
var response GetTicketRespons
err = json.Unmarshal(body, &response)

@ -1,16 +1,36 @@
package wechatoffice
import (
"encoding/json"
"fmt"
"net/http"
)
type MessageTemplateSendResponse struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
Msgid int `json:"msgid"`
}
type MessageTemplateSendResult struct {
Result MessageTemplateSendResponse // 结果
Byte []byte // 内容
Err error // 错误
}
func NewMessageTemplateSendResult(result MessageTemplateSendResponse, byte []byte, err error) *MessageTemplateSendResult {
return &MessageTemplateSendResult{Result: result, Byte: byte, Err: err}
}
// MessageTemplateSend 模板消息
// https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
func (app *App) MessageTemplateSend(notMustParams ...Params) (body []byte, err error) {
func (app *App) MessageTemplateSend(notMustParams ...Params) *MessageTemplateSendResult {
// 参数
params := app.NewParamsWith(notMustParams...)
// 请求
body, err = app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s", app.AccessToken), params, http.MethodPost)
return
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s", app.AccessToken), params, http.MethodPost)
// 定义
var response MessageTemplateSendResponse
err = json.Unmarshal(body, &response)
return NewMessageTemplateSendResult(response, body, err)
}

@ -1,9 +1,7 @@
package wechatoffice
import (
"encoding/json"
"fmt"
"log"
"net/url"
)
@ -16,60 +14,5 @@ func (app *App) Oauth2(redirectUri, state string) string {
param.Add("response_type", "code") // 返回类型
param.Add("scope", "snsapi_userinfo") // 应用授权作用域
param.Add("state", state) // 重定向后会带上state参数开发者可以填写a-zA-Z0-9的参数值最多128字节
log.Println(fmt.Sprintf("https://open.weixin.qq.com/connect/oauth2/authorize?%s#wechat_redirect", param.Encode()))
return fmt.Sprintf("https://open.weixin.qq.com/connect/oauth2/authorize?%s#wechat_redirect", param.Encode())
}
// Oauth2AccessTokenResult 返回参数
type Oauth2AccessTokenResult struct {
AccessToken string `json:"access_token"` // 网页授权接口调用凭证,注意此access_token与基础支持的access_token不同
ExpiresIn int `json:"expires_in"` // access_token接口调用凭证超时时间单位
RefreshToken string `json:"refresh_token"` // 用户刷新access_token
Openid string `json:"openid"` // 用户唯一标识
Scope string `json:"scope"` // 用户授权的作用域,使用逗号(,)分隔
}
// Oauth2AccessToken 通过code换取网页授权access_token
// https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
func (app *App) Oauth2AccessToken(code string) (result Oauth2AccessTokenResult, err error) {
// 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", app.AppId, app.AppSecret, code), map[string]interface{}{}, "GET")
if err != nil {
return result, err
}
// 解析
err = json.Unmarshal(body, &result)
if err != nil {
return result, err
}
return result, err
}
// Oauth2UserinfoResult 返回参数
type Oauth2UserinfoResult struct {
Openid string `json:"openid"` // 用户的唯一标识
Nickname string `json:"nickname"` // 用户昵称
Sex int `json:"sex"` // 用户的性别值为1时是男性值为2时是女性值为0时是未知
Province string `json:"province"` // 用户个人资料填写的省份
City string `json:"city"` // 普通用户个人资料填写的城市
Country string `json:"country"` // 国家如中国为CN
Headimgurl string `json:"headimgurl"` // 用户头像最后一个数值代表正方形头像大小有0、46、64、96、132数值可选0代表640*640正方形头像用户没有头像时该项为空。若用户更换头像原有头像URL将失效。
Privilege []string `json:"privilege"` // 用户特权信息json 数组如微信沃卡用户为chinaunicom
Unionid string `json:"unionid,omitempty"` // 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
}
// Oauth2Userinfo 拉取用户信息(需scope为 snsapi_userinfo)
// https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
func (app *App) Oauth2Userinfo(accessToken, openid string) (result Oauth2UserinfoResult, err error) {
// 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN", accessToken, openid), map[string]interface{}{}, "GET")
if err != nil {
return result, err
}
// 解析
err = json.Unmarshal(body, &result)
if err != nil {
return result, err
}
return result, err
}

@ -8,7 +8,7 @@ import (
"time"
)
type ShareResult struct {
type ShareResponse struct {
AppId string `json:"app_id"`
NonceStr string `json:"nonce_str"`
Timestamp int64 `json:"timestamp"`
@ -17,14 +17,24 @@ type ShareResult struct {
Signature string `json:"signature"`
}
func (app *App) Share(url string) (result ShareResult) {
result.AppId = app.AppId
result.NonceStr = gorandom.Alphanumeric(32)
result.Timestamp = time.Now().Unix()
result.Url = url
result.RawString = fmt.Sprintf("jsapi_ticket=%v&noncestr=%v&timestamp=%v&url=%v", app.JsapiTicket, result.NonceStr, result.Timestamp, result.Url)
type ShareResult struct {
Result ShareResponse // 结果
Err error // 错误
}
func NewShareResult(result ShareResponse, err error) *ShareResult {
return &ShareResult{Result: result, Err: err}
}
func (app *App) Share(url string) *ShareResult {
var response ShareResponse
response.AppId = app.AppId
response.NonceStr = gorandom.Alphanumeric(32)
response.Timestamp = time.Now().Unix()
response.Url = url
response.RawString = fmt.Sprintf("jsapi_ticket=%v&noncestr=%v&timestamp=%v&url=%v", app.JsapiTicket, response.NonceStr, response.Timestamp, response.Url)
t := sha1.New()
io.WriteString(t, result.RawString)
result.Signature = fmt.Sprintf("%x", t.Sum(nil))
return result
_, err := io.WriteString(t, response.RawString)
response.Signature = fmt.Sprintf("%x", t.Sum(nil))
return NewShareResult(response, err)
}

@ -0,0 +1,36 @@
package wechatoffice
import (
"encoding/json"
"fmt"
"net/http"
)
type Oauth2AccessTokenResponse struct {
AccessToken string `json:"access_token"` // 网页授权接口调用凭证,注意此access_token与基础支持的access_token不同
ExpiresIn int `json:"expires_in"` // access_token接口调用凭证超时时间单位
RefreshToken string `json:"refresh_token"` // 用户刷新access_token
Openid string `json:"openid"` // 用户唯一标识
Scope string `json:"scope"` // 用户授权的作用域,使用逗号(,)分隔
}
type Oauth2AccessTokenResult struct {
Result Oauth2AccessTokenResponse // 结果
Byte []byte // 内容
Err error // 错误
}
func NewOauth2AccessTokenResult(result Oauth2AccessTokenResponse, byte []byte, err error) *Oauth2AccessTokenResult {
return &Oauth2AccessTokenResult{Result: result, Byte: byte, Err: err}
}
// Oauth2AccessToken 通过code换取网页授权access_token
// https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
func (app *App) Oauth2AccessToken(code string) *Oauth2AccessTokenResult {
// 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", app.AppId, app.AppSecret, code), map[string]interface{}{}, http.MethodGet)
// 定义
var response Oauth2AccessTokenResponse
err = json.Unmarshal(body, &response)
return NewOauth2AccessTokenResult(response, body, err)
}

@ -0,0 +1,40 @@
package wechatoffice
import (
"encoding/json"
"fmt"
"net/http"
)
type SnsUserinfoResponse struct {
Openid string `json:"openid"` // 用户的唯一标识
Nickname string `json:"nickname"` // 用户昵称
Sex int `json:"sex"` // 用户的性别值为1时是男性值为2时是女性值为0时是未知
Province string `json:"province"` // 用户个人资料填写的省份
City string `json:"city"` // 普通用户个人资料填写的城市
Country string `json:"country"` // 国家如中国为CN
Headimgurl string `json:"headimgurl"` // 用户头像最后一个数值代表正方形头像大小有0、46、64、96、132数值可选0代表640*640正方形头像用户没有头像时该项为空。若用户更换头像原有头像URL将失效。
Privilege []string `json:"privilege"` // 用户特权信息json 数组如微信沃卡用户为chinaunicom
Unionid string `json:"unionid,omitempty"` // 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
}
type SnsUserinfoResult struct {
Result SnsUserinfoResponse // 结果
Byte []byte // 内容
Err error // 错误
}
func NewSnsUserinfoResult(result SnsUserinfoResponse, byte []byte, err error) *SnsUserinfoResult {
return &SnsUserinfoResult{Result: result, Byte: byte, Err: err}
}
// SnsUserinfo 拉取用户信息(需scope为 snsapi_userinfo)
// https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
func (app *App) SnsUserinfo(accessToken, openid string) *SnsUserinfoResult {
// 请求
body, err := app.request(fmt.Sprintf("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN", accessToken, openid), map[string]interface{}{}, http.MethodGet)
// 定义
var response SnsUserinfoResponse
err = json.Unmarshal(body, &response)
return NewSnsUserinfoResult(response, body, err)
}
Loading…
Cancel
Save