互动
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.

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