hotime/example/app/wechat.go
2022-05-02 15:42:54 +08:00

116 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
"fmt"
)
var Wechat = Ctr{
"login": func(that *Context) {
sessionKey := that.Req.FormValue("sessionkey")
encryptedData := that.Req.FormValue("encryptedData")
iv := that.Req.FormValue("iv")
if sessionKey == "" || encryptedData == "" || iv == "" {
that.Display(3, "参数不足")
return
}
eny := miniProgram.GetEncryptor()
re, e := eny.Decrypt(sessionKey, 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,
"sessionkey": 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)
},
}