auth.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 []authorizedSealRecordItem `json:"result"`
  23. }
  24. type authorizedSealRecordItem struct {
  25. AuthorizedSealRecord *authorizedSealRecord `json:"authorizedSealRecord"`
  26. SealBean *authorizedSealBean `json:"sealBean"`
  27. }
  28. type authorizedSealRecord struct {
  29. SealId string `json:"sealId"`
  30. StartTime string `json:"startTime"`
  31. EndTime string `json:"endTime"`
  32. AuthScope string `json:"authScope"`
  33. Status string `json:"status"`
  34. }
  35. type authorizedSealBean struct {
  36. ID flexStringID `json:"id"`
  37. Name string `json:"name"`
  38. Type string `json:"type"`
  39. SealAttribute string `json:"sealAttribute"`
  40. }
  41. // CompanyCertificationURL 获取法人单位在线认证页 URL。
  42. func (c *Client) CompanyCertificationURL(p OrgCertParams) (string, error) {
  43. if p.OpenCompanyID == "" || p.Mobile == "" || p.CompanyName == "" || p.Charger == "" {
  44. return "", ErrInvalidParams
  45. }
  46. type reqBody struct {
  47. Name string `json:"name"`
  48. Charger string `json:"charger"`
  49. Mobile string `json:"mobile"`
  50. OpenCompanyId string `json:"openCompanyId"`
  51. Modes []string `json:"modes"`
  52. Customer bool `json:"customer"`
  53. }
  54. fields, err := structFormFields(reqBody{
  55. Name: p.CompanyName,
  56. Charger: p.Charger,
  57. Mobile: p.Mobile,
  58. OpenCompanyId: p.OpenCompanyID,
  59. Modes: []string{"AUTHFILE", "BANKPAY"},
  60. Customer: true,
  61. }, ",")
  62. if err != nil {
  63. return "", err
  64. }
  65. var resp companyCertPageResp
  66. err = c.postMultipart("/companyauth/pcpage", fields, "", nil, &resp)
  67. if err != nil {
  68. return "", err
  69. }
  70. if err = resp.err(); err != nil {
  71. return "", err
  72. }
  73. return resp.AuthURL, nil
  74. }
  75. // CompanyLocateParams 定位契约锁法人单位;与开放平台一致:id > name > registerNo > openCompanyId。
  76. type CompanyLocateParams struct {
  77. ID string
  78. OpenCompanyID string
  79. Name string
  80. RegisterNo string
  81. }
  82. func (p CompanyLocateParams) toCompanyRequest() (*common.CompanyRequest, error) {
  83. switch {
  84. case p.ID != "":
  85. return &common.CompanyRequest{Id: p.ID}, nil
  86. case p.Name != "":
  87. return &common.CompanyRequest{Name: p.Name}, nil
  88. case p.RegisterNo != "":
  89. return &common.CompanyRequest{RegisterNo: p.RegisterNo}, nil
  90. case p.OpenCompanyID != "":
  91. return &common.CompanyRequest{OpenCompanyId: p.OpenCompanyID}, nil
  92. default:
  93. return nil, ErrInvalidParams
  94. }
  95. }
  96. // CompanyCertificationStatus 按 openCompanyId 查询法人单位认证状态;未注册时 found=false。
  97. func (c *Client) CompanyCertificationStatus(openCompanyID string) (*CompanyAuthStatus, bool, error) {
  98. return c.CompanyCertificationStatusFor(CompanyLocateParams{OpenCompanyID: openCompanyID})
  99. }
  100. // CompanyCertificationStatusFor 按多种键查询法人单位认证状态。
  101. func (c *Client) CompanyCertificationStatusFor(company CompanyLocateParams) (*CompanyAuthStatus, bool, error) {
  102. fields, err := company.toStatusFields()
  103. if err != nil {
  104. return nil, false, err
  105. }
  106. var resp companyCertStatusResp
  107. err = c.postMultipart("/companyauth/status", fields, "", nil, &resp)
  108. if err != nil {
  109. return nil, false, err
  110. }
  111. if err = resp.err(); err != nil {
  112. if resp.Code == 2002002 {
  113. return nil, false, nil
  114. }
  115. return nil, false, err
  116. }
  117. return &resp.Result, true, nil
  118. }
  119. func (p CompanyLocateParams) toStatusFields() (map[string]string, error) {
  120. switch {
  121. case p.ID != "":
  122. return map[string]string{"id": p.ID}, nil
  123. case p.Name != "":
  124. return map[string]string{"name": p.Name}, nil
  125. case p.RegisterNo != "":
  126. return map[string]string{"registerNo": p.RegisterNo}, nil
  127. case p.OpenCompanyID != "":
  128. return map[string]string{"openCompanyId": p.OpenCompanyID}, nil
  129. default:
  130. return nil, ErrInvalidParams
  131. }
  132. }
  133. // CompanySealAuthURL 获取机构印章静默授权页;authEnd 为空则默认 1 年。
  134. func (c *Client) CompanySealAuthURL(openCompanyID, adminMobile string, authEnd time.Time) (string, error) {
  135. return c.CompanySealAuthURLFor(CompanyLocateParams{OpenCompanyID: openCompanyID}, adminMobile, authEnd)
  136. }
  137. // CompanySealAuthURLFor 按法人单位定位参数获取印章静默授权页。
  138. func (c *Client) CompanySealAuthURLFor(company CompanyLocateParams, adminMobile string, authEnd time.Time) (string, error) {
  139. if adminMobile == "" {
  140. return "", ErrInvalidParams
  141. }
  142. companyReq, err := company.toCompanyRequest()
  143. if err != nil {
  144. return "", err
  145. }
  146. if authEnd.IsZero() {
  147. authEnd = time.Now().Add(DefaultCompanySealAuthDuration)
  148. }
  149. timeEditable := false
  150. req := v2auth_request.V2AuthCompanysignsilentUrlRequest{
  151. SealMultipleRequest: &common.SealMultipleRequest{
  152. Company: companyReq,
  153. },
  154. AuthUser: &common.SilentUserRequest{Mobile: adminMobile},
  155. AuthInformation: []string{"AUTHORIZE_SEAL", "AUTHORIZE_COMPANY_CERTIFICATE"},
  156. AuthorizedMode: []string{"FACEAUTH", "PINAUTH"},
  157. AuthEndDate: authEnd.Format("2006-01-02"),
  158. AuthTimeModifiable: &timeEditable,
  159. }
  160. var resp companySealAuthURLResp
  161. if err := c.postJSON("/v2/auth/companysignsilent/url", req, &resp); err != nil {
  162. return "", err
  163. }
  164. if err := resp.err(); err != nil {
  165. return "", err
  166. }
  167. return resp.Result.Url, nil
  168. }
  169. // AuthorizedCompanySeal 静默授权记录中的印章摘要(与签章权限一致,不依赖印章列表查看权限)。
  170. type AuthorizedCompanySeal struct {
  171. ID string
  172. Name string
  173. Type string
  174. SealAttribute string
  175. }
  176. // ListAuthorizedCompanySeals 查询法人单位静默授权中的印章;status 传 EFFECT 查生效授权。
  177. func (c *Client) ListAuthorizedCompanySeals(company CompanyLocateParams, status string) ([]AuthorizedCompanySeal, error) {
  178. companyReq, err := company.toCompanyRequest()
  179. if err != nil {
  180. return nil, err
  181. }
  182. req := v2auth_request.V2AuthSignsilentCompanyRecordRequest{
  183. AuthCompany: companyReq,
  184. Status: status,
  185. }
  186. var resp companyAuthRecordResp
  187. if err := c.postJSON("/v2/auth/signsilent/company/record", req, &resp); err != nil {
  188. return nil, err
  189. }
  190. if err := resp.err(); err != nil {
  191. return nil, err
  192. }
  193. out := make([]AuthorizedCompanySeal, 0, len(resp.Result))
  194. for _, item := range resp.Result {
  195. seal := authorizedSealFromRecord(item)
  196. if seal.ID == "" {
  197. continue
  198. }
  199. out = append(out, seal)
  200. }
  201. return out, nil
  202. }
  203. func authorizedSealFromRecord(item authorizedSealRecordItem) AuthorizedCompanySeal {
  204. seal := AuthorizedCompanySeal{}
  205. if item.AuthorizedSealRecord != nil {
  206. seal.ID = item.AuthorizedSealRecord.SealId
  207. }
  208. if item.SealBean != nil {
  209. if seal.ID == "" {
  210. seal.ID = item.SealBean.ID.String()
  211. }
  212. seal.Name = item.SealBean.Name
  213. seal.Type = item.SealBean.Type
  214. seal.SealAttribute = item.SealBean.SealAttribute
  215. }
  216. return seal
  217. }
  218. // QueryCompanySealAuthRecords 查询机构静默授权记录;status 传 "EFFECT" 查有效授权。
  219. func (c *Client) QueryCompanySealAuthRecords(openCompanyID, status string) ([]AuthRecord, error) {
  220. return c.queryCompanySealAuthRecordsFor(CompanyLocateParams{OpenCompanyID: openCompanyID}, status)
  221. }
  222. func (c *Client) queryCompanySealAuthRecordsFor(company CompanyLocateParams, status string) ([]AuthRecord, error) {
  223. companyReq, err := company.toCompanyRequest()
  224. if err != nil {
  225. return nil, err
  226. }
  227. req := v2auth_request.V2AuthSignsilentCompanyRecordRequest{
  228. AuthCompany: companyReq,
  229. Status: status,
  230. }
  231. var resp companyAuthRecordResp
  232. if err := c.postJSON("/v2/auth/signsilent/company/record", req, &resp); err != nil {
  233. return nil, err
  234. }
  235. if err := resp.err(); err != nil {
  236. return nil, err
  237. }
  238. out := make([]AuthRecord, 0, len(resp.Result))
  239. for _, item := range resp.Result {
  240. if item.AuthorizedSealRecord == nil {
  241. continue
  242. }
  243. out = append(out, AuthRecord{
  244. Status: item.AuthorizedSealRecord.Status,
  245. StartTime: item.AuthorizedSealRecord.StartTime,
  246. EndTime: item.AuthorizedSealRecord.EndTime,
  247. AuthScope: item.AuthorizedSealRecord.AuthScope,
  248. })
  249. }
  250. return out, nil
  251. }
  252. // IsCompanySealAuthorized 是否存在有效机构印章静默授权。
  253. func (c *Client) IsCompanySealAuthorized(openCompanyID string) (bool, error) {
  254. records, err := c.QueryCompanySealAuthRecords(openCompanyID, "EFFECT")
  255. if err != nil {
  256. return false, err
  257. }
  258. return len(records) > 0, nil
  259. }