| 
									
										
										
										
											2024-11-21 12:14:34 +08:00
										 |  |  | package service | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2024-12-30 19:09:10 +08:00
										 |  |  | 	"log" | 
					
						
							| 
									
										
										
										
											2024-11-21 12:14:34 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2025-04-27 12:17:18 +08:00
										 |  |  | 	"tyc-server/app/main/api/internal/config" | 
					
						
							| 
									
										
										
										
											2025-04-09 15:58:06 +08:00
										 |  |  | 	"tyc-server/pkg/lzkit/crypto" | 
					
						
							| 
									
										
										
										
											2025-03-21 20:39:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/pkg/errors" | 
					
						
							| 
									
										
										
										
											2024-11-21 12:14:34 +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"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 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 WestDexService struct { | 
					
						
							|  |  |  | 	config config.WestConfig | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewWestDexService 是一个构造函数,用于初始化 WestDexService | 
					
						
							|  |  |  | func NewWestDexService(c config.Config) *WestDexService { | 
					
						
							|  |  |  | 	return &WestDexService{ | 
					
						
							|  |  |  | 		config: c.WestConfig, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // CallAPI 调用西部数据的 API | 
					
						
							|  |  |  | func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (resp []byte, err error) { | 
					
						
							|  |  |  | 	// 生成当前的13位时间戳 | 
					
						
							|  |  |  | 	timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 构造请求URL | 
					
						
							|  |  |  | 	reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, w.config.SecretId, code, timestamp) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jsonData, marshalErr := json.Marshal(reqData) | 
					
						
							|  |  |  | 	if marshalErr != nil { | 
					
						
							|  |  |  | 		return nil, marshalErr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 创建HTTP POST请求 | 
					
						
							|  |  |  | 	req, newRequestErr := http.NewRequest("POST", reqUrl, bytes.NewBuffer(jsonData)) | 
					
						
							|  |  |  | 	if newRequestErr != nil { | 
					
						
							|  |  |  | 		return nil, newRequestErr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 设置请求头 | 
					
						
							|  |  |  | 	req.Header.Set("Content-Type", "application/json") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 发送请求 | 
					
						
							|  |  |  | 	client := &http.Client{} | 
					
						
							|  |  |  | 	httpResp, clientDoErr := client.Do(req) | 
					
						
							|  |  |  | 	if clientDoErr != nil { | 
					
						
							|  |  |  | 		return nil, clientDoErr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer func(Body io.ReadCloser) { | 
					
						
							|  |  |  | 		closeErr := Body.Close() | 
					
						
							|  |  |  | 		if closeErr != nil { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}(httpResp.Body) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 检查请求是否成功 | 
					
						
							|  |  |  | 	if httpResp.StatusCode == 200 { | 
					
						
							|  |  |  | 		// 读取响应体 | 
					
						
							|  |  |  | 		bodyBytes, ReadErr := io.ReadAll(httpResp.Body) | 
					
						
							|  |  |  | 		if ReadErr != nil { | 
					
						
							|  |  |  | 			return nil, ReadErr | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法 | 
					
						
							|  |  |  | 		var westDexResp WestResp | 
					
						
							|  |  |  | 		UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp) | 
					
						
							|  |  |  | 		if UnmarshalErr != nil { | 
					
						
							|  |  |  | 			return nil, UnmarshalErr | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-04-26 13:09:34 +08:00
										 |  |  | 		if westDexResp.Code != "00000" && westDexResp.Code != "0" { | 
					
						
							| 
									
										
										
										
											2024-11-21 12:14:34 +08:00
										 |  |  | 			if westDexResp.Data == "" { | 
					
						
							|  |  |  | 				return nil, errors.New(westDexResp.Message) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key) | 
					
						
							|  |  |  | 			if DecryptErr != nil { | 
					
						
							|  |  |  | 				return nil, DecryptErr | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return decryptedData, errors.New(westDexResp.Message) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if westDexResp.Data == "" { | 
					
						
							|  |  |  | 			return nil, errors.New(westDexResp.Message) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// 解密响应数据 | 
					
						
							|  |  |  | 		decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key) | 
					
						
							|  |  |  | 		if DecryptErr != nil { | 
					
						
							|  |  |  | 			return nil, DecryptErr | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// 输出解密后的数据 | 
					
						
							| 
									
										
										
										
											2024-12-30 19:09:10 +08:00
										 |  |  | 		log.Println(string(decryptedData)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-21 12:14:34 +08:00
										 |  |  | 		return decryptedData, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil, fmt.Errorf("西部请求失败Code: %d", httpResp.StatusCode) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // CallAPI 调用西部数据的 API | 
					
						
							|  |  |  | func (w *WestDexService) G05HZ01CallAPI(code string, reqData map[string]interface{}) (resp []byte, err error) { | 
					
						
							|  |  |  | 	// 生成当前的13位时间戳 | 
					
						
							|  |  |  | 	timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 构造请求URL | 
					
						
							|  |  |  | 	reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, w.config.SecretSecondId, code, timestamp) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jsonData, marshalErr := json.Marshal(reqData) | 
					
						
							|  |  |  | 	if marshalErr != nil { | 
					
						
							|  |  |  | 		return nil, marshalErr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 创建HTTP POST请求 | 
					
						
							|  |  |  | 	req, newRequestErr := http.NewRequest("POST", reqUrl, bytes.NewBuffer(jsonData)) | 
					
						
							|  |  |  | 	if newRequestErr != nil { | 
					
						
							|  |  |  | 		return nil, newRequestErr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 设置请求头 | 
					
						
							|  |  |  | 	req.Header.Set("Content-Type", "application/json") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 发送请求 | 
					
						
							|  |  |  | 	client := &http.Client{} | 
					
						
							|  |  |  | 	httpResp, clientDoErr := client.Do(req) | 
					
						
							|  |  |  | 	if clientDoErr != nil { | 
					
						
							|  |  |  | 		return nil, clientDoErr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer func(Body io.ReadCloser) { | 
					
						
							|  |  |  | 		closeErr := Body.Close() | 
					
						
							|  |  |  | 		if closeErr != nil { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}(httpResp.Body) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 检查请求是否成功 | 
					
						
							|  |  |  | 	if httpResp.StatusCode == 200 { | 
					
						
							|  |  |  | 		// 读取响应体 | 
					
						
							|  |  |  | 		bodyBytes, ReadErr := io.ReadAll(httpResp.Body) | 
					
						
							|  |  |  | 		if ReadErr != nil { | 
					
						
							|  |  |  | 			return nil, ReadErr | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法 | 
					
						
							|  |  |  | 		var westDexResp G05HZ01WestResp | 
					
						
							|  |  |  | 		UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp) | 
					
						
							|  |  |  | 		if UnmarshalErr != nil { | 
					
						
							|  |  |  | 			return nil, UnmarshalErr | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if westDexResp.Code != "0000" { | 
					
						
							|  |  |  | 			if westDexResp.Data == nil { | 
					
						
							|  |  |  | 				return nil, errors.New(westDexResp.Message) | 
					
						
							| 
									
										
										
										
											2025-01-03 02:13:31 +08:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				return westDexResp.Data, errors.New(string(westDexResp.Data)) | 
					
						
							| 
									
										
										
										
											2024-11-21 12:14:34 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if westDexResp.Data == nil { | 
					
						
							|  |  |  | 			return nil, errors.New(westDexResp.Message) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return westDexResp.Data, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil, fmt.Errorf("西部请求失败Code: %d", httpResp.StatusCode) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 11:37:25 +08:00
										 |  |  | func (w *WestDexService) Encrypt(data string) string { | 
					
						
							|  |  |  | 	encryptedValue, err := crypto.WestDexEncrypt(data, w.config.Key) | 
					
						
							| 
									
										
										
										
											2024-11-21 12:14:34 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2024-12-24 11:37:25 +08:00
										 |  |  | 		panic("WestDexEncrypt error: " + err.Error()) | 
					
						
							| 
									
										
										
										
											2024-11-21 12:14:34 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-12-24 11:37:25 +08:00
										 |  |  | 	return encryptedValue | 
					
						
							| 
									
										
										
										
											2024-11-21 12:14:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-06 22:19:09 +08:00
										 |  |  | // GetDateRange 返回前后五天的日期范围,格式为 "yyyyMMdd-yyyyMMdd" | 
					
						
							| 
									
										
										
										
											2024-12-24 11:37:25 +08:00
										 |  |  | func (w *WestDexService) GetDateRange() string { | 
					
						
							| 
									
										
										
										
											2025-08-06 22:19:09 +08:00
										 |  |  | 	// 获取前五天 | 
					
						
							|  |  |  | 	beforeFiveDays := time.Now().Add(-120 * time.Hour).Format("20060102") | 
					
						
							|  |  |  | 	// 获取后五天 | 
					
						
							|  |  |  | 	afterFiveDays := time.Now().Add(120 * time.Hour).Format("20060102") | 
					
						
							|  |  |  | 	return fmt.Sprintf("%s-%s", beforeFiveDays, afterFiveDays) // 拼接日期范围并返回 | 
					
						
							| 
									
										
										
										
											2024-11-21 12:14:34 +08:00
										 |  |  | } |