auth.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package qiyuesuosdk
  2. import (
  3. "time"
  4. v2auth_request "git.sxidc.com/student-physical-examination/contract_lock_sdk/model/v2auth/request"
  5. v2auth_response "git.sxidc.com/student-physical-examination/contract_lock_sdk/model/v2auth/response"
  6. "git.sxidc.com/student-physical-examination/contract_lock_sdk/model/common"
  7. )
  8. type companyCertPageResp struct {
  9. apiResponse
  10. AuthURL string `json:"authurl"`
  11. }
  12. type companyCertStatusResp struct {
  13. apiResponse
  14. Result CompanyAuthStatus `json:"result"`
  15. }
  16. type companySealAuthURLResp struct {
  17. apiResponse
  18. Result v2auth_response.V2AuthCompanysignsilentUrlResponse `json:"result"`
  19. }
  20. type companyAuthRecordResp struct {
  21. apiResponse
  22. Result []common.AuthorizedSealRecordBean `json:"result"`
  23. }
  24. // CompanyCertificationURL 获取法人单位在线认证页 URL。
  25. func (c *Client) CompanyCertificationURL(p OrgCertParams) (string, error) {
  26. if p.OpenCompanyID == "" || p.Mobile == "" || p.CompanyName == "" || p.Charger == "" {
  27. return "", ErrInvalidParams
  28. }
  29. type reqBody struct {
  30. Name string `json:"name"`
  31. Charger string `json:"charger"`
  32. Mobile string `json:"mobile"`
  33. OpenCompanyId string `json:"openCompanyId"`
  34. Modes []string `json:"modes"`
  35. Customer bool `json:"customer"`
  36. }
  37. fields, err := structFormFields(reqBody{
  38. Name: p.CompanyName,
  39. Charger: p.Charger,
  40. Mobile: p.Mobile,
  41. OpenCompanyId: p.OpenCompanyID,
  42. Modes: []string{"AUTHFILE", "BANKPAY"},
  43. Customer: true,
  44. }, ",")
  45. if err != nil {
  46. return "", err
  47. }
  48. var resp companyCertPageResp
  49. err = c.postMultipart("/companyauth/pcpage", fields, "", nil, &resp)
  50. if err != nil {
  51. return "", err
  52. }
  53. if err = resp.err(); err != nil {
  54. return "", err
  55. }
  56. return resp.AuthURL, nil
  57. }
  58. // CompanyCertificationStatus 查询法人单位认证状态;未注册时 found=false。
  59. func (c *Client) CompanyCertificationStatus(openCompanyID string) (*CompanyAuthStatus, bool, error) {
  60. var resp companyCertStatusResp
  61. err := c.postMultipart("/companyauth/status", map[string]string{
  62. "openCompanyId": openCompanyID,
  63. }, "", nil, &resp)
  64. if err != nil {
  65. return nil, false, err
  66. }
  67. if err = resp.err(); err != nil {
  68. if resp.Code == 2002002 {
  69. return nil, false, nil
  70. }
  71. return nil, false, err
  72. }
  73. return &resp.Result, true, nil
  74. }
  75. // CompanySealAuthURL 获取机构印章静默授权页;authEnd 为空则默认 1 年。
  76. func (c *Client) CompanySealAuthURL(openCompanyID, adminMobile string, authEnd time.Time) (string, error) {
  77. if authEnd.IsZero() {
  78. authEnd = time.Now().Add(DefaultCompanySealAuthDuration)
  79. }
  80. timeEditable := false
  81. req := v2auth_request.V2AuthCompanysignsilentUrlRequest{
  82. SealMultipleRequest: &common.SealMultipleRequest{
  83. Company: &common.CompanyRequest{OpenCompanyId: openCompanyID},
  84. },
  85. AuthUser: &common.SilentUserRequest{Mobile: adminMobile},
  86. AuthInformation: []string{"AUTHORIZE_SEAL", "AUTHORIZE_COMPANY_CERTIFICATE"},
  87. AuthorizedMode: []string{"FACEAUTH", "PINAUTH"},
  88. AuthEndDate: authEnd.Format("2006-01-02"),
  89. AuthTimeModifiable: &timeEditable,
  90. }
  91. var resp companySealAuthURLResp
  92. if err := c.postJSON("/v2/auth/companysignsilent/url", req, &resp); err != nil {
  93. return "", err
  94. }
  95. if err := resp.err(); err != nil {
  96. return "", err
  97. }
  98. return resp.Result.Url, nil
  99. }
  100. // QueryCompanySealAuthRecords 查询机构静默授权记录;status 传 "EFFECT" 查有效授权。
  101. func (c *Client) QueryCompanySealAuthRecords(openCompanyID, status string) ([]AuthRecord, error) {
  102. req := v2auth_request.V2AuthSignsilentCompanyRecordRequest{
  103. AuthCompany: &common.CompanyRequest{OpenCompanyId: openCompanyID},
  104. Status: status,
  105. }
  106. var resp companyAuthRecordResp
  107. if err := c.postJSON("/v2/auth/signsilent/company/record", req, &resp); err != nil {
  108. return nil, err
  109. }
  110. if err := resp.err(); err != nil {
  111. return nil, err
  112. }
  113. out := make([]AuthRecord, 0, len(resp.Result))
  114. for _, item := range resp.Result {
  115. out = append(out, AuthRecord{
  116. Status: item.AuthorizedSealRecord.Status,
  117. StartTime: item.AuthorizedSealRecord.StartTime,
  118. EndTime: item.AuthorizedSealRecord.EndTime,
  119. AuthScope: item.AuthorizedSealRecord.AuthScope,
  120. })
  121. }
  122. return out, nil
  123. }
  124. // IsCompanySealAuthorized 是否存在有效机构印章静默授权。
  125. func (c *Client) IsCompanySealAuthorized(openCompanyID string) (bool, error) {
  126. records, err := c.QueryCompanySealAuthRecords(openCompanyID, "EFFECT")
  127. if err != nil {
  128. return false, err
  129. }
  130. return len(records) > 0, nil
  131. }