更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -14,12 +14,6 @@ import (
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
// =============================================================
|
||||
// 关注绑定:扫码关注 → 公众号被动回复验证码 → 平台核销完成绑定
|
||||
// 一个绑定会话 = 一条 yz_platform_wechat_mp_verify_code(scene + 6位验证码 + 过期时间)
|
||||
// =============================================================
|
||||
|
||||
// DefaultVerifyTTL 验证码默认有效期
|
||||
const DefaultVerifyTTL = 5 * time.Minute
|
||||
|
||||
var (
|
||||
@@ -30,8 +24,6 @@ var (
|
||||
ErrSceneInvalid = errors.New("二维码无效或已失效")
|
||||
)
|
||||
|
||||
// CreateVerifyCode 创建绑定会话并生成带参二维码。
|
||||
// bindType 为 platform_user / tenant_user,bindID 为发起方用户 ID。
|
||||
func CreateVerifyCode(bindType string, bindID, bindTid uint64, ttl time.Duration) (scene, code, qrURL string, expireAt time.Time, err error) {
|
||||
cfg, err := LoadEnabledConfig()
|
||||
if err != nil {
|
||||
@@ -67,7 +59,6 @@ func CreateVerifyCode(bindType string, bindID, bindTid uint64, ttl time.Duration
|
||||
return scene, code, qrURL, expireAt, nil
|
||||
}
|
||||
|
||||
// GetVerifyCode 按 scene 查询绑定会话(过期时惰性标记)
|
||||
func GetVerifyCode(scene string) (*models.WechatMpVerifyCode, error) {
|
||||
scene = strings.TrimSpace(scene)
|
||||
if scene == "" {
|
||||
@@ -87,7 +78,6 @@ func GetVerifyCode(scene string) (*models.WechatMpVerifyCode, error) {
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// ConfirmVerifyCode 核销验证码并完成绑定,返回粉丝记录
|
||||
func ConfirmVerifyCode(bindType string, bindID, bindTid uint64, scene, code string) (*models.WechatMpFollower, error) {
|
||||
row, err := GetVerifyCode(scene)
|
||||
if err != nil {
|
||||
@@ -105,8 +95,6 @@ func ConfirmVerifyCode(bindType string, bindID, bindTid uint64, scene, code stri
|
||||
return nil, ErrCodeExpired
|
||||
}
|
||||
if strings.TrimSpace(code) != row.Code {
|
||||
// 验证码不属于该二维码会话(用户在公众号内单独发送「验证码」取得):
|
||||
// 回退按验证码认领,保证「不扫码直接取码」的绑定路径可用
|
||||
return ConfirmVerifyCodeByCode(bindType, bindID, bindTid, code)
|
||||
}
|
||||
now := time.Now()
|
||||
@@ -118,9 +106,6 @@ func ConfirmVerifyCode(bindType string, bindID, bindTid uint64, scene, code stri
|
||||
return BindFollower(row.OpenID, bindType, bindID, bindTid)
|
||||
}
|
||||
|
||||
// CreateOpenIDVerify 由公众号消息发起绑定会话:openid 已知(消息发送者),
|
||||
// 绑定目标(bind_type/bind_id)为空,待系统页面登录用户输入验证码后认领。
|
||||
// 与扫码会话共用一张表,区别仅在发起方与归属信息。
|
||||
func CreateOpenIDVerify(openid string, ttl time.Duration) (*models.WechatMpVerifyCode, error) {
|
||||
openid = strings.TrimSpace(openid)
|
||||
if openid == "" {
|
||||
@@ -135,7 +120,7 @@ func CreateOpenIDVerify(openid string, ttl time.Duration) (*models.WechatMpVerif
|
||||
Scene: randomScene(),
|
||||
Code: randomCode6(),
|
||||
OpenID: openid,
|
||||
Status: models.WechatVerifyStatusScanned, // 微信号已知,等同「已扫码待核销」
|
||||
Status: models.WechatVerifyStatusScanned,
|
||||
ExpireAt: &expireAt,
|
||||
CreateTime: now,
|
||||
UpdateTime: &now,
|
||||
@@ -146,9 +131,6 @@ func CreateOpenIDVerify(openid string, ttl time.Duration) (*models.WechatMpVerif
|
||||
return row, nil
|
||||
}
|
||||
|
||||
// ConfirmVerifyCodeByCode 按验证码认领「公众号发起」的绑定会话(系统页面未展示二维码时使用):
|
||||
// 匹配最近一条 bind_id=0、openid 非空、未核销、未过期的会话,用 CAS 更新占位后绑定到当前账号。
|
||||
// 只认领无归属的会话,不会影响他人扫码发起的会话,避免绑定串号。
|
||||
func ConfirmVerifyCodeByCode(bindType string, bindID, bindTid uint64, code string) (*models.WechatMpFollower, error) {
|
||||
code = strings.TrimSpace(code)
|
||||
if code == "" {
|
||||
@@ -170,7 +152,6 @@ func ConfirmVerifyCodeByCode(bindType string, bindID, bindTid uint64, code strin
|
||||
return nil, err
|
||||
}
|
||||
now := time.Now()
|
||||
// CAS:仅当仍是「待核销」时更新,避免同一验证码被并发认领
|
||||
affected, uerr := models.Orm.QueryTable(new(models.WechatMpVerifyCode)).
|
||||
Filter("id", row.ID).
|
||||
Filter("status", models.WechatVerifyStatusScanned).
|
||||
@@ -190,8 +171,6 @@ func ConfirmVerifyCodeByCode(bindType string, bindID, bindTid uint64, code strin
|
||||
return BindFollower(row.OpenID, bindType, bindID, bindTid)
|
||||
}
|
||||
|
||||
// BindFollower 将 openid 绑定到指定账号。
|
||||
// 同一账号只保留一个微信:旧的 openid 绑定关系会被清除。
|
||||
func BindFollower(openid, bindType string, bindID, bindTid uint64) (*models.WechatMpFollower, error) {
|
||||
openid = strings.TrimSpace(openid)
|
||||
if openid == "" {
|
||||
@@ -236,7 +215,6 @@ func BindFollower(openid, bindType string, bindID, bindTid uint64) (*models.Wech
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// UnbindByUser 清除某账号的微信绑定(按绑定对象维度)
|
||||
func UnbindByUser(bindType string, bindID uint64) error {
|
||||
if bindID == 0 {
|
||||
return nil
|
||||
@@ -251,7 +229,6 @@ func UnbindByUser(bindType string, bindID uint64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// GetFollowerByUser 查询某账号已绑定的粉丝记录(未绑定返回 nil, nil)
|
||||
func GetFollowerByUser(bindType string, bindID uint64) (*models.WechatMpFollower, error) {
|
||||
if bindID == 0 {
|
||||
return nil, nil
|
||||
@@ -272,7 +249,6 @@ func GetFollowerByUser(bindType string, bindID uint64) (*models.WechatMpFollower
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// GetFollowerByOpenID 按 openid 查询粉丝记录(不存在返回 nil, nil)
|
||||
func GetFollowerByOpenID(openid string) (*models.WechatMpFollower, error) {
|
||||
openid = strings.TrimSpace(openid)
|
||||
if openid == "" {
|
||||
@@ -289,9 +265,6 @@ func GetFollowerByOpenID(openid string) (*models.WechatMpFollower, error) {
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// LatestPendingVerifyByOpenID 查询该微信号最近一条「未核销且未过期」的绑定会话,用于重发验证码。
|
||||
// 命中范围:扫码会话(扫码时写入 openid)与公众号内发起的待认领会话(创建即带 openid)。
|
||||
// 他人发起的、尚未扫码的会话 openid 为空,不会被命中,避免绑定串号。
|
||||
func LatestPendingVerifyByOpenID(openid string) (*models.WechatMpVerifyCode, error) {
|
||||
openid = strings.TrimSpace(openid)
|
||||
if openid == "" {
|
||||
@@ -313,7 +286,6 @@ func LatestPendingVerifyByOpenID(openid string) (*models.WechatMpVerifyCode, err
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// UpsertSubscribe 关注事件:创建/恢复粉丝记录
|
||||
func UpsertSubscribe(openid string, at time.Time) (*models.WechatMpFollower, error) {
|
||||
openid = strings.TrimSpace(openid)
|
||||
if openid == "" {
|
||||
@@ -343,7 +315,6 @@ func UpsertSubscribe(openid string, at time.Time) (*models.WechatMpFollower, err
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
// MarkUnsubscribe 取关事件
|
||||
func MarkUnsubscribe(openid string, at time.Time) error {
|
||||
openid = strings.TrimSpace(openid)
|
||||
if openid == "" {
|
||||
@@ -357,7 +328,6 @@ func MarkUnsubscribe(openid string, at time.Time) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// RefreshFollowerProfile 尝试拉取粉丝昵称/头像(未认证公众号可能受限,失败静默忽略)
|
||||
func RefreshFollowerProfile(cfg *Config, openid string) {
|
||||
if cfg == nil || openid == "" {
|
||||
return
|
||||
@@ -383,7 +353,6 @@ func RefreshFollowerProfile(cfg *Config, openid string) {
|
||||
_, _ = models.Orm.QueryTable(new(models.WechatMpFollower)).Filter("openid", openid).Update(up)
|
||||
}
|
||||
|
||||
// HandleInbound 处理入站消息/事件,返回需要被动回复的文本(空字符串表示不回复)
|
||||
func HandleInbound(cfg *Config, msg *InboundMessage) string {
|
||||
if msg == nil {
|
||||
return ""
|
||||
@@ -398,13 +367,11 @@ func HandleInbound(cfg *Config, msg *InboundMessage) string {
|
||||
_, _ = UpsertSubscribe(openid, now)
|
||||
scene := strings.TrimPrefix(strings.TrimSpace(msg.EventKey), "qrscene_")
|
||||
if scene != "" {
|
||||
// 带参二维码:先看是否为扫码登录会话(自动回复确认链接)
|
||||
if reply, ok := HandleScanLoginEvent(scene, openid); ok {
|
||||
return reply
|
||||
}
|
||||
return handleScanScene(cfg, scene, openid)
|
||||
}
|
||||
// 普通关注:已绑定直接给「确认登录」链接,未绑定提示先绑定
|
||||
return DirectLoginReply(openid)
|
||||
case "scan":
|
||||
scene := strings.TrimSpace(msg.EventKey)
|
||||
@@ -416,7 +383,6 @@ func HandleInbound(cfg *Config, msg *InboundMessage) string {
|
||||
_ = MarkUnsubscribe(openid, now)
|
||||
return ""
|
||||
case "click":
|
||||
// 自定义菜单点击:键值为登录入口时直接回复确认登录链接
|
||||
if isLoginMenuKey(msg.EventKey) {
|
||||
return DirectLoginReply(openid)
|
||||
}
|
||||
@@ -428,17 +394,11 @@ func HandleInbound(cfg *Config, msg *InboundMessage) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// handleTextIntent 处理公众号文本消息:
|
||||
// - 发送「登录」:已绑定用户获取一次性登录链接(见 login.go);
|
||||
// - 发送「验证码」等关键词:返回该微信号的验证码(复用未核销会话;没有则在公众号侧
|
||||
// 新建「待认领」会话),用户回系统页面输入即完成绑定,无需先扫码;
|
||||
// - 其余文本:给出绑定引导。
|
||||
func handleTextIntent(openid, content string) string {
|
||||
text := strings.TrimSpace(content)
|
||||
if isLoginKeyword(text) {
|
||||
return DirectLoginReply(openid)
|
||||
}
|
||||
// 「注册」:注册页账号注册方式取码(无二维码兜底路径)
|
||||
if isRegisterKeyword(text) {
|
||||
row, err := LatestPendingRegisterVerifyByOpenID(openid)
|
||||
if err != nil || row == nil {
|
||||
@@ -458,10 +418,8 @@ func handleTextIntent(openid, content string) string {
|
||||
if !isBindKeyword(text) {
|
||||
return textBindGuide()
|
||||
}
|
||||
// 1) 复用该微信号未核销、未过期的会话(扫码会话或此前公众号发起的会话)
|
||||
row, err := LatestPendingVerifyByOpenID(openid)
|
||||
if err != nil || row == nil {
|
||||
// 2) 公众号侧直接发起:新建待认领会话,绑定目标由系统页面登录用户输入验证码后认领
|
||||
row, err = CreateOpenIDVerify(openid, 0)
|
||||
if err != nil {
|
||||
return "系统繁忙,请稍后重新发送。"
|
||||
@@ -480,12 +438,10 @@ func handleTextIntent(openid, content string) string {
|
||||
return fmt.Sprintf("您的验证码是:%s\n请在系统页面「绑定微信」处输入完成绑定(%d 分钟内有效)。%s", row.Code, minutes, tip)
|
||||
}
|
||||
|
||||
// isRegisterKeyword 判断文本是否为「注册」意图(注册页账号注册取码)
|
||||
func isRegisterKeyword(text string) bool {
|
||||
return text == "注册" || strings.Contains(text, "注册")
|
||||
}
|
||||
|
||||
// isBindKeyword 判断文本是否为「获取验证码 / 绑定」类意图
|
||||
func isBindKeyword(text string) bool {
|
||||
if text == "" {
|
||||
return false
|
||||
@@ -497,13 +453,11 @@ func isBindKeyword(text string) bool {
|
||||
return strings.Contains(text, "验证码")
|
||||
}
|
||||
|
||||
// textBindGuide 非关键词文本的绑定引导
|
||||
func textBindGuide() string {
|
||||
return "如需绑定账号:请在系统页面点击「绑定微信」,然后直接在本公众号发送「验证码」," +
|
||||
"收到 6 位验证码后回到系统页面输入即可完成绑定。\n绑定后在本公众号发送「登录」可获取一次性登录链接。"
|
||||
}
|
||||
|
||||
// BindGuides 系统页面「绑定微信」卡片的操作指引(未认证公众号无带参二维码,仅引导发码取码)
|
||||
func BindGuides() []string {
|
||||
return []string{
|
||||
"① 用微信扫码关注本公众号(或微信搜索公众号名称进入会话)",
|
||||
@@ -512,7 +466,6 @@ func BindGuides() []string {
|
||||
}
|
||||
}
|
||||
|
||||
// handleScanScene 扫码(关注/已关注)后的统一处理:写入验证码会话 + 回复验证码
|
||||
func handleScanScene(cfg *Config, scene, openid string) string {
|
||||
if strings.TrimSpace(scene) == "" {
|
||||
return "未能识别二维码信息,请在系统页面重新获取二维码后再扫码。"
|
||||
@@ -536,7 +489,6 @@ func handleScanScene(cfg *Config, scene, openid string) string {
|
||||
}); uerr != nil {
|
||||
return "系统繁忙,请稍后重新扫码。"
|
||||
}
|
||||
// 拉取昵称头像(失败不影响绑定流程)
|
||||
RefreshFollowerProfile(cfg, openid)
|
||||
|
||||
minutes := int(time.Until(*row.ExpireAt).Minutes())
|
||||
@@ -546,8 +498,6 @@ func handleScanScene(cfg *Config, scene, openid string) string {
|
||||
return fmt.Sprintf("您的验证码是:%s\n请回到系统页面输入完成绑定(%d 分钟内有效)。", row.Code, minutes)
|
||||
}
|
||||
|
||||
// ============================ 随机值 ============================
|
||||
|
||||
func randomScene() string {
|
||||
b := make([]byte, 16)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
|
||||
Reference in new issue
Block a user