callback.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package controller
  2. import (
  3. "ElectronicInvoice/internal/service"
  4. "encoding/base64"
  5. "fmt"
  6. "github.com/gogf/gf/v2/errors/gerror"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/net/ghttp"
  9. "net/url"
  10. "os"
  11. "strings"
  12. "time"
  13. )
  14. type callBackParam struct {
  15. CallType string `json:"calltype"` // 回调信息类型
  16. TaxNum string `json:"taxNum"` //税号
  17. Tel string `json:"tel"` //税号
  18. Parameter string `json:"parameter"` //校验
  19. }
  20. // CallBack 回调
  21. func CallBack(r *ghttp.Request) {
  22. err := func() error {
  23. callType := r.Get("calltype").String()
  24. if g.Cfg().MustGet(r.Context(), "reqDebug", false).Bool() {
  25. g.Log().Infof(r.Context(), "%s\ncallType: %s\ncallBack: %s", strings.Repeat("=", 50), callType, string(r.GetBody()))
  26. }
  27. switch callType {
  28. case "userQuit": //用户退出登录
  29. g.Log().Infof(r.Context(), "长在线退出登录:%s", "string(bb)")
  30. case "Invoicing": //开发票回调
  31. return InvoicingCallBackLogic(r)
  32. case "InvoicingAll": //批量开票
  33. g.Log().Infof(r.Context(), "用户退出登录:%s", "string(bb)")
  34. case "Offset":
  35. g.Log().Infof(r.Context(), "开红票:%s", "string(bb)")
  36. case "livenessDetection":
  37. g.Log().Infof(r.Context(), "活体认证:%s", "string(bb)")
  38. default:
  39. g.Log().Infof(r.Context(), "未设置消息回调:%s", callType)
  40. }
  41. return nil
  42. }()
  43. if err != nil {
  44. g.Log().Errorf(r.Context(), "回调异常:%v", err)
  45. return
  46. }
  47. r.Response.WriteJson(g.MapStrStr{"code": "200"})
  48. }
  49. //callbak = logging
  50. // InvoicingCallBackLogic 开票回调逻辑
  51. func InvoicingCallBackLogic(r *ghttp.Request) error {
  52. tType := r.Get("type").Int() //type [0 需要活体检测;1 成功 返回base64]
  53. switch tType {
  54. case 0: //需要活体检测
  55. imgVal, err := url.QueryUnescape(r.Get("img").String())
  56. if err != nil {
  57. return gerror.Wrap(err, "活体检测二维码解析失败")
  58. }
  59. if err := service.SendQrImage2ChatBot(imgVal); err != nil {
  60. return gerror.Wrap(err, "发送活体检测消息出错")
  61. }
  62. case 1: //开票成功
  63. pdfBase64 := r.Get("pdf").String()
  64. orderCode := r.Get("id").String()
  65. fmt.Println("orderCode")
  66. data, err := base64.StdEncoding.DecodeString(pdfBase64)
  67. if err != nil {
  68. return gerror.Wrap(err, "解析文件base64编码失败")
  69. }
  70. if err := os.WriteFile(fmt.Sprintf("./%s_%d.pdf", orderCode, time.Now().Unix()), data, 0644); err != nil {
  71. return gerror.Wrap(err, "解析文件base64编码失败")
  72. }
  73. g.Log().Infof(r.Context(), "pdf保存成功")
  74. case 3:
  75. g.Log().Info(r.Context(), "活体验证已过期,未完成活体验证")
  76. case 4:
  77. g.Log().Info(r.Context(), "系统错误")
  78. case 6:
  79. g.Log().Info(r.Context(), "税率不存在")
  80. case 7:
  81. g.Log().Info(r.Context(), "非数电票试点纳税人,未核定数电票票种,不允许开票或其他原因")
  82. default:
  83. g.Log().Infof(r.Context(), "InvoicingCallBackLogic tType:%s", tType)
  84. }
  85. return nil
  86. }