123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package wechat
- import (
- "rtzh_elec_temperature/tools"
- "time"
- "github.com/astaxie/beego"
- "github.com/patrickmn/go-cache"
- )
- var GoCahce *cache.Cache //定义全局变量
- type WechatController struct {
- beego.Controller
- }
- func init() {
- GoCahce = cache.New(5*time.Minute, 10*time.Minute)
- }
- // @router /api/common/weixindevapply [post,get]
- func (t *WechatController) WeixinDevApply() {
- wx := new(Wechat)
- wx.Init()
- wx.Message(t.Ctx)
- t.Data["json"] = map[string]interface{}{"success": true}
- t.ServeJSON()
- }
- // @router /api/wechat/r/chat [post,get]
- func (t *WechatController) WeixinRedirectChat() {
- wx := new(Wechat)
- wx.Init()
- oauth := wx.officialAccount.GetOauth()
- oauth.Redirect(t.Ctx.ResponseWriter, t.Ctx.Request, "https://www.jujutong.cloud/api/app/chat", "snsapi_base", "jujutong123")
- }
- // @router /api/wechat/message/push [post,get]
- func (t *WechatController) GetWeChatPushMessage() {
- to := t.GetString("to")
- txt := t.GetString("msg")
- if to == "" || txt == "" {
- t.Data["json"] = WarpError("无效的参数")
- t.ServeJSON()
- return
- }
- wx := new(Wechat)
- wx.Init()
- wx.PushMsg(to, txt)
- t.Data["json"] = WarpOK("")
- t.ServeJSON()
- }
- // @router /api/wechat/user/get [post,get]
- func (t *WechatController) GetWeChatUserinfo() {
- token := t.GetString("token")
- if token == "" {
- t.Data["json"] = WarpError("无效的参数")
- t.ServeJSON()
- return
- }
- data, _ := GoCahce.Get(token)
- if data == nil {
- t.Data["json"] = WarpError("数据已过期")
- t.ServeJSON()
- return
- }
- t.Data["json"] = WarpOK(data)
- t.ServeJSON()
- }
- // @router /api/app/chat [post,get]
- func (t *WechatController) AppRedirectChat() {
- wx := new(Wechat)
- wx.Init()
- oauth := wx.officialAccount.GetOauth()
- token, err := oauth.GetUserAccessToken(t.GetString("code"))
- if err != nil {
- tools.Log(err)
- return
- }
- //fmt.Println(token)
- wx.UpdateToken(token)
- openid := token.AccessToken
- /*
- //获取用户信息
- us := wx.officialAccount.GetUser()
- uinfo, er := us.GetUserInfo(openid)
- if er != nil {
- tools.Log(er)
- return
- }
- fmt.Println(uinfo)
- */
- u := wx.officialAccount.GetUser()
- uinfo, err := u.GetUserInfo(token.OpenID)
- //ustr, _ := json.Marshal(uinfo)
- chatUrl := "http://www.jujutong.cloud:39103/api/login/wechat?aid=" + wx.Cfg.AppID + "&uid=" + openid
- GoCahce.Add(token.OpenID, token.AccessToken, 5*time.Minute)
- GoCahce.Add(token.AccessToken, uinfo, 5*time.Minute)
- t.Ctx.Redirect(302, chatUrl)
- }
- func WarpOK(data interface{}) interface{} {
- user := make(map[string]interface{})
- user["data"] = data
- user["returncode"] = 200
- user["msg"] = ""
- return user
- }
- func WarpError(msg string) interface{} {
- user := make(map[string]interface{})
- user["data"] = ""
- user["returncode"] = 500
- user["msg"] = msg
- return user
- }
|