diff --git a/README.md b/README.md index 4a9c88c..3fc2628 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ ## TODO -活体认证回调在后端实现 - 实现 sse 检查订单,减少请求次数 trade/create 性能问题,缩短事务时间,考虑其他方式实现可靠分布式事务 diff --git a/web/handlers/iden.go b/web/handlers/iden.go index 40dba80..5e5c2ee 100644 --- a/web/handlers/iden.go +++ b/web/handlers/iden.go @@ -2,14 +2,12 @@ package handlers import ( "encoding/json" - "errors" "fmt" "platform/pkg/env" "platform/pkg/u" "platform/web/auth" "platform/web/core" g "platform/web/globals" - m "platform/web/models" q "platform/web/queries" "platform/web/services" "time" @@ -18,7 +16,6 @@ import ( jdc "github.com/jdcloud-api/jdcloud-sdk-go/core" "github.com/jdcloud-api/jdcloud-sdk-go/services/cloudauth/apis" jdclient "github.com/jdcloud-api/jdcloud-sdk-go/services/cloudauth/client" - "github.com/redis/go-redis/v9" ) // region Identify @@ -104,40 +101,27 @@ func Identify(c *fiber.Ctx) error { // endregion -// region IdentifyCallback - -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 { +// IdentifyCallbackNew 更新用户实名认证状态 +func IdentifyCallbackNew(c *fiber.Ctx) error { // 解析请求参数 - req := new(IdentifyCallbackReq) - if err := c.BodyParser(req); err != nil { - return err + req := new(struct { + Id string `json:"id" validate:"required"` + Success bool `json:"success" validate:"required"` + }) + if err := c.QueryParser(req); err != nil { + return core.NewBizErr("解析请求参数失败", err) } // 获取 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 errors.Is(err, redis.Nil) { - return c.JSON(IdentifyCallbackRes{ - Success: true, - Message: "认证已完成", - }) - } - return err + return core.NewBizErr("实名认证状态已失效", err) } info := idenInfo{} - err = json.Unmarshal([]byte(infoStr), &info) + err = json.Unmarshal(infoStr, &info) if err != nil { - return err + return core.NewServErr("解析实名认证信息失败", err) } // 获取认证结果 @@ -147,55 +131,32 @@ func IdentifyCallback(c *fiber.Ctx) error { info.Token, )) if err != nil { - return err + return core.NewServErr("获取实名认证结果失败", err) } if resp.Error.Code != 0 { - return c.JSON(IdentifyCallbackRes{ - Success: false, - Message: resp.Error.Message, - }) + return core.NewServErr(fmt.Sprintf("获取实名认证结果失败: %s", resp.Error.Message)) } - if resp.Result.H5Result != "ok" || resp.Result.SmResult != "ok" || resp.Result.RxResult != "ok" { - return c.JSON(IdentifyCallbackRes{ - Success: false, - Message: resp.Result.Desc, - }) + return core.NewBizErr(fmt.Sprintf("实名认证失败: %s", resp.Result.Desc)) } // 更新用户实名认证状态 _, err = q.User.Debug(). Where(q.User.ID.Eq(info.Uid)). - Select( - q.User.IDType, - q.User.IDNo, - q.User.IDToken, - q.User.Name, - ). - Updates(m.User{ - IDType: m.UserIDType(info.Type), - IDNo: &info.IdNo, - IDToken: &info.Token, - Name: &info.Name, - }) + UpdateSimple( + q.User.IDType.Value(info.Type), + q.User.IDNo.Value(info.IdNo), + q.User.Name.Value(info.Name), + q.User.IDToken.Value(info.Token), + ) if err != nil { - return err + return core.NewServErr("更新用户实名信息失败", err) } - // 删除认证中间状态 - err = g.Redis.Del(c.Context(), idenKey(req.Id)).Err() - if err != nil { - return err - } - - return c.JSON(IdentifyCallbackRes{ - Success: true, - Message: resp.Result.Desc, - }) + // 返回结果页面 + return c.SendString("🎉认证成功!现在可以安全关闭这个页面") } -// endregion - func idenKey(id string) string { return fmt.Sprintf("iden:%s", id) } diff --git a/web/routes.go b/web/routes.go index d4d7669..a988563 100644 --- a/web/routes.go +++ b/web/routes.go @@ -23,7 +23,6 @@ func ApplyRouters(app *fiber.App) { user.Post("/update/account", handlers.UpdateAccount) user.Post("/update/password", handlers.UpdatePassword) user.Post("/identify", handlers.Identify) - user.Post("/identify/callback", handlers.IdentifyCallback) // 白名单 whitelist := api.Group("/whitelist") @@ -77,4 +76,7 @@ func ApplyRouters(app *fiber.App) { debug := app.Group("/debug") debug.Get("/sms/:phone", handlers.DebugGetSmsCode) debug.Get("/proxy/register", handlers.DebugRegisterProxyBaiYin) + + callbacks := app.Group("/callback") + callbacks.Get("/identify", handlers.IdentifyCallbackNew) }