更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -13,36 +13,15 @@ import (
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
// =============================================================
|
||||
// 小程序订阅消息 / 客服消息发送
|
||||
//
|
||||
// 订阅消息(用户主动授权后才能收到):
|
||||
// POST /wxa/subscribe-message/send?access_token=xxx
|
||||
// {"touser":"openid","template_id":"xxx","page":"pages/index/index",
|
||||
// "data":{"thing1":{"value":"标题"},"time2":{"value":"2026-01-01 08:00"}}}
|
||||
//
|
||||
// 客服消息(用户在 48 小时内发过客服消息才能收到):
|
||||
// POST /cgi-bin/message/custom/send?access_token=xxx
|
||||
// {"touser":"openid","msgtype":"text","text":{"content":"..."}}
|
||||
//
|
||||
// 与「消息推送」(receive 方向)是两条不同的链路:
|
||||
// 消息推送 = 用户发给你(服务器收)
|
||||
// 订阅消息 = 你发给用户(用户收),需要用户在客户端授权
|
||||
// =============================================================
|
||||
|
||||
// 订阅消息常见错误码
|
||||
const (
|
||||
ErrCodeUserRefuse = 43101 // 用户未订阅 / 拒绝订阅(一次性订阅用尽)
|
||||
ErrCodeOpenIDInvalid = 40003 // openid 无效
|
||||
ErrCodeTplParamError = 47003 // 模板参数不匹配(字段键或类型不对)
|
||||
ErrCodeTokenInvalid = 40001 // access_token 无效或过期
|
||||
ErrCodeUserRefuse = 43101
|
||||
ErrCodeOpenIDInvalid = 40003
|
||||
ErrCodeTplParamError = 47003
|
||||
ErrCodeTokenInvalid = 40001
|
||||
)
|
||||
|
||||
// ErrNoSubscribe 未找到可用的订阅授权
|
||||
var ErrNoSubscribe = errors.New("用户未授权订阅消息")
|
||||
|
||||
// SendSubscribeMessage 发送订阅消息。
|
||||
// data 为「微信字段键 → 文本值」,例如 {"thing1":"标题","time2":"2026-10-31 18:00"}
|
||||
func SendSubscribeMessage(cfg *Config, openID, templateID, page string, data map[string]string) error {
|
||||
if cfg == nil || cfg.AppID == "" || cfg.AppSecret == "" {
|
||||
return ErrNotConfigured
|
||||
@@ -90,8 +69,6 @@ func SendSubscribeMessage(cfg *Config, openID, templateID, page string, data map
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendCustomerServiceText 发送客服消息(文本)。
|
||||
// 前提:用户 48 小时内向小程序客服发过消息,否则微信返回 43101 / 45009。
|
||||
func SendCustomerServiceText(cfg *Config, openID, content string) error {
|
||||
if cfg == nil || cfg.AppID == "" || cfg.AppSecret == "" {
|
||||
return ErrNotConfigured
|
||||
@@ -124,7 +101,6 @@ func SendCustomerServiceText(cfg *Config, openID, content string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExplainSubscribeError 订阅消息错误码的中文排查建议
|
||||
func ExplainSubscribeError(err error) string {
|
||||
var ae *APIError
|
||||
if !errors.As(err, &ae) {
|
||||
@@ -147,11 +123,6 @@ func ExplainSubscribeError(err error) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// =============================================================
|
||||
// 模板配置(平台端维护)
|
||||
// =============================================================
|
||||
|
||||
// TemplateInput 保存模板入参
|
||||
type TemplateInput struct {
|
||||
ID uint64
|
||||
AppID string
|
||||
@@ -159,12 +130,11 @@ type TemplateInput struct {
|
||||
TemplateID string
|
||||
Title string
|
||||
Page string
|
||||
Fields string // JSON 字符串
|
||||
Fields string
|
||||
Enabled bool
|
||||
Remark string
|
||||
}
|
||||
|
||||
// ListTemplates 查询某小程序的模板列表
|
||||
func ListTemplates(appID string) ([]models.WechatMiniTemplate, error) {
|
||||
var rows []models.WechatMiniTemplate
|
||||
q := models.Orm.QueryTable(new(models.WechatMiniTemplate)).
|
||||
@@ -178,7 +148,6 @@ func ListTemplates(appID string) ([]models.WechatMiniTemplate, error) {
|
||||
return rows, err
|
||||
}
|
||||
|
||||
// GetTemplateByCode 按场景码取模板
|
||||
func GetTemplateByCode(appID, code string) (*models.WechatMiniTemplate, error) {
|
||||
var row models.WechatMiniTemplate
|
||||
err := models.Orm.QueryTable(new(models.WechatMiniTemplate)).
|
||||
@@ -192,7 +161,6 @@ func GetTemplateByCode(appID, code string) (*models.WechatMiniTemplate, error) {
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// SaveTemplate 保存模板
|
||||
func SaveTemplate(in TemplateInput) error {
|
||||
appID := strings.TrimSpace(in.AppID)
|
||||
code := strings.TrimSpace(in.Code)
|
||||
@@ -248,7 +216,6 @@ func SaveTemplate(in TemplateInput) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteTemplate 软删模板
|
||||
func DeleteTemplate(id uint64) error {
|
||||
if id == 0 {
|
||||
return errors.New("无效ID")
|
||||
@@ -260,11 +227,6 @@ func DeleteTemplate(id uint64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// =============================================================
|
||||
// 订阅授权记录
|
||||
// =============================================================
|
||||
|
||||
// GrantSubscribe 记录用户授权(一次性订阅:每次授权 +1 次发送机会)
|
||||
func GrantSubscribe(identityID uint64, appID string, templateIDs []string) error {
|
||||
if identityID == 0 {
|
||||
return errors.New("未登录")
|
||||
@@ -306,7 +268,6 @@ func GrantSubscribe(identityID uint64, appID string, templateIDs []string) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// SubscribeRemain 查询剩余可发送次数
|
||||
func SubscribeRemain(identityID uint64, appID, templateID string) int {
|
||||
var row models.WechatMiniSubscribe
|
||||
err := models.Orm.QueryTable(new(models.WechatMiniSubscribe)).
|
||||
@@ -320,7 +281,6 @@ func SubscribeRemain(identityID uint64, appID, templateID string) int {
|
||||
return row.Remain
|
||||
}
|
||||
|
||||
// consumeSubscribe 消耗一次发送机会(成功发送后调用)
|
||||
func consumeSubscribe(identityID uint64, appID, templateID string) {
|
||||
now := time.Now()
|
||||
models.Orm.QueryTable(new(models.WechatMiniSubscribe)).
|
||||
@@ -331,11 +291,6 @@ func consumeSubscribe(identityID uint64, appID, templateID string) {
|
||||
Update(map[string]interface{}{"remain": orm.ColValue(orm.ColMinus, 1), "update_time": now})
|
||||
}
|
||||
|
||||
// =============================================================
|
||||
// 推送编排
|
||||
// =============================================================
|
||||
|
||||
// MiniOpenIDByIdentity 取身份绑定的小程序 openid(未绑定返回空串)
|
||||
func MiniOpenIDByIdentity(identityID uint64) string {
|
||||
if identityID == 0 {
|
||||
return ""
|
||||
@@ -351,20 +306,16 @@ func MiniOpenIDByIdentity(identityID uint64) string {
|
||||
return strings.TrimSpace(row.OpenID)
|
||||
}
|
||||
|
||||
// PushResult 一次推送的结果汇总
|
||||
type PushResult struct {
|
||||
Sent int // 成功条数
|
||||
Skipped int // 跳过(未授权 / 未绑定 / 无模板)
|
||||
Errors []error // 失败原因
|
||||
Sent int
|
||||
Skipped int
|
||||
Errors []error
|
||||
}
|
||||
|
||||
// PushSubscribe 按场景向某个身份推送订阅消息。
|
||||
// vars 为语义变量(如 title / content / time),按模板配置的字段映射填入微信字段。
|
||||
func PushSubscribe(scene string, identityID uint64, vars map[string]string) PushResult {
|
||||
return pushSubscribe(scene, "", identityID, vars)
|
||||
}
|
||||
|
||||
// PushSubscribeToTenantUser 按场景向租户用户推送(appID 为空表示所有启用的小程序)
|
||||
func PushSubscribeToTenantUser(tenantUserID uint64, scene, appID string, vars map[string]string) PushResult {
|
||||
identityID, err := tenantUserIdentityID(tenantUserID)
|
||||
if err != nil || identityID == 0 {
|
||||
@@ -414,7 +365,6 @@ func pushSubscribe(scene, appIDFilter string, identityID uint64, vars map[string
|
||||
data := buildTemplateData(tpl.Fields, vars)
|
||||
serr := SendSubscribeMessage(cfg, openID, tpl.TemplateID, tpl.Page, data)
|
||||
if serr != nil {
|
||||
// 用户拒绝 / 授权用尽:清零机会,避免反复调接口
|
||||
if ae, ok := serr.(*APIError); ok && ae.Code == ErrCodeUserRefuse {
|
||||
models.Orm.QueryTable(new(models.WechatMiniSubscribe)).
|
||||
Filter("identity_id", identityID).
|
||||
@@ -431,8 +381,6 @@ func pushSubscribe(scene, appIDFilter string, identityID uint64, vars map[string
|
||||
return res
|
||||
}
|
||||
|
||||
// buildTemplateData 按字段映射组装微信模板数据
|
||||
// mapping 形如 {"thing1":"title","phrase2":"content"}:微信字段键 → 语义变量名
|
||||
func buildTemplateData(mapping string, vars map[string]string) map[string]string {
|
||||
out := make(map[string]string)
|
||||
m := map[string]string{}
|
||||
@@ -442,12 +390,10 @@ func buildTemplateData(mapping string, vars map[string]string) map[string]string
|
||||
for field, semantic := range m {
|
||||
val, ok := vars[semantic]
|
||||
if !ok {
|
||||
// 映射里的变量没有值时给个占位,避免微信报字段缺失
|
||||
val = "-"
|
||||
}
|
||||
out[field] = val
|
||||
}
|
||||
// 变量表里多出来的键也直接按微信字段名填(允许不配映射直接用 thing1 这种键)
|
||||
for k, v := range vars {
|
||||
if _, exists := out[k]; !exists {
|
||||
out[k] = v
|
||||
@@ -456,7 +402,6 @@ func buildTemplateData(mapping string, vars map[string]string) map[string]string
|
||||
return out
|
||||
}
|
||||
|
||||
// PushTenantNotice 推送站内通知到小程序(按租户用户)
|
||||
func PushTenantNotice(tenantUserID uint64, title, content string) PushResult {
|
||||
vars := map[string]string{
|
||||
"title": title,
|
||||
@@ -466,12 +411,10 @@ func PushTenantNotice(tenantUserID uint64, title, content string) PushResult {
|
||||
return PushSubscribeToTenantUser(tenantUserID, SceneNotice, "", vars)
|
||||
}
|
||||
|
||||
// 场景码
|
||||
const (
|
||||
SceneNotice = "notice" // 站内通知 / 审批待办等通用提醒
|
||||
SceneNotice = "notice"
|
||||
)
|
||||
|
||||
// tenantUserIdentityID 租户用户 ID → 身份 ID(yz_system_tenant_user.uid)
|
||||
func tenantUserIdentityID(tenantUserID uint64) (uint64, error) {
|
||||
if tenantUserID == 0 {
|
||||
return 0, errors.New("无效用户")
|
||||
|
||||
Reference in new issue
Block a user