user.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. // applyCompany 传入当前体检机构(openCompanyId 通常等于 tenant_id),授权记录按机构维度拆分。
  67. func (c *Client) UserSignSilentURL(openUserID string, applyCompany ApplyCompanyParams, authEnd time.Time, completeToPage string) (string, error) {
  68. if authEnd.IsZero() {
  69. authEnd = time.Now().Add(DefaultPersonalAuthDuration)
  70. }
  71. timeEditable := false
  72. req := v2auth_request.V2AuthPersonalsignsilentUrlRequest{
  73. AuthUser: &common.SilentUserRequest{OpenUserId: openUserID},
  74. AuthEndDate: authEnd.Format("2006-01-02"),
  75. AuthTimeModifiable: &timeEditable,
  76. AuthorizedMode: []string{"FACEAUTH", "PINAUTH"},
  77. CompleteToPage: completeToPage,
  78. }
  79. if applyCompany.OpenCompanyID != "" || applyCompany.Name != "" || applyCompany.RegisterNo != "" {
  80. req.ApplyCompany = &common.PersonalApplyCompany{
  81. Name: applyCompany.Name,
  82. RegisterNo: applyCompany.RegisterNo,
  83. OpenCompanyId: applyCompany.OpenCompanyID,
  84. }
  85. }
  86. var resp struct {
  87. apiResponse
  88. Result v2auth_response.V2AuthPersonalsignsilentUrlResponse `json:"result"`
  89. }
  90. if err := c.postJSON("/v2/auth/personalsignsilent/url", req, &resp); err != nil {
  91. return "", err
  92. }
  93. if err := resp.err(); err != nil {
  94. return "", err
  95. }
  96. return resp.Result.Url, nil
  97. }
  98. // QueryPersonalSignAuthRecords 查询个人静默授权记录。
  99. func (c *Client) QueryPersonalSignAuthRecords(openUserID, status string) ([]AuthRecord, error) {
  100. req := v2auth_request.V2AuthSignsilentRecordRequest{
  101. AuthUser: &common.UserInfoRequest{OpenUserId: openUserID},
  102. Status: status,
  103. }
  104. var resp struct {
  105. apiResponse
  106. Result []v2auth_response.V2AuthSignsilentRecordResponse `json:"result"`
  107. }
  108. if err := c.postJSON("/v2/auth/signSilent/record", req, &resp); err != nil {
  109. return nil, err
  110. }
  111. if err := resp.err(); err != nil {
  112. return nil, err
  113. }
  114. out := make([]AuthRecord, 0, len(resp.Result))
  115. for _, item := range resp.Result {
  116. out = append(out, authRecordFromResponse(item))
  117. }
  118. return out, nil
  119. }
  120. func authRecordFromResponse(item v2auth_response.V2AuthSignsilentRecordResponse) AuthRecord {
  121. rec := AuthRecord{
  122. Status: item.Status,
  123. EndTime: item.AuthEndDate,
  124. AuthScope: item.AuthScope,
  125. }
  126. if item.ApplyCompany != nil {
  127. rec.ApplyCompanyOpenCompanyID = item.ApplyCompany.OpenCompanyId
  128. rec.ApplyCompanyName = item.ApplyCompany.Name
  129. }
  130. return rec
  131. }
  132. // PersonalSignAuthRecordForCompany 查询指定 openUserId 对某法人单位的静默授权记录。
  133. func (c *Client) PersonalSignAuthRecordForCompany(openUserID, openCompanyID, status string) (*AuthRecord, bool, error) {
  134. records, err := c.QueryPersonalSignAuthRecords(openUserID, status)
  135. if err != nil {
  136. return nil, false, err
  137. }
  138. for i := range records {
  139. if records[i].ApplyCompanyOpenCompanyID == openCompanyID {
  140. return &records[i], true, nil
  141. }
  142. }
  143. return nil, false, nil
  144. }
  145. // IsUserSignAuthorized 是否存在任意有效个人静默授权。
  146. func (c *Client) IsUserSignAuthorized(openUserID string) (bool, error) {
  147. records, err := c.QueryPersonalSignAuthRecords(openUserID, "EFFECT")
  148. if err != nil {
  149. return false, err
  150. }
  151. return len(records) > 0, nil
  152. }
  153. // IsUserSignAuthorizedForCompany 是否已对指定法人单位完成有效个人静默授权。
  154. func (c *Client) IsUserSignAuthorizedForCompany(openUserID, openCompanyID string) (bool, error) {
  155. _, found, err := c.PersonalSignAuthRecordForCompany(openUserID, openCompanyID, "EFFECT")
  156. return found, err
  157. }
  158. // structFormFields 将 struct 转为 multipart 表单字段(与旧系统 HttpMultipart 行为一致)。
  159. func structFormFields(v any, arrayConnector string) (map[string]string, error) {
  160. b, err := json.Marshal(v)
  161. if err != nil {
  162. return nil, err
  163. }
  164. var raw map[string]any
  165. if err = json.Unmarshal(b, &raw); err != nil {
  166. return nil, err
  167. }
  168. out := make(map[string]string, len(raw))
  169. for k, val := range raw {
  170. switch t := val.(type) {
  171. case nil:
  172. continue
  173. case string:
  174. out[k] = t
  175. case bool:
  176. out[k] = strconv.FormatBool(t)
  177. case float64:
  178. out[k] = strconv.FormatInt(int64(t), 10)
  179. case []any:
  180. parts := make([]string, 0, len(t))
  181. for _, item := range t {
  182. parts = append(parts, fmt.Sprint(item))
  183. }
  184. out[k] = strings.Join(parts, arrayConnector)
  185. default:
  186. out[k] = fmt.Sprint(val)
  187. }
  188. }
  189. return out, nil
  190. }