2025-01-10 00:09:25 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func processG35SC01Response(resp []byte) ([]byte, error) {
|
|
|
|
|
// 第一步:提取外层的 data 字段
|
|
|
|
|
dataResult := gjson.GetBytes(resp, "data")
|
|
|
|
|
if !dataResult.Exists() {
|
|
|
|
|
return nil, fmt.Errorf("外层 data 字段不存在")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第二步:解析外层 data 的 JSON 字符串
|
|
|
|
|
var outerDataMap map[string]interface{}
|
|
|
|
|
if err := json.Unmarshal([]byte(dataResult.String()), &outerDataMap); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("解析外层 data 字段失败: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第三步:提取内层的 data 字段
|
|
|
|
|
innerData, ok := outerDataMap["data"].(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("内层 data 字段不存在或类型错误")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第四步:解析内层 data 的 JSON 字符串
|
|
|
|
|
var finalDataMap map[string]interface{}
|
|
|
|
|
if err := json.Unmarshal([]byte(innerData), &finalDataMap); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("解析内层 data 字段失败: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将最终的 JSON 对象编码为字节数组返回
|
|
|
|
|
finalDataBytes, err := json.Marshal(finalDataMap)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("编码最终的 JSON 对象失败: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return finalDataBytes, nil
|
|
|
|
|
}
|
|
|
|
|
func main() {
|
|
|
|
|
a := map[string]interface{}{
|
|
|
|
|
"data": "{\"msg\":\"接口调用成功\",\"data\":\"{\\\"preservation\\\":{},\\\"crc\\\":41615638,\\\"cases_tree\\\":{},\\\"administrative\\\":{},\\\"civil\\\":{},\\\"count\\\":{\\\"money_jie_total\\\":0,\\\"count_total\\\":5,\\\"larq_stat\\\":\\\"2013(1),2016(4)\\\",\\\"area_stat\\\":\\\"广西壮族自治区(5)\\\",\\\"money_jie_beigao\\\":0,\\\"count_jie_total\\\":5,\\\"money_jie_other\\\":0,\\\"count_wei_total\\\":0,\\\"count_jie_beigao\\\":4,\\\"money_yuangao\\\":0,\\\"money_beigao\\\":0,\\\"ay_stat\\\":\\\"未知(5)\\\",\\\"count_wei_other\\\":0,\\\"count_wei_beigao\\\":0,\\\"count_wei_yuangao\\\":0,\\\"money_other\\\":0,\\\"count_yuangao\\\":1,\\\"money_wei_yuangao\\\":0,\\\"money_jie_yuangao\\\":0,\\\"money_wei_beigao\\\":0,\\\"count_jie_yuangao\\\":1,\\\"count_other\\\":0,\\\"count_jie_other\\\":0,\\\"count_beigao\\\":4,\\\"money_wei_total\\\":0,\\\"money_wei_other\\\":0,\\\"money_total\\\":0,\\\"jafs_stat\\\":\\\"执行完毕(5)\\\"},\\\"implement\\\":{\\\"cases\\\":[{\\\"d_jarq\\\":\\\"2013-04-18\\\",\\\"n_jaay\\\":\\\"未知\\\",\\\"n_ssdw\\\":\\\"申请执行人\\\",\\\"c_ah\\\":\\\"(2013)城中执字第66号\\\",\\\"c_ssdy\\\":\\\"广西壮族自治区\\\",\\\"n_jafs\\\":\\\"执行完毕\\\",\\\"n_ajbs\\\":\\\"a77a247bf43b60846393460d5d0c5a16\\\",\\\"n_jbfy_cj\\\":\\\"基层法院\\\",\\\"c_id\\\":\\\"686a6371ca4af42d2a3a8207e7bb57be\\\",\\\"n_crc\\\":514109936,\\\"n_ajlx\\\":\\\"首次执行\\\",\\\"n_ajjzjd\\\":\\\"已结案\\\",\\\"c_dsrxx\\\":[{\\\"n_ssdw\\\":\\\"申请执行人\\\",\\\"c_mc\\\":\\\"伍艳红\\\",\\\"n_dsrlx\\\":\\\"自然人\\\"},{\\\"n_ssdw\\\":\\\"申请执行人\\\",\\\"c_mc\\\":\\\"庞汉章\\\",\\\"n_dsrlx\\\":\\\"自然人\\\"},{\\\"n_ssdw\\\":\\\"申请执行人\\\",\\\"c_mc\\\":\\\"曹智\\\",\\\"n_dsrlx\\\":\\\"自然人\\\"},{\\\"n_ssdw\\\":\\\"申请执行人\\\",\\\"c_mc\\\":\\\"李健\\\",\\\"n_dsrlx\\\":\\\"自然人\\\"},{\\\"n_ssdw\\\":\\\"申请执行人\\\",\\\"c_mc\\\":\\\"李强\\\",\\\"n_dsrlx\\\":\\\"自然人\\\"},{\\\"n_ssdw\\\":\\\"申请执行人\\\",\\\"c_mc\\\":\\\"梁辉\\\",\\\"n_dsrlx\\\":\\\"自然人\\\"},{\\\"n_ssdw\\\":\\\"申请执行人\\\",\\\"c_mc\\\":\\\"中国农业银行股份有限公司柳州城中支行\\\",\\\"n_dsrlx\\\":\\\"企业组织\\\"}],\\\"n_laay\\\":\\\"未知\\\",\\\"n_jbfy\\\":\\\"柳州市城中区人民法院\\\",\\\"d_larq\\\":\\\"2013-02-27\\\"},{\\\"d_jarq\\\":\\\"2016-12-20\\\",\\\"n_jaay\\\":\\\"民事\\\",\\\"n_ssdw\\\":\\\"被执行人\\\",\\\"c_gkws_glah\\\":\\\"(2016)桂0107民初1800号\\\",\\\"c_ah\\\":\\\"(2016)桂0107执1708号\\\",\\\"c_gkws_pjjg\\\":\\\"查封被执行人广西鼎铭房地产开发有限公司名下所有的位于南宁市西乡塘区秀灵路7号鼎盛国际B座单元1028号商铺,期限为三年。\\\",\\\"c_ssdy\\\":\\\"广西壮族自治区\\\",\\\"c_gkws_dsr\\\":\\\"申请执行人张海婷,女,1977年6月7日出生,汉族,住所地位于江苏省泰兴市。被执行人广西鼎铭房地产开发有限公司,住所地位于南宁市西乡塘区科园大道31号高新苑29栋06号。法定代表人李强,董事长。\\\",\\\"n_jafs\\\":\\\"执行完毕\\\",\\\"n_ajbs\\\":\\\"5ac3742e7b01d827596138397a183bc8\\\",\\\"n_jbfy_cj\\\":\\\"基层法院\\\",\\\"c_id\\\":\\\"565dc891866688bf1cad480d1abf4c00\\\",\\\"n_sqzxbdje\\\":1947919,\\\"n_crc\\\":606433511,\\\"n_ajlx\\\":\\\"首次执行\\\",\\\"n_ajjzjd\\\":\\\"已结案\\\",\\\"c_dsrxx\\\":[{\\\"n_ssdw\\\":\\\"其他\\\",\\\"c_mc\\\":\\\"张海婷\\\",\\\"n_dsrlx\\\":\\\"自然人\\\"},{\\\"n_ssdw\\\":\\\"申请执行人\\\",\\\"c_mc\\\":\\\"张海婷\\\",\\\"n_dsrlx\\\":\\\"自然人\\\"},{\\\"n_ssdw\\\":\\\"被执行人\\\",\\\"c_mc\\\":\\\"李强\\\",\\\"n_dsrlx\\\":\\\"自然人\\\"},{\\\"n_ssdw\\\":\\\"被执行人\\\",\\\"c_mc\\\":\\\"广西鼎铭房地产开发有限公司\\\",\\\"n_dsrlx\\\":\\\"企业组织\\\"},{\\\"n_ssdw\\\":\\\"被执行人\\\",\\\"c_mc\\\":\\\"广西鼎铭房地产开发有限公司\\\",\\\"n_dsrlx\\\":\\\"企业组织\\\"}],\\\"n_laay\\\":\\\"民事\\\",\\\"n_jbfy\\\":\\\"南宁市西乡塘区<EFBFBD>
|
|
|
|
|
"message": "调用成功",
|
|
|
|
|
"resultCode": "200",
|
|
|
|
|
"success": true,
|
|
|
|
|
}
|
|
|
|
|
marshal, err := json.Marshal(a)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resp, err := processG35SC01Response(marshal)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(string(resp))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
func (w *WestDexService) ProcessRequests(data interface{}, requests []types.WestDexServiceRequestParams) ([]byte, error) {
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
responseData := make([]APIResponseData, len(requests)) // 使用 APIResponseData 结构存储响应数据
|
|
|
|
|
var errorsCh []error
|
|
|
|
|
mutex := sync.Mutex{}
|
|
|
|
|
encryptedFields, err := w.EncryptStructFields(data)
|
|
|
|
|
if err != nil {
|
2025-03-07 03:48:59 +08:00
|
|
|
|
return nil, fmt.Errorf("西部请求, 生成请求数据失败: %v", err)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background()) // 创建 context
|
|
|
|
|
defer cancel() // 确保函数结束时取消 context
|
|
|
|
|
|
|
|
|
|
errorLimit := 3
|
|
|
|
|
errorCount := 0 // 记录错误次数
|
|
|
|
|
|
|
|
|
|
// 并发处理每个请求
|
|
|
|
|
for i, req := range requests {
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func(i int, req types.WestDexServiceRequestParams) {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done(): // 检查 context 是否已取消
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将加密后的数据映射成 API 请求格式
|
|
|
|
|
apiRequest := w.MapStructToAPIRequest(encryptedFields, req.FieldMapping, "data")
|
|
|
|
|
resp, callApiErr := w.CallAPI(req.ApiID, apiRequest)
|
|
|
|
|
timestamp := time.Now().Format(time.RFC3339)
|
|
|
|
|
var processedResp []byte
|
|
|
|
|
if resp != nil {
|
|
|
|
|
processedResp = processResponse(resp, req.ApiID)
|
|
|
|
|
}
|
|
|
|
|
if callApiErr != nil {
|
|
|
|
|
mutex.Lock()
|
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
|
|
|
|
errorsCh = append(errorsCh, fmt.Errorf("西部请求, 请求失败: %+v", callApiErr))
|
|
|
|
|
errorCount++
|
|
|
|
|
|
|
|
|
|
// 如果错误次数超过限制,取消 context
|
|
|
|
|
if errorCount >= errorLimit {
|
|
|
|
|
cancel()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 存储失败的响应
|
|
|
|
|
responseData[i] = APIResponseData{
|
|
|
|
|
ApiID: req.ApiID,
|
|
|
|
|
Error: callApiErr.Error(),
|
|
|
|
|
Data: processedResp,
|
|
|
|
|
Success: false,
|
|
|
|
|
Timestamp: timestamp,
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 存储成功的响应数据,使用 RawMessage 来存储原始数据
|
|
|
|
|
mutex.Lock()
|
|
|
|
|
responseData[i] = APIResponseData{
|
|
|
|
|
ApiID: req.ApiID,
|
|
|
|
|
Data: processedResp, // 保持原始数据
|
|
|
|
|
Success: true,
|
|
|
|
|
Timestamp: timestamp,
|
|
|
|
|
}
|
|
|
|
|
mutex.Unlock()
|
|
|
|
|
}(i, req)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wg.Wait() // 等待所有 goroutine 完成
|
|
|
|
|
|
|
|
|
|
if errorCount >= errorLimit {
|
2025-03-07 03:48:59 +08:00
|
|
|
|
return nil, fmt.Errorf("请求失败次数超过 %d 次: %v", errorLimit, errorsCh)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将 responseData 转换为 JSON
|
|
|
|
|
combinedResponse, err := json.Marshal(responseData)
|
|
|
|
|
if err != nil {
|
2025-03-07 03:48:59 +08:00
|
|
|
|
return nil, fmt.Errorf("响应数据转 JSON 失败: %v", err)
|
2025-01-10 00:09:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return combinedResponse, nil
|
|
|
|
|
}
|