main.go 6.4 KB

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