This commit is contained in:
2024-12-28 02:21:47 +08:00
parent 97e14bbe37
commit e8261a948c
20 changed files with 1283 additions and 53 deletions

View File

@@ -71,7 +71,7 @@ func (a *AliPayService) CreateAlipayH5Order(amount float64, subject string, outT
TotalAmount: totalAmount,
ProductCode: "QUICK_WAP_WAY", // H5支付专用产品码
NotifyURL: a.config.NotifyUrl, // 异步回调通知地址
ReturnURL: "http://192.168.1.124:5173/#/pages/report",
ReturnURL: a.config.ReturnURL,
},
}

View File

@@ -8,9 +8,13 @@ import (
"github.com/Masterminds/squirrel"
"github.com/tidwall/gjson"
"github.com/zeromicro/go-zero/core/logx"
"io"
"net/http"
"net/url"
"qnc-server/app/user/cmd/api/internal/config"
"qnc-server/app/user/model"
"qnc-server/pkg/lzkit/crypto"
"strings"
"sync"
"sync/atomic"
"time"
@@ -19,17 +23,19 @@ import (
type ApiRequestService struct {
config config.Config
westDexService *WestDexService
yushanService *YushanService
featureModel model.FeatureModel
productFeatureModel model.ProductFeatureModel
}
// NewApiRequestService 是一个构造函数,用于初始化 ApiRequestService
func NewApiRequestService(c config.Config, westDexService *WestDexService, featureModel model.FeatureModel, productFeatureModel model.ProductFeatureModel) *ApiRequestService {
func NewApiRequestService(c config.Config, westDexService *WestDexService, yushanService *YushanService, featureModel model.FeatureModel, productFeatureModel model.ProductFeatureModel) *ApiRequestService {
return &ApiRequestService{
config: c,
featureModel: featureModel,
productFeatureModel: productFeatureModel,
westDexService: westDexService,
yushanService: yushanService,
}
}
@@ -97,7 +103,7 @@ func (a *ApiRequestService) ProcessRequests(params []byte, productID int64) ([]b
result.Error = preprocessErr.Error()
result.Data = resp
resultsCh <- result
errorsCh <- fmt.Errorf("请求预处理失败: %v", preprocessErr)
errorsCh <- fmt.Errorf("请求失败: %v", preprocessErr)
atomic.AddInt32(&errorCount, 1)
if atomic.LoadInt32(&errorCount) >= int32(errorLimit) {
cancel()
@@ -140,14 +146,26 @@ func (a *ApiRequestService) ProcessRequests(params []byte, productID int64) ([]b
// ------------------------------------请求处理器--------------------------
var requestProcessors = map[string]func(*ApiRequestService, []byte) ([]byte, error){
"G09SC02": (*ApiRequestService).ProcessG09SC02Request,
"G27BJ05": (*ApiRequestService).ProcessG27BJ05Request,
"G26BJ05": (*ApiRequestService).ProcessG26BJ05Request,
"G34BJ03": (*ApiRequestService).ProcessG34BJ03Request,
"G35SC01": (*ApiRequestService).ProcessG35SC01Request,
"G28BJ05": (*ApiRequestService).ProcessG28BJ05Request,
"G05HZ01": (*ApiRequestService).ProcessG05HZ01Request,
"Q23SC01": (*ApiRequestService).ProcessQ23SC01Request,
"G09SC02": (*ApiRequestService).ProcessG09SC02Request,
"G27BJ05": (*ApiRequestService).ProcessG27BJ05Request,
"G26BJ05": (*ApiRequestService).ProcessG26BJ05Request,
"G34BJ03": (*ApiRequestService).ProcessG34BJ03Request,
"G35SC01": (*ApiRequestService).ProcessG35SC01Request,
"G28BJ05": (*ApiRequestService).ProcessG28BJ05Request,
"G05HZ01": (*ApiRequestService).ProcessG05HZ01Request,
"Q23SC01": (*ApiRequestService).ProcessQ23SC01Request,
"G15BJ02": (*ApiRequestService).ProcessG15BJ02Request,
"G17BJ02": (*ApiRequestService).ProcessG17BJ02Request,
"G08SC02": (*ApiRequestService).ProcessG08SC02Request,
"KZEYS": (*ApiRequestService).ProcessKZEYSRequest,
"P_C_B332": (*ApiRequestService).ProcessP_C_B332Request,
"FIN019": (*ApiRequestService).ProcessFIN019Request,
"CAR061": (*ApiRequestService).ProcessCAR061Request,
"G10SC02": (*ApiRequestService).ProcessG10SC02Request,
"G03HZ01": (*ApiRequestService).ProcessG03HZ01Request,
"G02BJ02": (*ApiRequestService).ProcessG02BJ02Request,
"G19BJ02": (*ApiRequestService).ProcessG19BJ02Request,
"G20GZ01": (*ApiRequestService).ProcessG20GZ01Request,
}
// PreprocessRequestApi 调用指定的请求处理函数
@@ -524,3 +542,388 @@ func (a *ApiRequestService) ProcessQ23SC01Request(params []byte) ([]byte, error)
}
return finalDataBytes, nil
}
func (a *ApiRequestService) ProcessG15BJ02Request(params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
if !name.Exists() || !idCard.Exists() || !mobile.Exists() {
return nil, errors.New("api请求, G15BJ02, 获取相关参数失败")
}
request := map[string]interface{}{
"data": map[string]interface{}{
"name": a.westDexService.Encrypt(name.String()),
"idNo": a.westDexService.Encrypt(idCard.String()),
"phone": a.westDexService.Encrypt(mobile.String()),
},
}
resp, callApiErr := a.westDexService.CallAPI("G15BJ02", request)
if callApiErr != nil {
return nil, callApiErr
}
dataResult := gjson.GetBytes(resp, "data.code")
if !dataResult.Exists() {
return nil, fmt.Errorf("code 字段不存在")
}
code := dataResult.Int()
// 处理允许的 code 值
if code == 1000 || code == 1003 || code == 1004 || code == 1005 {
return resp, nil
}
return nil, fmt.Errorf("三要素核验失败: %+v", resp)
}
func (a *ApiRequestService) ProcessG17BJ02Request(params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
mobile := gjson.GetBytes(params, "mobile")
if !name.Exists() || !mobile.Exists() {
return nil, errors.New("api请求, G17BJ02, 获取相关参数失败")
}
request := map[string]interface{}{
"data": map[string]interface{}{
"name": a.westDexService.Encrypt(name.String()),
"phone": a.westDexService.Encrypt(mobile.String()),
},
}
resp, callApiErr := a.westDexService.CallAPI("G17BJ02", request)
if callApiErr != nil {
return nil, callApiErr
}
dataResult := gjson.GetBytes(resp, "data.code")
if !dataResult.Exists() {
return nil, fmt.Errorf("code 字段不存在")
}
code := dataResult.Int()
// 处理允许的 code 值
if code == 1000 || code == 1001 {
return resp, nil
}
return nil, fmt.Errorf("手机二要素核验失败: %+v", resp)
}
func (a *ApiRequestService) ProcessG08SC02Request(params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
if !name.Exists() || !idCard.Exists() {
return nil, errors.New("api请求, G08SC02, 获取相关参数失败")
}
request := map[string]interface{}{
"data": map[string]interface{}{
"xm": a.westDexService.Encrypt(name.String()),
"gmsfzhm": a.westDexService.Encrypt(idCard.String()),
},
}
resp, callApiErr := a.westDexService.CallAPI("G08SC02", request)
if callApiErr != nil {
return nil, callApiErr
}
return resp, nil
}
func (a *ApiRequestService) ProcessKZEYSRequest(params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
if !name.Exists() || !idCard.Exists() {
return nil, errors.New("api请求, KZEYS, 获取相关参数失败")
}
appCode := a.config.Ali.Code
requestUrl := "https://kzidcardv1.market.alicloudapi.com/api-mall/api/id_card/check"
// 构造查询参数
data := url.Values{}
data.Add("name", name.String())
data.Add("idcard", idCard.String())
req, err := http.NewRequest(http.MethodPost, requestUrl, strings.NewReader(data.Encode()))
if err != nil {
return nil, fmt.Errorf("KZEYS 创建请求失败: %+v", err)
}
req.Header.Set("Authorization", "APPCODE "+appCode)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("KZEYS 请求失败: %+v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("KZEYS 请求失败, 状态码: %d", resp.StatusCode)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("KZEYS 响应体读取失败:%v", err)
}
// 使用 gjson 解析 JSON 数据
code := gjson.GetBytes(respBody, "code").Int()
if code != 200 {
msg := gjson.GetBytes(respBody, "msg").String()
if msg == "" {
msg = "未知错误"
}
return nil, fmt.Errorf("KZEYS 响应失败: %s", msg)
}
respData := gjson.GetBytes(respBody, "data")
if !respData.Exists() {
return nil, fmt.Errorf("KZEYS 响应, data 字段不存在")
}
dataRaw := respData.Raw
// 成功返回
return []byte(dataRaw), nil
}
// 人车核验
func (a *ApiRequestService) ProcessP_C_B332Request(params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
carType := gjson.GetBytes(params, "car_type")
carLicense := gjson.GetBytes(params, "car_license")
if !name.Exists() || !carType.Exists() || !carLicense.Exists() {
return nil, errors.New("api请求, P_C_B332, 获取相关参数失败: car_number")
}
request := map[string]interface{}{
"name": name.String(),
"carType": carType.String(),
"carNumber": carLicense.String(),
}
resp, err := a.yushanService.request("P_C_B332", request)
if err != nil {
return nil, fmt.Errorf("人车核验查询失败: %+v", err)
}
return resp, nil
}
// 银行卡黑名单
func (a *ApiRequestService) ProcessFIN019Request(params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
bankCard := gjson.GetBytes(params, "bank_card")
if !name.Exists() || !idCard.Exists() || !mobile.Exists() || !bankCard.Exists() {
return nil, errors.New("api请求, FIN019, 获取相关参数失败: car_number")
}
request := map[string]interface{}{
"name": name.String(),
"cardNo": idCard.String(),
"mobile": mobile.String(),
"cardld": bankCard.String(),
}
resp, err := a.yushanService.request("FIN019", request)
if err != nil {
return nil, fmt.Errorf("银行卡黑名单查询失败: %+v", err)
}
return resp, nil
}
// 名下车辆
func (a *ApiRequestService) ProcessCAR061Request(params []byte) ([]byte, error) {
idCard := gjson.GetBytes(params, "id_card")
if !idCard.Exists() {
return nil, errors.New("api请求, CAR061, 获取相关参数失败")
}
request := map[string]interface{}{
"cardNo": idCard.String(),
}
resp, err := a.yushanService.request("CAR061", request)
if err != nil {
return nil, fmt.Errorf("名下车辆查询失败: %+v", err)
}
return resp, nil
}
func (a *ApiRequestService) ProcessG10SC02Request(params []byte) ([]byte, error) {
// 提取男方和女方信息
nameMan := gjson.GetBytes(params, "nameMan")
idCardMan := gjson.GetBytes(params, "idCardMan")
nameWoman := gjson.GetBytes(params, "nameWoman")
idCardWoman := gjson.GetBytes(params, "idCardWoman")
// 校验是否存在必要参数
if !nameMan.Exists() || !idCardMan.Exists() || !nameWoman.Exists() || !idCardWoman.Exists() {
return nil, errors.New("请求参数缺失:需要提供男方和女方的姓名及身份证号")
}
// 构造请求数据
request := map[string]interface{}{
"data": map[string]interface{}{
"certNumMan": a.westDexService.Encrypt(idCardMan.String()),
"nameMan": a.westDexService.Encrypt(nameMan.String()),
"certNumWoman": a.westDexService.Encrypt(idCardWoman.String()),
"nameWoman": a.westDexService.Encrypt(nameWoman.String()),
},
}
// 调用 API
resp, callApiErr := a.westDexService.CallAPI("G10SC02", request)
if callApiErr != nil {
return nil, callApiErr
}
// 解析响应数据
code := gjson.GetBytes(resp, "code").String()
// 状态码校验
if code != "200" {
return nil, fmt.Errorf("婚姻查询失败:%s", string(resp))
}
result := gjson.GetBytes(resp, "data.0.maritalStatus")
if result.Exists() {
responseMap := map[string]string{"status": result.String()}
jsonResponse, err := json.Marshal(responseMap)
if err != nil {
return nil, err
}
return jsonResponse, nil
} else {
return nil, errors.New("查询为空")
}
}
// 手机号码风险
func (a *ApiRequestService) ProcessG03HZ01Request(params []byte) ([]byte, error) {
mobile := gjson.GetBytes(params, "mobile")
if !mobile.Exists() {
return nil, errors.New("api请求, G03HZ01, 获取相关参数失败")
}
request := map[string]interface{}{
"data": map[string]interface{}{
"mobile": a.westDexService.Encrypt(mobile.String()),
},
}
resp, callApiErr := a.westDexService.CallAPI("G03HZ01", request)
if callApiErr != nil {
return nil, callApiErr
}
// 获取 code 字段
codeResult := gjson.GetBytes(resp, "code")
if !codeResult.Exists() || codeResult.String() != "0000" {
return nil, fmt.Errorf("查询手机号码风险失败, %s", string(resp))
}
data := gjson.GetBytes(resp, "data.data")
if !data.Exists() {
return nil, fmt.Errorf("查询手机号码风险失败, %s", string(resp))
}
return []byte(data.Raw), nil
}
// 手机在网时长
func (a *ApiRequestService) ProcessG02BJ02Request(params []byte) ([]byte, error) {
mobile := gjson.GetBytes(params, "mobile")
if !mobile.Exists() {
return nil, errors.New("api请求, G02BJ02, 获取相关参数失败")
}
request := map[string]interface{}{
"data": map[string]interface{}{
"phone": a.westDexService.Encrypt(mobile.String()),
},
}
resp, callApiErr := a.westDexService.CallAPI("G02BJ02", request)
if callApiErr != nil {
return nil, callApiErr
}
// 获取 code 字段
codeResult := gjson.GetBytes(resp, "code")
validCodes := map[string]bool{"1006": true, "1007": true, "1008": true, "1009": true, "1010": true}
if !validCodes[codeResult.String()] {
return nil, fmt.Errorf("查询手机在网时长失败, %s", string(resp))
}
data := gjson.GetBytes(resp, "data")
if !data.Exists() {
return nil, fmt.Errorf("查询手机在网时长失败, %s", string(resp))
}
return []byte(data.Raw), nil
}
// 手机二次卡
func (a *ApiRequestService) ProcessG19BJ02Request(params []byte) ([]byte, error) {
mobile := gjson.GetBytes(params, "mobile")
startDate := gjson.GetBytes(params, "startDate")
if !mobile.Exists() {
return nil, errors.New("api请求, G19BJ02, 获取相关参数失败")
}
request := map[string]interface{}{
"data": map[string]interface{}{
"phone": a.westDexService.Encrypt(mobile.String()),
"startDate": startDate.String(),
},
}
resp, callApiErr := a.westDexService.CallAPI("G19BJ02", request)
if callApiErr != nil {
return nil, callApiErr
}
// 获取 code 字段
codeResult := gjson.GetBytes(resp, "code")
if !codeResult.Exists() || (codeResult.String() != "1025" && codeResult.String() != "1026") {
return nil, fmt.Errorf("手机二次卡失败, %s", string(resp))
}
data := gjson.GetBytes(resp, "data")
if !data.Exists() {
return nil, fmt.Errorf("手机二次卡失败, %s", string(resp))
}
return []byte(data.Raw), nil
}
// 银行卡四要素
func (a *ApiRequestService) ProcessG20GZ01Request(params []byte) ([]byte, error) {
name := gjson.GetBytes(params, "name")
idCard := gjson.GetBytes(params, "id_card")
mobile := gjson.GetBytes(params, "mobile")
bankCard := gjson.GetBytes(params, "bank_card")
if !mobile.Exists() {
return nil, errors.New("api请求, G20GZ01, 获取相关参数失败")
}
request := map[string]interface{}{
"data": map[string]interface{}{
"name": a.westDexService.Encrypt(name.String()),
"idcard": a.westDexService.Encrypt(idCard.String()),
"acc_no": a.westDexService.Encrypt(bankCard.String()),
"mobile": a.westDexService.Encrypt(mobile.String()),
},
}
resp, callApiErr := a.westDexService.CallAPI("G20GZ01", request)
if callApiErr != nil {
return nil, callApiErr
}
// 获取 code 字段
codeResult := gjson.GetBytes(resp, "code")
if !codeResult.Exists() || codeResult.String() != "10000" {
return nil, fmt.Errorf("银行卡四要素失败, %s", string(resp))
}
data := gjson.GetBytes(resp, "data")
if !data.Exists() {
return nil, fmt.Errorf("银行卡四要素失败, %s", string(resp))
}
// 解析 data.Raw 字符串为接口类型
var parsedData interface{}
err := json.Unmarshal([]byte(data.String()), &parsedData)
if err != nil {
return nil, fmt.Errorf("解析 data 失败: %v", err)
}
// 将解析后的数据重新编码为 []byte
resultBytes, err := json.Marshal(parsedData)
if err != nil {
return nil, fmt.Errorf("重新编码 data 失败: %v", err)
}
return resultBytes, nil
}

View File

@@ -0,0 +1,186 @@
package service
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"io"
"net/http"
"qnc-server/app/user/cmd/api/internal/config"
"strings"
"time"
)
type YushanService struct {
config config.YushanConfig
}
func NewYushanService(c config.Config) *YushanService {
return &YushanService{
config: c.YushanConfig,
}
}
func (y *YushanService) request(prodID string, params map[string]interface{}) ([]byte, error) {
// 获取当前时间戳
unixMilliseconds := time.Now().UnixNano() / int64(time.Millisecond)
// 生成请求序列号
requestSN, _ := y.GenerateRandomString()
// 构建请求数据
reqData := map[string]interface{}{
"prod_id": prodID,
"req_time": unixMilliseconds,
"request_sn": requestSN,
"req_data": params,
}
// 将请求数据转换为 JSON 字节数组
messageBytes, err := json.Marshal(reqData)
if err != nil {
return nil, err
}
// 获取 API 密钥
key, err := hex.DecodeString(y.config.ApiKey)
if err != nil {
return nil, err
}
// 使用 AES CBC 加密请求数据
cipherText := y.AES_CBC_Encrypt(messageBytes, key)
// 将加密后的数据编码为 Base64 字符串
content := base64.StdEncoding.EncodeToString(cipherText)
// 发起 HTTP 请求
client := &http.Client{}
req, err := http.NewRequest("POST", y.config.Url, strings.NewReader(content))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("ACCT_ID", y.config.AcctID)
// 执行请求
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// 读取响应体
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var respData []byte
if IsJSON(string(body)) {
respData = body
} else {
sDec, err := base64.StdEncoding.DecodeString(string(body))
if err != nil {
return nil, err
}
respData = y.AES_CBC_Decrypt(sDec, key)
}
retCode := gjson.GetBytes(respData, "retcode").String()
if retCode == "100000" {
// retcode 为 100000表示查询为空
return nil, fmt.Errorf("羽山请求查空: %s", string(respData))
} else if retCode == "000000" {
// retcode 为 000000表示有数据返回 retdata
retData := gjson.GetBytes(respData, "retdata")
if !retData.Exists() {
return nil, fmt.Errorf("羽山请求retdata为空: %s", string(respData))
}
return []byte(retData.Raw), nil
} else {
return nil, fmt.Errorf("羽山请求未知的状态码: %s", string(respData))
}
}
// 判断字符串是否为 JSON 格式
func IsJSON(s string) bool {
var js interface{}
return json.Unmarshal([]byte(s), &js) == nil
}
// GenerateRandomString 生成一个32位的随机字符串订单号
func (y *YushanService) GenerateRandomString() (string, error) {
// 创建一个16字节的数组
bytes := make([]byte, 16)
// 读取随机字节到数组中
if _, err := rand.Read(bytes); err != nil {
return "", err
}
// 将字节数组编码为16进制字符串
return hex.EncodeToString(bytes), nil
}
// AEC加密CBC模式
func (y *YushanService) AES_CBC_Encrypt(plainText []byte, key []byte) []byte {
//指定加密算法返回一个AES算法的Block接口对象
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
//进行填充
plainText = Padding(plainText, block.BlockSize())
//指定初始向量vi,长度和block的块尺寸一致
iv := []byte("0000000000000000")
//指定分组模式返回一个BlockMode接口对象
blockMode := cipher.NewCBCEncrypter(block, iv)
//加密连续数据库
cipherText := make([]byte, len(plainText))
blockMode.CryptBlocks(cipherText, plainText)
//返回base64密文
return cipherText
}
// AEC解密CBC模式
func (y *YushanService) AES_CBC_Decrypt(cipherText []byte, key []byte) []byte {
//指定解密算法返回一个AES算法的Block接口对象
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
//指定初始化向量IV,和加密的一致
iv := []byte("0000000000000000")
//指定分组模式返回一个BlockMode接口对象
blockMode := cipher.NewCBCDecrypter(block, iv)
//解密
plainText := make([]byte, len(cipherText))
blockMode.CryptBlocks(plainText, cipherText)
//删除填充
plainText = UnPadding(plainText)
return plainText
} // 对明文进行填充
func Padding(plainText []byte, blockSize int) []byte {
//计算要填充的长度
n := blockSize - len(plainText)%blockSize
//对原来的明文填充n个n
temp := bytes.Repeat([]byte{byte(n)}, n)
plainText = append(plainText, temp...)
return plainText
}
// 对密文删除填充
func UnPadding(cipherText []byte) []byte {
//取出密文最后一个字节end
end := cipherText[len(cipherText)-1]
//删除填充
cipherText = cipherText[:len(cipherText)-int(end)]
return cipherText
}