account_controller.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package user
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/common/exception"
  5. "eta/eta_mini_ht_api/controllers"
  6. "eta/eta_mini_ht_api/service/facade"
  7. "eta/eta_mini_ht_api/service/user"
  8. "github.com/google/uuid"
  9. "net/url"
  10. "strings"
  11. )
  12. type AccountController struct {
  13. controllers.BaseController
  14. }
  15. type RiskInfoTokenResp struct {
  16. Token string `json:"token"`
  17. }
  18. const (
  19. TraceHeaderKey = "X-Trace-ID"
  20. MDCTraceId = "traceId"
  21. )
  22. const (
  23. AccountNotOpen = 430
  24. IDExpired = 431
  25. RiskUnTest = 432
  26. RiskNotMatch = 433
  27. RiskExpired = 434
  28. AccountOpening = 435
  29. orderPending = "pending"
  30. orderPaid = "paid"
  31. orderRefund = "refund"
  32. orderClosed = "closed"
  33. )
  34. func (a *AccountController) Prepare() {
  35. var requestBody string
  36. uri := a.Ctx.Input.URI()
  37. method := a.Ctx.Input.Method()
  38. if method == "GET" {
  39. requestBody = a.Ctx.Request.RequestURI
  40. } else {
  41. requestBody, _ = url.QueryUnescape(string(a.Ctx.Input.RequestBody))
  42. }
  43. ip := a.Ctx.Input.IP()
  44. a.Ctx.Input.URL()
  45. traceId := uuid.NewString()
  46. traceId = strings.ReplaceAll(traceId, "-", "")
  47. a.Ctx.Output.Header(TraceHeaderKey, traceId)
  48. //// 设置MDC中的traceId
  49. a.Ctx.Input.SetData(MDCTraceId, traceId)
  50. // 清理MDC中的traceId
  51. logger.InfoWithTraceId(a.Ctx, "webhookRequest:[uri:%s, requestBody:%s, ip:%s]", uri, requestBody, ip)
  52. }
  53. // GetRiskInfoToken 获取风险测评问卷请求Token
  54. // @Summary 获取风险测评问卷请求Token
  55. // @Description 获取风险测评问卷请求Token
  56. // @Success 200 {object} controllers.BaseResponse
  57. // @router /v1/riskTestToken/ [get]
  58. func (a *AccountController) GetRiskInfoToken() {
  59. controllers.Wrap(&a.BaseController, func() (result *controllers.WrapData, err error) {
  60. result = a.InitWrapData("获取风险测评问卷请求Token失败")
  61. userInfo := a.Data["user"].(user.User)
  62. token, err := facade.GetRiskTestToken(userInfo.Id, userInfo.Mobile)
  63. if err != nil {
  64. logger.InfoWithTraceId(a.Ctx, err.Error())
  65. a.FailedResult("风险测评问卷请求token生成失败", result)
  66. return
  67. }
  68. result = a.InitWrapData("风险测评问卷请求token生成成功")
  69. a.SuccessResult("success", RiskInfoTokenResp{
  70. Token: token,
  71. }, result)
  72. return
  73. })
  74. }
  75. // CheckUserStatus 获取风险测评问卷请求Token
  76. // @Summary 获取风险测评问卷请求Token
  77. // @Description 获取风险测评问卷请求Token
  78. // @Success 200 {object} controllers.BaseResponse
  79. // @router /checkUserStatus/ [post]
  80. func (a *AccountController) CheckUserStatus() {
  81. controllers.Wrap(&a.BaseController, func() (result *controllers.WrapData, err error) {
  82. result = a.InitWrapData("获取风险测评问卷请求Token失败")
  83. userInfo := a.Data["user"].(user.User)
  84. //校验是否已经购买并且在有效期内
  85. //是否开户
  86. //未开户
  87. officialUser, err := user.GetUserByTemplateUserId(userInfo.Id)
  88. if err != nil {
  89. if err.Error() == exception.GetMsg(exception.OfficialUserNotFound) {
  90. err = exception.New(exception.OfficialUserNotFound)
  91. result.Ret = AccountNotOpen
  92. a.FailedResult("用户未开通账户", result)
  93. } else {
  94. a.FailedResult("获取用户账户信息失败", result)
  95. }
  96. return
  97. }
  98. switch officialUser.AccountStatus {
  99. case "unopen":
  100. result.Ret = AccountNotOpen
  101. err = exception.New(exception.AccountNotOpen)
  102. a.FailedResult("用户未开通账户", result)
  103. return
  104. case "opening":
  105. result.Ret = AccountOpening
  106. err = exception.New(exception.AccountNotOpen)
  107. a.FailedResult("用户开户中", result)
  108. return
  109. case "opened":
  110. default:
  111. err = exception.New(exception.IllegalAccountStatus)
  112. a.FailedResult(result.Msg, result)
  113. return
  114. }
  115. //证件信息是否过期
  116. if !officialUser.IDValid {
  117. err = exception.New(exception.IDExpired)
  118. result.Ret = IDExpired
  119. a.FailedResult("用户证件有效期过期", result)
  120. return
  121. }
  122. a.SuccessResult("校验用户信息成功", nil, result)
  123. return
  124. })
  125. }