user.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. if IsPersonalSignAuthRecordNotFound(err) {
  119. return []AuthRecord{}, nil
  120. }
  121. return nil, err
  122. }
  123. out := make([]AuthRecord, 0, len(resp.Result))
  124. for _, item := range resp.Result {
  125. out = append(out, authRecordFromResponse(item))
  126. }
  127. return out, nil
  128. }
  129. func authRecordFromResponse(item v2auth_response.V2AuthSignsilentRecordResponse) AuthRecord {
  130. rec := AuthRecord{
  131. Status: item.Status,
  132. EndTime: item.AuthEndDate,
  133. AuthScope: item.AuthScope,
  134. }
  135. if item.ApplyCompany != nil {
  136. rec.ApplyCompanyID = item.ApplyCompany.Id
  137. rec.ApplyCompanyOpenCompanyID = item.ApplyCompany.OpenCompanyId
  138. rec.ApplyCompanyName = item.ApplyCompany.Name
  139. rec.ApplyCompanyRegisterNo = item.ApplyCompany.RegisterNo
  140. }
  141. return rec
  142. }
  143. // PersonalSignAuthRecordForCompany 查询 openUserId 对指定法人单位的静默授权记录。
  144. func (c *Client) PersonalSignAuthRecordForCompany(openUserID string, company ApplyCompanyParams, status string) (*AuthRecord, bool, error) {
  145. records, err := c.QueryPersonalSignAuthRecords(openUserID, status)
  146. if err != nil {
  147. return nil, false, err
  148. }
  149. if rec, ok := findAuthRecordForCompany(records, company, status); ok {
  150. return rec, true, nil
  151. }
  152. if status != "" {
  153. all, err := c.QueryPersonalSignAuthRecords(openUserID, "")
  154. if err != nil {
  155. return nil, false, err
  156. }
  157. if rec, ok := findAuthRecordForCompany(all, company, status); ok {
  158. return rec, true, nil
  159. }
  160. }
  161. return nil, false, nil
  162. }
  163. func findAuthRecordForCompany(records []AuthRecord, company ApplyCompanyParams, wantStatus string) (*AuthRecord, bool) {
  164. for i := range records {
  165. if wantStatus != "" && records[i].Status != wantStatus {
  166. continue
  167. }
  168. if authRecordMatchesCompany(&records[i], company) {
  169. return &records[i], true
  170. }
  171. }
  172. return nil, false
  173. }
  174. func authRecordMatchesCompany(rec *AuthRecord, company ApplyCompanyParams) bool {
  175. if company.CompanyID != "" && rec.ApplyCompanyID != "" && company.CompanyID == rec.ApplyCompanyID {
  176. return true
  177. }
  178. if company.OpenCompanyID != "" && rec.ApplyCompanyOpenCompanyID != "" && company.OpenCompanyID == rec.ApplyCompanyOpenCompanyID {
  179. return true
  180. }
  181. if company.RegisterNo != "" && rec.ApplyCompanyRegisterNo != "" && company.RegisterNo == rec.ApplyCompanyRegisterNo {
  182. return true
  183. }
  184. return false
  185. }
  186. // IsUserSignAuthorized 是否存在任意有效个人静默授权。
  187. func (c *Client) IsUserSignAuthorized(openUserID string) (bool, error) {
  188. records, err := c.QueryPersonalSignAuthRecords(openUserID, "EFFECT")
  189. if err != nil {
  190. return false, err
  191. }
  192. return len(records) > 0, nil
  193. }
  194. // IsUserSignAuthorizedForCompany 是否已对指定法人单位完成有效个人静默授权。
  195. func (c *Client) IsUserSignAuthorizedForCompany(openUserID string, company ApplyCompanyParams) (bool, error) {
  196. _, found, err := c.PersonalSignAuthRecordForCompany(openUserID, company, "EFFECT")
  197. return found, err
  198. }
  199. // structFormFields 将 struct 转为 multipart 表单字段(与旧系统 HttpMultipart 行为一致)。
  200. func structFormFields(v any, arrayConnector string) (map[string]string, error) {
  201. b, err := json.Marshal(v)
  202. if err != nil {
  203. return nil, err
  204. }
  205. var raw map[string]any
  206. if err = json.Unmarshal(b, &raw); err != nil {
  207. return nil, err
  208. }
  209. out := make(map[string]string, len(raw))
  210. for k, val := range raw {
  211. switch t := val.(type) {
  212. case nil:
  213. continue
  214. case string:
  215. out[k] = t
  216. case bool:
  217. out[k] = strconv.FormatBool(t)
  218. case float64:
  219. out[k] = strconv.FormatInt(int64(t), 10)
  220. case []any:
  221. parts := make([]string, 0, len(t))
  222. for _, item := range t {
  223. parts = append(parts, fmt.Sprint(item))
  224. }
  225. out[k] = strings.Join(parts, arrayConnector)
  226. default:
  227. out[k] = fmt.Sprint(val)
  228. }
  229. }
  230. return out, nil
  231. }