callback.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package controller
  2. import (
  3. "ElectronicInvoice/internal/service"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/ghttp"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. "net/url"
  8. "strings"
  9. )
  10. // CallBack 回调
  11. func CallBack(r *ghttp.Request) {
  12. err := func() error {
  13. var (
  14. callType = r.Get("calltype").String()
  15. ctx = r.Context()
  16. )
  17. if g.Cfg().MustGet(r.Context(), "reqDebug", false).Bool() {
  18. g.Log().Infof(r.Context(), "%s\n接口回调 信息调试\ncallType: %s\ncallBack form: %+v", strings.Repeat("=", 50), callType, printFilter(r.Request.Form))
  19. }
  20. switch callType {
  21. case "userQuit": //用户退出登录
  22. service.JyInvoiceManager.Login = false
  23. g.Log().Info(ctx, "长在线退出登录")
  24. case "Invoicing": //开发票回调
  25. return service.InvoicingCallBackLogic(r)
  26. case "InvoicingAll": //批量开票
  27. g.Log().Info(ctx, "用户退出登录")
  28. case "offset":
  29. return service.InvoicingMakeRedCallBackLogic(r)
  30. case "livenessDetection":
  31. g.Log().Info(ctx, "活体认证:%s")
  32. case "login": //扫码登录回调
  33. var flag bool
  34. if r.Get("code").String() == "200" {
  35. flag = true
  36. }
  37. service.JyInvoiceManager.ScanLogin <- flag
  38. case "quit":
  39. qData := gconv.Map(r.Get("data"))
  40. switch qData["taskType"] {
  41. case "2": //电子发票开具
  42. <-service.JyInvoiceManager.RunPool //可进行下次开票请求
  43. g.Log().Infof(ctx, "开具发票结束")
  44. case "4": //活体认证
  45. case "5": //验证电子税局登录授权
  46. case "9": //红冲
  47. <-service.JyInvoiceManager.RunPool //可进行下次开票请求
  48. g.Log().Infof(ctx, "发票冲红结束")
  49. default:
  50. g.Log().Infof(ctx, "未知回调任务退出 %+v", qData)
  51. }
  52. default:
  53. g.Log().Infof(ctx, "未设置消息回调:%s", callType)
  54. }
  55. return nil
  56. }()
  57. if err != nil {
  58. g.Log().Errorf(r.Context(), "回调异常:%v", err)
  59. return
  60. }
  61. r.Response.WriteJson(g.MapStrStr{"code": "200"})
  62. }
  63. // printFilter 打印参数调试
  64. func printFilter(value url.Values) map[string][]string {
  65. var filter = []string{"pdf"} //过滤部分字段
  66. res := map[string][]string{}
  67. for key, val := range value {
  68. add := true
  69. for _, v := range filter {
  70. if key == v {
  71. add = false
  72. break
  73. }
  74. }
  75. if add {
  76. res[key] = val
  77. }
  78. }
  79. return res
  80. }