更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -14,17 +14,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// =============================================================
|
||||
// 微信小程序服务端 API
|
||||
// 1. GET /sns/jscode2session code → openid / unionid / session_key
|
||||
// 2. POST /wxa/business/getuserphonenumber phone_code → 手机号(需 access_token)
|
||||
// =============================================================
|
||||
|
||||
const apiBase = "https://api.weixin.qq.com"
|
||||
|
||||
var httpClient = &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
// APIError 微信接口业务错误(HTTP 200 但 errcode 非 0)
|
||||
type APIError struct {
|
||||
API string
|
||||
Code int
|
||||
@@ -35,7 +28,6 @@ func (e *APIError) Error() string {
|
||||
return fmt.Sprintf("%s 失败:errcode=%d errmsg=%s", e.API, e.Code, e.Msg)
|
||||
}
|
||||
|
||||
// Explain 返回常见 errcode 的中文排查建议;未知错误码返回空串
|
||||
func (e *APIError) Explain() string {
|
||||
switch e.Code {
|
||||
case 40029:
|
||||
@@ -56,7 +48,6 @@ func (e *APIError) Explain() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// ExplainError 提取中文排查建议;无可用建议时返回 ("", false)
|
||||
func ExplainError(err error) (string, bool) {
|
||||
if ae, ok := err.(*APIError); ok {
|
||||
if hint := ae.Explain(); hint != "" {
|
||||
@@ -66,16 +57,12 @@ func ExplainError(err error) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// ============================ code 换 openid ============================
|
||||
|
||||
// Session jscode2session 返回结果
|
||||
type Session struct {
|
||||
OpenID string `json:"openid"`
|
||||
UnionID string `json:"unionid"`
|
||||
SessionKey string `json:"session_key"`
|
||||
}
|
||||
|
||||
// Code2Session 用 wx.login / uni.login 拿到的 code 换 openid(+ unionid)
|
||||
func Code2Session(cfg *Config, code string) (*Session, error) {
|
||||
if cfg == nil || cfg.AppID == "" || cfg.AppSecret == "" {
|
||||
return nil, ErrNotConfigured
|
||||
@@ -99,17 +86,12 @@ func Code2Session(cfg *Config, code string) (*Session, error) {
|
||||
return &Session{OpenID: resp.OpenID, UnionID: resp.UnionID, SessionKey: resp.SessionKey}, nil
|
||||
}
|
||||
|
||||
// ============================ 手机号快速验证 ============================
|
||||
|
||||
// PhoneNumber getuserphonenumber 返回结果
|
||||
type PhoneNumber struct {
|
||||
PhoneNumber string `json:"phoneNumber"`
|
||||
PurePhoneNumber string `json:"purePhoneNumber"`
|
||||
CountryCode string `json:"countryCode"`
|
||||
}
|
||||
|
||||
// GetPhoneNumber 用 getPhoneNumber 事件返回的动态 code 换真实手机号。
|
||||
// 仅企业主体小程序可用;未开通该能力时微信返回 40226。
|
||||
func GetPhoneNumber(cfg *Config, phoneCode string) (*PhoneNumber, error) {
|
||||
if cfg == nil || cfg.AppID == "" || cfg.AppSecret == "" {
|
||||
return nil, ErrNotConfigured
|
||||
@@ -143,14 +125,6 @@ func GetPhoneNumber(cfg *Config, phoneCode string) (*PhoneNumber, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ============================ 小程序码 ============================
|
||||
|
||||
// CreateUnlimitedQRCode 生成小程序码(wxa/getwxacodeunlimited,数量无限制),
|
||||
// 返回图片 base64 data URL,可直接用于 <img src>。
|
||||
//
|
||||
// scene 会话标识,扫码后作为 options.scene 传给目标页面,1-32 个字符
|
||||
// page 小程序页面路径(如 pages/register/confirm),必须已发布,不带前导 /
|
||||
// envVersion release(正式版,默认)/ trial(体验版)/ develop(开发版)
|
||||
func CreateUnlimitedQRCode(cfg *Config, scene, page, envVersion string) (string, error) {
|
||||
if cfg == nil || cfg.AppID == "" || cfg.AppSecret == "" {
|
||||
return "", ErrNotConfigured
|
||||
@@ -177,8 +151,6 @@ func CreateUnlimitedQRCode(cfg *Config, scene, page, envVersion string) (string,
|
||||
"env_version": envVersion,
|
||||
"width": 280,
|
||||
}
|
||||
// 体验版 / 开发版下该页面尚未发布到正式版,必须关闭路径校验,
|
||||
// 否则微信返回 40066 invalid url(正式版可保留默认校验)
|
||||
if envVersion != "release" {
|
||||
payload["check_path"] = false
|
||||
}
|
||||
@@ -187,7 +159,6 @@ func CreateUnlimitedQRCode(cfg *Config, scene, page, envVersion string) (string,
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 失败时微信仍返回 200,但响应体是 JSON 错误而非图片
|
||||
if strings.HasPrefix(strings.TrimSpace(string(body)), "{") {
|
||||
var resp struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
@@ -202,8 +173,6 @@ func CreateUnlimitedQRCode(cfg *Config, scene, page, envVersion string) (string,
|
||||
return "data:" + contentType + ";base64," + base64.StdEncoding.EncodeToString(body), nil
|
||||
}
|
||||
|
||||
// ============================ access_token 缓存 ============================
|
||||
|
||||
type tokenEntry struct {
|
||||
appID string
|
||||
token string
|
||||
@@ -216,7 +185,6 @@ var (
|
||||
tokenMinTTL = 5 * time.Minute
|
||||
)
|
||||
|
||||
// GetAccessToken 获取小程序 access_token(内存缓存,提前 5 分钟刷新)
|
||||
func GetAccessToken(cfg *Config) (string, error) { return getAccessToken(cfg) }
|
||||
|
||||
func getAccessToken(cfg *Config) (string, error) {
|
||||
@@ -252,8 +220,6 @@ func getAccessToken(cfg *Config) (string, error) {
|
||||
return resp.AccessToken, nil
|
||||
}
|
||||
|
||||
// ============================ HTTP 辅助 ============================
|
||||
|
||||
func httpGetJSON(api string, out interface{}) error {
|
||||
resp, err := httpClient.Get(api)
|
||||
if err != nil {
|
||||
@@ -290,7 +256,6 @@ func httpPostJSON(api string, payload interface{}, out interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// httpPostBinary POST JSON 并返回原始响应体与 Content-Type(小程序码接口返回图片流)
|
||||
func httpPostBinary(api string, payload interface{}) ([]byte, string, error) {
|
||||
bs, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user