wxapp

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2022 License: Apache-2.0 Imports: 24 Imported by: 0

README

介绍

微信小程序服务端SDK,实现了小程序的登录、数据解密、客服消息、订阅消息、生成小程序码、生成小程序shortLink、生成页面Scheme码、生成小程序URL Link等功能。

安装说明

go get gitee.com/haming123/wxapp4go

快速上手

  1. 小程序对象初始化
var WxApp *wxapp.WeixinApp
func main() {
	WxApp = wxapp.NewWeixinApp("appID_string", "app_Secret", wxapp.GetAccessToken)
    //其他业务逻辑
}
  1. 登录微信后获取用户的openid
func HandlerWxLogin(w http.ResponseWriter, r *http.Request) {
	code := r.FormValue("code")
	//获取openid以及Sessionkey
	wxtoken, err := WxApp.GetOpenIdByCode(code)
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}
	w.Write([]byte(wxtoken.OpenId))
}
  1. 解析手机号码数据
func HandlerWxPhone(w http.ResponseWriter, r *http.Request) {
	data := r.FormValue("data")
	iv := r.FormValue("iv")
	ret, err := WxApp.DecodePhoneNumber(data, "填入微信登录时获取的session_key", iv)
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}
	fmt.Fprintf(w, ret.PhoneNumber)
}
  1. 生成小程序码
func HandlerGenQrcode(w http.ResponseWriter, r *http.Request) {
	var param wxapp.ACodeParam
	page := r.FormValue("page");
	scene := r.FormValue("scene");
	param.Path = page
	param.Scene = scene
	param.Width = 430
	raw, err := WxApp.CreateACode(param)
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}

	w.Header().Set("Content-Type","image/gif")
	io.WriteString(w, string(raw))
}

使用中控服务器获取access_token

建议开发者使用中控服务器统一获取和刷新access_token,服务器所使用的access_token均来自于该中控服务器,不应该各自去刷新,否则容易造成冲突,导致access_token覆盖而影响业务。

  1. 实现中控服务器
package main
import (
	wxapp "gitee.com/haming123/wxapp4go"
	"net/http"
)
func HandlerGetAccessToken(w http.ResponseWriter, r *http.Request)  {
	app_id := r.FormValue("app_id")
	app_secret := r.FormValue("app_secret")
	token, err := wxapp.GetAccessToken(app_id, app_secret)
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}
	w.Write([]byte(token))
}
func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/access_token", HandlerGetAccessToken)
	http.ListenAndServe(":8091", mux)
}
  1. 设置getAccessToken函数
var WxApp *wxapp.WeixinApp
func InitWxApp()  {
	WxApp = wxapp.NewWeixinApp("appID_string", "app_Secret", getAccessToken)
}
func getAccessToken(appid string, secret string) (string, error) {
	addr := "http://127.0.0.1:8091/access_token"
	addr = fmt.Sprintf("%s?app_id=%s&app_secret=%s", addr, appid, secret)
	res, err := http.Get(addr)
	if err != nil {
		return "", err
	}

	raw, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		return "", err
	}
	return string(raw), nil
}
  1. 初始化小程序
func main() {
	//初始化微信小程序
	InitWxApp()
    //其他业务逻辑
}

功能列表

//建议使用中控服务器统一获取和刷新 access_token,因此采用自定义逻辑去获取 access_token
func NewWeixinApp(appid string, secret string, fn TokenGetFunc) *WeixinApp {
	return &WeixinApp{AppID: appid, AppSecret: secret, TokenGetter:fn}
}

//小程序数据解密
func (app *WeixinApp)WxDncrypt(rawData, iv string) (string, error) {
	return WxDncrypt(rawData, app.AppSecret, iv)
}

//根据登录凭证code获取opendid以及session_key
func (app *WeixinApp)GetOpenIdByCode(code string) (WxSessionData, error) {
	return GetOpenIdByCode(app.AppID, app.AppSecret, code)
}

//检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据
func (app *WeixinApp)EncryptedCheck(encrypted_msg_hash string) (EncryptedCheckRet, error) {
	access_token := app.getAccessToken()
	return EncryptedCheck(access_token, encrypted_msg_hash)
}

//用户支付完成后,获取该用户的 UnionId,无需用户授权
func  (app *WeixinApp)GetPaidInfo(param GetPaidInfoReq) (GetPaidInfoRet, error) {
	access_token := app.getAccessToken()
	return GetPaidInfo(access_token, param)
}

//code换取用户手机号
func (app *WeixinApp)GetPhoneNumber(code string) (GetPhoneNumberRet, error) {
	access_token := app.getAccessToken()
	return GetPhoneNumber(access_token, code)
}

//解密用户手机号数据
func (app *WeixinApp)DecodePhoneNumber(rawData, key, iv string) (PhoneInfo, error) {
	return DecodePhoneNumber(rawData, key, iv)
}

//检查一段文本是否含有违法违规内容。
func (app *WeixinApp)CheckText(param CheckTextReq) (CheckTextRet, error) {
	access_token := app.getAccessToken()
	return CheckText(access_token, param)
}

//异步校验图片/音频是否含有违法违规内容。
func (app *WeixinApp)CheckMedia(param CheckMediaReq) (CheckMediaRet, error) {
	access_token := app.getAccessToken()
	return CheckMedia(access_token, param)
}

//发送客服消息给用户
func (app *WeixinApp)SendCustomMessage(param CustomerMessage) error {
	access_token := app.getAccessToken()
	return SendCustomMessage(access_token, param)
}

//发送客服消息给用户:文本消息
func (app *WeixinApp)SendCustomText(touser string, text string) error {
	access_token := app.getAccessToken()
	return SendCustomText(access_token, touser, text)
}

//发送客服消息给用户:图片消息
func (app *WeixinApp)SendCustomImage(touser string, media_id string) error {
	access_token := app.getAccessToken()
	return SendCustomImage(access_token, touser, media_id)
}

//发送客服消息给用户:图文链接
func (app *WeixinApp)SendCustomLink(touser string, title,description,url,thumb_url string) error {
	access_token := app.getAccessToken()
	return SendCustomLink(access_token, touser, title,description,url,thumb_url)
}

//发送客服消息给用户:小程序卡片
func (app *WeixinApp)SendCustomAppPage(touser string, title,pagepath,thumb_media_id string) error {
	access_token := app.getAccessToken()
	return SendCustomAppPage(access_token, touser, title,pagepath,thumb_media_id)
}

//下发客服当前输入状态给用户
func (app *WeixinApp)SendCustomTyping(touser, command string) error {
	access_token := app.getAccessToken()
	return SendCustomTyping(access_token, touser, command)
}

//把媒体文件上传到微信服务器,目前仅支持图片
func (app *WeixinApp)UploadTempMedia(file_path string) (UploadTempMediaRet, error) {
	access_token := app.getAccessToken()
	return UploadTempMedia(access_token, file_path)
}

//获取客服消息内的临时素材。即下载临时的多媒体文件。目前小程序仅支持下载图片文件。
func (app *WeixinApp)GetTempMedia(media_id string) ([]byte, error) {
	access_token := app.getAccessToken()
	return GetTempMedia(access_token, media_id)
}

//发送微信订阅消息
func (app *WeixinApp)SendSubscribeMsg(param SubscribeMsgReq) error {
	access_token := app.getAccessToken()
	return SendSubscribeMsg(access_token, param)
}

//创建被分享动态消息或私密消息的 activity_id,
func (app *WeixinApp)CreateActivity(userid string, is_unionid bool) (CreateActivityRet, error) {
	access_token := app.getAccessToken()
	return CreateActivity(access_token, userid, is_unionid)
}

//修改被分享的动态消息
func (app *WeixinApp)UpdateActivity(param UpdateActivityReq) error {
	access_token := app.getAccessToken()
	return UpdateActivity(access_token, param)
}

//通过该接口生成的小程序码,永久有效,有数量限制
func (app *WeixinApp)CreateACode(param ACodeParam) ([]byte, error) {
	access_token := app.getAccessToken()
	return CreateACode(access_token, param)
}

//通过该接口生成的小程序码,永久有效,数量暂无限制
func (app *WeixinApp)CreateACodeUnlimited(param ACodeParam) ([]byte, error) {
	access_token := app.getAccessToken()
	return CreateACodeUnlimited(access_token, param)
}

//生成页面二维码
func (app *WeixinApp)CreateQCode(page string, width int) ([]byte, error) {
	access_token := app.getAccessToken()
	return CreateQCode(access_token, page, width)
}

//生成小程序shortLink
func (app *WeixinApp)CreateShortLink(param ShortLinkReq) (string, error) {
	access_token := app.getAccessToken()
	return CreateShortLink(access_token, param)
}

//生成临时shortLink
func (app *WeixinApp)CreateShortLinkTemp(page_url string, page_title string) (string, error) {
	access_token := app.getAccessToken()
	return CreateShortLinkTemp(access_token, page_url, page_title)
}

//生成永久shortLink
func (app *WeixinApp)CreateShortLinkPermanent(page_url string, page_title string) (string, error) {
	access_token := app.getAccessToken()
	return CreateShortLinkPermanent(access_token, page_url, page_title)
}

//生成页面Scheme码
func (app *WeixinApp)CreateUrlScheme(param WxUrlSchemeReq) (string, error) {
	access_token := app.getAccessToken()
	return CreateUrlScheme(access_token, param)
}

//查询小程序 scheme 码,及长期有效 quota
func (app *WeixinApp)GetSchemeInfo(scheme string) (SchemeQueryRet, error) {
	access_token := app.getAccessToken()
	return GetSchemeInfo(access_token, scheme)
}

//生成小程序URL Link,
func (app *WeixinApp)CreateUrlLink(param UrlLinkReq) (string, error) {
	access_token := app.getAccessToken()
	return CreateUrlLink(access_token, param)
}

//查询小程序 url_link 配置,及长期有效 quota
func (app *WeixinApp)GetUrlLinkInfo(url_link string) (UrlLinkQueryRet, error) {
	access_token := app.getAccessToken()
	return GetUrlLinkInfo(access_token, url_link)
}

//用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
func (app *WeixinApp)GenIMUserSig(sdkappid int, key string, identifier string, expire int) (string, error) {
	return GenIMUserSig(sdkappid, key, identifier, expire)
}

Documentation

Index

Constants

View Source
const ENV_D = "develop"
View Source
const ENV_R = "release"
View Source
const ENV_T = "trial"
View Source
const FLUSH_TOKEN_TM int64 = 240

access_token 的有效期通过返回的 expires_in 来传达,目前是7200秒之内的值,中控服务器需要根据这个有效时间提前去刷新 在刷新过程中,中控服务器可对外继续输出的老 access_token,此时公众平台后台会保证在5分钟内,新老 access_token 都可用 设置240秒(<5分钟)刷新一次,确保能够获取有效的access_token

Variables

This section is empty.

Functions

func AesCBCDncrypt

func AesCBCDncrypt(encryptData, key, iv []byte) ([]byte, error)

AES-128-CBC解密

func CreateACode

func CreateACode(access_token string, param ACodeParam) ([]byte, error)

获取小程序码 适用于需要的码数量较少的业务场景。 通过该接口生成的小程序码,永久有效,有数量限制

func CreateACodeUnlimited

func CreateACodeUnlimited(access_token string, param ACodeParam) ([]byte, error)

获取小程序码, 适用于需要的码数量极多的业务场景。 通过该接口生成的小程序码,永久有效,数量暂无限制

func CreateQCode

func CreateQCode(access_token string, page string, width int) ([]byte, error)

生成页面二维码 page:扫码进入的小程序页面路径,最大长度 128 字节 width:二维码的宽度,单位 px。最小 280px,最大 1280px

func CreateShortLink(access_token string, param ShortLinkReq) (string, error)

获取小程序 Short Link, 适用于微信内拉起小程序的业务场景。 通过该接口,可以选择生成到期失效和永久有效的小程序短链

func CreateShortLinkPermanent

func CreateShortLinkPermanent(access_token string, page_url string, page_title string) (string, error)

func CreateShortLinkTemp

func CreateShortLinkTemp(access_token string, page_url string, page_title string) (string, error)
func CreateUrlLink(access_token string, param UrlLinkReq) (string, error)

获取小程序 URL Link, 适用于短信、邮件、网页、微信内等拉起小程序的业务场景 通过该接口,可以选择生成到期失效和永久有效的小程序链接,有数量限制

func CreateUrlScheme

func CreateUrlScheme(access_token string, param WxUrlSchemeReq) (string, error)

生成页面Scheme码 适用于短信、邮件、外部网页、微信内等拉起小程序的业务场景。 通过该接口,可以选择生成到期失效和永久有效的小程序码,有数量限制

func GenIMUserSig

func GenIMUserSig(sdkappid int, key string, identifier string, expire int) (string, error)

*

*【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
* sdkappid - 应用id
* key - 计算 usersig 用的加密密钥,控制台可获取
* userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
* expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。

func GenSessionString

func GenSessionString(openid string, hash_key string) string

func GetAccessToken

func GetAccessToken(appid string, secret string) (string, error)

func GetNonceStr

func GetNonceStr(n int) string

func GetPluginPid

func GetPluginPid(access_token string, code string) (string, error)

通过 wx.pluginLogin 接口获得插件用户标志凭证 code 后传到开发者服务器,开发者服务器调用此接口换取插件用户的唯一标识 openpid。

func GetTempMedia

func GetTempMedia(access_token string, media_id string) ([]byte, error)

获取客服消息内的临时素材。即下载临时的多媒体文件。目前小程序仅支持下载图片文件。 如果调用成功,会直接返回图片二进制内容,如果请求失败,会返回 JSON 格式的数据。

func PKCS7UnPadding

func PKCS7UnPadding(origData []byte) []byte

解除KCS#7填充

func PostFileByFormData

func PostFileByFormData(url string, params map[string]string, file_path string, file_key string) (string, error)

使用multipart/form-data发送文件

func SendCustomAppPage

func SendCustomAppPage(access_token string, touser string, title, pagepath, thumb_media_id string) error

func SendCustomImage

func SendCustomImage(access_token string, touser string, media_id string) error
func SendCustomLink(access_token string, touser string, title, description, url, thumb_url string) error

func SendCustomMessage

func SendCustomMessage(access_token string, param CustomerMessage) error

发送客服消息给用户

func SendCustomText

func SendCustomText(access_token string, touser string, text string) error

func SendCustomTyping

func SendCustomTyping(access_token string, touser, command string) error

下发客服当前输入状态给用户

func SendSubscribeMsg

func SendSubscribeMsg(access_token string, param SubscribeMsgReq) error

发送微信订阅消息

func SignSHA1

func SignSHA1(signstr string) string

func UpdateActivity

func UpdateActivity(access_token string, param UpdateActivityReq) error

修改被分享的动态消息 消息有两个状态(target_state),分别有其对应的文字内容和颜色。文字内容模板和颜色不支持变更。 0 "成员正在加入,当前 {member_count}/{room_limit} 人" 1 "已开始"

func WeixinCheck

func WeixinCheck(p WeixinParam) string

消息推送接入请求验证

func WxApiGet

func WxApiGet(wx_addr string) ([]byte, error)

func WxApiPost

func WxApiPost(wx_addr string, data []byte) ([]byte, error)

func WxApiPostStruct

func WxApiPostStruct(wx_addr string, param interface{}) ([]byte, error)

func WxDncrypt

func WxDncrypt(rawData, key, iv string) (string, error)

解密算法如下: 1)对称解密使用的算法为 AES-128-CBC,数据采用PKCS#7填充。 2)对称解密的目标密文为 Base64_Decode(encryptedData)。 3)对称解密秘钥 aeskey = Base64_Decode(session_key)。 4)对称解密算法初始向量 为Base64_Decode(iv),其中iv由数据接口返回。

Types

type ACodeParam

type ACodeParam struct {
	//扫码进入的小程序页面路径,最大长度 128 字节
	Path string `json:"path"`
	//最大32个可见字符,只支持数字,大小写英文以及部分特殊字符
	Scene string `json:"scene,omitempty"`
	//检查 page 是否存在,为 true 时 page 必须是已经发布的小程序存在的页面(否则报错);
	//为 false 时允许小程序未发布或者 page 不存在, 但 page 有数量上限
	CheckPath string `json:"check_path,omitempty"`
	//要打开的小程序版本。正式版为 release,体验版为 trial,开发版为 develop
	AppEnv string `json:"env_version,omitempty"`
	//二维码的宽度,单位 px。最小 280px,最大 1280px
	Width int `json:"width,omitempty"`
	//自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
	AutoColor bool `json:"auto_color,omitempty"`
	//auto_color 为 false 时生效,使用 rgb 设置颜色
	LineColor RGBColor `json:"line_color,omitempty"`
	//是否需要透明底色,为 true 时,生成透明底色的小程序码
	IdHyaline bool `json:"is_hyaline,omitempty"`
}

type CMType

type CMType string
const (
	CMText    CMType = "text"
	CMImage   CMType = "image"
	CMLink    CMType = "link"
	CMAppPage CMType = "miniprogrampage"
)

type CheckDetail

type CheckDetail struct {
	//唯一请求标识,标记单次请求
	Strategy string `json:"strategy"`
	//详细检测结果
	Errcode string `json:"errcode"`
	//建议,有risky、pass、review三种值
	Suggest string `json:"suggest"`
	//命中标签枚举值,100 正常;10001 广告;20001 时政;20002 色情;20003 辱骂;20006 违法犯罪;20008 欺诈;20012 低俗;20013 版权;21000 其他
	Label string `json:"label"`
	//唯一请求标识,标记单次请求
	Prob int `json:"prob"`
	//命中的自定义关键词
	Keyword string `json:"keyword"`
}

type CheckMediaReq

type CheckMediaReq struct {
	//要检测的图片或音频的url
	MediaUrl string `json:"media_url"`
	//1:音频;2:图片
	MediaType MediaType `json:"media_type"`
	//接口版本号,2.0版本为固定值2
	Version string `json:"version"`
	//用户的openid(用户需在近两小时访问过小程序)
	OpenId string `json:"openid"`
	//场景枚举值(1 资料;2 评论;3 论坛;4 社交日志)
	Scene SceneType `json:"scene"`
}

type CheckMediaRet

type CheckMediaRet struct {
	WxApiRet
	//唯一请求标识,标记单次请求
	TraceId string `json:"trace_id"`
}

func CheckMedia

func CheckMedia(access_token string, param CheckMediaReq) (CheckMediaRet, error)

异步校验图片/音频是否含有违法违规内容。 异步检测结果在 30 分钟内会推送到你的消息接收服务器

type CheckResult

type CheckResult struct {
	//建议,有risky、pass、review三种值
	Suggest string `json:"suggest"`
	//命中标签枚举值,100 正常;10001 广告;20001 时政;20002 色情;20003 辱骂;20006 违法犯罪;20008 欺诈;20012 低俗;20013 版权;21000 其他
	Label string `json:"label"`
}

type CheckTextReq

type CheckTextReq struct {
	//接口版本号,2.0版本为固定值2
	Version string `json:"version"`
	//用户的openid(用户需在近两小时访问过小程序)
	OpenId string `json:"openid"`
	//场景枚举值(1 资料;2 评论;3 论坛;4 社交日志)
	Scene SceneType `json:"scene"`
	//需检测的文本内容,文本字数的上限为2500字,需使用UTF-8编码
	Content string `json:"content"`
	//用户昵称,需使用UTF-8编码
	NickName string `json:"nickname"`
	//文本标题,需使用UTF-8编码
	Title string `json:"title"`
	//个性签名,该参数仅在资料类场景有效(scene=1),需使用UTF-8编码
	Signature string `json:"signature"`
}

type CheckTextRet

type CheckTextRet struct {
	WxApiRet
	//唯一请求标识,标记单次请求
	TraceId string `json:"trace_id"`
	//综合结果
	Result CheckResult `json:"result"`
	//详细检测结果
	Detail []CheckDetail `json:"detail"`
}

func CheckText

func CheckText(access_token string, param CheckTextReq) (CheckTextRet, error)

检查一段文本是否含有违法违规内容。

type CloudParam

type CloudParam struct {
	//云开发环境
	Env string `json:"env"`
	//静态网站自定义域名,不填则使用默认域名
	Domain string `json:"domain"`
	//云开发静态网站 H5 页面路径,不可携带 query
	Path string `json:"path"`
	//云开发静态网站 H5 页面 query 参数,最大 1024 个字符,只支持数字,大小写英文以及部分特殊字符
	Query string `json:"query"`
	//第三方批量代云开发时必填,表示创建该 env 的 appid
	ResourceAppId string `json:"resource_appid"`
}

type CreateActivityRet

type CreateActivityRet struct {
	WxApiRet
	//动态消息的 ID
	ActivityId string `json:"activity_id"`
	//activity_id 的过期时间戳。默认24小时后过期。
	ExpirationTime int64 `json:"expiration_time"`
}

func CreateActivity

func CreateActivity(access_token string, userid string, is_unionid bool) (CreateActivityRet, error)

创建被分享动态消息或私密消息的 activity_id,

type CreateQCodeReq added in v0.0.2

type CreateQCodeReq struct {
	//扫码进入的小程序页面路径,最大长度 128 字节
	Path string `json:"path"`
	//二维码的宽度,单位 px。最小 280px,最大 1280px
	Width int `json:"width"`
}

type CustomTypingReq

type CustomTypingReq struct {
	//用户的 OpenID
	ToUser string `json:"touser"`
	//命令
	Command string `json:"command"`
}

发送客服消息给用户

type CustomerMessage

type CustomerMessage struct {
	//用户的 OpenID
	ToUser string `json:"touser"`
	//消息类型
	MsgType CMType `json:"msgtype"`
	//文本消息,msgtype="text" 时必填
	Text *MediaText `json:"text,omitempty"`
	//图片消息,msgtype="image" 时必填
	Image *MediaImage `json:"image,omitempty"`
	//图文链接,msgtype="link" 时必填
	Link *MediaLink `json:"link,omitempty"`
	//小程序卡片,msgtype="miniprogrampage" 时必填
	AppPage *MediaWxApp `json:"miniprogrampage,omitempty"`
}

发送客服消息给用户

type EncryptedCheckRet

type EncryptedCheckRet struct {
	WxApiRet
	//是否是合法的数据
	Vaild bool `json:"vaild"`
	//加密数据生成的时间戳
	CreateTime int64 `json:"create_time"`
}

func EncryptedCheck

func EncryptedCheck(access_token string, encrypted_msg_hash string) (EncryptedCheckRet, error)

检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据

type GetPaidInfoReq

type GetPaidInfoReq struct {
	OpenId        string `json:"openid"`
	TransactionId string `json:"transaction_id"`
	MchId         string `json:"mch_id"`
	OuTradeNo     string `json:"out_trade_no"`
}

type GetPaidInfoRet

type GetPaidInfoRet struct {
	WxApiRet
	UnionId string `json:"unionid"`
}

func GetPaidInfo

func GetPaidInfo(access_token string, param GetPaidInfoReq) (GetPaidInfoRet, error)

用户支付完成后,获取该用户的 UnionId,无需用户授权

type GetPhoneNumberRet

type GetPhoneNumberRet struct {
	WxApiRet
	PhoneInfo PhoneInfo `json:"phone_info"`
}

func GetPhoneNumber

func GetPhoneNumber(access_token string, code string) (GetPhoneNumberRet, error)

code换取用户手机号。 每个code只能使用一次,code的有效期为5min

type GetPluginPidRet

type GetPluginPidRet struct {
	WxApiRet
	//插件用户的唯一标识
	OpenpId string `json:"openpid"`
}

type MediaImage

type MediaImage struct {
	//发送的图片的媒体ID,通过 新增素材接口 上传图片文件获得
	MediaId string `json:"media_id"`
}

微信消息内容对应的结构:图片消息

type MediaLink struct {
	//消息标题
	Title string `json:"title"`
	//图文链接消息
	Description string `json:"description"`
	//图文链接消息被点击后跳转的链接
	Url string `json:"url"`
	//图文链接消息的图片链接,支持 JPG、PNG 格式,较好的效果为大图 640 X 320,小图 80 X 80
	ThumbUrl string `json:"thumb_url"`
}

微信消息内容对应的结构:图文链接

type MediaRecive

type MediaRecive struct {
	ToUserName   string `json:"ToUserName"`
	FromUserName string `json:"FromUserName"`
	CreateTime   int64  `json:"CreateTime"`
	MsgType      string `json:"MsgType"`
	MsgId        int64  `json:"MsgId"`
	Content      string `json:"Content"`
	PicUrl       string `json:"PicUrl"`
	MediaId      string `json:"MediaId"`
}

微信用户消息

type MediaText

type MediaText struct {
	//文本消息内容
	Content string `json:"content"`
}

微信消息内容对应的结构:文本消息

type MediaType

type MediaType int
const (
	MTText  MediaType = 1
	MTImage MediaType = 2
)

type MediaWxApp

type MediaWxApp struct {
	//消息标题
	Title string `json:"title"`
	//小程序的页面路径,跟app.json对齐,支持参数,比如pages/index/index?foo=bar
	PagePath string `json:"pagepath"`
	//小程序消息卡片的封面, image 类型的 media_id,通过 新增素材接口 上传图片文件获得
	ThumbMediaId string `json:"thumb_media_id"`
}

微信消息内容对应的结构:小程序卡片

type PhoneInfo

type PhoneInfo struct {
	//用户绑定的手机号(国外手机号会有区号)
	PhoneNumber string `json:"phoneNumber"`
	//没有区号的手机号
	PurePhoneNumber string `json:"purePhoneNumber"`
	//区号
	CountryCode string      `json:"countryCode"`
	WaterMark   WxWaterMark `json:"watermark"`
}

func DecodePhoneNumber

func DecodePhoneNumber(rawData, key, iv string) (PhoneInfo, error)

解密用户手机号数据

type RGBColor

type RGBColor struct {
	R int `json:"r"`
	G int `json:"g"`
	B int `json:"b"`
}

type SceneType

type SceneType int
const (
	SCENE_DOC SceneType = 1
	SCENE_CMT SceneType = 2
	SCENE_FOM SceneType = 3
	SCENE_SOC SceneType = 4
)

type SchemeInfo

type SchemeInfo struct {
	//小程序 appid。
	AppId string `json:"appid"`
	//小程序页面路径。
	Path string `json:"path"`
	//小程序页面query。
	Query string `json:"query"`
	//创建时间,为 Unix 时间戳。
	CreateTime int64 `json:"expire_time"`
	//到期失效时间,为 Unix 时间戳,0 表示永久生效。
	ExpireTime int64 `json:"expire_time"`
	//要打开的小程序版本。正式版为"release",体验版为"trial",开发版为"develop"。
	AppEnv string `json:"env_version"`
}

type SchemeQueryRet

type SchemeQueryRet struct {
	WxApiRet
	SchemeInfo SchemeInfo `json:"scheme_info"`
	SchemeQuot SchemeQuot `json:"scheme_quota"`
}

func GetSchemeInfo

func GetSchemeInfo(access_token string, scheme string) (SchemeQueryRet, error)

查询小程序 scheme 码,及长期有效 quota

type SchemeQuot

type SchemeQuot struct {
	//长期有效 url_link 已生成次数。
	LongTimeUsed int64 `json:"long_time_used"`
	//长期有效 url_link 生成次数上限。
	LongTimeLimit int64 `json:"long_time_limit"`
}

type ShortLinkReq

type ShortLinkReq struct {
	//通过 Short Link 进入的小程序页面路径,必须是已经发布的小程序存在的页面,可携带 query,最大1024个字符
	PageUrl string `json:"page_url"`
	//页面标题,不能包含违法信息,超过20字符会用... 截断代替
	PageTitle string `json:"page_title"`
	//生成的 Short Link 类型,短期有效:false,永久有效:true
	IsPermanent bool `json:"is_permanent"`
}

type ShortLinkRet

type ShortLinkRet struct {
	WxApiRet
	Link string `json:"link"`
}

type SubscribeMsgReq

type SubscribeMsgReq struct {
	//接收者(用户)的 openid
	Touser string `json:"touser"`
	//所需下发的订阅模板id
	TemplID string `json:"template_id"`
	//点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
	Page string `json:"page"`
	//模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
	TemplData TemplData `json:"data"`
	//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
	AppEnv string `json:"miniprogram_state"`
	//支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
	Lang string `json:"lang"`
}

type TemplData

type TemplData map[string]TemplItem

type TemplInfo

type TemplInfo struct {
	//模板中需要修改的参数
	Items []TemplValue `json:"parameter_list"`
}

type TemplItem

type TemplItem struct {
	Value string `json:"value"`
}

type TemplValue

type TemplValue struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

member_count target_state = 0 时必填,文字内容模板中 member_count 的值 room_limit target_state = 0 时必填,文字内容模板中 room_limit 的值 path target_state = 1 时必填,点击「进入」启动小程序时使用的路径。 version_type target_state = 1 时必填,有效参数值为:develop(开发版),trial(体验版),release(正式版)

type TokenCache

type TokenCache struct {
	// contains filtered or unexported fields
}

func (*TokenCache) GetAccessToken

func (cache *TokenCache) GetAccessToken(appid string, secret string) (string, error)

type TokenGetFunc

type TokenGetFunc func(appid string, secret string) (string, error)

建议使用中控服务器统一获取和刷新 access_token,因此采用自定义逻辑去获取 access_token

type TokenInfo

type TokenInfo struct {
	Token      string
	ExpireTime int64
}

type UpdateActivityReq

type UpdateActivityReq struct {
	//动态消息的 ID
	ActivityId string `json:"activity_id"`
	//动态消息修改后的状态: 0 未开始 1已开始
	TargetState int `json:"target_state"`
	//动态消息对应的模板信息
	TemplInfo TemplInfo `json:"template_info"`
}

type UploadTempMediaRet

type UploadTempMediaRet struct {
	WxApiRet
	FileType    string `json:"type"`
	MediaId     string `json:"media_id"`
	CreatedTime int64  `json:"created_at"`
}

媒体文件上传返回结果

func UploadTempMedia

func UploadTempMedia(access_token string, file_path string) (UploadTempMediaRet, error)

把媒体文件上传到微信服务器,目前仅支持图片

type UrlLinkInfo

type UrlLinkInfo struct {
	//小程序 appid。
	AppId string `json:"appid"`
	//小程序页面路径。
	Path string `json:"path"`
	//小程序页面query。
	Query string `json:"query"`
	//创建时间,为 Unix 时间戳。
	CreateTime int64 `json:"expire_time"`
	//到期失效时间,为 Unix 时间戳,0 表示永久生效。
	ExpireTime int64 `json:"expire_time"`
	//要打开的小程序版本。正式版为"release",体验版为"trial",开发版为"develop"。
	AppEnv string `json:"env_version"`
	//云开发静态网站自定义 H5 配置参数,可配置中转的云开发 H5 页面。不填默认用官方 H5 页面
	CloudBase CloudParam `json:"cloud_base"`
}

type UrlLinkQueryRet

type UrlLinkQueryRet struct {
	ErrCode     int64       `json:"errcode"`
	ErrMsg      string      `json:"errmsg"`
	UrlLinkInfo UrlLinkInfo `json:"url_link_info"`
	UrlLinkQuot UrlLinkQuot `json:"url_link_quota"`
}

func GetUrlLinkInfo

func GetUrlLinkInfo(access_token string, url_link string) (UrlLinkQueryRet, error)

查询小程序 url_link 配置,及长期有效 quota

type UrlLinkQuot

type UrlLinkQuot struct {
	//长期有效 url_link 已生成次数。
	LongTimeUsed int64 `json:"long_time_used"`
	//长期有效 url_link 生成次数上限。
	LongTimeLimit int64 `json:"long_time_limit"`
}

type UrlLinkReq

type UrlLinkReq struct {
	//通过 URL Link 进入的小程序页面路径,必须是已经发布的小程序存在的页面,不可携带 query 。path 为空时会跳转小程序主页
	Path string `json:"path"`
	//通过 URL Link 进入小程序时的query,最大1024个字符,只支持数字,大小写英文以及部分特殊字符
	Query string `json:"query"`
	//要打开的小程序版本。正式版为 release,体验版为 trial,开发版为 develop
	AppEnv string `json:"env_version"`
	//生成的 URL Link 类型,到期失效:true,永久有效:false。
	IsExpire bool `json:"is_expire"`
	//小程序 URL Link 失效类型,失效时间:0,失效间隔天数:1
	ExpireType int64 `json:"expire_type"`
	//到期失效的 scheme 码的失效时间,为 Unix 时间戳
	ExpireTime int64 `json:"expire_time"`
	//到期失效的 scheme 码的失效间隔天数
	ExpireInterval int64 `json:"expire_interval"`
	//云开发静态网站自定义 H5 配置参数,可配置中转的云开发 H5 页面。不填默认用官方 H5 页面
	CloudBase CloudParam `json:"cloud_base"`
}

type UrlLinkRet

type UrlLinkRet struct {
	WxApiRet
	UrlLink string `json:"url_link"`
}

type WeixinApp

type WeixinApp struct {
	AppID       string
	AppSecret   string
	AccessToken string
	TokenGetter TokenGetFunc
	TokenTime   int64
	// contains filtered or unexported fields
}

func NewWeixinApp

func NewWeixinApp(appid string, secret string, fn TokenGetFunc) *WeixinApp

func (*WeixinApp) CheckMedia

func (app *WeixinApp) CheckMedia(param CheckMediaReq) (CheckMediaRet, error)

异步校验图片/音频是否含有违法违规内容。

func (*WeixinApp) CheckText

func (app *WeixinApp) CheckText(param CheckTextReq) (CheckTextRet, error)

检查一段文本是否含有违法违规内容。

func (*WeixinApp) CreateACode

func (app *WeixinApp) CreateACode(param ACodeParam) ([]byte, error)

通过该接口生成的小程序码,永久有效,有数量限制

func (*WeixinApp) CreateACodeUnlimited

func (app *WeixinApp) CreateACodeUnlimited(param ACodeParam) ([]byte, error)

通过该接口生成的小程序码,永久有效,数量暂无限制

func (*WeixinApp) CreateActivity

func (app *WeixinApp) CreateActivity(userid string, is_unionid bool) (CreateActivityRet, error)

创建被分享动态消息或私密消息的 activity_id,

func (*WeixinApp) CreateQCode

func (app *WeixinApp) CreateQCode(page string, width int) ([]byte, error)

生成页面二维码

func (app *WeixinApp) CreateShortLink(param ShortLinkReq) (string, error)

生成小程序shortLink

func (*WeixinApp) CreateShortLinkPermanent

func (app *WeixinApp) CreateShortLinkPermanent(page_url string, page_title string) (string, error)

生成永久shortLink

func (*WeixinApp) CreateShortLinkTemp

func (app *WeixinApp) CreateShortLinkTemp(page_url string, page_title string) (string, error)

生成临时shortLink

func (app *WeixinApp) CreateUrlLink(param UrlLinkReq) (string, error)

生成小程序URL Link,

func (*WeixinApp) CreateUrlScheme

func (app *WeixinApp) CreateUrlScheme(param WxUrlSchemeReq) (string, error)

生成页面Scheme码

func (*WeixinApp) DecodePhoneNumber

func (app *WeixinApp) DecodePhoneNumber(rawData, key, iv string) (PhoneInfo, error)

解密用户手机号数据

func (*WeixinApp) EncryptedCheck

func (app *WeixinApp) EncryptedCheck(encrypted_msg_hash string) (EncryptedCheckRet, error)

检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据

func (*WeixinApp) GenIMUserSig

func (app *WeixinApp) GenIMUserSig(sdkappid int, key string, identifier string, expire int) (string, error)

用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据

func (*WeixinApp) GetOpenIdByCode

func (app *WeixinApp) GetOpenIdByCode(code string) (WxSessionData, error)

根据登录凭证code获取opendid以及session_key

func (*WeixinApp) GetPaidInfo

func (app *WeixinApp) GetPaidInfo(param GetPaidInfoReq) (GetPaidInfoRet, error)

用户支付完成后,获取该用户的 UnionId,无需用户授权

func (*WeixinApp) GetPhoneNumber

func (app *WeixinApp) GetPhoneNumber(code string) (GetPhoneNumberRet, error)

code换取用户手机号

func (*WeixinApp) GetSchemeInfo

func (app *WeixinApp) GetSchemeInfo(scheme string) (SchemeQueryRet, error)

查询小程序 scheme 码,及长期有效 quota

func (*WeixinApp) GetTempMedia

func (app *WeixinApp) GetTempMedia(media_id string) ([]byte, error)

获取客服消息内的临时素材。即下载临时的多媒体文件。目前小程序仅支持下载图片文件。

func (*WeixinApp) GetUrlLinkInfo

func (app *WeixinApp) GetUrlLinkInfo(url_link string) (UrlLinkQueryRet, error)

查询小程序 url_link 配置,及长期有效 quota

func (*WeixinApp) SendCustomAppPage

func (app *WeixinApp) SendCustomAppPage(touser string, title, pagepath, thumb_media_id string) error

发送客服消息给用户:小程序卡片

func (*WeixinApp) SendCustomImage

func (app *WeixinApp) SendCustomImage(touser string, media_id string) error

发送客服消息给用户:图片消息

func (app *WeixinApp) SendCustomLink(touser string, title, description, url, thumb_url string) error

发送客服消息给用户:图文链接

func (*WeixinApp) SendCustomMessage

func (app *WeixinApp) SendCustomMessage(param CustomerMessage) error

发送客服消息给用户

func (*WeixinApp) SendCustomText

func (app *WeixinApp) SendCustomText(touser string, text string) error

发送客服消息给用户:文本消息

func (*WeixinApp) SendCustomTyping

func (app *WeixinApp) SendCustomTyping(touser, command string) error

下发客服当前输入状态给用户

func (*WeixinApp) SendSubscribeMsg

func (app *WeixinApp) SendSubscribeMsg(param SubscribeMsgReq) error

发送微信订阅消息

func (*WeixinApp) UpdateActivity

func (app *WeixinApp) UpdateActivity(param UpdateActivityReq) error

修改被分享的动态消息

func (*WeixinApp) UploadTempMedia

func (app *WeixinApp) UploadTempMedia(file_path string) (UploadTempMediaRet, error)

把媒体文件上传到微信服务器,目前仅支持图片

func (*WeixinApp) WxDncrypt

func (app *WeixinApp) WxDncrypt(rawData, iv string) (string, error)

小程序数据解密

type WeixinParam

type WeixinParam struct {
	// contains filtered or unexported fields
}

type WxApiRet

type WxApiRet struct {
	ErrCode int64  `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
}

type WxApiTokenRet

type WxApiTokenRet struct {
	WxApiRet
	AccessToken string `json:"access_token"`
	ExpiresIn   int64  `json:"expires_in"`
}

type WxJumpWxa

type WxJumpWxa struct {
	//通过scheme码进入的小程序页面路径
	Path string `json:"path"`
	//通过scheme码进入小程序时的query
	Query string `json:"query"`
	//要打开的小程序版本。正式版为 release,体验版为 trial,开发版为 develop
	AppEnv string `json:"env_version"`
}

type WxSessionData

type WxSessionData struct {
	WxApiRet
	OpenId     string `json:"openid"`
	UnionId    string `json:"unionid"`
	Sessionkey string `json:"session_key"`
}

func GetOpenIdByCode

func GetOpenIdByCode(appid string, secret string, code string) (WxSessionData, error)

登录凭证校验。 通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。 根据code获取opendid以及session_key

type WxUrlSchemeReq

type WxUrlSchemeReq struct {
	//跳转到的目标小程序信息
	Jump_wxa WxJumpWxa `json:"jump_wxa"`
	//到期失效:true,永久有效:false
	IsExpire bool `json:"is_expire"`
	//失效时间:0,失效间隔天数:1
	ExpireType int `json:"expire_type"`
	//到期失效的 scheme 码的失效时间,为 Unix 时间戳
	ExpireTime int64 `json:"expire_time"`
	//到期失效的 scheme 码的失效间隔天数
	ExpireInterval int64 `json:"expire_interval"`
}

type WxUrlSchemeRet

type WxUrlSchemeRet struct {
	WxApiRet
	Openlink string `json:"openlink"`
}

type WxWaterMark

type WxWaterMark struct {
	//敏感数据归属 appId,开发者可校验此参数与自身 appId 是否一致
	AppId string `json:"appid"`
	//敏感数据获取的时间戳, 开发者可以用于数据时效性校验
	TimeStamp int `json:"timestamp"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL