hotime/example/app/wechat.go

192 lines
5.2 KiB
Go
Raw Normal View History

2022-03-14 08:48:19 +00:00
package app
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/cache"
. "code.hoteas.com/golang/hotime/common"
"github.com/silenceper/wechat/v2/cache"
//"fmt"
"github.com/silenceper/wechat/v2"
//"github.com/silenceper/wechat/v2/cache"
"github.com/silenceper/wechat/v2/officialaccount/config"
"time"
)
type WechatCache struct {
CacheIns
}
func (that WechatCache) Get(key string) interface{} {
return that.Cache("x" + key).Data
//return nil
}
func (that WechatCache) Set(key string, val interface{}, timeout time.Duration) error {
that.Cache("x"+key, val, ObjToInt64(timeout.Seconds()))
return nil
}
func (that WechatCache) IsExist(key string) bool {
c := that.Cache("x" + key).Data
if c != nil {
return true
}
return false
}
func (that WechatCache) Delete(key string) error {
that.Cache("x"+key, nil)
return nil
}
var Wechat = Ctr{
"user": func(that *Context) {
if that.Session("wechatInfo").Data == nil {
that.Display(2, "没有登录")
}
that.Display(0, that.Session("wechatInfo").ToMap())
},
////微信注册0已经完整的注册了1还没有注册
//"codebase": func(that *Context) {
// wx := Weixin(that)
// auth := wx.GetOauth()
// resToken, err := auth.GetUserAccessToken(that.Req.FormValue("code"))
// if err != nil {
// that.Display(6, "code错误")
// return
// }
//
// //判断用户是否已经注册
// user := that.Db.Get("wechat", Map{"[><]user": "wechat.user_id=user.id"}, "user.id,user.state", Map{"openid": resToken.OpenID})
// if user != nil {
// that.Session("user_id", user.Get("id"))
// that.Display(0, 0)
// return
// }
//
// user_id := that.Db.Insert("user", Map{"time": time.Now().Unix(), "state": 2})
// if user_id == 0 {
// that.Display(4, "创建用户失败")
// return
// }
//
// wid := that.Db.Insert("wechat", Map{"openid": resToken.OpenID, "appid": that.Config.GetString("wechatAppID"), "state": 1, "user_id": user_id})
// if wid == 0 {
// that.Display(4, "关联微信失败!")
// return
// }
//
// that.Session("user_id", user.Get("id"))
//
// that.Display(0, 1)
//},
////微信注册0已经完整的注册了1还没有注册
//"code": func(that *Context) {
//
// orgId := ObjToInt(that.Req.FormValue("org_id"))
// //if orgId==0{
// // that.Display(3, "缺少组织id")
// // return
// //}
//
// wx := Weixin(that)
// auth := wx.GetOauth()
// resToken, err := auth.GetUserAccessToken(that.Req.FormValue("code"))
// if err != nil {
// that.Display(5, "code错误")
// return
// }
//
// //判断用户是否已经注册
// user := that.Db.Get("wechat", Map{"[><]user": "wechat.user_id=user.id"}, "user.id,user.state", Map{"openid": resToken.OpenID})
// if user != nil && user.GetCeilInt("id") != 0 && user.GetCeilInt("state") == 0 {
// that.Session("user_id", user.Get("id"))
// that.Display(0, Map{"type": 0, "token": that.SessionId})
// return
// }
// //getUserInfo
// userInfo, err := auth.GetUserInfo(resToken.AccessToken, resToken.OpenID)
// if err != nil {
// that.Display(6, "微信个人信息无法获取")
// return
// }
//
// //wechatInfo := ObjToMap(userInfo)
// t := time.Now().Unix()
// wechatInfo := Map{
// "openid": userInfo.OpenID,
// "acttoken": resToken.AccessToken,
// "retoken": resToken.RefreshToken,
// "appid": that.Config.GetString("wechatAppID"),
// "unionid": userInfo.Unionid,
// "nickname": userInfo.Nickname,
// "avatar": userInfo.HeadImgURL,
// }
// if orgId != 0 {
// wechatInfo["org_id"] = orgId
// wechatInfo["status"] = 0
// }
//
// wechatDb := that.Db.Get("wechat", "*", Map{"openid": wechatInfo.GetString("openid")})
//
// if wechatDb != nil {
//
// that.Db.Update("wechat", wechatInfo, Map{"id": wechatDb.GetCeilInt("id")})
// //userInfo["wid"]=wechatDb.GetCeilInt("wid")
// } else {
// wechatInfo["create_time"] = t
// wechatInfo["id"] = that.Db.Insert("wechat", wechatInfo)
// }
// wechatDb = that.Db.Get("wechat", "*", Map{"openid": wechatInfo.GetString("openid")})
//
// that.Session("wechatInfo", wechatDb)
// fmt.Println(wechatDb)
// fmt.Println(that.Session("wechatInfo"))
// //that.Display(0, 1)
// that.Display(0, Map{"type": 1, "token": that.SessionId})
//},
//网页签名
"sign": func(that *Context) {
if that.Req.FormValue("sign_url") == "" {
that.Display(2, "参数不足")
return
}
signUrl := that.Req.FormValue("sign_url")
signUrl = "https://hycb.hoteas.com/wxapp"
if weixin == nil {
weixin = wechat.NewWechat()
memory := cache.NewMemory()
cfg := &config.Config{
AppID: that.Config.GetString("wechatAppID"),
AppSecret: that.Config.GetString("wechatAppSecret"),
//Token: "xxx",
//EncodingAESKey: "xxxx",
Cache: memory,
}
//config := wechat.Config{ AppID: that.Config.GetString("wechatAppID"), AppSecret: that.Config.GetString("wechatAppSecret")}
//weixin = wechat.NewWechat()
officialAccount = weixin.GetOfficialAccount(cfg)
}
js := officialAccount.GetJs()
//js.GetConfig()
//js := weixin.GetJs()
cfg1, e := js.GetConfig(signUrl)
if e != nil {
that.Display(7, e)
return
}
sign := Map{
"appId": cfg1.AppID,
"timestamp": cfg1.Timestamp,
"nonceStr": cfg1.NonceStr,
"signature": cfg1.Signature,
}
that.Display(0, sign)
},
}