1 Star 4 Fork 0

火火 / golang-common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
login.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
火火 提交于 2019-10-29 17:14 . 12
package wxminipro
import (
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/httplib"
"github.com/pkg/errors"
)
type LoginResult struct {
Openid string `json:"openid"`
SessionKey string `json:"session_key"`
Unionid string `json:"unionid"`
Errcode LoginResultCode `json:"errcode"`
Errmsg string `json:"errmsg"`
}
type LoginResultCode int
const (
LoginResultCodeBusy LoginResultCode = -1 // 系统繁忙,此时请开发者稍候再试
LoginResultCodeSuccess LoginResultCode = 0 // 请求成功
LoginResultCodeInValid LoginResultCode = 40029 // code 无效
LoginResultCodeLimit LoginResultCode = 45011 // 频率限制,每个用户每分钟100次
)
func Login(code string) (result *LoginResult, err error) {
appId := beego.AppConfig.String("appId")
secret := beego.AppConfig.String("appSecret")
if appId == "" {
err = errors.New("appId can not be empty")
return
}
if secret == "" {
err = errors.New("secret can not be empty")
return
}
// GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
req := httplib.Get(fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, secret, code))
result = &LoginResult{}
err = req.ToJSON(result)
if err != nil {
return
}
return
}
Go
1
https://gitee.com/yxd1990/golang-common.git
git@gitee.com:yxd1990/golang-common.git
yxd1990
golang-common
golang-common
62252a89ea79

搜索帮助