package service import ( "encoding/base64" "encoding/hex" "encoding/json" "log" "qnc-server/config" "qnc-server/db" "qnc-server/model/model" "qnc-server/utils" "time" ) const RetCodeYiDong = "000001" const RetCodeLianTong = "000002" const RetCodeDianXin = "000003" type LoanEvaluationService struct { } func (a *LoanEvaluationService) QueryLoanEvaluationOrder(orderID uint) (oanEvaluation []model.LoanEvaluation, err error) { err = db.DB.Where("order_id = ?", orderID).Find(&oanEvaluation).Error if err != nil { log.Printf("query oanEvaluation order failed: %v", err) } return } func (a *LoanEvaluationService) ReqYuShan(data *model.LoanEvaluationReqPayload) (plainText []byte, err error) { currentTime := time.Now() unixMilliseconds := currentTime.UnixNano() / int64(time.Millisecond) requestSN, _ := utils.GenerateRandomString() apiKey := config.ConfigData.Antifraud.ApiKey acctId := config.ConfigData.Antifraud.AcctId httpUrl := config.ConfigData.Antifraud.HttpUrl // 根据 ProdID 动态构建请求数据 reqData := map[string]interface{}{ "prod_id": data.ProdID, "req_time": unixMilliseconds, "request_sn": requestSN, "req_data": map[string]interface{}{}, } for _, param := range model.LoanEvaluationProdIDParams[data.ProdID] { switch param { case "cardNo": reqData["req_data"].(map[string]interface{})["cardNo"] = data.CardNo case "name": reqData["req_data"].(map[string]interface{})["name"] = data.Name case "mobile": reqData["req_data"].(map[string]interface{})["mobile"] = data.Mobile case "encryptionType": reqData["req_data"].(map[string]interface{})["encryptionType"] = 1 case "idCard": reqData["req_data"].(map[string]interface{})["idCard"] = data.CardNo case "phone": reqData["req_data"].(map[string]interface{})["phone"] = data.Mobile } } messageBytes, err := json.Marshal(reqData) if err != nil { return } key, err := hex.DecodeString(apiKey) if err != nil { return } //加密 cipherText := utils.AES_CBC_Encrypt(messageBytes, key) content := base64.StdEncoding.EncodeToString(cipherText) respStr, err := httpDo(httpUrl, content, acctId) if err != nil { return } //解密 sDec, err := base64.StdEncoding.DecodeString(respStr) if err != nil { if utils.IsJSON(respStr) { return []byte(respStr), nil } return } plainText = utils.AES_CBC_Decrypt(sDec, key) return } // 对返回信息进行处理,返回一份完整的报告 func (a *LoanEvaluationService) DataAnalysis(loanEvaluation model.LoanEvaluation) (report model.LoanEvaluationReport) { // 手机月消费档次 var mobileMonthlyConsumptionLevelResp model.YuShanResponse[map[string]interface{}] err := json.Unmarshal([]byte(loanEvaluation.MobileMonthlyConsumptionLevelResp), &mobileMonthlyConsumptionLevelResp) if err != nil { log.Printf("mobileMonthlyConsumptionLevelResp Unmarshal error:%v", err) } if mobileMonthlyConsumptionLevelResp.Retcode == RetCodeSuccess || mobileMonthlyConsumptionLevelResp.Retcode == RetCodeYiDong || mobileMonthlyConsumptionLevelResp.Retcode == RetCodeLianTong || mobileMonthlyConsumptionLevelResp.Retcode == RetCodeDianXin { report.MobileMonthlyConsumptionLevel = mobileMonthlyConsumptionLevelResp.Retdata } else { log.Printf("mobileMonthlyConsumptionLevelResp error:%s", mobileMonthlyConsumptionLevelResp.Retmsg) } // 近六个月欠费停机次数查询 var recentSixMonthsSuspendedTimesResp model.YuShanResponse[map[string]interface{}] err = json.Unmarshal([]byte(loanEvaluation.RecentSixMonthsSuspendedTimesResp), &recentSixMonthsSuspendedTimesResp) if err != nil { log.Printf("recentSixMonthsSuspendedTimesResp Unmarshal error:%v", err) } if recentSixMonthsSuspendedTimesResp.Retcode == RetCodeSuccess || recentSixMonthsSuspendedTimesResp.Retcode == RetCodeYiDong || recentSixMonthsSuspendedTimesResp.Retcode == RetCodeLianTong || recentSixMonthsSuspendedTimesResp.Retcode == RetCodeDianXin { report.RecentSixMonthsSuspendedTimes = recentSixMonthsSuspendedTimesResp.Retdata } else { log.Printf("recentSixMonthsSuspendedTimesResp error:%s", recentSixMonthsSuspendedTimesResp.Retmsg) } // 逾选指数 OverdueSelectionIndexResp var overdueSelectionIndexResp model.YuShanResponse[map[string]interface{}] err = json.Unmarshal([]byte(loanEvaluation.OverdueSelectionIndexResp), &overdueSelectionIndexResp) if err != nil { log.Printf("overdueSelectionIndexResp Unmarshal error:%v", err) } if overdueSelectionIndexResp.Retcode == RetCodeSuccess { report.OverdueSelectionIndex = overdueSelectionIndexResp.Retdata } else { log.Printf("overdueSelectionIndexResp error:%s", overdueSelectionIndexResp.Retmsg) } // 付选指数 PaymentSelectionIndexResp var paymentSelectionIndexResp model.YuShanResponse[map[string]interface{}] err = json.Unmarshal([]byte(loanEvaluation.PaymentSelectionIndexResp), &paymentSelectionIndexResp) if err != nil { log.Printf("paymentSelectionIndexResp Unmarshal error:%v", err) } if paymentSelectionIndexResp.Retcode == RetCodeSuccess { report.PaymentSelectionIndex = paymentSelectionIndexResp.Retdata } else { log.Printf("paymentSelectionIndexResp error:%s", paymentSelectionIndexResp.Retmsg) } // 贷选指数 LoanSelectionIndexResp var loanSelectionIndexResp model.YuShanResponse[map[string]interface{}] err = json.Unmarshal([]byte(loanEvaluation.LoanSelectionIndexResp), &loanSelectionIndexResp) if err != nil { log.Printf("loanSelectionIndexResp Unmarshal error:%v", err) } if loanSelectionIndexResp.Retcode == RetCodeSuccess { report.LoanSelectionIndex = loanSelectionIndexResp.Retdata } else { log.Printf("loanSelectionIndexResp error:%s", loanSelectionIndexResp.Retmsg) } // 借选指数 BorrowSelectionIndexResp var borrowSelectionIndexResp model.YuShanResponse[map[string]interface{}] err = json.Unmarshal([]byte(loanEvaluation.BorrowSelectionIndexResp), &borrowSelectionIndexResp) if err != nil { log.Printf("borrowSelectionIndexResp Unmarshal error:%v", err) } if borrowSelectionIndexResp.Retcode == RetCodeSuccess { report.BorrowSelectionIndex = borrowSelectionIndexResp.Retdata } else { log.Printf("borrowSelectionIndexResp error:%s", borrowSelectionIndexResp.Retmsg) } // 金融黑名单 FinancialBlacklistResp var financialBlacklistResp model.YuShanResponse[map[string]interface{}] err = json.Unmarshal([]byte(loanEvaluation.FinancialBlacklistResp), &financialBlacklistResp) if err != nil { log.Printf("financialBlacklistResp Unmarshal error:%v", err) } if financialBlacklistResp.Retcode == RetCodeSuccess { report.FinancialBlacklist = financialBlacklistResp.Retdata } else { log.Printf("financialBlacklistResp error:%s", financialBlacklistResp.Retmsg) } return report }