- 新增「简记memo」一体化小程序产品原型设计文档 - 新增简记memo完整版UI视觉设计规范和界面细节 - 添加IDEA项目配置文件.gitignore - 创建404页面HTML文件,包含响应式布局和错误提示 - 添加关于页面HTML文件,展示品牌介绍和团队信息 - 实现AES加解密工具函数,支持请求体加密 - 添加用户协议页面基础框架
121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
// 微信登录工具
|
|
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"simple-memo/global"
|
|
)
|
|
|
|
// WXLoginResp 微信登录响应
|
|
type WXLoginResp struct {
|
|
OpenID string `json:"openid"`
|
|
SessionKey string `json:"session_key"`
|
|
UnionID string `json:"unionid"`
|
|
ErrCode int `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
}
|
|
|
|
// WXWebLoginResp 微信网页授权响应
|
|
type WXWebLoginResp struct {
|
|
AccessToken string `json:"access_token"`
|
|
ExpiresIn int `json:"expires_in"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
OpenID string `json:"openid"`
|
|
Scope string `json:"scope"`
|
|
UnionID string `json:"unionid"`
|
|
ErrCode int `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
}
|
|
|
|
// WXUserInfo 微信用户信息
|
|
type WXUserInfo struct {
|
|
OpenID string `json:"openid"`
|
|
Nickname string `json:"nickname"`
|
|
Sex int `json:"sex"`
|
|
Province string `json:"province"`
|
|
City string `json:"city"`
|
|
Country string `json:"country"`
|
|
HeadImgURL string `json:"headimgurl"`
|
|
Privilege []string `json:"privilege"`
|
|
UnionID string `json:"unionid"`
|
|
}
|
|
|
|
// MiniProgramCode2Session 小程序登录:获取openid和session_key
|
|
func MiniProgramCode2Session(code string) (WXLoginResp, error) {
|
|
appID := global.VP.GetString("wechat.app_id")
|
|
appSecret := global.VP.GetString("wechat.app_secret")
|
|
|
|
url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",
|
|
appID, appSecret, code)
|
|
|
|
return requestWXAPI(url)
|
|
}
|
|
|
|
// WebCode2Session 网页授权:获取access_token和openid
|
|
func WebCode2Session(code string) (WXWebLoginResp, error) {
|
|
appID := global.VP.GetString("wechat.app_id")
|
|
appSecret := global.VP.GetString("wechat.app_secret")
|
|
|
|
url := fmt.Sprintf("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
|
|
appID, appSecret, code)
|
|
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return WXWebLoginResp{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
data, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return WXWebLoginResp{}, err
|
|
}
|
|
|
|
var wxResp WXWebLoginResp
|
|
if err = json.Unmarshal(data, &wxResp); err != nil {
|
|
return WXWebLoginResp{}, err
|
|
}
|
|
|
|
if wxResp.ErrCode != 0 {
|
|
return WXWebLoginResp{}, fmt.Errorf("微信授权失败:%s", wxResp.ErrMsg)
|
|
}
|
|
|
|
return wxResp, nil
|
|
}
|
|
|
|
// GetWebAuthURL 获取微信网页授权URL
|
|
func GetWebAuthURL(state string) string {
|
|
appID := global.VP.GetString("wechat.app_id")
|
|
redirectURI := global.VP.GetString("wechat.web_redirect_uri")
|
|
|
|
return fmt.Sprintf("https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect",
|
|
appID, redirectURI, state)
|
|
}
|
|
|
|
// requestWXAPI 请求微信API通用方法
|
|
func requestWXAPI(url string) (WXLoginResp, error) {
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return WXLoginResp{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
data, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return WXLoginResp{}, err
|
|
}
|
|
|
|
var wxResp WXLoginResp
|
|
if err = json.Unmarshal(data, &wxResp); err != nil {
|
|
return WXLoginResp{}, err
|
|
}
|
|
|
|
if wxResp.ErrCode != 0 {
|
|
return WXLoginResp{}, fmt.Errorf("微信登录失败:%s", wxResp.ErrMsg)
|
|
}
|
|
|
|
return wxResp, nil
|
|
}
|