更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -1,13 +1,3 @@
|
||||
// Package wechatmini 微信小程序「消息推送」服务端能力。
|
||||
//
|
||||
// 对应小程序后台:开发管理 → 开发设置 → 消息推送。
|
||||
// 后台需填写 URL / Token / EncodingAESKey / 加解密方式 / 数据格式,
|
||||
// 微信会在保存时向 URL 发一次 GET 校验(signature + echostr),
|
||||
// 保存成功后,用户向小程序客服发消息等事件会以 POST 推送到该 URL。
|
||||
//
|
||||
// 协议与公众号「服务器配置」一致(signature = sha1(sort(token,timestamp,nonce))),
|
||||
// 复用 services/wechatmp 的签名与 AES 加解密原语,本文件补齐小程序侧
|
||||
// 的多应用路由、JSON 数据格式与被动回复构造。
|
||||
package wechatmini
|
||||
|
||||
import (
|
||||
@@ -27,26 +17,22 @@ import (
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// 消息数据格式(小程序后台「数据格式」)
|
||||
const (
|
||||
DataFormatXML = "xml"
|
||||
DataFormatJSON = "json"
|
||||
)
|
||||
|
||||
// PushConfig 小程序消息推送运行配置(敏感字段已解密)
|
||||
type PushConfig struct {
|
||||
AppID string
|
||||
Token string
|
||||
AESKey string // 明文(43 位 EncodingAESKey)
|
||||
EncryptMode string // plain / compatible / safe
|
||||
DataFormat string // xml / json
|
||||
ReplyText string // 客服消息自动回复文案(空 = 不自动回复)
|
||||
AESKey string
|
||||
EncryptMode string
|
||||
DataFormat string
|
||||
ReplyText string
|
||||
}
|
||||
|
||||
// ErrPushNotConfigured 未配置消息推送
|
||||
var ErrPushNotConfigured = errors.New("小程序消息推送未配置")
|
||||
|
||||
// pushConfigFromRow 读取行数据并解密 AESKey
|
||||
func pushConfigFromRow(row models.WechatMiniApp) (*PushConfig, error) {
|
||||
cfg := &PushConfig{
|
||||
AppID: strings.TrimSpace(row.AppID),
|
||||
@@ -71,8 +57,6 @@ func pushConfigFromRow(row models.WechatMiniApp) (*PushConfig, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// LoadPushConfigs 读取全部已配置 Token 的小程序推送配置(未删除的记录)。
|
||||
// 微信回调不携带 appid(明文模式下 ToUserName 即 appid),故按应用逐个匹配签名。
|
||||
func LoadPushConfigs() ([]PushConfig, error) {
|
||||
var rows []models.WechatMiniApp
|
||||
_, err := models.Orm.QueryTable(new(models.WechatMiniApp)).
|
||||
@@ -85,18 +69,17 @@ func LoadPushConfigs() ([]PushConfig, error) {
|
||||
list := make([]PushConfig, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
if strings.TrimSpace(r.Token) == "" {
|
||||
continue // 未配置 Token 的应用不参与签名匹配
|
||||
continue
|
||||
}
|
||||
cfg, cerr := pushConfigFromRow(r)
|
||||
if cerr != nil {
|
||||
continue // 解密失败的应用跳过,避免阻塞其它小程序
|
||||
continue
|
||||
}
|
||||
list = append(list, *cfg)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// LoadPushConfigByAppID 按 appid 读取单个小程序的推送配置
|
||||
func LoadPushConfigByAppID(appID string) (*PushConfig, error) {
|
||||
appID = strings.TrimSpace(appID)
|
||||
if appID == "" {
|
||||
@@ -116,17 +99,15 @@ func LoadPushConfigByAppID(appID string) (*PushConfig, error) {
|
||||
return pushConfigFromRow(row)
|
||||
}
|
||||
|
||||
// SavePushConfigInput 保存推送配置入参(敏感字段支持掩码表示不修改)
|
||||
type SavePushConfigInput struct {
|
||||
AppID string
|
||||
Token string
|
||||
AESKey string // 掩码 / 空串 = 保持原值
|
||||
AESKey string
|
||||
EncryptMode string
|
||||
DataFormat string
|
||||
ReplyText string
|
||||
}
|
||||
|
||||
// SavePushConfig 保存小程序消息推送配置
|
||||
func SavePushConfig(in SavePushConfigInput) error {
|
||||
appID := strings.TrimSpace(in.AppID)
|
||||
if appID == "" {
|
||||
@@ -188,9 +169,6 @@ func SavePushConfig(in SavePushConfigInput) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// PushCallbackURL 小程序消息推送回调地址(填到小程序后台「消息推送 - URL」)。
|
||||
// 优先读取 app.conf 的 wechat_mini_callback_base,其次回落 wechat_mp_callback_base
|
||||
// / payment_callback_base(均为对外 API 域名)。
|
||||
func PushCallbackURL() string {
|
||||
base, _ := beego.AppConfig.String("wechat_mini_callback_base")
|
||||
if strings.TrimSpace(base) == "" {
|
||||
@@ -206,11 +184,8 @@ func PushCallbackURL() string {
|
||||
return base + "/api/wechat/mini/callback"
|
||||
}
|
||||
|
||||
// ============================ 随机 Token / EncodingAESKey ============================
|
||||
|
||||
const aesKeyAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
|
||||
// randomString 从指定字符集随机取 n 个字符(crypto/rand 源)
|
||||
func randomString(alphabet string, n int) string {
|
||||
b := []byte(alphabet)
|
||||
out := make([]byte, n)
|
||||
@@ -218,7 +193,6 @@ func randomString(alphabet string, n int) string {
|
||||
for i := range out {
|
||||
idx, err := rand.Int(rand.Reader, max)
|
||||
if err != nil {
|
||||
// 随机源异常时退化为时间派生,保证不返回空串
|
||||
out[i] = b[(time.Now().UnixNano()+int64(i))%int64(len(b))]
|
||||
continue
|
||||
}
|
||||
@@ -227,39 +201,31 @@ func randomString(alphabet string, n int) string {
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// GeneratePushToken 生成消息推送 Token(3-32 位字母数字)
|
||||
func GeneratePushToken() string {
|
||||
return randomString(aesKeyAlphabet, 24)
|
||||
}
|
||||
|
||||
// GeneratePushAESKey 生成消息推送 EncodingAESKey(43 位字母数字)
|
||||
// 43 位 base64 字符解码后为 32 字节,符合微信 AES-256-CBC 的密钥长度要求。
|
||||
func GeneratePushAESKey() string {
|
||||
return randomString(aesKeyAlphabet, 43)
|
||||
}
|
||||
|
||||
// ============================ 消息解析 ============================
|
||||
|
||||
// PushMessage 小程序推送的消息 / 事件(已解密)
|
||||
type PushMessage struct {
|
||||
ToUserName string
|
||||
FromUserName string
|
||||
CreateTime int64
|
||||
MsgType string // text / image / event ...
|
||||
MsgType string
|
||||
Content string
|
||||
Event string // subscribe / unsubscribe / SCAN / CLICK ...
|
||||
Event string
|
||||
EventKey string
|
||||
MsgID int64
|
||||
Encrypt string // 兼容/安全模式外层携带的密文
|
||||
Encrypt string
|
||||
MsgSignature string
|
||||
TimeStamp string
|
||||
Nonce string
|
||||
}
|
||||
|
||||
// IsEvent 是否为事件推送
|
||||
func (m *PushMessage) IsEvent() bool { return strings.EqualFold(m.MsgType, "event") }
|
||||
|
||||
// pushXML 微信 XML 推送体
|
||||
type pushXML struct {
|
||||
XMLName struct{} `xml:"xml"`
|
||||
ToUserName string `xml:"ToUserName"`
|
||||
@@ -276,7 +242,6 @@ type pushXML struct {
|
||||
Nonce string `xml:"Nonce"`
|
||||
}
|
||||
|
||||
// pushJSON 微信 JSON 推送体
|
||||
type pushJSON struct {
|
||||
ToUserName string `json:"ToUserName"`
|
||||
FromUserName string `json:"FromUserName"`
|
||||
@@ -292,7 +257,6 @@ type pushJSON struct {
|
||||
Nonce string `json:"Nonce"`
|
||||
}
|
||||
|
||||
// ParsePushXML 解析 XML 推送体
|
||||
func ParsePushXML(raw string) (*PushMessage, error) {
|
||||
var p pushXML
|
||||
if err := xml.Unmarshal([]byte(raw), &p); err != nil {
|
||||
@@ -314,7 +278,6 @@ func ParsePushXML(raw string) (*PushMessage, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ParsePushJSON 解析 JSON 推送体
|
||||
func ParsePushJSON(raw string) (*PushMessage, error) {
|
||||
var p pushJSON
|
||||
if err := json.Unmarshal([]byte(raw), &p); err != nil {
|
||||
@@ -336,7 +299,6 @@ func ParsePushJSON(raw string) (*PushMessage, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DetectPushFormat 根据请求体判断数据格式(JSON 以 { 开头)
|
||||
func DetectPushFormat(raw string) string {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
if strings.HasPrefix(trimmed, "{") {
|
||||
@@ -345,9 +307,6 @@ func DetectPushFormat(raw string) string {
|
||||
return DataFormatXML
|
||||
}
|
||||
|
||||
// ============================ 被动回复构造 ============================
|
||||
|
||||
// BuildTextReply 构造文本被动回复(按数据格式输出 XML / JSON)
|
||||
func BuildTextReply(format, toUser, fromUser, content string, timestamp int64) (string, error) {
|
||||
if format == DataFormatJSON {
|
||||
payload := map[string]interface{}{
|
||||
@@ -369,7 +328,6 @@ func BuildTextReply(format, toUser, fromUser, content string, timestamp int64) (
|
||||
esc.Replace(toUser), esc.Replace(fromUser), timestamp, esc.Replace(content)), nil
|
||||
}
|
||||
|
||||
// BuildEncryptedReply 构造加密模式的被动回复(安全模式 / 兼容模式共用)
|
||||
func BuildEncryptedReply(cfg *PushConfig, plainReply, timestamp, nonce, format string) (string, error) {
|
||||
encrypt, err := wechatmp.EncryptMessage(cfg.AESKey, cfg.AppID, plainReply)
|
||||
if err != nil {
|
||||
@@ -394,9 +352,6 @@ func BuildEncryptedReply(cfg *PushConfig, plainReply, timestamp, nonce, format s
|
||||
encrypt, sig, timestamp, nonce), nil
|
||||
}
|
||||
|
||||
// ============================ 多应用匹配 ============================
|
||||
|
||||
// MatchPushConfigBySignature 按明文签名匹配小程序(sha1(sort(token,timestamp,nonce)))
|
||||
func MatchPushConfigBySignature(cfgs []PushConfig, timestamp, nonce, signature string) (*PushConfig, bool) {
|
||||
for i := range cfgs {
|
||||
if wechatmp.CheckSignature(cfgs[i].Token, timestamp, nonce, signature) {
|
||||
@@ -406,8 +361,6 @@ func MatchPushConfigBySignature(cfgs []PushConfig, timestamp, nonce, signature s
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// MatchPushConfigByMsgSignature 按加密消息签名匹配小程序
|
||||
// (sha1(sort(token,timestamp,nonce,encrypt)))
|
||||
func MatchPushConfigByMsgSignature(cfgs []PushConfig, timestamp, nonce, encrypt, msgSignature string) (*PushConfig, bool) {
|
||||
for i := range cfgs {
|
||||
if wechatmp.CheckMsgSignature(cfgs[i].Token, timestamp, nonce, encrypt, msgSignature) {
|
||||
@@ -417,16 +370,12 @@ func MatchPushConfigByMsgSignature(cfgs []PushConfig, timestamp, nonce, encrypt,
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// DecryptPushMessage 用匹配的 AESKey 解密消息体(安全 / 兼容模式)
|
||||
func DecryptPushMessage(cfg *PushConfig, encrypt string) (string, error) {
|
||||
return wechatmp.DecryptMessage(cfg.AESKey, cfg.AppID, encrypt)
|
||||
}
|
||||
|
||||
// PushHandler 消息处理钩子:返回给用户的被动回复文案,空串表示不回复。
|
||||
// 默认按小程序配置的「客服自动回复文案」回复,可在业务层覆盖(如按关键字回复)。
|
||||
var PushHandler func(cfg *PushConfig, msg *PushMessage) string
|
||||
|
||||
// HandlePushMessage 处理推送消息并返回被动回复文案
|
||||
func HandlePushMessage(cfg *PushConfig, msg *PushMessage) string {
|
||||
if PushHandler != nil {
|
||||
return strings.TrimSpace(PushHandler(cfg, msg))
|
||||
|
||||
Reference in new issue
Block a user