Documentation
¶
Overview ¶
Package webDev
English:
webDev is the core package of this project, which includes various functions for handling WeChat public account development. All functions are based on the WeChat public account development documentation, so the correctness of the functions can be guaranteed.
Currently, all functions are bound to the WcTool structure, and various functions can be called through the methods of the WcTool structure.
Currently, the main functions in this package include:
1. Get user information
2. Get accessToken
3. Get jsapiTicket
Chinese:
webDev 是本项目的核心包,其中包括了处理微信公众号开发的各种功能。所有的功能都是基于微信公众号开发文档的,因此可以保证功能的正确性。
目前所有功能都绑定到了 WcTool 结构体上,通过 WcTool 结构体的方法来调用各种功能。
目前本包中的功能主要包括:
1. 获取用户信息
2. 获取 access_token
3. 获取 jsapi_ticket
Index ¶
- type UserInfo
- type WcTool
- func (wt *WcTool) GetAccessToken() (string, error)
- func (wt *WcTool) GetAppId() string
- func (wt *WcTool) GetCanGetInfo() bool
- func (wt *WcTool) GetJsapiTicket() (string, error)
- func (wt *WcTool) GetSecret() string
- func (wt *WcTool) GetUserInfoByCode(code string) (*UserInfo, bool, error)
- func (wt *WcTool) SetAppId(newAppId string)
- func (wt *WcTool) SetCanGetInfo(newScope string)
- func (wt *WcTool) SetSecret(newSecret string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type UserInfo ¶
type UserInfo struct {
// OpenId is the unique identifier of the user.
//
// OpenId 是用户的唯一标识符。
OpenId string `json:"openid"`
// Nickname is the nickname of the user.
//
// Nickname 是用户的昵称。
Nickname string `json:"nickname"`
// Sex is the user's gender, with a value of 1 indicating male, a value of 2 indicating female, and a value of 0 indicating unknown.
//
// Sex 是用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
Sex int `json:"sex"`
// Province is the province where users fill in their personal information.
//
// Province 是用户个人资料填写的省份。
Province string `json:"province"`
// City is the city where ordinary users fill in their personal information.
//
// City 是普通用户个人资料填写的城市。
City string `json:"city"`
// Country is the country filled in by the user, such as CN for China.
//
// Country 是用户填的国家,如中国为CN。
Country string `json:"country"`
// HeadImg is the user's avatar, and the last value represents the size of the square avatar (there are 0, 46, 64, 96, 132 values to choose from, 0 represents 640640 square avatar). When the user does not have an avatar, this item is empty. If the user changes their avatar, the original avatar URL will become invalid.
//
// HeadImg 是用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
HeadImg string `json:"headimgurl"`
// Privilege is user privilege information, JSON array, for example, WeChat Woka user is (Chinaunicom)
//
// Privilege 是用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
Privilege []string `json:"privilege"`
// UnionId is the union ID of the user, which is the same as the OpenId of the same user in different WeChat applications.This field will appear only after the user binds the official account to the WeChat open platform account.
//
// UnionId 是用户的统一标识符,同一个用户在不同微信应用中的 OpenId 是相同的。只有在用户将公众号绑定到微信开放平台账号后,才会出现该字段。
UnionId string `json:"unionid"`
// ErrCode is the error code returned by the WeChat server.
//
// ErrCode 是微信服务器返回的错误码。
ErrCode int `json:"errcode"`
// ErrMsg is the error message returned by the WeChat server.
//
// ErrMsg 是微信服务器返回的错误消息。
ErrMsg string `json:"errmsg"`
}
UserInfo
English:
UserInfo represents the user information structure.
It contains various fields that store user details such as OpenId, Nickname, Sex, etc.
The corresponding fields are all from the user information returned by the WeChat server.
Chinese:
UserInfo 表示用户信息结构体。
它包含了各种字段,用于存储用户的详细信息,如 OpenId、Nickname、Sex 等。
对应的字段都是来自于微信服务器返回的用户信息。
type WcTool ¶
type WcTool struct {
// contains filtered or unexported fields
}
WcTool
English:
WcPool is the core structure of the entire package. All functions are encapsulated here.
Currently supported functions:
1. Get AccessToken
2. Get JsapiTicket
3. Get user information.
4. Set and get appid, secret, and scope.
Chinese:
WcTool 是整个包的核心结构体。封装了所有功能在此。
目前支持的功能:
1. 获取 AccessToken
2. 获取 JsapiTicket
3. 获取用户信息。
4. 对appid、secret、scope的设置和获取。
func New ¶
New
English:
New is a constructor for WcTool. It initializes the WcTool structure and returns a pointer to the structure.
appId: The appid of the WeChat public account.
secret: The secret of the WeChat public account.
scope: The scope of the WeChat public account. It can be "snsapi_userinfo" or "snsapi_base".
If the scope is "snsapi_userinfo", the user information can be obtained. If the scope is "snsapi_base", the user information cannot be obtained.
Chinese:
New 是 WcTool 的构造函数。它初始化 WcTool 结构体并返回结构体的指针。
appId: 微信公众号的 appid。
secret: 微信公众号的 secret。
scope: 微信公众号的 scope。可以是 "snsapi_userinfo" 或 "snsapi_base"。
如果 scope 是 "snsapi_userinfo",则可以获取用户详细信息。如果 scope 是 "snsapi_base",则无法获取用户详细信息。
func (*WcTool) GetAccessToken ¶
GetAccessToken
English:
GetAccessToken is a method that gets the AccessToken.
It calls the getAccessToken method of the accessToken structure to get the AccessToken.
If the AccessToken is not expired, it will return the AccessToken directly.
If the AccessToken is expired, it will call the refreshAccessToken method of the accessToken structure to refresh the AccessToken.
Chinese:
GetAccessToken 是获取 AccessToken 的方法。
它调用 accessToken 结构体的 getAccessToken 方法来获取 AccessToken。
如果 AccessToken 没有过期,它将直接返回 AccessToken。
如果 AccessToken 过期,它将调用 accessToken 结构体的 refreshAccessToken 方法来刷新 AccessToken。
func (*WcTool) GetAppId ¶
GetAppId
English:
GetAppId is a getter method for the appId field.
Chinese:
GetAppId 是 appId 字段的 getter 方法。
func (*WcTool) GetCanGetInfo ¶
GetCanGetInfo
English:
GetCanGetInfo is a getter method for the canGetInfo field.
Chinese:
GetCanGetInfo 是 canGetInfo 字段的 getter 方法。
func (*WcTool) GetJsapiTicket ¶
GetJsapiTicket
English:
GetJsapiTicket is a method that gets the JsapiTicket.
It calls the getJsapiTicket method of the jsapiTicket structure to get the JsapiTicket.
If the JsapiTicket is not expired, it will return the JsapiTicket directly.
If the JsapiTicket is expired, it will call the refreshJsapiTicket method of the jsapiTicket structure to refresh the JsapiTicket.
Chinese:
GetJsapiTicket 是获取 JsapiTicket 的方法。
它调用 jsapiTicket 结构体的 getJsapiTicket 方法来获取 JsapiTicket。
如果 JsapiTicket 没有过期,它将直接返回 JsapiTicket。
如果 JsapiTicket 过期,它将调用 jsapiTicket 结构体的 refreshJsapiTicket 方法来刷新 JsapiTicket。
func (*WcTool) GetSecret ¶
GetSecret
English:
GetSecret is a getter method for the secret field.
Chinese:
GetSecret 是 secret 字段的 getter 方法。
func (*WcTool) GetUserInfoByCode ¶
GetUserInfoByCode
English:
GetUserInfoByCode is a method to get user information by code.
It sends a request to get userToken.
Then it calls the getUserInfo method to get user information.
If the scope is "snsapi_userinfo", detailed user information can be obtained. If the scope is "snsapi_base", OpenId can be obtained.
Chinese:
GetUserInfoByCode 是通过 code 获取用户信息的方法。
它发送请求获取 userToken。
然后调用 getUserInfo 方法来获取用户信息。
如果 scope 是 "snsapi_userinfo",则可以获取用户详细信息。如果 scope 是 "snsapi_base",则获取用户OpenId。
func (*WcTool) SetAppId ¶
SetAppId
English:
SetAppId is a setter method for the appId field.
Chinese:
SetAppId 是 appId 字段的 setter 方法。
func (*WcTool) SetCanGetInfo ¶
SetCanGetInfo
English:
SetCanGetInfo is a setter method for the canGetInfo field.
Chinese:
SetCanGetInfo 是 canGetInfo 字段的 setter 方法。