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