package utils import ( "encoding/json" "errors" "net/http" "chainweaver.org.cn/chainweaver/servicecommon/res/code" "github.com/zeromicro/go-zero/core/logx" ) var ( AddAuthorityUrl = "/api/v1/did-mgr/authority/add" QueryAuthorityUrl = "/api/v1/did-mgr/authority/query" EnterpriseAuthVerifyUrl = "/api/v1/did-mgr/operator/enterpriseAuthVerify" EnterpriseAuthNumberUrl = "/api/v1/did-mgr/operator/enterpriseAuthNumber" AuthenticationUrl = "/api/v1/did-mgr/operator/authentication" FindDidByCompanyNameUrl = "/api/v1/did/vc/find-did-by-company-name" AddTrustIssuerUrl = "/api/v1/did/trust-issuer/add" DelTrustIssuerUrl = "/api/v1/did/trust-issuer/delete" ) type AddAuthorityResp struct { Code int `json:"code"` Msg string `json:"msg"` Data struct { OrgId int64 `json:"orgId"` } `json:"data"` } type QueryAuthorityResp struct { Code int `json:"code"` Msg string `json:"msg"` Data struct { State int `json:"state"` } `json:"data"` } func AddAuthorityInfo(targetUrl, proxy string, req interface{}) (orgId int64, err error) { param, _ := json.Marshal(req) respBz, err := DidServerReq(targetUrl, proxy, http.MethodPost, param) if err != nil { return } respDid := &AddAuthorityResp{} err = json.Unmarshal(respBz, respDid) if err != nil { return 0, errors.New(respDid.Msg) } logx.Debugf("Request document from[%s] code[%d],msg[%s]", targetUrl, respDid.Code, respDid.Msg) if code.CodeOk.NotEquals(int(respDid.Code)) { return 0, errors.New(respDid.Msg) } return respDid.Data.OrgId, nil } func QueryAuthorityInfo(targetUrl, proxy string, req interface{}) (res int, err error) { param, _ := json.Marshal(req) respBz, err := DidServerReq(targetUrl, proxy, http.MethodPost, param) if err != nil { return } respDid := &QueryAuthorityResp{} err = json.Unmarshal(respBz, respDid) if err != nil { return 0, errors.New(respDid.Msg) } logx.Debugf("Request document from[%s] code[%d],msg[%s]", targetUrl, respDid.Code, respDid.Msg) if code.CodeOk.NotEquals(int(respDid.Code)) { return 0, errors.New(respDid.Msg) } return respDid.Data.State, nil } type EnterpriseAuthVerifyResp struct { Code int `json:"code"` Msg string `json:"msg"` Data *AuthServiceCreateReq `json:"data"` } type AuthServiceCreateReq struct { MessageHeader struct { Syscode string `json:"syscode"` Authcode string `json:"authcode"` Businesstype string `json:"businesstype"` Sign string `json:"sign"` Version string `json:"version"` ErrorCode int `json:"errorCode"` ErrorInfo string `json:"errorInfo"` } `json:"message_header"` MessageContent *EnterRealNameAuthInfo `json:"message_content"` } type EnterRealNameAuthInfo struct { Qrtype string `json:"qrtype"` Rettype string `json:"rettype"` Opertime string `json:"opertime"` Qrid string `json:"qrid"` Qrinfo string `json:"qrinfo"` Qrimage string `json:"qrimage"` Dom string `json:"dom"` Entname string `json:"entname"` LegalName string `json:"legalName"` Licencesn string `json:"licencesn"` Opfrom string `json:"opfrom"` Opscope string `json:"opscope"` Opto string `json:"opto"` Uniscid string `json:"uniscid"` LegalIDCard string `json:"legalIDCard"` } func EnterpriseAuthVerify(targetUrl, proxy string, req interface{}) (res *EnterpriseAuthVerifyResp, err error) { param, _ := json.Marshal(req) respBz, err := DidServerReq(targetUrl, proxy, http.MethodPost, param) if err != nil { return } respInfo := &EnterpriseAuthVerifyResp{} err = json.Unmarshal(respBz, respInfo) if err != nil { return nil, errors.New(respInfo.Msg) } logx.Debugf("Request document from[%s] code[%d],msg[%s]", targetUrl, respInfo.Code, respInfo.Msg) if code.CodeOk.NotEquals(int(respInfo.Code)) { return nil, errors.New(respInfo.Msg) } return respInfo, nil } type EnterpriseAuthNumberResp struct { Code int `json:"code"` Msg string `json:"msg"` Data struct { Random string `json:"random"` SysName string `json:"sysName"` Qrimage string `json:"qrimage"` } `json:"data"` } func EnterpriseAuthNumber(targetUrl, proxy string) (res *EnterpriseAuthNumberResp, err error) { respBz, err := DidServerReq(targetUrl, proxy, http.MethodPost, nil) if err != nil { return } respInfo := &EnterpriseAuthNumberResp{} err = json.Unmarshal(respBz, respInfo) if err != nil { return nil, errors.New(respInfo.Msg) } logx.Debugf("Request document from[%s] code[%d],msg[%s]", targetUrl, respInfo.Code, respInfo.Msg) if code.CodeOk.NotEquals(int(respInfo.Code)) { return nil, errors.New(respInfo.Msg) } return respInfo, nil } type AuthenticationResp struct { Code int `json:"code"` Msg string `json:"msg"` Data int `json:"data"` } func Authentication(targetUrl, proxy string, req interface{}) (res int, err error) { param, _ := json.Marshal(req) respBz, err := DidServerReq(targetUrl, proxy, http.MethodPost, param) if err != nil { return } respInfo := &AuthenticationResp{} err = json.Unmarshal(respBz, respInfo) if err != nil { return 0, errors.New(respInfo.Msg) } logx.Debugf("Request document from[%s] code[%d],msg[%s]", targetUrl, respInfo.Code, respInfo.Msg) if code.CodeOk.NotEquals(int(respInfo.Code)) { return 0, errors.New(respInfo.Msg) } return respInfo.Data, nil } type FindDidByCompanyNameResp struct { Code int `json:"code"` Msg string `json:"msg"` Data struct { Did string `json:"did"` } `json:"data"` } func FindDidByCompanyName(targetUrl, proxy string, req interface{}) (did string, err error) { param, _ := json.Marshal(req) respBz, err := DidServerReq(targetUrl, proxy, http.MethodPost, param) if err != nil { return } respInfo := &FindDidByCompanyNameResp{} err = json.Unmarshal(respBz, respInfo) if err != nil { return "", err } logx.Debugf("Request document from[%s] code[%d],msg[%s]", targetUrl, respInfo.Code, respInfo.Msg) if code.CodeOk.NotEquals(int(respInfo.Code)) { return "", errors.New(respInfo.Msg) } did = respInfo.Data.Did return } type TrustIssuerReq struct { Did []string `json:"did"` } func AddTrustIssuer(targetUrl, proxy, did string) (err error) { req := TrustIssuerReq{ []string{did}, } param, _ := json.Marshal(req) respBz, err := DidServerReq(targetUrl, proxy, http.MethodPost, param) if err != nil { return } respInfo := &FindDidByCompanyNameResp{} err = json.Unmarshal(respBz, respInfo) if err != nil { return err } logx.Debugf("Request document from[%s] code[%d],msg[%s]", targetUrl, respInfo.Code, respInfo.Msg) if code.CodeOk.NotEquals(int(respInfo.Code)) { return errors.New(respInfo.Msg) } did = respInfo.Data.Did return } func DelTrustIssuer(targetUrl, proxy, did string) (err error) { req := TrustIssuerReq{ []string{did}, } param, _ := json.Marshal(req) respBz, err := DidServerReq(targetUrl, proxy, http.MethodPost, param) if err != nil { return } respInfo := &FindDidByCompanyNameResp{} err = json.Unmarshal(respBz, respInfo) if err != nil { return err } logx.Debugf("Request document from[%s] code[%d],msg[%s]", targetUrl, respInfo.Code, respInfo.Msg) if code.CodeOk.NotEquals(int(respInfo.Code)) { return errors.New(respInfo.Msg) } did = respInfo.Data.Did return }