| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | package westdex | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import ( | 
					
						
							|  |  |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	"context" | 
					
						
							|  |  |  |  | 	"crypto/md5" | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	"encoding/json" | 
					
						
							|  |  |  |  | 	"errors" | 
					
						
							|  |  |  |  | 	"fmt" | 
					
						
							|  |  |  |  | 	"io" | 
					
						
							|  |  |  |  | 	"net/http" | 
					
						
							|  |  |  |  | 	"strconv" | 
					
						
							|  |  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	"tyapi-server/internal/shared/crypto" | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	"tyapi-server/internal/shared/external_logger" | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | var ( | 
					
						
							|  |  |  |  | 	ErrDatasource = errors.New("数据源异常") | 
					
						
							|  |  |  |  | 	ErrSystem     = errors.New("系统异常") | 
					
						
							| 
									
										
										
										
											2025-09-30 12:03:51 +08:00
										 |  |  |  | 	ErrNotFound      = errors.New("查询为空") | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | type WestResp struct { | 
					
						
							|  |  |  |  | 	Message   string `json:"message"` | 
					
						
							|  |  |  |  | 	Code      string `json:"code"` | 
					
						
							|  |  |  |  | 	Data      string `json:"data"` | 
					
						
							|  |  |  |  | 	ID        string `json:"id"` | 
					
						
							|  |  |  |  | 	ErrorCode *int   `json:"error_code"` | 
					
						
							|  |  |  |  | 	Reason    string `json:"reason"` | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | type G05HZ01WestResp struct { | 
					
						
							|  |  |  |  | 	Message   string          `json:"message"` | 
					
						
							|  |  |  |  | 	Code      string          `json:"code"` | 
					
						
							|  |  |  |  | 	Data      json.RawMessage `json:"data"` | 
					
						
							|  |  |  |  | 	ID        string          `json:"id"` | 
					
						
							|  |  |  |  | 	ErrorCode *int            `json:"error_code"` | 
					
						
							|  |  |  |  | 	Reason    string          `json:"reason"` | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | type WestConfig struct { | 
					
						
							|  |  |  |  | 	Url            string | 
					
						
							|  |  |  |  | 	Key            string | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	SecretID       string | 
					
						
							|  |  |  |  | 	SecretSecondID string | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | type WestDexService struct { | 
					
						
							|  |  |  |  | 	config WestConfig | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	logger *external_logger.ExternalServiceLogger | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // NewWestDexService 是一个构造函数,用于初始化 WestDexService | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | func NewWestDexService(url, key, secretID, secretSecondID string, logger *external_logger.ExternalServiceLogger) *WestDexService { | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	return &WestDexService{ | 
					
						
							|  |  |  |  | 		config: WestConfig{ | 
					
						
							|  |  |  |  | 			Url:            url, | 
					
						
							|  |  |  |  | 			Key:            key, | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			SecretID:       secretID, | 
					
						
							|  |  |  |  | 			SecretSecondID: secretSecondID, | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		logger: logger, | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | // generateRequestID 生成请求ID | 
					
						
							|  |  |  |  | func (w *WestDexService) generateRequestID() string { | 
					
						
							|  |  |  |  | 	timestamp := time.Now().UnixNano() | 
					
						
							|  |  |  |  | 	hash := md5.Sum([]byte(fmt.Sprintf("%d_%s", timestamp, w.config.Key))) | 
					
						
							|  |  |  |  | 	return fmt.Sprintf("westdex_%x", hash[:8]) | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // buildRequestURL 构建请求URL | 
					
						
							|  |  |  |  | func (w *WestDexService) buildRequestURL(code string) string { | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	return fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, w.config.SecretID, code, timestamp) | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | // CallAPI 调用西部数据的 API | 
					
						
							|  |  |  |  | func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[string]interface{}) (resp []byte, err error) { | 
					
						
							|  |  |  |  | 	startTime := time.Now() | 
					
						
							|  |  |  |  | 	requestID := w.generateRequestID() | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	// 从ctx中获取transactionId | 
					
						
							|  |  |  |  | 	var transactionID string | 
					
						
							|  |  |  |  | 	if ctxTransactionID, ok := ctx.Value("transaction_id").(string); ok { | 
					
						
							|  |  |  |  | 		transactionID = ctxTransactionID | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 构建请求URL | 
					
						
							|  |  |  |  | 	reqUrl := w.buildRequestURL(code) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 记录请求日志 | 
					
						
							|  |  |  |  | 	if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 		w.logger.LogRequest(requestID, transactionID, code, reqUrl, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	jsonData, marshalErr := json.Marshal(reqData) | 
					
						
							|  |  |  |  | 	if marshalErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 		err = errors.Join(ErrSystem, marshalErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 			w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 创建HTTP POST请求 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	req, newRequestErr := http.NewRequestWithContext(ctx, "POST", reqUrl, bytes.NewBuffer(jsonData)) | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	if newRequestErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 		err = errors.Join(ErrSystem, newRequestErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 			w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 设置请求头 | 
					
						
							|  |  |  |  | 	req.Header.Set("Content-Type", "application/json") | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 发送请求 | 
					
						
							|  |  |  |  | 	client := &http.Client{} | 
					
						
							|  |  |  |  | 	httpResp, clientDoErr := client.Do(req) | 
					
						
							|  |  |  |  | 	if clientDoErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 		err = errors.Join(ErrSystem, clientDoErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 			w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 	defer func(Body io.ReadCloser) { | 
					
						
							|  |  |  |  | 		closeErr := Body.Close() | 
					
						
							|  |  |  |  | 		if closeErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			// 记录关闭错误 | 
					
						
							|  |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 				w.logger.LogError(requestID, transactionID, code, errors.Join(ErrSystem, fmt.Errorf("关闭响应体失败: %w", closeErr)), reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	}(httpResp.Body) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	// 计算请求耗时 | 
					
						
							|  |  |  |  | 	duration := time.Since(startTime) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	// 检查请求是否成功 | 
					
						
							|  |  |  |  | 	if httpResp.StatusCode == 200 { | 
					
						
							|  |  |  |  | 		// 读取响应体 | 
					
						
							|  |  |  |  | 		bodyBytes, ReadErr := io.ReadAll(httpResp.Body) | 
					
						
							|  |  |  |  | 		if ReadErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 			err = errors.Join(ErrSystem, ReadErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 				w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return nil, err | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法 | 
					
						
							|  |  |  |  | 		var westDexResp WestResp | 
					
						
							|  |  |  |  | 		UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp) | 
					
						
							|  |  |  |  | 		if UnmarshalErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 			err = errors.Join(ErrSystem, UnmarshalErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 				w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 		// 记录响应日志,包含响应ID | 
					
						
							|  |  |  |  | 		if w.logger != nil { | 
					
						
							|  |  |  |  | 			w.logger.LogResponseWithID(requestID, transactionID, code, httpResp.StatusCode, bodyBytes, duration, westDexResp.ID) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		if westDexResp.Code != "00000" && westDexResp.Code != "200" && westDexResp.Code != "0" { | 
					
						
							|  |  |  |  | 			if westDexResp.Data == "" { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 				err = errors.Join(ErrSystem, fmt.Errorf(westDexResp.Message)) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 				if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 					w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 				} | 
					
						
							|  |  |  |  | 				return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key) | 
					
						
							|  |  |  |  | 			if DecryptErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 				err = errors.Join(ErrSystem, DecryptErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 				if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 					w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 				} | 
					
						
							|  |  |  |  | 				return nil, err | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 			// 记录业务错误日志,包含响应ID | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 				w.logger.LogErrorWithResponseID(requestID, transactionID, code, errors.Join(ErrDatasource, fmt.Errorf(westDexResp.Message)), reqData, westDexResp.ID) | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 			// 记录性能日志(失败) | 
					
						
							|  |  |  |  | 			// 注意:通用日志系统不包含性能日志功能 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 			return decryptedData, errors.Join(ErrDatasource, fmt.Errorf(westDexResp.Message)) | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		if westDexResp.Data == "" { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 			err = errors.Join(ErrSystem, fmt.Errorf(westDexResp.Message)) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 				w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key) | 
					
						
							|  |  |  |  | 		if DecryptErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 			err = errors.Join(ErrSystem, DecryptErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 				w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		// 记录性能日志(成功) | 
					
						
							|  |  |  |  | 		// 注意:通用日志系统不包含性能日志功能 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		return decryptedData, nil | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	// 记录HTTP错误 | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 	err = errors.Join(ErrSystem, fmt.Errorf("西部请求失败Code: %d", httpResp.StatusCode)) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 		w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		// 注意:通用日志系统不包含性能日志功能 | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // G05HZ01CallAPI 调用西部数据的 G05HZ01 API | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqData map[string]interface{}) (resp []byte, err error) { | 
					
						
							|  |  |  |  | 	startTime := time.Now() | 
					
						
							|  |  |  |  | 	requestID := w.generateRequestID() | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	// 从ctx中获取transactionId | 
					
						
							|  |  |  |  | 	var transactionID string | 
					
						
							|  |  |  |  | 	if ctxTransactionID, ok := ctx.Value("transaction_id").(string); ok { | 
					
						
							|  |  |  |  | 		transactionID = ctxTransactionID | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	// 构建请求URL | 
					
						
							|  |  |  |  | 	reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%d", w.config.Url, w.config.SecretSecondID, code, time.Now().UnixNano()/int64(time.Millisecond)) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 记录请求日志 | 
					
						
							|  |  |  |  | 	if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 		w.logger.LogRequest(requestID, transactionID, code, reqUrl, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	jsonData, marshalErr := json.Marshal(reqData) | 
					
						
							|  |  |  |  | 	if marshalErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 		err = errors.Join(ErrSystem, marshalErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 			w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 创建HTTP POST请求 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	req, newRequestErr := http.NewRequestWithContext(ctx, "POST", reqUrl, bytes.NewBuffer(jsonData)) | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	if newRequestErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 		err = errors.Join(ErrSystem, newRequestErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 			w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 设置请求头 | 
					
						
							|  |  |  |  | 	req.Header.Set("Content-Type", "application/json") | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 发送请求 | 
					
						
							|  |  |  |  | 	client := &http.Client{} | 
					
						
							|  |  |  |  | 	httpResp, clientDoErr := client.Do(req) | 
					
						
							|  |  |  |  | 	if clientDoErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 		err = errors.Join(ErrSystem, clientDoErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 			w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 	defer func(Body io.ReadCloser) { | 
					
						
							|  |  |  |  | 		closeErr := Body.Close() | 
					
						
							|  |  |  |  | 		if closeErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			// 记录关闭错误 | 
					
						
							|  |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 				w.logger.LogError(requestID, transactionID, code, errors.Join(ErrSystem, fmt.Errorf("关闭响应体失败: %w", closeErr)), reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	}(httpResp.Body) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	// 计算请求耗时 | 
					
						
							|  |  |  |  | 	duration := time.Since(startTime) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	if httpResp.StatusCode == 200 { | 
					
						
							|  |  |  |  | 		bodyBytes, ReadErr := io.ReadAll(httpResp.Body) | 
					
						
							|  |  |  |  | 		if ReadErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 			err = errors.Join(ErrSystem, ReadErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 				w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return nil, err | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		var westDexResp G05HZ01WestResp | 
					
						
							|  |  |  |  | 		UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp) | 
					
						
							|  |  |  |  | 		if UnmarshalErr != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 			err = errors.Join(ErrSystem, UnmarshalErr) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 				w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 		// 记录响应日志,包含响应ID | 
					
						
							|  |  |  |  | 		if w.logger != nil { | 
					
						
							|  |  |  |  | 			w.logger.LogResponseWithID(requestID, transactionID, code, httpResp.StatusCode, bodyBytes, duration, westDexResp.ID) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		if westDexResp.Code != "0000" { | 
					
						
							| 
									
										
										
										
											2025-09-30 12:03:51 +08:00
										 |  |  |  | 			if westDexResp.Data == nil || westDexResp.Code == "1404" { | 
					
						
							|  |  |  |  | 				err = errors.Join(ErrNotFound, fmt.Errorf(westDexResp.Message)) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 				if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 					w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 				} | 
					
						
							|  |  |  |  | 				return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 				// 记录业务错误日志,包含响应ID | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 				if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 					w.logger.LogErrorWithResponseID(requestID, transactionID, code, errors.Join(ErrSystem, fmt.Errorf(string(westDexResp.Data))), reqData, westDexResp.ID) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				// 记录性能日志(失败) | 
					
						
							|  |  |  |  | 				// 注意:通用日志系统不包含性能日志功能 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 				return westDexResp.Data, errors.Join(ErrSystem, fmt.Errorf(string(westDexResp.Data))) | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		if westDexResp.Data == nil { | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 			err = errors.Join(ErrSystem, fmt.Errorf(westDexResp.Message)) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 				w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// 记录性能日志(成功) | 
					
						
							|  |  |  |  | 		// 注意:通用日志系统不包含性能日志功能 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 		return westDexResp.Data, nil | 
					
						
							|  |  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		// 记录HTTP错误 | 
					
						
							| 
									
										
										
										
											2025-08-27 22:19:19 +08:00
										 |  |  |  | 		err = errors.Join(ErrSystem, fmt.Errorf("西部请求失败Code: %d", httpResp.StatusCode)) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 		if w.logger != nil { | 
					
						
							| 
									
										
										
										
											2025-08-26 16:03:46 +08:00
										 |  |  |  | 			w.logger.LogError(requestID, transactionID, code, err, reqData) | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 			// 注意:通用日志系统不包含性能日志功能 | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | func (w *WestDexService) Encrypt(data string) (string, error) { | 
					
						
							|  |  |  |  | 	encryptedValue, err := crypto.WestDexEncrypt(data, w.config.Key) | 
					
						
							|  |  |  |  | 	if err != nil { | 
					
						
							|  |  |  |  | 		return "", ErrSystem | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | 	return encryptedValue, nil | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | func (w *WestDexService) Md5Encrypt(data string) string { | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | 	result := Md5Encrypt(data) | 
					
						
							|  |  |  |  | 	return result | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | func (w *WestDexService) GetConfig() WestConfig { | 
					
						
							|  |  |  |  | 	return w.config | 
					
						
							| 
									
										
										
										
											2025-08-25 15:44:06 +08:00
										 |  |  |  | } |