框架优化
This commit is contained in:
+160
-74
@@ -2,50 +2,149 @@ package app
|
||||
|
||||
import (
|
||||
. "code.hoteas.com/golang/hotime"
|
||||
. "code.hoteas.com/golang/hotime/cache"
|
||||
. "code.hoteas.com/golang/hotime/common"
|
||||
"code.hoteas.com/golang/hotime/dri/download"
|
||||
"fmt"
|
||||
"github.com/silenceper/wechat/v2/cache"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/config"
|
||||
"time"
|
||||
|
||||
//"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, "没有登录")
|
||||
"login": func(that *Context) {
|
||||
nickname := that.Req.FormValue("nickname")
|
||||
avatar := that.Req.FormValue("avatarUrl")
|
||||
encryptedData := that.Req.FormValue("encryptedData")
|
||||
iv := that.Req.FormValue("iv")
|
||||
|
||||
if that.Session("wechat_id").Data == nil {
|
||||
that.Display(2, "还没有获取token")
|
||||
return
|
||||
}
|
||||
|
||||
that.Display(0, that.Session("wechatInfo").ToMap())
|
||||
if nickname == "" || avatar == "" || encryptedData == "" || iv == "" {
|
||||
that.Display(3, "参数不足")
|
||||
return
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
miniProgram = weixin.GetMiniProgram(cfg)
|
||||
}
|
||||
|
||||
wechatId := that.Session("wechat_id").ToCeilInt()
|
||||
|
||||
wchat := that.Db.Get("wechat", "*", Map{"id": wechatId})
|
||||
|
||||
if wchat == nil {
|
||||
that.Display(4, "找不到该用户")
|
||||
return
|
||||
}
|
||||
|
||||
eny := miniProgram.GetEncryptor()
|
||||
re, e := eny.Decrypt(wchat.GetString("retoken"), encryptedData, iv)
|
||||
|
||||
if e != nil {
|
||||
that.Display(4, e)
|
||||
return
|
||||
}
|
||||
|
||||
admin := that.Db.Get("admin", "*", Map{"phone": re.PhoneNumber})
|
||||
if admin == nil {
|
||||
that.Display(5, "你不是本系统用户,无法使用本系统")
|
||||
return
|
||||
}
|
||||
|
||||
t := time.Now().Unix()
|
||||
path := time.Now().Format(that.Config.GetString("imgPath"))
|
||||
filename := Md5(ObjToStr(t)) + ".jpg"
|
||||
down := download.Down(avatar, that.Config.GetString("tpt")+"/"+path, filename)
|
||||
if down {
|
||||
avatar = path + filename
|
||||
}
|
||||
|
||||
that.Db.Update("wechat", Map{"admin_id": admin.GetCeilInt("id"), "modify_time": t, "avatar": avatar, "nickname": nickname}, Map{"id": wchat.GetCeilInt64("id")})
|
||||
that.Db.Update("admin", Map{"avatar_img": avatar, "modify_time": t}, Map{"id": admin.GetCeilInt64("id")})
|
||||
|
||||
that.Session("admin_id", admin.GetCeilInt64("id"))
|
||||
that.Display(0, "成功!")
|
||||
},
|
||||
"code": func(that *Context) {
|
||||
|
||||
that.Session("admin_id", 1)
|
||||
that.Display(0, Map{"status": 1, "token": that.SessionId})
|
||||
return
|
||||
|
||||
code := that.Req.FormValue("code")
|
||||
if code == "" {
|
||||
that.Display(3, "缺少code")
|
||||
return
|
||||
}
|
||||
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,
|
||||
}
|
||||
|
||||
miniProgram = weixin.GetMiniProgram(cfg)
|
||||
}
|
||||
//mini := weixin.GetMiniProgram(cfg)
|
||||
a := miniProgram.GetAuth()
|
||||
re, e := a.Code2Session(code)
|
||||
fmt.Println(re)
|
||||
if e != nil {
|
||||
that.Display(4, e)
|
||||
return
|
||||
}
|
||||
wchat := that.Db.Get("wechat", "*", Map{"openid": re.OpenID})
|
||||
|
||||
if wchat == nil {
|
||||
wchat = Map{"openid": re.OpenID,
|
||||
"appid": a.AppID,
|
||||
"state": 0,
|
||||
"retoken": re.SessionKey,
|
||||
"modify_time": time.Now().Unix(),
|
||||
"create_time": time.Now().Unix(),
|
||||
"unionid": re.UnionID,
|
||||
}
|
||||
wchat["id"] = that.Db.Insert("wechat", wchat)
|
||||
|
||||
} else {
|
||||
wchat["modify_time"] = time.Now().Unix()
|
||||
wchat["retoken"] = re.SessionKey
|
||||
wchat["unionid"] = re.UnionID
|
||||
that.Db.Update("wechat", wchat, Map{"id": wchat.GetCeilInt("id")})
|
||||
}
|
||||
|
||||
that.Session("wechat_id", wchat.GetCeilInt("id"))
|
||||
|
||||
if wchat.GetCeilInt("admin_id") == 0 {
|
||||
that.Display(0, Map{"status": 0, "token": that.SessionId})
|
||||
return
|
||||
}
|
||||
|
||||
that.Session("admin_id", wchat.GetCeilInt("admin_id"))
|
||||
that.Display(0, Map{"status": 1, "token": that.SessionId})
|
||||
return
|
||||
|
||||
},
|
||||
////微信注册,0已经完整的注册了,1还没有注册
|
||||
//"codebase": func(that *Context) {
|
||||
@@ -147,45 +246,32 @@ var Wechat = Ctr{
|
||||
// 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)
|
||||
|
||||
},
|
||||
//"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"
|
||||
//
|
||||
// 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)
|
||||
//
|
||||
//},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user