diff --git a/api/h5/index.go b/api/h5/index.go index 2da4ae4..7f02870 100644 --- a/api/h5/index.go +++ b/api/h5/index.go @@ -3,8 +3,10 @@ package h5 import ( "encoding/json" "github.com/gin-gonic/gin" + "insure/dto/request" "insure/e" "insure/global" + "insure/model" "insure/service" "insure/third_party/signer" "insure/utils/result" @@ -86,3 +88,39 @@ func PostToZking(c *gin.Context) (result.Data, error) { } return result.Data{"result": res}, nil } + +func UpdateApplicant(c *gin.Context) (result.Data, error) { + req := request.UpdateApplicantRequest{} + err := c.ShouldBind(&req) + + if err != nil { + return nil, e.NewError("参数错误") + } + + _, err = service.OrderService.UpdateApplicant(req) + if err != nil { + return nil, e.NewError("保存错误") + } + + return result.Data{}, nil +} + +func UploadSignFile(c *gin.Context) (result.Data, error) { + token := c.DefaultPostForm("token", "") + if token == "" { + return nil, e.NewError("缺少参数") + } + order, err := service.OrderService.GetOrderByToken(token) + if err != nil { + return nil, err + } + + signUrl, err := service.OrderService.UploadPdf("pdf", c) + if err != nil { + return nil, err + } + + global.DB.Model(&model.Applicant{}).Where("order_sn", order.OrderSn).UpdateColumn("sign_url", signUrl) + + return result.Data{}, nil +} diff --git a/api/suixing/index.go b/api/suixing/index.go index df1f2ab..59f3ff8 100644 --- a/api/suixing/index.go +++ b/api/suixing/index.go @@ -10,12 +10,7 @@ import ( "insure/service" "insure/third_party/signer" "insure/third_party/suixing" - "insure/utils" "io" - "net/http" - "strconv" - "strings" - "time" ) func Create(c *gin.Context) { @@ -96,44 +91,32 @@ func UploadSignedFile(c *gin.Context) { return } - file, err := c.FormFile("pdf") + signFileUrl, err := service.OrderService.UploadPdf("pdf", c) if err != nil { result["code"] = 201 - result["msg"] = "文件上传失败" + result["msg"] = err.Error() c.JSON(200, result) return } - reader, err := file.Open() + + global.DB.Model(&model.Applicant{}).Where("order_sn", order.OrderSn).UpdateColumn("sign_url", signFileUrl) + service.OrderService.AddSignLog(order.OrderSn, param) + + res, err := service.OrderService.PostToZking(order.OrderSn) if err != nil { result["code"] = 201 - result["msg"] = "文件打开失败:" + err.Error() - c.JSON(200, result) - return - } - b := make([]byte, 512) - reader.Read(b) - contentType := http.DetectContentType(b) - if contentType != "application/pdf" && contentType != "application/zip" { - result["code"] = 201 - result["msg"] = "文件格式错误:" + contentType + result["msg"] = "文件保存失败:" + err.Error() c.JSON(200, result) return } - fileNameItems := strings.Split(file.Filename, ".") - filePath := time.Now().Format("20060102") + "/" + utils.Md5(file.Filename+strconv.Itoa(int(time.Now().UnixMicro()))+utils.GenerateRandomString(10)) + "." + fileNameItems[len(fileNameItems)-1] - dest := "./static/uploads/" + filePath - err = c.SaveUploadedFile(file, dest) - if err != nil { - result["code"] = 201 - result["msg"] = "文件保存失败:" + err.Error() + + if res.Code != "200" { + result["code"] = 202 + result["msg"] = res.Code c.JSON(200, result) return } - signFileUrl := global.Config.Server.Domain + "/uploads/" + filePath - global.DB.Model(&model.Applicant{}).Where("order_sn", order.OrderSn).UpdateColumn("sign_url", signFileUrl) - service.OrderService.AddSignLog(order.OrderSn, param) - c.JSON(200, result) return } diff --git a/api/zking/index.go b/api/zking/index.go new file mode 100644 index 0000000..1599a54 --- /dev/null +++ b/api/zking/index.go @@ -0,0 +1,105 @@ +package zking + +import ( + "encoding/json" + "fmt" + "github.com/gin-gonic/gin" + "insure/e" + "insure/service" + "insure/third_party/zking" + "io" +) + +func checkParam(c *gin.Context) (*zking.CommonParam, error) { + body, _ := io.ReadAll(c.Request.Body) + if body != nil { + fmt.Printf("push body:%s\n", body) + } + + param := zking.CommonParam{} + err := json.Unmarshal(body, ¶m) + if err != nil { + return nil, e.NewError("参数异常:" + err.Error()) + } + if !zking.InsureApi.Verify(param) { + return nil, e.NewError("验签错误") + } + + return ¶m, nil +} + +func Push(c *gin.Context) { + data := map[string]interface{}{} + result := map[string]interface{}{ + "code": 200, + "msg": "成功", + "data": data, + } + + param, err := checkParam(c) + if err != nil { + fmt.Println(err.Error()) + result["code"] = 201 + result["msg"] = err.Error() + c.JSON(200, result) + return + } + + pushData := zking.PushCallbackData{} + err = json.Unmarshal([]byte(param.Data), &pushData) + if err != nil { + fmt.Println(err.Error()) + result["code"] = 201 + result["msg"] = "data异常" + c.JSON(200, result) + return + } + orderResult, err := service.OrderService.SaveOrderResult(pushData) + if err != nil { + fmt.Println(err.Error()) + result["code"] = 201 + result["msg"] = "内部错误" + c.JSON(200, result) + return + } + fmt.Println("orderResult:", orderResult) +} + +func Revoke(c *gin.Context) { + data := map[string]interface{}{} + result := map[string]interface{}{ + "code": 200, + "msg": "成功", + "data": data, + } + + param, err := checkParam(c) + if err != nil { + fmt.Println(err.Error()) + result["code"] = 201 + result["msg"] = err.Error() + c.JSON(200, result) + return + } + + fmt.Println(param) +} + +func Claim(c *gin.Context) { + data := map[string]interface{}{} + result := map[string]interface{}{ + "code": 200, + "msg": "成功", + "data": data, + } + + param, err := checkParam(c) + if err != nil { + fmt.Println(err.Error()) + result["code"] = 201 + result["msg"] = err.Error() + c.JSON(200, result) + return + } + fmt.Println(param) +} diff --git a/conf/config.yaml b/conf/config.yaml index d8109d6..f15bd09 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -1,5 +1,6 @@ server: port: 9096 + root-path: "" domain: "http://fjtb.bj-eib.com" alipay: app-id: "" diff --git a/config/server.go b/config/server.go index a2fdfc8..f5f0a65 100644 --- a/config/server.go +++ b/config/server.go @@ -1,6 +1,7 @@ package config type Server struct { - Port string - Domain string + Port string + Domain string + RootPath string `yaml:"root-path"` } diff --git a/dto/request/update_applicant_request.go b/dto/request/update_applicant_request.go new file mode 100644 index 0000000..2d57b1d --- /dev/null +++ b/dto/request/update_applicant_request.go @@ -0,0 +1,11 @@ +package request + +type UpdateApplicantRequest struct { + Token string `form:"token"` + ApplicantName string `form:"applicantName"` + CreditCode string `form:"creditCode"` + ContactName string `form:"contactName"` + ContactMobile string `form:"contactMobile"` + BankName string `form:"bankName"` + BankCardNum string `form:"bankCardNum"` +} diff --git a/insure b/insure index b045784..fc240e6 100755 Binary files a/insure and b/insure differ diff --git a/router/common.go b/router/common.go index 0a0523b..cc61d94 100644 --- a/router/common.go +++ b/router/common.go @@ -9,4 +9,5 @@ func Initial(r *gin.Engine) { webRouteInit(r) adminRouteInit(r) suixingRouteInit(r) + zkingRouteInit(r) } diff --git a/router/web.go b/router/web.go index caef64f..4ff0b92 100644 --- a/router/web.go +++ b/router/web.go @@ -12,4 +12,6 @@ func webRouteInit(r *gin.Engine) { webGroup.Match([]string{"GET", "OPTIONS"}, "/get-sign-url", result.Json(h5.GetSignUrl)) webGroup.Match([]string{"GET", "OPTIONS"}, "/get-order-result", result.Json(h5.GetOrderResult)) webGroup.Match([]string{"GET", "OPTIONS"}, "/post-to-zking", result.Json(h5.PostToZking)) + webGroup.Match([]string{"POST", "OPTIONS"}, "/update-applicant", result.Json(h5.UpdateApplicant)) + webGroup.Match([]string{"POST", "OPTIONS"}, "/upload-sign-file", result.Json(h5.UploadSignFile)) } diff --git a/router/zking.go b/router/zking.go new file mode 100644 index 0000000..1a483e7 --- /dev/null +++ b/router/zking.go @@ -0,0 +1,13 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "insure/api/zking" +) + +func zkingRouteInit(r *gin.Engine) { + zkGroup := r.Group("/zking") + zkGroup.Match([]string{"POST", "GET", "OPTIONS"}, "/push", zking.Push) + zkGroup.Match([]string{"POST", "GET", "OPTIONS"}, "/revoke", zking.Revoke) + zkGroup.Match([]string{"POST", "GET", "OPTIONS"}, "/claim", zking.Claim) +} diff --git a/service/order_service.go b/service/order_service.go index 08c12c4..c0cd989 100644 --- a/service/order_service.go +++ b/service/order_service.go @@ -3,8 +3,10 @@ package service import ( "encoding/json" "errors" + "github.com/gin-gonic/gin" "github.com/xuri/excelize/v2" "gorm.io/gorm" + "insure/dto/request" "insure/dto/request/manage" "insure/e" "insure/global" @@ -14,7 +16,9 @@ import ( "insure/third_party/zking" "insure/utils" "insure/utils/excel" + "net/http" "strconv" + "strings" "time" ) @@ -498,3 +502,86 @@ func (orderService) buildOrdersExcel(orders []model.Order, sheet string, f *exce row++ } } + +func (orderService) SaveOrderResult(data zking.PushCallbackData) (*model.OrderResult, error) { + orderResult := model.OrderResult{} + orderResult.OrderSn = data.OrderSn + orderResult.PolicyNo = data.PolicyNo + orderResult.Status = data.Status + orderResult.Remark = data.Remark + orderResult.ConfirmReceiptLink = data.ConfirmReceiptLink + orderResult.ConfirmReceiptLinkMd5 = data.ConfirmReceiptLinkMd5 + orderResult.GuranteeLink = data.GuranteeLink + orderResult.GuranteeLinkMd5 = data.GuranteeLinkMd5 + orderResult.GuranteeOfdLink = data.GuranteeOfdLink + orderResult.GuranteeOfdLinkMd5 = data.GuranteeOfdLinkMd5 + orderResult.InvoiceLink = data.InvoiceLink + orderResult.InvoiceLinkMd5 = data.InvoiceLinkMd5 + orderResult.MinPremium = data.MinPremium + orderResult.Rate = data.Rate + orderResult.ServiceAmount = data.ServiceAmount + err := global.DB.Save(&orderResult).Error + if err != nil { + return nil, err + } + return &orderResult, nil +} + +func (orderService) UploadPdf(fileParam string, c *gin.Context) (string, error) { + file, err := c.FormFile(fileParam) + if err != nil { + return "", e.NewError("文件上传失败") + } + reader, err := file.Open() + if err != nil { + return "", e.NewError("文件打开失败:" + err.Error()) + } + b := make([]byte, 512) + reader.Read(b) + contentType := http.DetectContentType(b) + if contentType != "application/pdf" && contentType != "application/zip" { + return "", e.NewError("文件格式错误:" + contentType) + } + fileNameItems := strings.Split(file.Filename, ".") + filePath := time.Now().Format("20060102") + "/" + utils.Md5(file.Filename+strconv.Itoa(int(time.Now().UnixMicro()))+utils.GenerateRandomString(10)) + "." + fileNameItems[len(fileNameItems)-1] + uploadPath := "." + if global.Config.Server.RootPath != "" { + uploadPath = global.Config.Server.RootPath + } + dest := uploadPath + "/static/uploads/" + filePath + err = c.SaveUploadedFile(file, dest) + if err != nil { + return "", e.NewError("文件保存失败:" + err.Error()) + } + return global.Config.Server.Domain + "/uploads/" + filePath, err +} + +func (s *orderService) UpdateApplicant(req request.UpdateApplicantRequest) (*model.Order, error) { + order, err := s.GetOrderByToken(req.Token) + if err != nil { + return nil, err + } + + user := order.User + user.BankName = req.BankName + user.BankCardNum = req.BankCardNum + err = global.DB.Save(&user).Error + if err != nil { + return nil, err + } + + applicant := order.Applicant + applicant.ContactName = req.ContactName + applicant.ContactMobile = req.ContactMobile + applicant.CreditCode = req.CreditCode + applicant.ApplicantName = req.ApplicantName + err = global.DB.Save(&applicant).Error + if err != nil { + return nil, err + } + + order.User = user + order.Applicant = applicant + + return order, nil +} diff --git a/static/uploads/20240703/09d403e84da1b5ff497d1948d5fd9e53.pdf b/static/uploads/20240703/09d403e84da1b5ff497d1948d5fd9e53.pdf new file mode 100644 index 0000000..0ab424d Binary files /dev/null and b/static/uploads/20240703/09d403e84da1b5ff497d1948d5fd9e53.pdf differ diff --git a/static/uploads/20240703/144055af7a16281e17c89af18d178d1f.pdf b/static/uploads/20240703/144055af7a16281e17c89af18d178d1f.pdf new file mode 100644 index 0000000..0ab424d Binary files /dev/null and b/static/uploads/20240703/144055af7a16281e17c89af18d178d1f.pdf differ diff --git a/third_party/zking/claim_callback_data.go b/third_party/zking/claim_callback_data.go new file mode 100644 index 0000000..1f705f4 --- /dev/null +++ b/third_party/zking/claim_callback_data.go @@ -0,0 +1,7 @@ +package zking + +type ClaimCallbackData struct { + PolicyNo string `json:"policyNo" form:"policyNo"` + Msg string `json:"msg" form:"msg"` + Status string `json:"status" form:"status"` //2 已理赔,0 理赔失败 +} diff --git a/third_party/zking/common_param.go b/third_party/zking/common_param.go index 9052841..cc65cdd 100644 --- a/third_party/zking/common_param.go +++ b/third_party/zking/common_param.go @@ -5,7 +5,6 @@ type CommonParam struct { RequestID string `json:"requestid" form:"requestid"` Timestamp string `json:"timestamp" form:"timestamp"` Version string `json:"version" form:"version"` - //Data suixing.CreateData `json:"data" form:"data"` - Data string `json:"data" form:"data"` - Sign string `json:"sign" form:"sign"` + Data string `json:"data" form:"data"` + Sign string `json:"sign" form:"sign"` } diff --git a/third_party/zking/push_callback_data.go b/third_party/zking/push_callback_data.go new file mode 100644 index 0000000..ee59d6a --- /dev/null +++ b/third_party/zking/push_callback_data.go @@ -0,0 +1,20 @@ +package zking + +type PushCallbackData struct { + DataType string `json:"data_type" form:"data_type"` + OrderSn string `json:"ordersn" form:"ordersn"` + PolicyNo string `json:"policyNo" form:"policyNo"` + MinPremium string `json:"min_premium" form:"min_premium"` + Rate string `json:"rate" form:"rate"` + Status string `json:"status" form:"status"` // 3 已出单,其他状态待定 + ServiceAmount string `json:"service_amount" form:"service_amount"` + GuranteeLink string `json:"gurantee_link" form:"gurantee_link"` + GuranteeLinkMd5 string `json:"gurantee_link_md5" form:"gurantee_link_md5"` + GuranteeOfdLink string `json:"gurantee_ofd_link" form:"gurantee_ofd_link"` + GuranteeOfdLinkMd5 string `json:"gurantee_ofd_link_md5" form:"gurantee_ofd_link_md5"` + InvoiceLink string `json:"invoice_link" form:"invoice_link"` + InvoiceLinkMd5 string `json:"invoice_link_md5" form:"invoice_link_md5"` + ConfirmReceiptLink string `json:"confirm_receipt_link" form:"confirm_receipt_link"` + ConfirmReceiptLinkMd5 string `json:"confirm_receipt_link_md5" form:"confirm_receipt_link_md5"` + Remark string `json:"remark" form:"remark"` +} diff --git a/third_party/zking/revoke_callback_data.go b/third_party/zking/revoke_callback_data.go new file mode 100644 index 0000000..2cc9d10 --- /dev/null +++ b/third_party/zking/revoke_callback_data.go @@ -0,0 +1,7 @@ +package zking + +type RevokeCallbackData struct { + PolicyNo string `json:"policyNo" form:"policyNo"` + Msg string `json:"msg" form:"msg"` + Status string `json:"status" form:"status"` // 7 已撤销,3 已出单(客户申请退保之后又后悔了,可以传这个状态还原) +}