wechatController.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package wechat
  2. import (
  3. "rtzh_elec_temperature/tools"
  4. "time"
  5. "github.com/astaxie/beego"
  6. "github.com/patrickmn/go-cache"
  7. )
  8. var GoCahce *cache.Cache //定义全局变量
  9. type WechatController struct {
  10. beego.Controller
  11. }
  12. func init() {
  13. GoCahce = cache.New(5*time.Minute, 10*time.Minute)
  14. }
  15. // @router /api/common/weixindevapply [post,get]
  16. func (t *WechatController) WeixinDevApply() {
  17. wx := new(Wechat)
  18. wx.Init()
  19. wx.Message(t.Ctx)
  20. t.Data["json"] = map[string]interface{}{"success": true}
  21. t.ServeJSON()
  22. }
  23. // @router /api/wechat/r/chat [post,get]
  24. func (t *WechatController) WeixinRedirectChat() {
  25. wx := new(Wechat)
  26. wx.Init()
  27. oauth := wx.officialAccount.GetOauth()
  28. oauth.Redirect(t.Ctx.ResponseWriter, t.Ctx.Request, "https://www.jujutong.cloud/api/app/chat", "snsapi_base", "jujutong123")
  29. }
  30. // @router /api/wechat/message/push [post,get]
  31. func (t *WechatController) GetWeChatPushMessage() {
  32. to := t.GetString("to")
  33. txt := t.GetString("msg")
  34. if to == "" || txt == "" {
  35. t.Data["json"] = WarpError("无效的参数")
  36. t.ServeJSON()
  37. return
  38. }
  39. wx := new(Wechat)
  40. wx.Init()
  41. wx.PushMsg(to, txt)
  42. t.Data["json"] = WarpOK("")
  43. t.ServeJSON()
  44. }
  45. // @router /api/wechat/user/get [post,get]
  46. func (t *WechatController) GetWeChatUserinfo() {
  47. token := t.GetString("token")
  48. if token == "" {
  49. t.Data["json"] = WarpError("无效的参数")
  50. t.ServeJSON()
  51. return
  52. }
  53. data, _ := GoCahce.Get(token)
  54. if data == nil {
  55. t.Data["json"] = WarpError("数据已过期")
  56. t.ServeJSON()
  57. return
  58. }
  59. t.Data["json"] = WarpOK(data)
  60. t.ServeJSON()
  61. }
  62. // @router /api/app/chat [post,get]
  63. func (t *WechatController) AppRedirectChat() {
  64. wx := new(Wechat)
  65. wx.Init()
  66. oauth := wx.officialAccount.GetOauth()
  67. token, err := oauth.GetUserAccessToken(t.GetString("code"))
  68. if err != nil {
  69. tools.Log(err)
  70. return
  71. }
  72. //fmt.Println(token)
  73. wx.UpdateToken(token)
  74. openid := token.AccessToken
  75. /*
  76. //获取用户信息
  77. us := wx.officialAccount.GetUser()
  78. uinfo, er := us.GetUserInfo(openid)
  79. if er != nil {
  80. tools.Log(er)
  81. return
  82. }
  83. fmt.Println(uinfo)
  84. */
  85. u := wx.officialAccount.GetUser()
  86. uinfo, err := u.GetUserInfo(token.OpenID)
  87. //ustr, _ := json.Marshal(uinfo)
  88. chatUrl := "http://www.jujutong.cloud:39103/api/login/wechat?aid=" + wx.Cfg.AppID + "&uid=" + openid
  89. GoCahce.Add(token.OpenID, token.AccessToken, 5*time.Minute)
  90. GoCahce.Add(token.AccessToken, uinfo, 5*time.Minute)
  91. t.Ctx.Redirect(302, chatUrl)
  92. }
  93. func WarpOK(data interface{}) interface{} {
  94. user := make(map[string]interface{})
  95. user["data"] = data
  96. user["returncode"] = 200
  97. user["msg"] = ""
  98. return user
  99. }
  100. func WarpError(msg string) interface{} {
  101. user := make(map[string]interface{})
  102. user["data"] = ""
  103. user["returncode"] = 500
  104. user["msg"] = msg
  105. return user
  106. }