main.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package main
  2. import (
  3. "ElectronicInvoice/util"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "github.com/gogf/gf/v2/net/ghttp"
  9. "github.com/gogf/gf/v2/os/gctx"
  10. "github.com/gogf/gf/v2/util/gconv"
  11. "github.com/gogf/gf/v2/util/grand"
  12. "github.com/gogf/gf/v2/util/guid"
  13. "time"
  14. )
  15. type (
  16. // 开发票
  17. makeInvoiceAllParam struct {
  18. TaxNum string `json:"taxNum"` //企业税号*
  19. Tel string `json:"tel"` //登录电子税局手机号或身份证号*
  20. Data []MakeInvoiceData `json:"data"`
  21. }
  22. MakeInvoiceData struct {
  23. Type string `json:"type"` //票类* 1 增值税专用发票;2 普通发票
  24. Gmfmc string `json:"gmfmc"` //购买方名称* 人名
  25. Gmfnsrsbh string `json:"gmfnsrsbh"` //购买方纳税人识别号* 身份证号
  26. Id string `json:"id"` //发票流水号* 合作商开具的发票流水号,涉及到回调通知
  27. Gmfdz string `json:"gmfdz"` //购买方地址
  28. Lxdh string `json:"lxdh"` //购买方联系方式
  29. Yhyywdmc string `json:"yhyywdmc"` //购买方开户行
  30. Yhzh string `json:"yhzh"` //购买方银行账号
  31. Fhr string `json:"fhr"` //复核人
  32. Notes string `json:"notes"` //发票备注
  33. InvoiceArr []MakeInvoiceItems `json:"invoiceArr"`
  34. }
  35. MakeInvoiceItems struct {
  36. Xmmc string `json:"xmmc"` //项目名称*
  37. Je string `json:"je"` //开票金额*
  38. WhStatus int `json:"wh_status"` //该开票项是否已调用接口进行维护: 1 已维护;0 未维护
  39. Xhgg string `json:"xhgg"` //型号规格
  40. Dw string `json:"dw"` //单位
  41. Sl string `json:"sl"` //数量
  42. Tsaxrate string `json:"taxrate"` //税率
  43. }
  44. //红冲
  45. makeRedInvoiceAllParam struct {
  46. TaxNum string `json:"taxNum"` //企业税号*
  47. Tel string `json:"tel"` //登录电子税局手机号或身份证号*
  48. Data []MakeRedInvoiceData `json:"data"`
  49. }
  50. MakeRedInvoiceData struct {
  51. Num string `json:"num"` //蓝票号码
  52. Date string `json:"date"` //购买方名称* 人名
  53. }
  54. CommonRes struct {
  55. Code int `json:"code"`
  56. Msg string `json:"msg"`
  57. Data interface{} `json:"data"`
  58. }
  59. )
  60. func main() {
  61. s := g.Server()
  62. s.Group("/", func(group *ghttp.RouterGroup) {
  63. group.POST("/authority_token/getToken", getToken) //获取token
  64. group.Group("/index_index", func(group2 *ghttp.RouterGroup) {
  65. group2.POST("/makeInvoiceC", makeInvoiceC) //开票
  66. group2.POST("/makeRedC", makeRedC) //红票
  67. })
  68. })
  69. s.Run()
  70. }
  71. var (
  72. getToken = func(r *ghttp.Request) {
  73. resp := struct {
  74. Token string `json:"token"`
  75. ExpiresIn int `json:"expiresIn"` //一天有效期
  76. }{
  77. Token: fmt.Sprintf("MOCKTOKEN_%s", guid.S()),
  78. ExpiresIn: 60 * 60 * 24,
  79. }
  80. r.Response.Write(CommonRes{
  81. Code: 200,
  82. Msg: "",
  83. Data: resp,
  84. })
  85. }
  86. makeInvoiceC = func(r *ghttp.Request) {
  87. var (
  88. ctx = r.Context()
  89. param = makeInvoiceAllParam{}
  90. )
  91. err := json.Unmarshal(r.GetBody(), &param)
  92. if err != nil {
  93. g.Log().Errorf(ctx, "开票任务-接收参数异常 err:%v", err)
  94. return
  95. }
  96. g.Log().Infof(ctx, "开票任务-接收到开票信息:%s", gconv.String(param))
  97. time.Sleep(time.Second * time.Duration(grand.Intn(3))) //模拟处理
  98. go func() {
  99. goCtx := gctx.New()
  100. context, err := util.GetPdfBase64(fmt.Sprintf("./pdfDir/invoice_Blue_%s.pdf", param.Data[0].Type)) //pdf文件
  101. if err != nil {
  102. g.Log().Errorf(goCtx, "读取pdf文件异常%v", err)
  103. return
  104. }
  105. callBackParam := g.Map{
  106. "calltype": "Invoicing",
  107. "type": 1,
  108. "pdf": context,
  109. "num": fmt.Sprintf("MOCKBLUE_%s", guid.S()),
  110. "id": param.Data[0].Id,
  111. "kptime": time.Now().Format("2006-01-02 15:04:05"),
  112. }
  113. //TODO 回调开票成功
  114. for {
  115. if checkCallBackOk(goCtx, callBackParam) {
  116. g.Log().Infof(goCtx, "开票任务-开票成功")
  117. break
  118. }
  119. time.Sleep(time.Second * 3)
  120. }
  121. //TODO 任务完成通知
  122. for {
  123. if checkCallBackOk(goCtx, g.Map{"calltype": "quit", "data": g.Map{"taskType": "2"}}) {
  124. g.Log().Infof(goCtx, "开票任务-通知完成")
  125. break
  126. }
  127. time.Sleep(time.Second * 3)
  128. }
  129. }()
  130. r.Response.WriteJson(CommonRes{
  131. Code: 200,
  132. Msg: "",
  133. })
  134. }
  135. makeRedC = func(r *ghttp.Request) {
  136. var (
  137. ctx = r.Context()
  138. param = makeRedInvoiceAllParam{}
  139. )
  140. err := json.Unmarshal(r.GetBody(), &param)
  141. if err != nil {
  142. g.Log().Errorf(ctx, "红冲任务-接收参数异常 err:%v", err)
  143. return
  144. }
  145. g.Log().Infof(ctx, "红冲任务-接收到红冲信息:%s", gconv.String(param))
  146. time.Sleep(time.Second * time.Duration(grand.Intn(3))) //模拟处理
  147. go func() {
  148. goCtx := gctx.New()
  149. context, err := util.GetPdfBase64("./pdfDir/invoice_Red.pdf") //pdf文件
  150. if err != nil {
  151. g.Log().Errorf(goCtx, "读取pdf文件异常%v", err)
  152. return
  153. }
  154. callBackParam := g.Map{
  155. "calltype": "offset",
  156. "type": 0,
  157. "pdf": context,
  158. "num": param.Data[0].Num,
  159. "offsetNum": fmt.Sprintf("MOCKRED_%s", guid.S()),
  160. "kptime": time.Now().Format("2006-01-02 15:04:05"),
  161. }
  162. //TODO 回调红冲成功
  163. for {
  164. if checkCallBackOk(goCtx, callBackParam) {
  165. g.Log().Infof(goCtx, "红冲任务-红冲成功")
  166. break
  167. }
  168. time.Sleep(time.Second * 3)
  169. }
  170. //TODO 任务完成通知
  171. for {
  172. if checkCallBackOk(goCtx, g.Map{"calltype": "quit", "data": g.Map{"taskType": "9"}}) {
  173. g.Log().Infof(goCtx, "红冲任务-通知完成")
  174. break
  175. }
  176. time.Sleep(time.Second * 3)
  177. }
  178. }()
  179. r.Response.WriteJson(CommonRes{
  180. Code: 200,
  181. Msg: "",
  182. })
  183. }
  184. )
  185. func checkCallBackOk(ctx context.Context, param map[string]interface{}) bool {
  186. res, err := g.Client().Header(g.MapStrStr{"Content-Type": "application/x-www-form-urlencoded"}).Post(ctx, g.Cfg().MustGet(ctx, "callBackAction").String(), param)
  187. if err != nil {
  188. g.Log().Errorf(ctx, "模拟请求异常 %v", err)
  189. return false
  190. }
  191. defer res.Close()
  192. rData := gconv.MapStrStr(res.ReadAll())
  193. if rData["code"] == "200" {
  194. return true
  195. }
  196. return false
  197. }