f
This commit is contained in:
@@ -39,15 +39,17 @@ type ApiRequestService struct {
|
||||
featureModel model.FeatureModel
|
||||
productFeatureModel model.ProductFeatureModel
|
||||
tianyuanapi *tianyuanapi.Client
|
||||
authService *AuthorizationService
|
||||
}
|
||||
|
||||
// NewApiRequestService 是一个构造函数,用于初始化 ApiRequestService
|
||||
func NewApiRequestService(c config.Config, featureModel model.FeatureModel, productFeatureModel model.ProductFeatureModel, tianyuanapi *tianyuanapi.Client) *ApiRequestService {
|
||||
func NewApiRequestService(c config.Config, featureModel model.FeatureModel, productFeatureModel model.ProductFeatureModel, tianyuanapi *tianyuanapi.Client, authService *AuthorizationService) *ApiRequestService {
|
||||
return &ApiRequestService{
|
||||
config: c,
|
||||
featureModel: featureModel,
|
||||
productFeatureModel: productFeatureModel,
|
||||
tianyuanapi: tianyuanapi,
|
||||
authService: authService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,7 +219,7 @@ var requestProcessors = map[string]func(*ApiRequestService, []byte) ([]byte, err
|
||||
"QCXG5F3A": (*ApiRequestService).ProcessQCXG5F3ARequest,
|
||||
"FLXG0687": (*ApiRequestService).ProcessFLXG0687Request,
|
||||
"FLXG3D56": (*ApiRequestService).ProcessFLXG3D56Request,
|
||||
"FLXG0V4B": (*ApiRequestService).ProcesFLXG0V4BRequest,
|
||||
"FLXG0V4B": (*ApiRequestService).ProcessFLXG0V4BRequest,
|
||||
"QYGL8271": (*ApiRequestService).ProcessQYGL8271Request,
|
||||
"IVYZ5733": (*ApiRequestService).ProcessIVYZ5733Request,
|
||||
"IVYZ9A2B": (*ApiRequestService).ProcessIVYZ9A2BRequest,
|
||||
@@ -270,6 +272,7 @@ var requestProcessors = map[string]func(*ApiRequestService, []byte) ([]byte, err
|
||||
"FLXG3A9B": (*ApiRequestService).ProcessFLXG3A9BRequest,
|
||||
"QYGL2S0W": (*ApiRequestService).ProcessQYGL2S0WRequest,
|
||||
"FLXGDEA9": (*ApiRequestService).ProcessFLXGDEA9Request,
|
||||
"IVYZ4Y27": (*ApiRequestService).ProcessIVYZ4Y27Request,
|
||||
}
|
||||
|
||||
// PreprocessRequestApi 调用指定的请求处理函数
|
||||
@@ -1927,3 +1930,35 @@ func (a *ApiRequestService) ProcessQCXG9P1CFRequest(params []byte) ([]byte, erro
|
||||
|
||||
return convertTianyuanResponse(resp)
|
||||
}
|
||||
|
||||
// ProcessIVYZ4Y27Request 学历信息查询(需生成专用授权书PDF并Base64编码传入)
|
||||
func (a *ApiRequestService) ProcessIVYZ4Y27Request(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() {
|
||||
return nil, errors.New("api请求, IVYZ4Y27, 获取相关参数失败")
|
||||
}
|
||||
|
||||
// 生成专用授权书PDF并Base64编码
|
||||
userInfo := map[string]interface{}{
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"mobile": mobile.String(),
|
||||
}
|
||||
authFileBase64, err := a.authService.GenerateIVYZ4Y27AuthorizationBase64(userInfo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("生成IVYZ4Y27授权书失败: %v", err)
|
||||
}
|
||||
|
||||
resp, err := a.tianyuanapi.CallInterface("IVYZ4Y27", map[string]interface{}{
|
||||
"name": name.String(),
|
||||
"id_card": idCard.String(),
|
||||
"auth_authorize_file_base64": authFileBase64,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return convertTianyuanResponse(resp)
|
||||
}
|
||||
|
||||
@@ -4,11 +4,13 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
"tyc-server/app/main/api/internal/config"
|
||||
"tyc-server/app/main/model"
|
||||
@@ -329,3 +331,116 @@ func getUserInfoString(userInfo map[string]interface{}, key string) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// GenerateIVYZ4Y27AuthorizationBase64 生成IVYZ4Y27专用授权书PDF并返回Base64编码字符串
|
||||
func (s *AuthorizationService) GenerateIVYZ4Y27AuthorizationBase64(userInfo map[string]interface{}) (string, error) {
|
||||
name := getUserInfoString(userInfo, "name")
|
||||
idCard := getUserInfoString(userInfo, "id_card")
|
||||
if name == "" || idCard == "" {
|
||||
return "", fmt.Errorf("缺少必要的用户信息(name或id_card)")
|
||||
}
|
||||
|
||||
// 构建模板变量
|
||||
data := map[string]string{
|
||||
"CompanyName": "海南海宇大数据科技有限公司",
|
||||
"Name": name,
|
||||
"IdCard": idCard,
|
||||
"Mobile": getUserInfoString(userInfo, "mobile"),
|
||||
"Date": time.Now().Format("2006年1月2日"),
|
||||
}
|
||||
|
||||
// 从模板生成PDF
|
||||
pdfBytes, err := s.generatePDFFromTemplate("static/authorization_ivyz4y27.tmpl", data)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "生成IVYZ4Y27授权书PDF失败")
|
||||
}
|
||||
// Base64编码
|
||||
return base64.StdEncoding.EncodeToString(pdfBytes), nil
|
||||
}
|
||||
|
||||
// generatePDFFromTemplate 从模板生成PDF
|
||||
func (s *AuthorizationService) generatePDFFromTemplate(templatePath string, data interface{}) ([]byte, error) {
|
||||
// 1. 读取模板文件
|
||||
tmplContent, err := os.ReadFile(templatePath)
|
||||
if err != nil {
|
||||
// 尝试从项目根目录读取
|
||||
absPath, _ := filepath.Abs(templatePath)
|
||||
if _, err := os.Stat(absPath); err == nil {
|
||||
tmplContent, err = os.ReadFile(absPath)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "读取模板文件失败: %s", templatePath)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 解析并执行模板
|
||||
tmpl, err := template.New("auth").Parse(string(tmplContent))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "解析模板失败")
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tmpl.Execute(&buf, data); err != nil {
|
||||
return nil, errors.Wrapf(err, "执行模板失败")
|
||||
}
|
||||
content := buf.String()
|
||||
|
||||
// 3. 创建PDF
|
||||
pdf := gofpdf.New("P", "mm", "A4", "")
|
||||
pdf.AddPage()
|
||||
|
||||
// 4. 加载中文字体
|
||||
fontPaths := []string{
|
||||
"static/SIMHEI.TTF",
|
||||
"/app/static/SIMHEI.TTF",
|
||||
"app/main/api/static/SIMHEI.TTF",
|
||||
"../static/SIMHEI.TTF",
|
||||
}
|
||||
|
||||
fontAdded := false
|
||||
for _, fontPath := range fontPaths {
|
||||
if _, err := os.Stat(fontPath); err == nil {
|
||||
pdf.AddUTF8Font("ChineseFont", "", fontPath)
|
||||
fontAdded = true
|
||||
logx.Infof("generatePDFFromTemplate 成功加载字体: %s", fontPath)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if fontAdded {
|
||||
pdf.SetFont("ChineseFont", "", 12)
|
||||
} else {
|
||||
pdf.SetFont("Arial", "", 12)
|
||||
logx.Errorf("generatePDFFromTemplate 未找到中文字体文件,使用默认Arial字体")
|
||||
}
|
||||
|
||||
// 5. 写入内容
|
||||
// 简单处理:按行分割并使用 MultiCell
|
||||
lines := strings.Split(content, "\n")
|
||||
for _, line := range lines {
|
||||
if strings.TrimSpace(line) == "" {
|
||||
pdf.Ln(5)
|
||||
continue
|
||||
}
|
||||
// MultiCell 支持自动换行
|
||||
pdf.MultiCell(0, 6, line, "", "L", false)
|
||||
}
|
||||
|
||||
// 6. 添加水印 (简单实现:在页面中间添加浅灰色文字)
|
||||
if fontAdded {
|
||||
pdf.SetFont("ChineseFont", "", 40)
|
||||
pdf.SetTextColor(220, 220, 220) // 非常浅的灰色
|
||||
// 将坐标移动到页面中间附近,并旋转或倾斜(gofpdf 旋转比较复杂,这里简单放几个位置)
|
||||
pdf.Text(40, 100, "仅供背景调查使用")
|
||||
pdf.Text(40, 180, "仅供背景调查使用")
|
||||
pdf.Text(40, 260, "仅供背景调查使用")
|
||||
}
|
||||
|
||||
// 7. 输出PDF
|
||||
var pdfBuf bytes.Buffer
|
||||
if err := pdf.Output(&pdfBuf); err != nil {
|
||||
return nil, errors.Wrapf(err, "输出PDF字节数组失败")
|
||||
}
|
||||
|
||||
return pdfBuf.Bytes(), nil
|
||||
}
|
||||
|
||||
@@ -205,7 +205,8 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
alipayService := service.NewAliPayService(c)
|
||||
wechatPayService := service.NewWechatPayService(c, userAuthModel, service.InitTypeWxPayPubKey)
|
||||
applePayService := service.NewApplePayService(c)
|
||||
apiRequestService := service.NewApiRequestService(c, featureModel, productFeatureModel, tianyuanapi)
|
||||
authorizationService := service.NewAuthorizationService(c, authorizationDocumentModel)
|
||||
apiRequestService := service.NewApiRequestService(c, featureModel, productFeatureModel, tianyuanapi, authorizationService)
|
||||
verificationService := service.NewVerificationService(c, tianyuanapi, apiRequestService)
|
||||
asynqService := service.NewAsynqService(c)
|
||||
agentService := service.NewAgentService(c, orderModel, agentModel, agentAuditModel, agentClosureModel,
|
||||
@@ -218,7 +219,6 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
adminPromotionLinkStatsService := service.NewAdminPromotionLinkStatsService(adminPromotionLinkModel,
|
||||
adminPromotionLinkStatsTotalModel, adminPromotionLinkStatsHistoryModel)
|
||||
imageService := service.NewImageService()
|
||||
authorizationService := service.NewAuthorizationService(c, authorizationDocumentModel)
|
||||
toolboxService := service.NewToolboxService(tianxingjuhe)
|
||||
tianxingjuheService := tianxingjuhe
|
||||
|
||||
|
||||
Reference in New Issue
Block a user