互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package common
  2. import (
  3. "crypto/sha1"
  4. "encoding/xml"
  5. "fmt"
  6. "go.uber.org/atomic"
  7. "hudongzhuanjia/controllers"
  8. "hudongzhuanjia/libs/filter"
  9. "hudongzhuanjia/logger"
  10. pay_service "hudongzhuanjia/services/pay"
  11. "os"
  12. "time"
  13. )
  14. var Now = time.Now()
  15. type WeChatOauthCtl struct {
  16. controllers.BaseCtl
  17. }
  18. const token = "hd_oauth"
  19. // 验证服务器资源配置
  20. func (t *WeChatOauthCtl) Oauth() {
  21. signature := t.MustGet("signature")
  22. timestamp := t.MustGet("timestamp")
  23. nonce := t.MustGet("nonce")
  24. echoStr := t.MustGet("echostr")
  25. tokenStr := fmt.Sprintf("%s%s%s", token, timestamp, nonce)
  26. s := fmt.Sprintf("%s", sha1.New().Sum([]byte(tokenStr)))
  27. if s == signature {
  28. t.OriginResponseWriter.Write([]byte(echoStr))
  29. }
  30. t.OriginResponseWriter.Write([]byte("failed"))
  31. }
  32. func (t *WeChatOauthCtl) Checkin() {
  33. content, _ := t.Get("content")
  34. path, _ := os.Getwd()
  35. t.JSON(map[string]interface{}{
  36. "success": "你好, tommy黄梓健",
  37. "version": Now,
  38. "wd": path,
  39. "content": filter.Replace(content),
  40. })
  41. }
  42. //
  43. //func init() {
  44. // ticker := time.NewTicker(5 * time.Second)
  45. // go func() {
  46. // fmt.Println("循环查询订单数据")
  47. // for range ticker.C {
  48. // pay_service.BatchQuery()
  49. // }
  50. // }()
  51. //
  52. //}
  53. type CDATA struct {
  54. Text string `xml:",cdata"`
  55. }
  56. type CallbackParam struct {
  57. XMLName xml.Name `xml:"xml"`
  58. ReturnCode CDATA `xml:"return_code"`
  59. ReturnMsg CDATA `xml:"return_msg"`
  60. }
  61. var counter = new(atomic.Int64)
  62. func (t *WeChatOauthCtl) CallbackOrder() {
  63. counter.Add(1)
  64. //搜索支付的order表, 查找到某条记录
  65. order, err := pay_service.NotifyOrder(t.Request.OriginRequest.Body)
  66. if err != nil {
  67. logger.Error(fmt.Sprintf("出现错误: %v", err))
  68. return
  69. }
  70. err = pay_service.HandleSuccess(&pay_service.OrderDelayQueueParam{
  71. Order: order,
  72. })
  73. if err != nil {
  74. logger.Error(fmt.Sprintf("出现错误: %v", err))
  75. return
  76. }
  77. param := new(CallbackParam)
  78. param.ReturnCode = CDATA{Text: "SUCCESS"}
  79. param.ReturnMsg = CDATA{Text: "OK"}
  80. xmlRes, _ := xml.Marshal(param)
  81. if t.Request.OriginRequest.Method == "GET" {
  82. t.JSON(map[string]interface{}{
  83. "counter": counter.Load(),
  84. "time": time.Now(),
  85. })
  86. }
  87. logger.Error(fmt.Sprintf("微信响应次数: %v", counter.Load()))
  88. t.XML(xmlRes)
  89. }
  90. //func (t *WeChatOauthCtl) CallbackRefund() {
  91. // logger.Sugar.Infof("%s", t.Request.OriginRequest.RemoteAddr)
  92. // //t.CheckErr(pay_service.NotifyRefund(t.Request.OriginRequest))
  93. // param := new(CallbackParam)
  94. // param.ReturnCode = "SUCCESS"
  95. // param.ReturnMsg = "OK"
  96. // xmlRes, _ := xml.Marshal(param)
  97. // t.XML(xmlRes)
  98. //}
  99. //func (t *WeChatOauthCtl) SendRedPack() {
  100. // user := new(models.User)
  101. // exist, err := models.GetById(user, 234)
  102. // t.CheckErr(err)
  103. // if !exist {
  104. // t.ERROR(fmt.Sprintf("error occur: %s", err), 120)
  105. // }
  106. // res, err := pay_service.SendRedPack("欧轩互动", user.Openid, "测试", "123.207.246.51",
  107. // "测试", "测试", 100, 1, 1, user.Id, 1)
  108. // t.CheckErr(err)
  109. // fmt.Printf("%+v", res)
  110. // t.JSON(map[string]interface{}{
  111. // "result": res,
  112. // })
  113. //}