callback.go 2.2 KB

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