package controller import ( "ElectronicInvoice/internal/service" "encoding/base64" "fmt" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "net/url" "os" "strings" "time" ) type callBackParam struct { CallType string `json:"calltype"` // 回调信息类型 TaxNum string `json:"taxNum"` //税号 Tel string `json:"tel"` //税号 Parameter string `json:"parameter"` //校验 } // CallBack 回调 func CallBack(r *ghttp.Request) { err := func() error { callType := r.Get("calltype").String() if g.Cfg().MustGet(r.Context(), "reqDebug", false).Bool() { g.Log().Infof(r.Context(), "%s\ncallType: %s\ncallBack: %s", strings.Repeat("=", 50), callType, string(r.GetBody())) } switch callType { case "userQuit": //用户退出登录 g.Log().Infof(r.Context(), "长在线退出登录:%s", "string(bb)") case "Invoicing": //开发票回调 return InvoicingCallBackLogic(r) case "InvoicingAll": //批量开票 g.Log().Infof(r.Context(), "用户退出登录:%s", "string(bb)") case "Offset": g.Log().Infof(r.Context(), "开红票:%s", "string(bb)") case "livenessDetection": g.Log().Infof(r.Context(), "活体认证:%s", "string(bb)") default: g.Log().Infof(r.Context(), "未设置消息回调:%s", callType) } return nil }() if err != nil { g.Log().Errorf(r.Context(), "回调异常:%v", err) return } r.Response.WriteJson(g.MapStrStr{"code": "200"}) } //callbak = logging // InvoicingCallBackLogic 开票回调逻辑 func InvoicingCallBackLogic(r *ghttp.Request) error { tType := r.Get("type").Int() //type [0 需要活体检测;1 成功 返回base64] switch tType { case 0: //需要活体检测 imgVal, err := url.QueryUnescape(r.Get("img").String()) if err != nil { return gerror.Wrap(err, "活体检测二维码解析失败") } if err := service.SendQrImage2ChatBot(imgVal); err != nil { return gerror.Wrap(err, "发送活体检测消息出错") } case 1: //开票成功 pdfBase64 := r.Get("pdf").String() orderCode := r.Get("id").String() fmt.Println("orderCode") data, err := base64.StdEncoding.DecodeString(pdfBase64) if err != nil { return gerror.Wrap(err, "解析文件base64编码失败") } if err := os.WriteFile(fmt.Sprintf("./%s_%d.pdf", orderCode, time.Now().Unix()), data, 0644); err != nil { return gerror.Wrap(err, "解析文件base64编码失败") } g.Log().Infof(r.Context(), "pdf保存成功") case 3: g.Log().Info(r.Context(), "活体验证已过期,未完成活体验证") case 4: g.Log().Info(r.Context(), "系统错误") case 6: g.Log().Info(r.Context(), "税率不存在") case 7: g.Log().Info(r.Context(), "非数电票试点纳税人,未核定数电票票种,不允许开票或其他原因") default: g.Log().Infof(r.Context(), "InvoicingCallBackLogic tType:%s", tType) } return nil }