更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -14,27 +14,15 @@ import (
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// 小程序登录失败原因(前端按此分支给出不同引导)
|
||||
const (
|
||||
MiniReasonNotConfigured = "not_configured" // 服务端未配置小程序
|
||||
MiniReasonIdentityNotFound = "identity_not_found" // 微信未匹配到温新云账号
|
||||
MiniReasonNoTenant = "no_tenant" // 账号未加入任何企业
|
||||
MiniReasonWechatAPI = "wechat_api_error" // 微信接口报错
|
||||
MiniReasonNotConfigured = "not_configured"
|
||||
MiniReasonIdentityNotFound = "identity_not_found"
|
||||
MiniReasonNoTenant = "no_tenant"
|
||||
MiniReasonWechatAPI = "wechat_api_error"
|
||||
)
|
||||
|
||||
// idPWechatMini 第三方身份源标识(写入 yz_auth_identity_third.provider)
|
||||
const idPWechatMini = "wechat_mini"
|
||||
|
||||
// AuthWechatMiniController 微信小程序一键登录(统一认证中心)
|
||||
//
|
||||
// 与公众号确认链接登录(/api/wechat/mp/login)不同,小程序端由前端直接
|
||||
// 携带 wx.login 的 code 与 getPhoneNumber 的动态 code 兑换登录态:
|
||||
//
|
||||
// POST /auth/wechat/miniapp/login
|
||||
// { "code": "wx.login的code", "phone_code": "getPhoneNumber的动态code", "client_id": "yz-miniapp" }
|
||||
//
|
||||
// 返回结构与 POST /auth/login 完全一致(tokens / need_choose_tenant / tenants 均在顶层),
|
||||
// 前端可复用同一套「写入令牌 → 选企业 → switch-tenant」流程。
|
||||
type AuthWechatMiniController struct {
|
||||
beego.Controller
|
||||
}
|
||||
@@ -49,8 +37,6 @@ func (c *AuthWechatMiniController) fail(reason string, bizCode int, msg string)
|
||||
c.serveJSON(map[string]interface{}{"code": bizCode, "msg": msg, "reason": reason})
|
||||
}
|
||||
|
||||
// loadMiniConfig 按客户端传入的 appid 选择小程序配置(多应用支持),
|
||||
// 未传 appid 时回退第一条启用配置
|
||||
func loadMiniConfig(appID string) (*wechatmini.Config, error) {
|
||||
if appID = strings.TrimSpace(appID); appID != "" {
|
||||
return wechatmini.LoadEnabledAppByAppID(appID)
|
||||
@@ -58,8 +44,6 @@ func loadMiniConfig(appID string) (*wechatmini.Config, error) {
|
||||
return wechatmini.LoadEnabledConfig()
|
||||
}
|
||||
|
||||
// Config 小程序登录是否可用
|
||||
// GET /auth/wechat/miniapp/config?appid=xxx(多应用时可指定,缺省取第一条启用配置)
|
||||
func (c *AuthWechatMiniController) Config() {
|
||||
cfg, err := loadMiniConfig(c.GetString("appid"))
|
||||
if err != nil {
|
||||
@@ -75,7 +59,6 @@ func (c *AuthWechatMiniController) Config() {
|
||||
})
|
||||
}
|
||||
|
||||
// Login 小程序一键登录
|
||||
func (c *AuthWechatMiniController) Login() {
|
||||
var req struct {
|
||||
Code string `json:"code"`
|
||||
@@ -108,7 +91,6 @@ func (c *AuthWechatMiniController) Login() {
|
||||
return
|
||||
}
|
||||
|
||||
// code → openid / unionid
|
||||
session, err := wechatmini.Code2Session(cfg, req.Code)
|
||||
if err != nil {
|
||||
msg := err.Error()
|
||||
@@ -120,12 +102,10 @@ func (c *AuthWechatMiniController) Login() {
|
||||
return
|
||||
}
|
||||
|
||||
// 定位温新云身份:手机号优先(已通过微信验证),其次查已绑定的小程序 openid
|
||||
var mobile string
|
||||
if req.PhoneCode != "" {
|
||||
phone, perr := wechatmini.GetPhoneNumber(cfg, req.PhoneCode)
|
||||
if perr != nil {
|
||||
// 手机号能力未开通 / code 失效时不阻断:仍尝试按已绑定 openid 登录
|
||||
logs.Warn("[wechat-mini] 获取手机号失败: %v", perr)
|
||||
} else {
|
||||
mobile = strings.TrimSpace(phone.PurePhoneNumber)
|
||||
@@ -145,14 +125,11 @@ func (c *AuthWechatMiniController) Login() {
|
||||
return
|
||||
}
|
||||
|
||||
// 企业选择:单企业直接进入,多企业签发待选令牌由前端展示选择界面
|
||||
tenants, lerr := authsvc.ListTenantOptions(identity.ID)
|
||||
if lerr != nil {
|
||||
c.fail("", 400, "获取企业列表失败,请重试")
|
||||
return
|
||||
}
|
||||
// 新流程:账号已注册但尚未加入企业时仍签发正式令牌(tid=0),
|
||||
// 端上可继续使用原免登录模块,并在进入需企业模块时引导创建/挂靠企业。
|
||||
noTenant := len(tenants) == 0
|
||||
tid := uint64(0)
|
||||
needChoose := false
|
||||
@@ -191,13 +168,6 @@ func (c *AuthWechatMiniController) Login() {
|
||||
c.serveJSON(resp)
|
||||
}
|
||||
|
||||
// RegisterConfirm 小程序端确认注册(网页注册页展示的小程序码扫码进入)
|
||||
// POST /auth/register/mini/confirm
|
||||
//
|
||||
// { "scene": "小程序码携带的会话标识", "code": "wx.login的code", "phone_code": "可选", "appid": "可选,多小程序" }
|
||||
//
|
||||
// 无登录态:scene + code 即凭证,服务端用 code 换 openid 后标记该会话已确认,
|
||||
// 网页端轮询到确认后继续填写账号密码;手机号快速验证为可选项(失败不阻断)。
|
||||
func (c *AuthWechatMiniController) RegisterConfirm() {
|
||||
var req struct {
|
||||
Scene string `json:"scene"`
|
||||
@@ -230,13 +200,6 @@ func (c *AuthWechatMiniController) RegisterConfirm() {
|
||||
c.serveJSON(map[string]interface{}{"code": 200, "msg": "确认成功,请回到电脑继续完成注册"})
|
||||
}
|
||||
|
||||
// Bind 把当前小程序 openid 绑定到已登录身份
|
||||
// POST /auth/wechat/miniapp/bind { "code": "wx.login的code" } Header: Bearer 令牌
|
||||
//
|
||||
// 用途:小程序认证中 / 未开通「手机号快速验证」时,getPhoneNumber 拿不到手机号,
|
||||
// 一键登录退化为「按 openid 匹配已绑定身份」。用户先用账号密码登录一次,
|
||||
// 前端调用本接口静默绑定 openid,之后即可无手机号完成微信一键登录。
|
||||
// 显式绑定视为本人操作(同时持有微信与该账号登录态),允许换绑覆盖旧绑定。
|
||||
func (c *AuthWechatMiniController) Bind() {
|
||||
claims := claimsFromCtxThird(c.Ctx)
|
||||
if claims == nil || claims.UserID <= 0 {
|
||||
@@ -284,10 +247,7 @@ func (c *AuthWechatMiniController) Bind() {
|
||||
c.serveJSON(map[string]interface{}{"code": 200, "msg": "绑定成功"})
|
||||
}
|
||||
|
||||
// resolveIdentity 定位身份并维护小程序绑定关系。
|
||||
// mobile 非空表示本次已通过微信手机号验证,是可信的强证据,优先采用。
|
||||
func (c *AuthWechatMiniController) resolveIdentity(cfg *wechatmini.Config, mobile string, session *wechatmini.Session) (*models.AuthIdentity, error) {
|
||||
// 1) 手机号命中身份(手机号已由微信验证,属于当前用户的强凭证)
|
||||
if mobile != "" {
|
||||
if identity, err := authsvc.FindIdentityByAccount(mobile); err == nil {
|
||||
c.syncBinding(cfg, identity.ID, session, true)
|
||||
@@ -295,7 +255,6 @@ func (c *AuthWechatMiniController) resolveIdentity(cfg *wechatmini.Config, mobil
|
||||
}
|
||||
}
|
||||
|
||||
// 2) 已绑定过该小程序 openid / unionid
|
||||
bind := findMiniBinding(session.OpenID, session.UnionID)
|
||||
if bind == nil {
|
||||
return nil, orm.ErrNoRows
|
||||
@@ -310,10 +269,6 @@ func (c *AuthWechatMiniController) resolveIdentity(cfg *wechatmini.Config, mobil
|
||||
return &identity, nil
|
||||
}
|
||||
|
||||
// syncBinding 把当前小程序 openid 绑定到该身份。
|
||||
// verified 表示本次带通过了微信手机号验证:此时手机号即身份凭证,
|
||||
// 允许覆盖历史绑定(用户换绑手机号 / 换微信号场景);
|
||||
// 未验证时只允许「新增绑定」,不抢占已绑定到其他身份的 openid。
|
||||
func (c *AuthWechatMiniController) syncBinding(cfg *wechatmini.Config, identityID uint64, session *wechatmini.Session, verified bool) {
|
||||
bind := findMiniBinding(session.OpenID, session.UnionID)
|
||||
if bind != nil && bind.IdentityID == identityID {
|
||||
@@ -340,7 +295,6 @@ func (c *AuthWechatMiniController) syncBinding(cfg *wechatmini.Config, identityI
|
||||
_, _ = models.Orm.Insert(item)
|
||||
}
|
||||
|
||||
// findMiniBinding 按 openid 查绑定,其次按 unionid 查
|
||||
func findMiniBinding(openID, unionID string) *models.AuthIdentityThird {
|
||||
qs := models.Orm.QueryTable(new(models.AuthIdentityThird)).Filter("provider", idPWechatMini)
|
||||
if openID != "" {
|
||||
@@ -358,11 +312,10 @@ func findMiniBinding(openID, unionID string) *models.AuthIdentityThird {
|
||||
return nil
|
||||
}
|
||||
|
||||
// issueTokens 建会话并签发 UAC 令牌(含登录日志,失败不影响主流程)
|
||||
func (c *AuthWechatMiniController) issueTokens(identityID, tid uint64, clientID, username string, needChoose bool) (*authsvc.TokenPair, error) {
|
||||
accessTTL := 0
|
||||
if needChoose {
|
||||
accessTTL = 600 // 待选状态令牌只够用户完成企业选择
|
||||
accessTTL = 600
|
||||
}
|
||||
sess, serr := authsvc.CreateSession(authsvc.SessionInfo{
|
||||
IdentityID: identityID,
|
||||
|
||||
Reference in new issue
Block a user