hotime/example/app/wechat.go

115 lines
2.3 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/common"
2022-03-16 01:58:24 +00:00
"fmt"
2022-03-14 08:48:19 +00:00
)
2022-03-16 01:58:24 +00:00
var Wechat = Ctr{
"login": func(that *Context) {
2022-04-17 22:28:01 +00:00
2022-04-20 10:36:05 +00:00
sessionKey := that.Req.FormValue("sessionkey")
2022-03-16 01:58:24 +00:00
encryptedData := that.Req.FormValue("encryptedData")
iv := that.Req.FormValue("iv")
2022-03-14 08:48:19 +00:00
2022-04-20 10:36:05 +00:00
if sessionKey == "" || encryptedData == "" || iv == "" {
2022-03-16 01:58:24 +00:00
that.Display(3, "参数不足")
return
}
2022-03-14 08:48:19 +00:00
2022-03-16 01:58:24 +00:00
eny := miniProgram.GetEncryptor()
2022-04-20 10:36:05 +00:00
re, e := eny.Decrypt(sessionKey, encryptedData, iv)
2022-03-16 01:58:24 +00:00
if e != nil {
that.Display(4, e)
return
}
2022-04-17 22:28:01 +00:00
that.Display(0, re.PhoneNumber)
2022-04-20 10:36:05 +00:00
2022-03-16 01:58:24 +00:00
},
"code": func(that *Context) {
code := that.Req.FormValue("code")
if code == "" {
that.Display(3, "缺少code")
return
}
a := miniProgram.GetAuth()
re, e := a.Code2Session(code)
fmt.Println(re)
if e != nil {
that.Display(4, e)
return
}
2022-04-17 22:28:01 +00:00
wchat := Map{"openid": re.OpenID,
2022-04-20 10:36:05 +00:00
"appid": a.AppID,
"state": 0,
"sessionkey": re.SessionKey,
"unionid": re.UnionID,
2022-04-17 22:28:01 +00:00
}
//wchat["id"] = that.Db.Insert("wechat", wchat)
2022-03-16 01:58:24 +00:00
2022-04-17 22:28:01 +00:00
that.Display(0, wchat)
return
2022-03-16 01:58:24 +00:00
2022-04-17 22:28:01 +00:00
},
2022-03-14 08:48:19 +00:00
2022-04-17 22:28:01 +00:00
////微信注册0已经完整的注册了1还没有注册
"codeh5": func(that *Context) {
2022-03-16 01:58:24 +00:00
2022-04-17 22:28:01 +00:00
auth := h5Program.GetOauth()
//weixin.GetOpenPlatform()
resToken, err := auth.GetUserAccessToken(that.Req.FormValue("code"))
if err != nil {
that.Display(5, "code错误")
2022-03-16 01:58:24 +00:00
return
}
2022-04-17 22:28:01 +00:00
//getUserInfo
userInfo, err := auth.GetUserInfo(resToken.AccessToken, resToken.OpenID, "")
if err != nil {
that.Display(6, "微信个人信息无法获取")
return
}
//wechatInfo := ObjToMap(userInfo)
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,
}
2022-03-16 01:58:24 +00:00
2022-04-17 22:28:01 +00:00
that.Display(0, wechatInfo)
2022-03-14 08:48:19 +00:00
},
//网页签名
2022-04-17 22:28:01 +00:00
"sign": func(that *Context) {
if that.Req.FormValue("sign_url") == "" {
that.Display(2, "参数不足")
return
}
signUrl := that.Req.FormValue("sign_url")
js := h5Program.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)
},
2022-03-14 08:48:19 +00:00
}