Files
hotime/example/app/wechat.go
T
2022-04-18 06:28:01 +08:00

114 lines
2.3 KiB
Go

package app
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
"fmt"
)
var Wechat = Ctr{
"login": func(that *Context) {
retoken := that.Req.FormValue("retoken")
encryptedData := that.Req.FormValue("encryptedData")
iv := that.Req.FormValue("iv")
if retoken == "" || encryptedData == "" || iv == "" {
that.Display(3, "参数不足")
return
}
eny := miniProgram.GetEncryptor()
re, e := eny.Decrypt(retoken, encryptedData, iv)
if e != nil {
that.Display(4, e)
return
}
that.Display(0, re.PhoneNumber)
},
"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
}
wchat := Map{"openid": re.OpenID,
"appid": a.AppID,
"state": 0,
"retoken": re.SessionKey,
"unionid": re.UnionID,
}
//wchat["id"] = that.Db.Insert("wechat", wchat)
that.Display(0, wchat)
return
},
////微信注册,0已经完整的注册了,1还没有注册
"codeh5": func(that *Context) {
auth := h5Program.GetOauth()
//weixin.GetOpenPlatform()
resToken, err := auth.GetUserAccessToken(that.Req.FormValue("code"))
if err != nil {
that.Display(5, "code错误")
return
}
//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,
}
that.Display(0, wechatInfo)
},
//网页签名
"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)
},
}