Files
toutoukan/controllers/user/userLogin.go

208 lines
5.7 KiB
Go
Raw Normal View History

2025-08-10 02:10:01 +08:00
package user
import (
2025-08-11 00:45:06 +08:00
"crypto/aes"
"crypto/cipher"
"encoding/base64"
2025-09-23 18:39:01 +08:00
"encoding/json"
"errors"
"fmt"
2025-08-11 00:45:06 +08:00
"math/rand"
2025-09-23 18:39:01 +08:00
"net/http"
"net/url"
2025-08-11 00:45:06 +08:00
"strconv"
2025-08-10 02:10:01 +08:00
"time"
2025-09-23 18:39:01 +08:00
"toutoukan/init/config"
"toutoukan/init/databaseInit"
"toutoukan/model/usermodel"
"toutoukan/utill/jwt"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
2025-08-10 02:10:01 +08:00
)
const wxLoginURL = "https://api.weixin.qq.com/sns/jscode2session"
func UserLogin(c *gin.Context) {
2025-09-23 18:39:01 +08:00
fmt.Println("Request Body:", c.Request.Body)
var req usermodel.WxLoginM
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
fmt.Println("req:123")
fmt.Println(req.EncryptedData)
fmt.Println(req.Code)
fmt.Println(req.Iv)
fmt.Println("使用的appid:", config.Conf.WxID)
fmt.Println("使用的secret:", config.Conf.Wxsecret)
//----------------发送验证请求
params := url.Values{}
params.Add("appid", config.Conf.WxID)
params.Add("secret", config.Conf.Wxsecret)
params.Add("js_code", req.Code)
params.Add("grant_type", "authorization_code")
requestURL := fmt.Sprintf("%s?%s", wxLoginURL, params.Encode())
fmt.Println("微信接口请求URL:", requestURL)
2025-08-11 00:45:06 +08:00
2025-09-23 18:39:01 +08:00
resp, err := http.Get(fmt.Sprintf("%s?%s", wxLoginURL, params.Encode()))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "调用微信登录接口失败: " + err.Error()})
return
}
fmt.Println("微信登录返回结果:", resp.Body)
defer resp.Body.Close()
var wxResp usermodel.WxLoginResponse
if err := json.NewDecoder(resp.Body).Decode(&wxResp); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "解析微信登录响应失败: " + err.Error(), "code": "10026"})
return
}
if wxResp.ErrCode != 0 {
c.JSON(http.StatusBadRequest, gin.H{
"error": fmt.Sprintf("微信登录失败: %s (错误码: %d)", wxResp.ErrCode, wxResp.ErrMsg),
"code": "10027",
})
return
}
phoneData, err := decryptWxData(wxResp.SessionKey, req.EncryptedData, req.Iv)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "解密失败: " + err.Error()})
return
}
var phoneInfo usermodel.WxPhoneInfo
if err := json.Unmarshal(phoneData, &phoneInfo); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "解析手机号失败"})
return
}
2025-08-11 00:45:06 +08:00
2025-09-23 18:39:01 +08:00
if phoneInfo.Watermark.AppID != config.Conf.WxID {
c.JSON(http.StatusForbidden, gin.H{"error": "数据水印验证失败"})
return
}
fmt.Println("用户手机号为:", phoneInfo.PhoneNumber)
openid := wxResp.OpenID
// 业务逻辑实现
var user UserInfo
2025-09-24 01:48:12 +08:00
username := ""
2025-09-23 18:39:01 +08:00
// 查找用户是否存在
result := databaseInit.UserDB.Where("uid = ?", openid).First(&user)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
// 用户不存在,创建新用户
2025-09-24 01:48:12 +08:00
username = generateUsername()
2025-09-23 18:39:01 +08:00
now := time.Now()
newUser := UserInfo{
Uid: openid,
Gender: 2, // 这里假设 gender 传 2可根据实际需求修改
CreatedTime: now,
UpdatedTime: now,
Username: username,
Telephone: phoneInfo.PhoneNumber,
// 若还有 password、avatar_url、birthdate、bio 等字段需要赋值,可在此补充
// 比如 Password: "默认密码"实际中密码应加密存储AvatarUrl: "默认头像地址" 等
2025-09-23 20:28:56 +08:00
Birthdate: nil,
2025-09-23 18:39:01 +08:00
}
if err := databaseInit.UserDB.Create(&newUser).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "插入新用户失败: " + err.Error(),
"code": "10029",
})
return
}
} else {
// 数据库查询错误
c.JSON(http.StatusInternalServerError, gin.H{
"error": "查询用户存在性失败: " + result.Error.Error(),
"code": "10029",
})
return
}
} else {
// 用户已存在,获取用户名
username = user.Username
}
token, err := jwt.GenerateJWTAndStore(openid)
if err != nil {
c.JSON(500, gin.H{"error": "生成令牌失败", "code": "10036"})
return
}
c.JSON(http.StatusOK, gin.H{"result": "success", "error": nil, "code": "20001", "token": token,
2025-09-27 18:25:25 +08:00
"userinfo": struct {
Username string `json:"username"`
UID string `json:"uid"`
Telephone string `json:"telephone"`
AvatarURL string `json:"avatar_url"`
Gender int `json:"gender"`
Birthdate string `json:"birth_date"`
Bio string `json:"bio"`
Total_points int `json:"total_points"`
}{
Username: username,
UID: openid,
Telephone: phoneInfo.PhoneNumber,
AvatarURL: user.AvatarURL,
Gender: user.Gender,
Birthdate: func() string {
if user.Birthdate != nil {
// 如果指针不为空,解引用并格式化
return user.Birthdate.Format("2006-01-02")
}
// 如果指针为空(数据库中为 NULL返回空字符串或默认值
return ""
}(), // 格式化 time.Time
Bio: user.Bio,
Total_points: user.TotalPoints,
2025-09-23 18:39:01 +08:00
},
})
}
2025-09-13 20:00:49 +08:00
2025-08-11 00:45:06 +08:00
func generateUsername() string {
rand.Seed(time.Now().UnixNano())
2025-08-14 03:10:24 +08:00
randomNum := 10000000 + rand.Intn(90000000)
2025-08-11 00:45:06 +08:00
return "用户" + strconv.Itoa(randomNum)
}
func decryptWxData(sessionKey, encryptedData, iv string) ([]byte, error) {
key, _ := base64.StdEncoding.DecodeString(sessionKey)
data, _ := base64.StdEncoding.DecodeString(encryptedData)
ivBytes, _ := base64.StdEncoding.DecodeString(iv)
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
mode := cipher.NewCBCDecrypter(block, ivBytes)
mode.CryptBlocks(data, data)
2025-08-14 03:10:24 +08:00
return pkcs7Unpad(data), nil
2025-08-11 00:45:06 +08:00
}
func pkcs7Unpad(data []byte) []byte {
if len(data) == 0 {
return nil
}
padding := int(data[len(data)-1])
if padding > len(data) {
return nil
}
return data[:len(data)-padding]
2025-08-10 02:10:01 +08:00
}
2025-09-23 18:39:01 +08:00
// 定义与表结构对应的用户模型
// 自定义表名
func (UserInfo) TableName() string {
return "user_info"
}