mop_verify_code_gateway.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "mop-account-system/common"
  7. )
  8. const (
  9. PHONE_MSG_TEMPLATE_ID_1 = 1
  10. EMAIL_MSG_TEMPLATE_ID_1 = 1
  11. )
  12. type PhoneVerifyCodeReq struct {
  13. Type string `json:"type"`
  14. Phone string `json:"phone"`
  15. Length int `json:"length"`
  16. Gap int `json:"gap"`
  17. ValidTime int `json:"validTime"`
  18. }
  19. type VerifyCodeRsp struct {
  20. Errcode string `json:"errcode"`
  21. Error string `json:"error"`
  22. Data map[string]interface{} `json:"data"`
  23. }
  24. func PhoneVerifyCode(ctx context.Context, length, gap, validTime int, codeType, phone string) (string, error) {
  25. var headers = map[string]string{
  26. "Accept": "application/json, text/plain, */*",
  27. "Content-Type": "application/json",
  28. }
  29. var req PhoneVerifyCodeReq
  30. req.Type = codeType
  31. req.Length = length
  32. req.Gap = gap
  33. req.ValidTime = validTime
  34. req.Phone = phone
  35. body, _ := json.Marshal(req)
  36. url := cfg.VerifyCodeUrlPrefix + "/api/v1/mop/verifycode-server/phone/verifyCode"
  37. rsp, err := Request(ctx).SetHeaders(headers).SetBody(body).Post(url)
  38. if err != nil {
  39. common.GinLog.Error("PhoneVerifyCode err:" + err.Error())
  40. return "", err
  41. }
  42. if rsp.StatusCode() != 200 {
  43. common.GinLog.Error("PhoneVerifyCode status err:" + rsp.String())
  44. return "", errors.New("status err")
  45. }
  46. var verifyCodeRsp VerifyCodeRsp
  47. err = json.Unmarshal(rsp.Body(), &verifyCodeRsp)
  48. if err != nil {
  49. common.GinLog.Error("json.Unmarshal error:" + err.Error())
  50. return "", err
  51. }
  52. common.GinLog.Debugf("PhoneVerifyCode rsp:%+v", rsp.String())
  53. return verifyCodeRsp.Data["verifyCode"].(string), nil
  54. }
  55. type EmailVerifyCodeReq struct {
  56. Type string `json:"type"`
  57. Email string `json:"email"`
  58. Length int `json:"length"`
  59. Gap int `json:"gap"`
  60. ValidTime int `json:"validTime"`
  61. }
  62. func EmailVerifyCode(ctx context.Context, length, gap, validTime int, codeType, email string) (string, error) {
  63. var headers = map[string]string{
  64. "Accept": "application/json, text/plain, */*",
  65. "Content-Type": "application/json",
  66. }
  67. var req EmailVerifyCodeReq
  68. req.Type = codeType
  69. req.Length = length
  70. req.Gap = gap
  71. req.ValidTime = validTime
  72. req.Email = email
  73. body, _ := json.Marshal(req)
  74. url := cfg.VerifyCodeUrlPrefix + "/api/v1/mop/verifycode-server/email/verifyCode"
  75. rsp, err := Request(ctx).SetHeaders(headers).SetBody(body).Post(url)
  76. if err != nil {
  77. common.GinLog.Error("EmailVerifyCode err:" + err.Error())
  78. return "", err
  79. }
  80. if rsp.StatusCode() != 200 {
  81. common.GinLog.Error("EmailVerifyCode status err:" + rsp.String())
  82. return "", errors.New("status err")
  83. }
  84. var verifyCodeRsp VerifyCodeRsp
  85. err = json.Unmarshal(rsp.Body(), &verifyCodeRsp)
  86. if err != nil {
  87. common.GinLog.Error("json.Unmarshal error:" + err.Error())
  88. return "", err
  89. }
  90. return verifyCodeRsp.Data["verifyCode"].(string), nil
  91. }
  92. type DelPhoneCodeLimitReq struct {
  93. Phone string `json:"phone"`
  94. }
  95. func DelPhoneCodeLimit(ctx context.Context, phone string) error {
  96. var headers = map[string]string{
  97. "Accept": "application/json, text/plain, */*",
  98. "Content-Type": "application/json",
  99. }
  100. req := DelPhoneCodeLimitReq{}
  101. req.Phone = phone
  102. body, _ := json.Marshal(req)
  103. url := cfg.VerifyCodeServerHost + cfg.DelPhoneLimitUrl
  104. common.GinLog.Debugf("DelPhoneCodeLimit url:%s", url)
  105. rsp, err := Request(ctx).SetHeaders(headers).SetBody(body).Delete(url)
  106. if err != nil {
  107. common.GinLog.Error("DelPhoneCodeLimit err:" + err.Error())
  108. return err
  109. }
  110. if rsp.StatusCode() != 200 {
  111. common.GinLog.Error("DelPhoneCodeLimit status err:" + rsp.String())
  112. return errors.New("status err")
  113. }
  114. common.GinLog.Debugf("DelPhoneCodeLimit rsp:%s", rsp.String())
  115. return nil
  116. }