后端直接处理实名认证回调

This commit is contained in:
2025-12-02 17:02:44 +08:00
parent d96f4bc4ef
commit e1e499c19b
3 changed files with 27 additions and 66 deletions

View File

@@ -1,7 +1,5 @@
## TODO ## TODO
活体认证回调在后端实现
实现 sse 检查订单,减少请求次数 实现 sse 检查订单,减少请求次数
trade/create 性能问题,缩短事务时间,考虑其他方式实现可靠分布式事务 trade/create 性能问题,缩短事务时间,考虑其他方式实现可靠分布式事务

View File

@@ -2,14 +2,12 @@ package handlers
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"platform/pkg/env" "platform/pkg/env"
"platform/pkg/u" "platform/pkg/u"
"platform/web/auth" "platform/web/auth"
"platform/web/core" "platform/web/core"
g "platform/web/globals" g "platform/web/globals"
m "platform/web/models"
q "platform/web/queries" q "platform/web/queries"
"platform/web/services" "platform/web/services"
"time" "time"
@@ -18,7 +16,6 @@ import (
jdc "github.com/jdcloud-api/jdcloud-sdk-go/core" jdc "github.com/jdcloud-api/jdcloud-sdk-go/core"
"github.com/jdcloud-api/jdcloud-sdk-go/services/cloudauth/apis" "github.com/jdcloud-api/jdcloud-sdk-go/services/cloudauth/apis"
jdclient "github.com/jdcloud-api/jdcloud-sdk-go/services/cloudauth/client" jdclient "github.com/jdcloud-api/jdcloud-sdk-go/services/cloudauth/client"
"github.com/redis/go-redis/v9"
) )
// region Identify // region Identify
@@ -104,40 +101,27 @@ func Identify(c *fiber.Ctx) error {
// endregion // endregion
// region IdentifyCallback // IdentifyCallbackNew 更新用户实名认证状态
func IdentifyCallbackNew(c *fiber.Ctx) error {
type IdentifyCallbackReq struct {
Id string `json:"id" validate:"required"`
}
type IdentifyCallbackRes struct {
Success bool `json:"success"`
Message string `json:"desc"`
}
func IdentifyCallback(c *fiber.Ctx) error {
// 解析请求参数 // 解析请求参数
req := new(IdentifyCallbackReq) req := new(struct {
if err := c.BodyParser(req); err != nil { Id string `json:"id" validate:"required"`
return err Success bool `json:"success" validate:"required"`
})
if err := c.QueryParser(req); err != nil {
return core.NewBizErr("解析请求参数失败", err)
} }
// 获取 token // 获取 token
infoStr, err := g.Redis.Get(c.Context(), idenKey(req.Id)).Result() infoStr, err := g.Redis.GetDel(c.Context(), idenKey(req.Id)).Bytes()
if err != nil { if err != nil {
if errors.Is(err, redis.Nil) { return core.NewBizErr("实名认证状态已失效", err)
return c.JSON(IdentifyCallbackRes{
Success: true,
Message: "认证已完成",
})
}
return err
} }
info := idenInfo{} info := idenInfo{}
err = json.Unmarshal([]byte(infoStr), &info) err = json.Unmarshal(infoStr, &info)
if err != nil { if err != nil {
return err return core.NewServErr("解析实名认证信息失败", err)
} }
// 获取认证结果 // 获取认证结果
@@ -147,55 +131,32 @@ func IdentifyCallback(c *fiber.Ctx) error {
info.Token, info.Token,
)) ))
if err != nil { if err != nil {
return err return core.NewServErr("获取实名认证结果失败", err)
} }
if resp.Error.Code != 0 { if resp.Error.Code != 0 {
return c.JSON(IdentifyCallbackRes{ return core.NewServErr(fmt.Sprintf("获取实名认证结果失败: %s", resp.Error.Message))
Success: false,
Message: resp.Error.Message,
})
} }
if resp.Result.H5Result != "ok" || resp.Result.SmResult != "ok" || resp.Result.RxResult != "ok" { if resp.Result.H5Result != "ok" || resp.Result.SmResult != "ok" || resp.Result.RxResult != "ok" {
return c.JSON(IdentifyCallbackRes{ return core.NewBizErr(fmt.Sprintf("实名认证失败: %s", resp.Result.Desc))
Success: false,
Message: resp.Result.Desc,
})
} }
// 更新用户实名认证状态 // 更新用户实名认证状态
_, err = q.User.Debug(). _, err = q.User.Debug().
Where(q.User.ID.Eq(info.Uid)). Where(q.User.ID.Eq(info.Uid)).
Select( UpdateSimple(
q.User.IDType, q.User.IDType.Value(info.Type),
q.User.IDNo, q.User.IDNo.Value(info.IdNo),
q.User.IDToken, q.User.Name.Value(info.Name),
q.User.Name, q.User.IDToken.Value(info.Token),
). )
Updates(m.User{
IDType: m.UserIDType(info.Type),
IDNo: &info.IdNo,
IDToken: &info.Token,
Name: &info.Name,
})
if err != nil { if err != nil {
return err return core.NewServErr("更新用户实名信息失败", err)
} }
// 删除认证中间状态 // 返回结果页面
err = g.Redis.Del(c.Context(), idenKey(req.Id)).Err() return c.SendString("🎉认证成功!现在可以安全关闭这个页面")
if err != nil {
return err
}
return c.JSON(IdentifyCallbackRes{
Success: true,
Message: resp.Result.Desc,
})
} }
// endregion
func idenKey(id string) string { func idenKey(id string) string {
return fmt.Sprintf("iden:%s", id) return fmt.Sprintf("iden:%s", id)
} }

View File

@@ -23,7 +23,6 @@ func ApplyRouters(app *fiber.App) {
user.Post("/update/account", handlers.UpdateAccount) user.Post("/update/account", handlers.UpdateAccount)
user.Post("/update/password", handlers.UpdatePassword) user.Post("/update/password", handlers.UpdatePassword)
user.Post("/identify", handlers.Identify) user.Post("/identify", handlers.Identify)
user.Post("/identify/callback", handlers.IdentifyCallback)
// 白名单 // 白名单
whitelist := api.Group("/whitelist") whitelist := api.Group("/whitelist")
@@ -77,4 +76,7 @@ func ApplyRouters(app *fiber.App) {
debug := app.Group("/debug") debug := app.Group("/debug")
debug.Get("/sms/:phone", handlers.DebugGetSmsCode) debug.Get("/sms/:phone", handlers.DebugGetSmsCode)
debug.Get("/proxy/register", handlers.DebugRegisterProxyBaiYin) debug.Get("/proxy/register", handlers.DebugRegisterProxyBaiYin)
callbacks := app.Group("/callback")
callbacks.Get("/identify", handlers.IdentifyCallbackNew)
} }