hotime/example/provider/wechat.go
2022-05-13 09:30:09 +08:00

98 lines
2.1 KiB
Go

package provider
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
"code.hoteas.com/golang/hotime/dri/wechat"
)
var Wechat = Ctr{
////微信注册
"code": func(that *Context) {
if that.Req.FormValue("code") == "" {
that.Display(3, "参数不足")
return
}
appid, resToken, userInfo, err := wechat.H5Program.GetUserInfo(that.Req.FormValue("code"))
if err != nil {
that.Display(4, err)
return
}
//此次获取的微信信息
wechatInfo := Map{
"openid": userInfo.OpenID,
"acttoken": resToken.AccessToken,
"retoken": resToken.RefreshToken,
"appid": appid,
"unionid": userInfo.Unionid,
"nickname": FilterEmoji(userInfo.Nickname),
"avatar": userInfo.HeadImgURL,
//"create_time[#]":"now()",
"modify_time[#]": "now()",
"del_flag": 0,
"type": 1,
}
wechat := that.Db.Get("wechat", "*", Map{"AND": Map{"openid": userInfo.OpenID, "del_flag": 0}})
if wechat != nil {
//有用户直接返回
if wechat.GetCeilInt("salesman_id") != 0 {
that.Db.Update("salesman", Map{"login_time[#]": "now()"},
Map{"id": wechat.GetCeilInt("salesman_id")})
that.Session("salesman_id", wechat.GetCeilInt("salesman_id"))
that.Display(0, "登录成功")
return
}
that.Session("wechat_id", wechat.GetCeilInt("id"))
that.Display(2, "暂未绑定")
return
}
wechatInfo["create_time[#]"] = "now()"
wechatInfo["id"] = that.Db.Insert("wechat", wechatInfo)
if wechatInfo.GetCeilInt("id") == 0 {
that.Display(4, "创建用户失败")
return
}
that.Session("wechat_id", wechatInfo.GetCeilInt("id"))
that.Display(2, "暂未绑定")
},
//网页签名
"sign": func(that *Context) {
signUrl := that.Req.FormValue("url")
if signUrl == "" {
that.Display(3, "参数不足")
return
}
cfg1, e := wechat.H5Program.GetSignUrl(signUrl)
if e != nil {
that.Display(4, e)
return
}
sign := Map{
"appId": cfg1.AppID,
"timestamp": cfg1.Timestamp,
"nonceStr": cfg1.NonceStr,
"signature": cfg1.Signature,
}
that.Display(0, sign)
},
}