Selaa lähdekoodia

抽取工具类

张亚军 6 kuukautta sitten
vanhempi
commit
69c4abe2f5

+ 4 - 4
go.mod

@@ -5,13 +5,13 @@ go 1.21.3
 toolchain go1.22.7
 
 require (
-	git.sxidc.com/go-tools/utils v1.5.29
-	git.sxidc.com/service-supports/dapr_api v0.2.0
+	github.com/go-resty/resty/v2 v2.11.0
+	github.com/pkg/errors v0.9.1
+	github.com/satori/go.uuid v1.2.0
 )
 
 require (
-	github.com/go-resty/resty/v2 v2.11.0 // indirect
 	github.com/kr/pretty v0.3.1 // indirect
-	github.com/satori/go.uuid v1.2.0 // indirect
 	golang.org/x/net v0.21.0 // indirect
+	gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
 )

+ 5 - 4
go.sum

@@ -1,15 +1,16 @@
-git.sxidc.com/go-tools/utils v1.5.29 h1:OUrjzmnjxn+Anmz6FK2X5Pr4ljyrq5qNBaDs1dJLM6g=
-git.sxidc.com/go-tools/utils v1.5.29/go.mod h1:uTDb6QK5JZzK5+Fzsfeng7TwmnRDZiTY6JLYxIX94Qw=
-git.sxidc.com/service-supports/dapr_api v0.2.0 h1:OFTvpvWeIIHASnwPm+7khb6A5kOmy8dtH5C3CO2J2Kk=
-git.sxidc.com/service-supports/dapr_api v0.2.0/go.mod h1:lvu4Pba0W9ne4vZFs/H5cxhjedD80MxAeLtCw9B8jJs=
 github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
 github.com/go-resty/resty/v2 v2.11.0 h1:i7jMfNOJYMp69lq7qozJP+bjgzfAzeOhuGlyDrqxT/8=
 github.com/go-resty/resty/v2 v2.11.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A=
+github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
 github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
 github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
+github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
 github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
+github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
+github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
 github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
 github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=

+ 0 - 27
ng_cw_client/ng_cw_client.go

@@ -1,27 +0,0 @@
-package ng_cw_client
-
-import (
-	"git.sxidc.com/service-supports/dapr_api/invoke"
-	"time"
-)
-
-var invokeAPI *invoke.API
-
-func Init(baseUrl string, timeoutSec time.Duration) {
-	if invokeAPI == nil {
-		invokeAPI = invoke.NewAPI(baseUrl, timeoutSec)
-	}
-}
-
-func Destroy() {
-	if invokeAPI == nil {
-		return
-	}
-
-	invoke.DestroyAPI(invokeAPI)
-	invokeAPI = nil
-}
-
-func GetInvokeAPI() *invoke.API {
-	return invokeAPI
-}

+ 34 - 12
service/workflow.go → ng_cws_client/ng_cws_client.go

@@ -1,14 +1,36 @@
-package service
+package ng_cws_client
 
 import (
-	"cws-sdk/ng_cw_client"
-	"cws-sdk/service/request"
 	"encoding/json"
 	"errors"
-	"git.sxidc.com/go-tools/utils/strutils"
-	"yunji/Gateway/websocket/api/response"
+	"git.sxidc.com/service-supports/cws-sdk/service"
+	"git.sxidc.com/service-supports/cws-sdk/service/request"
+	"git.sxidc.com/service-supports/cws-sdk/service/response"
+	"git.sxidc.com/service-supports/cws-sdk/utils"
+	"git.sxidc.com/service-supports/cws-sdk/utils/http_client"
+	"time"
 )
 
+type NGCwsClient struct {
+	timeout    time.Duration
+	cwsUrl     string
+	httpClient *http_client.Client
+}
+
+func (svc *NGCwsClient) Init(cwsUrl string) error {
+	if utils.IsStringEmpty(cwsUrl) {
+		return errors.New("未配置CWS地址")
+	}
+	svc.cwsUrl = cwsUrl
+	svc.httpClient = http_client.New()
+	return nil
+}
+
+func (svc *NGCwsClient) Destroy() error {
+	http_client.Destroy(svc.httpClient)
+	return nil
+}
+
 var workflowTemplateMap = map[string]string{
 	"CGJH":  "4e0f65ab8547436ba9f744c4dcaef14e",
 	"CGDD":  "5e9bb84779b34d31947107f2c4ead93d",
@@ -29,10 +51,10 @@ var workflowTemplateMap = map[string]string{
 	"GYSTH": "459542e8b07a4cda93d98ba0234c079f",
 }
 
-func StartWorkFlowByBusinessType(reqParams *request.StartWorkFlowByBusinessTypeRequest) error {
+func (svc *NGCwsClient) StartWorkFlowByBusinessType(reqParams *request.StartWorkFlowByBusinessTypeRequest) error {
 	//根据业务类型获取流程模型ID
 	workflowTempalteId := workflowTemplateMap[reqParams.BusinessType]
-	if strutils.IsStringEmpty(workflowTempalteId) {
+	if utils.IsStringEmpty(workflowTempalteId) {
 		return errors.New("未配置流程")
 	}
 
@@ -46,7 +68,7 @@ func StartWorkFlowByBusinessType(reqParams *request.StartWorkFlowByBusinessTypeR
 	}
 	businessObjectStr := string(jsonBytes)
 
-	err = StartWorkflow(&request.StartWorkflowRequest{
+	err = svc.StartWorkflow(&request.StartWorkflowRequest{
 		WorkflowTemplateID: workflowTempalteId,
 		Matter:             reqParams.Matter,
 		BusinessObject:     businessObjectStr,
@@ -61,19 +83,19 @@ func StartWorkFlowByBusinessType(reqParams *request.StartWorkFlowByBusinessTypeR
 
 }
 
-func StartWorkflow(reqParams *request.StartWorkflowRequest) error {
+func (svc *NGCwsClient) StartWorkflow(reqParams *request.StartWorkflowRequest) error {
 	requestJson, err := json.Marshal(reqParams)
 	if err != nil {
 		return err
 	}
-
-	responseBytes, err := ng_cw_client.GetInvokeAPI().PostJSON(startWorkFlowMethodName, requestJson, nil)
+	postResponse, err := svc.httpClient.NewRequest(http_client.WithNewRequestTimeout(svc.timeout)).
+		Post(svc.cwsUrl+service.StartWorkFlowMethodName, requestJson)
 	if err != nil {
 		return err
 	}
 
 	resp := new(response.MsgResponse)
-	err = json.Unmarshal(responseBytes, resp)
+	err = postResponse.Json(resp)
 	if err != nil {
 		return err
 	}

+ 1 - 1
service/method_names.go

@@ -2,5 +2,5 @@ package service
 
 const (
 	methodNamePrefix        = "cws/api/v1"
-	startWorkFlowMethodName = methodNamePrefix + "/workflow/launch"
+	StartWorkFlowMethodName = methodNamePrefix + "/workflow/launch"
 )

+ 0 - 1
service/reponse/workflow.go

@@ -1 +0,0 @@
-package reponse

+ 7 - 0
service/response/workflow.go

@@ -0,0 +1,7 @@
+package response
+
+type MsgResponse struct {
+	Success bool   `json:"success"`
+	ErrCode int    `json:"errCode"`
+	Msg     string `json:"msg"`
+}

+ 44 - 0
utils/http_client/http_client.go

@@ -0,0 +1,44 @@
+package http_client
+
+import (
+	"github.com/go-resty/resty/v2"
+	"time"
+)
+
+type NewRequestOption func(client *Client)
+
+func WithNewRequestTimeout(timeout time.Duration) NewRequestOption {
+	return func(client *Client) {
+		client.setTimeout(timeout)
+	}
+}
+
+type Client struct {
+	client *resty.Client
+}
+
+func New() *Client {
+	return &Client{
+		client: resty.New(),
+	}
+}
+
+func Destroy(client *Client) {
+	if client == nil {
+		return
+	}
+
+	client.client = nil
+}
+
+func (client *Client) NewRequest(opts ...NewRequestOption) *Request {
+	for _, opt := range opts {
+		opt(client)
+	}
+
+	return &Request{request: client.client.R()}
+}
+
+func (client *Client) setTimeout(timeout time.Duration) {
+	client.client.SetTimeout(timeout)
+}

+ 176 - 0
utils/http_client/request.go

@@ -0,0 +1,176 @@
+package http_client
+
+import (
+	"encoding/json"
+	"github.com/go-resty/resty/v2"
+	"github.com/pkg/errors"
+)
+
+type RequestOption func(request *Request)
+
+func WithRequestHeaders(headers map[string]string) RequestOption {
+	return func(request *Request) {
+		request.SetHeaders(headers)
+	}
+}
+
+func WithRequestQueryParams(queryParams map[string]string) RequestOption {
+	return func(request *Request) {
+		request.SetQueryParams(queryParams)
+	}
+}
+
+func WithRequestPathParams(pathParams map[string]string) RequestOption {
+	return func(request *Request) {
+		request.SetPathParams(pathParams)
+	}
+}
+
+func WithRequestFiles(files map[string]string) RequestOption {
+	return func(request *Request) {
+		request.SetFiles(files)
+	}
+}
+
+func WithRequestFormData(data map[string]string) RequestOption {
+	return func(request *Request) {
+		request.SetFormData(data)
+	}
+}
+
+type Request struct {
+	request *resty.Request
+}
+
+func (request *Request) SetHeaders(headers map[string]string) {
+	if headers == nil || len(headers) == 0 {
+		return
+	}
+
+	request.request.SetHeaders(headers)
+}
+
+func (request *Request) SetQueryParams(queryParams map[string]string) {
+	if queryParams == nil || len(queryParams) == 0 {
+		return
+	}
+
+	request.request.SetQueryParams(queryParams)
+}
+
+func (request *Request) SetPathParams(pathParams map[string]string) {
+	if pathParams == nil || len(pathParams) == 0 {
+		return
+	}
+
+	request.request.SetPathParams(pathParams)
+}
+
+func (request *Request) SetFiles(files map[string]string) {
+	if files == nil || len(files) == 0 {
+		return
+	}
+
+	request.request.SetFiles(files)
+}
+
+func (request *Request) SetFormData(data map[string]string) {
+	if data == nil || len(data) == 0 {
+		return
+	}
+
+	request.request.SetFormData(data)
+}
+
+func (request *Request) Post(url string, body any, opts ...RequestOption) (*Response, error) {
+	for _, opt := range opts {
+		opt(request)
+	}
+
+	restyRequest := request.request
+
+	resp, err := restyRequest.SetBody(body).Post(url)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.IsError() {
+		return nil, errors.New(resp.Status())
+	}
+
+	return &Response{response: resp}, nil
+}
+
+func (request *Request) Delete(url string, opts ...RequestOption) (*Response, error) {
+	for _, opt := range opts {
+		opt(request)
+	}
+
+	restyRequest := request.request
+
+	resp, err := restyRequest.Delete(url)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.IsError() {
+		return nil, errors.New(resp.Status())
+	}
+
+	return &Response{response: resp}, nil
+}
+
+func (request *Request) Put(url string, body any, opts ...RequestOption) (*Response, error) {
+	for _, opt := range opts {
+		opt(request)
+	}
+
+	restyRequest := request.request
+
+	resp, err := restyRequest.SetBody(body).Put(url)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.IsError() {
+		return nil, errors.New(resp.Status())
+	}
+
+	return &Response{response: resp}, nil
+}
+
+func (request *Request) Get(url string, opts ...RequestOption) (*Response, error) {
+	for _, opt := range opts {
+		opt(request)
+	}
+
+	restyRequest := request.request
+
+	resp, err := restyRequest.Get(url)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.IsError() {
+		return nil, errors.New(resp.Status())
+	}
+
+	return &Response{response: resp}, nil
+}
+
+type Response struct {
+	response *resty.Response
+}
+
+func (response *Response) Body() []byte {
+	return response.response.Body()
+}
+
+func (response *Response) Json(resp any) error {
+	body := response.response.Body()
+	if body == nil || len(body) == 0 {
+		return errors.New("响应体为空")
+	}
+
+	return json.Unmarshal(body, resp)
+}

+ 68 - 0
utils/strutils.go

@@ -0,0 +1,68 @@
+package utils
+
+import (
+	uuid "github.com/satori/go.uuid"
+	"strings"
+)
+
+func AllBlank(str ...string) bool {
+	for _, s := range str {
+		if !IsStringEmpty(s) {
+			return false
+		}
+	}
+
+	return true
+}
+
+func HasBlank(str ...string) bool {
+	for _, s := range str {
+		if strings.Trim(s, " ") == "" {
+			return true
+		}
+	}
+
+	return false
+}
+
+func HasText(str ...string) bool {
+	for _, s := range str {
+		if strings.Trim(s, " ") != "" {
+			return true
+		}
+	}
+
+	return false
+}
+
+func IsStringEmpty(s string) bool {
+	return strings.Trim(s, " ") == ""
+}
+
+func IsStringNotEmpty(s string) bool {
+	return strings.Trim(s, " ") != ""
+}
+
+func GetUUID() string {
+	return uuid.NewV4().String()
+}
+
+func SimpleUUID() string {
+	return strings.ReplaceAll(GetUUID(), "-", "")
+}
+
+func GenerateUpperLetters(length int) []string {
+	var str []string
+	for i := 0; i < length; i++ {
+		str = append(str, string(rune('A'+i)))
+	}
+	return str
+}
+
+func GenerateLowerLetters(length int) []string {
+	var str []string
+	for i := 0; i < length; i++ {
+		str = append(str, string(rune('a'+i)))
+	}
+	return str
+}