auth.go 8.8 KB

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