callback.go 2.5 KB

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