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

96 lines
2.3 KiB

5 years ago
  1. package common
  2. import (
  3. "crypto/sha1"
  4. "encoding/xml"
  5. "fmt"
  6. "hudongzhuanjia/controllers"
  7. "hudongzhuanjia/logger"
  8. "hudongzhuanjia/services/pay"
  9. "time"
  10. )
  11. var Now = time.Now()
  12. type WeChatOauthCtl struct {
  13. controllers.BaseCtl
  14. }
  15. const token = "hd_oauth"
  16. // 验证服务器资源配置
  17. func (t *WeChatOauthCtl) Oauth() {
  18. signature := t.MustGet("signature")
  19. timestamp := t.MustGet("timestamp")
  20. nonce := t.MustGet("nonce")
  21. echoStr := t.MustGet("echostr")
  22. tokenStr := fmt.Sprintf("%s%s%s", token, timestamp, nonce)
  23. s := fmt.Sprintf("%s", sha1.New().Sum([]byte(tokenStr)))
  24. if s == signature {
  25. t.OriginResponseWriter.Write([]byte(echoStr))
  26. }
  27. t.OriginResponseWriter.Write([]byte("failed"))
  28. }
  29. func (t *WeChatOauthCtl) Checkin() {
  30. t.JSON(map[string]interface{}{
  31. "success": "你好, tommy黄梓健",
  32. "version": Now,
  33. "ip": t.Request.OriginRequest.RemoteAddr,
  34. })
  35. }
  36. //
  37. //func init() {
  38. // ticker := time.NewTicker(5 * time.Second)
  39. // go func() {
  40. // fmt.Println("循环查询订单数据")
  41. // for range ticker.C {
  42. // pay_service.BatchQuery()
  43. // }
  44. // }()
  45. //
  46. //}
  47. type CallbackParam struct {
  48. XMLName xml.Name `xml:"xml"`
  49. ReturnCode string `xml:"return_code"`
  50. ReturnMsg string `xml:"return_msg"`
  51. }
  52. func (t *WeChatOauthCtl) CallbackOrder() {
  53. // 搜索支付的order表, 查找到某条记录
  54. logger.Sugar.Infof("%s", t.Request.OriginRequest.RemoteAddr)
  55. t.CheckErr(pay_service.CallbackOrder(t.Request.OriginRequest))
  56. param := new(CallbackParam)
  57. param.ReturnCode = "SUCCESS"
  58. param.ReturnMsg = "OK"
  59. xmlRes, _ := xml.Marshal(param)
  60. t.XML(xmlRes)
  61. }
  62. func (t *WeChatOauthCtl) CallbackRefund() {
  63. logger.Sugar.Infof("%s", t.Request.OriginRequest.RemoteAddr)
  64. t.CheckErr(pay_service.CallbackRefund(t.Request.OriginRequest))
  65. param := new(CallbackParam)
  66. param.ReturnCode = "SUCCESS"
  67. param.ReturnMsg = "OK"
  68. xmlRes, _ := xml.Marshal(param)
  69. t.XML(xmlRes)
  70. }
  71. //func (t *WeChatOauthCtl) SendRedPack() {
  72. // user := new(models.User)
  73. // exist, err := models.GetById(user, 234)
  74. // t.CheckErr(err)
  75. // if !exist {
  76. // t.ERROR(fmt.Sprintf("error occur: %s", err), 120)
  77. // }
  78. // res, err := pay_service.SendRedPack("欧轩互动", user.Openid, "测试", "123.207.246.51",
  79. // "测试", "测试", 100, 1, 1, user.Id, 1)
  80. // t.CheckErr(err)
  81. // fmt.Printf("%+v", res)
  82. // t.JSON(map[string]interface{}{
  83. // "result": res,
  84. // })
  85. //}