user.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package qiyuesuosdk
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "git.sxidc.com/student-physical-examination/contract_lock_sdk/model/common"
  10. user_request "git.sxidc.com/student-physical-examination/contract_lock_sdk/model/user/request"
  11. v2auth_request "git.sxidc.com/student-physical-examination/contract_lock_sdk/model/v2auth/request"
  12. v2auth_response "git.sxidc.com/student-physical-examination/contract_lock_sdk/model/v2auth/response"
  13. )
  14. // FindUserByAccountNo 按 accountNo(通常等于 openUserId)查询用户。
  15. func (c *Client) FindUserByAccountNo(accountNo string) (*UserInfo, error) {
  16. type result struct {
  17. Name string `json:"name"`
  18. }
  19. var resp struct {
  20. apiResponse
  21. Result result `json:"result"`
  22. }
  23. q := url.Values{"accountNo": {accountNo}}
  24. if err := c.getQuery("/user", q, &resp); err != nil {
  25. return nil, err
  26. }
  27. if err := resp.err(); err != nil {
  28. return nil, err
  29. }
  30. if resp.Result.Name == "" {
  31. return nil, nil
  32. }
  33. return &UserInfo{Name: resp.Result.Name}, nil
  34. }
  35. // CreateInternalUser 在契约锁创建内部用户。
  36. func (c *Client) CreateInternalUser(p CreateUserParams) error {
  37. req := user_request.UserV2CreateRequest{
  38. Name: p.Name,
  39. OpenUserId: p.OpenUserID,
  40. AccountNo: p.AccountNo,
  41. Password: p.Password,
  42. Mobile: p.Mobile,
  43. }
  44. if req.AccountNo == "" {
  45. req.AccountNo = p.OpenUserID
  46. }
  47. var resp apiResponse
  48. if err := c.postJSON("/user/v2/create", req, &resp); err != nil {
  49. return err
  50. }
  51. return resp.err()
  52. }
  53. // CreateInternalUserSkipMobileConflict 创建用户;手机号冲突时去掉手机号重试(旧系统行为)。
  54. func (c *Client) CreateInternalUserSkipMobileConflict(p CreateUserParams) error {
  55. err := c.CreateInternalUser(p)
  56. if err == nil {
  57. return nil
  58. }
  59. if p.Mobile != "" && strings.Contains(err.Error(), "手机号已被其他用户绑定") {
  60. p.Mobile = ""
  61. return c.CreateInternalUser(p)
  62. }
  63. return err
  64. }
  65. // UserSignSilentURL 获取个人签名静默授权页;authEnd 为空则默认 10 个月。
  66. func (c *Client) UserSignSilentURL(openUserID string, authEnd time.Time, completeToPage string) (string, error) {
  67. if authEnd.IsZero() {
  68. authEnd = time.Now().Add(DefaultPersonalAuthDuration)
  69. }
  70. timeEditable := false
  71. req := v2auth_request.V2AuthPersonalsignsilentUrlRequest{
  72. AuthUser: &common.SilentUserRequest{OpenUserId: openUserID},
  73. AuthEndDate: authEnd.Format("2006-01-02"),
  74. AuthTimeModifiable: &timeEditable,
  75. AuthorizedMode: []string{"FACEAUTH", "PINAUTH"},
  76. CompleteToPage: completeToPage,
  77. }
  78. var resp struct {
  79. apiResponse
  80. Result v2auth_response.V2AuthPersonalsignsilentUrlResponse `json:"result"`
  81. }
  82. if err := c.postJSON("/v2/auth/personalsignsilent/url", req, &resp); err != nil {
  83. return "", err
  84. }
  85. if err := resp.err(); err != nil {
  86. return "", err
  87. }
  88. return resp.Result.Url, nil
  89. }
  90. // QueryPersonalSignAuthRecords 查询个人静默授权记录。
  91. func (c *Client) QueryPersonalSignAuthRecords(openUserID, status string) ([]AuthRecord, error) {
  92. req := v2auth_request.V2AuthSignsilentRecordRequest{
  93. AuthUser: &common.UserInfoRequest{OpenUserId: openUserID},
  94. Status: status,
  95. }
  96. var resp struct {
  97. apiResponse
  98. Result []v2auth_response.V2AuthSignsilentRecordResponse `json:"result"`
  99. }
  100. if err := c.postJSON("/v2/auth/signSilent/record", req, &resp); err != nil {
  101. return nil, err
  102. }
  103. if err := resp.err(); err != nil {
  104. return nil, err
  105. }
  106. out := make([]AuthRecord, 0, len(resp.Result))
  107. for _, item := range resp.Result {
  108. out = append(out, AuthRecord{
  109. Status: item.Status,
  110. EndTime: item.AuthEndDate,
  111. AuthScope: item.AuthScope,
  112. })
  113. }
  114. return out, nil
  115. }
  116. // IsUserSignAuthorized 是否存在有效个人静默授权。
  117. func (c *Client) IsUserSignAuthorized(openUserID string) (bool, error) {
  118. records, err := c.QueryPersonalSignAuthRecords(openUserID, "EFFECT")
  119. if err != nil {
  120. return false, err
  121. }
  122. return len(records) > 0, nil
  123. }
  124. // structFormFields 将 struct 转为 multipart 表单字段(与旧系统 HttpMultipart 行为一致)。
  125. func structFormFields(v any, arrayConnector string) (map[string]string, error) {
  126. b, err := json.Marshal(v)
  127. if err != nil {
  128. return nil, err
  129. }
  130. var raw map[string]any
  131. if err = json.Unmarshal(b, &raw); err != nil {
  132. return nil, err
  133. }
  134. out := make(map[string]string, len(raw))
  135. for k, val := range raw {
  136. switch t := val.(type) {
  137. case nil:
  138. continue
  139. case string:
  140. out[k] = t
  141. case bool:
  142. out[k] = strconv.FormatBool(t)
  143. case float64:
  144. out[k] = strconv.FormatInt(int64(t), 10)
  145. case []any:
  146. parts := make([]string, 0, len(t))
  147. for _, item := range t {
  148. parts = append(parts, fmt.Sprint(item))
  149. }
  150. out[k] = strings.Join(parts, arrayConnector)
  151. default:
  152. out[k] = fmt.Sprint(val)
  153. }
  154. }
  155. return out, nil
  156. }