user.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.CompanyID != "" || applyCompany.OpenCompanyID != "" || applyCompany.Name != "" || applyCompany.RegisterNo != "" {
  80. pac := &common.PersonalApplyCompany{
  81. Name: applyCompany.Name,
  82. RegisterNo: applyCompany.RegisterNo,
  83. OpenCompanyId: applyCompany.OpenCompanyID,
  84. }
  85. if applyCompany.CompanyID != "" {
  86. if id, err := strconv.ParseInt(applyCompany.CompanyID, 10, 64); err == nil {
  87. pac.Id = &id
  88. }
  89. }
  90. req.ApplyCompany = pac
  91. }
  92. var resp struct {
  93. apiResponse
  94. Result v2auth_response.V2AuthPersonalsignsilentUrlResponse `json:"result"`
  95. }
  96. if err := c.postJSON("/v2/auth/personalsignsilent/url", req, &resp); err != nil {
  97. return "", err
  98. }
  99. if err := resp.err(); err != nil {
  100. return "", err
  101. }
  102. return resp.Result.Url, nil
  103. }
  104. // QueryPersonalSignAuthRecords 查询个人静默授权记录。
  105. func (c *Client) QueryPersonalSignAuthRecords(openUserID, status string) ([]AuthRecord, error) {
  106. req := v2auth_request.V2AuthSignsilentRecordRequest{
  107. AuthUser: &common.UserInfoRequest{OpenUserId: openUserID},
  108. Status: status,
  109. }
  110. var resp struct {
  111. apiResponse
  112. Result []v2auth_response.V2AuthSignsilentRecordResponse `json:"result"`
  113. }
  114. if err := c.postJSON("/v2/auth/signSilent/record", req, &resp); err != nil {
  115. return nil, err
  116. }
  117. if err := resp.err(); err != nil {
  118. return nil, err
  119. }
  120. out := make([]AuthRecord, 0, len(resp.Result))
  121. for _, item := range resp.Result {
  122. out = append(out, authRecordFromResponse(item))
  123. }
  124. return out, nil
  125. }
  126. func authRecordFromResponse(item v2auth_response.V2AuthSignsilentRecordResponse) AuthRecord {
  127. rec := AuthRecord{
  128. Status: item.Status,
  129. EndTime: item.AuthEndDate,
  130. AuthScope: item.AuthScope,
  131. }
  132. if item.ApplyCompany != nil {
  133. rec.ApplyCompanyID = item.ApplyCompany.Id
  134. rec.ApplyCompanyOpenCompanyID = item.ApplyCompany.OpenCompanyId
  135. rec.ApplyCompanyName = item.ApplyCompany.Name
  136. rec.ApplyCompanyRegisterNo = item.ApplyCompany.RegisterNo
  137. }
  138. return rec
  139. }
  140. // PersonalSignAuthRecordForCompany 查询 openUserId 对指定法人单位的静默授权记录。
  141. func (c *Client) PersonalSignAuthRecordForCompany(openUserID string, company ApplyCompanyParams, status string) (*AuthRecord, bool, error) {
  142. records, err := c.QueryPersonalSignAuthRecords(openUserID, status)
  143. if err != nil {
  144. return nil, false, err
  145. }
  146. for i := range records {
  147. if authRecordMatchesCompany(&records[i], company) {
  148. return &records[i], true, nil
  149. }
  150. }
  151. return nil, false, nil
  152. }
  153. func authRecordMatchesCompany(rec *AuthRecord, company ApplyCompanyParams) bool {
  154. if company.CompanyID != "" && rec.ApplyCompanyID != "" && company.CompanyID == rec.ApplyCompanyID {
  155. return true
  156. }
  157. if company.OpenCompanyID != "" && rec.ApplyCompanyOpenCompanyID != "" && company.OpenCompanyID == rec.ApplyCompanyOpenCompanyID {
  158. return true
  159. }
  160. if company.RegisterNo != "" && rec.ApplyCompanyRegisterNo != "" && company.RegisterNo == rec.ApplyCompanyRegisterNo {
  161. return true
  162. }
  163. return false
  164. }
  165. // IsUserSignAuthorized 是否存在任意有效个人静默授权。
  166. func (c *Client) IsUserSignAuthorized(openUserID string) (bool, error) {
  167. records, err := c.QueryPersonalSignAuthRecords(openUserID, "EFFECT")
  168. if err != nil {
  169. return false, err
  170. }
  171. return len(records) > 0, nil
  172. }
  173. // IsUserSignAuthorizedForCompany 是否已对指定法人单位完成有效个人静默授权。
  174. func (c *Client) IsUserSignAuthorizedForCompany(openUserID string, company ApplyCompanyParams) (bool, error) {
  175. _, found, err := c.PersonalSignAuthRecordForCompany(openUserID, company, "EFFECT")
  176. return found, err
  177. }
  178. // structFormFields 将 struct 转为 multipart 表单字段(与旧系统 HttpMultipart 行为一致)。
  179. func structFormFields(v any, arrayConnector string) (map[string]string, error) {
  180. b, err := json.Marshal(v)
  181. if err != nil {
  182. return nil, err
  183. }
  184. var raw map[string]any
  185. if err = json.Unmarshal(b, &raw); err != nil {
  186. return nil, err
  187. }
  188. out := make(map[string]string, len(raw))
  189. for k, val := range raw {
  190. switch t := val.(type) {
  191. case nil:
  192. continue
  193. case string:
  194. out[k] = t
  195. case bool:
  196. out[k] = strconv.FormatBool(t)
  197. case float64:
  198. out[k] = strconv.FormatInt(int64(t), 10)
  199. case []any:
  200. parts := make([]string, 0, len(t))
  201. for _, item := range t {
  202. parts = append(parts, fmt.Sprint(item))
  203. }
  204. out[k] = strings.Join(parts, arrayConnector)
  205. default:
  206. out[k] = fmt.Sprint(val)
  207. }
  208. }
  209. return out, nil
  210. }