[优化] 代码优化

This commit is contained in:
ryan
2026-05-31 20:29:15 +08:00
parent 97c10b8d0b
commit 7ab11154e3
13 changed files with 197 additions and 554 deletions
+22 -63
View File
@@ -4,12 +4,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"io"
"log/slog"
"net/http"
"openflare/common"
"openflare/model"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
type wechatLoginResponse struct {
@@ -34,7 +36,12 @@ func getWeChatIdByCode(code string) (string, error) {
if err != nil {
return "", err
}
defer httpResponse.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
slog.Error("Failed to close response body", "error", err)
}
}(httpResponse.Body)
var res wechatLoginResponse
err = json.NewDecoder(httpResponse.Body).Decode(&res)
if err != nil {
@@ -51,19 +58,13 @@ func getWeChatIdByCode(code string) (string, error) {
func WeChatAuth(c *gin.Context) {
if !common.WeChatAuthEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "管理员未开启通过微信登录以及注册",
"success": false,
})
respondFailure(c, "管理员未开启通过微信登录以及注册")
return
}
code := c.Query("code")
wechatId, err := getWeChatIdByCode(code)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"message": err.Error(),
"success": false,
})
respondFailure(c, err.Error())
return
}
user := model.User{
@@ -72,40 +73,16 @@ func WeChatAuth(c *gin.Context) {
if model.IsWeChatIdAlreadyTaken(wechatId) {
err := user.FillUserByWeChatId()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
respondFailure(c, err.Error())
return
}
} else {
if common.RegisterEnabled {
user.Username = "wechat_" + strconv.Itoa(model.GetMaxUserId()+1)
user.DisplayName = "WeChat User"
user.Role = common.RoleCommonUser
user.Status = common.UserStatusEnabled
if err := user.Insert(); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
} else {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "管理员关闭了新用户注册",
})
return
}
respondFailure(c, "管理员关闭了新用户注册")
return
}
if user.Status != common.UserStatusEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "用户已被封禁",
"success": false,
})
respondFailure(c, "用户已被封禁")
return
}
setupLogin(&user, c)
@@ -113,26 +90,17 @@ func WeChatAuth(c *gin.Context) {
func WeChatBind(c *gin.Context) {
if !common.WeChatAuthEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "管理员未开启通过微信登录以及注册",
"success": false,
})
respondFailure(c, "管理员未开启通过微信登录以及注册")
return
}
code := c.Query("code")
wechatId, err := getWeChatIdByCode(code)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"message": err.Error(),
"success": false,
})
respondFailure(c, err.Error())
return
}
if model.IsWeChatIdAlreadyTaken(wechatId) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "该微信账号已被绑定",
})
respondFailure(c, "该微信账号已被绑定")
return
}
id := c.GetInt("id")
@@ -141,24 +109,15 @@ func WeChatBind(c *gin.Context) {
}
err = user.FillUserById()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
respondFailure(c, err.Error())
return
}
user.WeChatId = wechatId
err = user.Update(false)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
respondFailure(c, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
})
respondSuccessMessage(c, "")
return
}