|
@@ -2,6 +2,7 @@ package controller
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"net/http"
|
|
"net/http"
|
|
|
"net/url"
|
|
"net/url"
|
|
@@ -213,10 +214,7 @@ func Register(c *gin.Context) {
|
|
|
token.Group = "auto"
|
|
token.Group = "auto"
|
|
|
}
|
|
}
|
|
|
if err := token.Insert(); err != nil {
|
|
if err := token.Insert(); err != nil {
|
|
|
- c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
- "success": false,
|
|
|
|
|
- "message": "创建默认令牌失败",
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ common.ApiErrorI18n(c, i18n.MsgCreateDefaultTokenErr)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -294,20 +292,14 @@ func GenerateAccessToken(c *gin.Context) {
|
|
|
randI := common.GetRandomInt(4)
|
|
randI := common.GetRandomInt(4)
|
|
|
key, err := common.GenerateRandomKey(29 + randI)
|
|
key, err := common.GenerateRandomKey(29 + randI)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
- "success": false,
|
|
|
|
|
- "message": "生成失败",
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ common.ApiErrorI18n(c, i18n.MsgGenerateFailed)
|
|
|
common.SysLog("failed to generate key: " + err.Error())
|
|
common.SysLog("failed to generate key: " + err.Error())
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
user.SetAccessToken(key)
|
|
user.SetAccessToken(key)
|
|
|
|
|
|
|
|
if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
|
|
if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
|
|
|
- c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
- "success": false,
|
|
|
|
|
- "message": "请重试,系统生成的 UUID 竟然重复了!",
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ common.ApiErrorI18n(c, i18n.MsgUuidDuplicate)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -658,18 +650,12 @@ func UpdateSelf(c *gin.Context) {
|
|
|
var user model.User
|
|
var user model.User
|
|
|
requestDataBytes, err := json.Marshal(requestData)
|
|
requestDataBytes, err := json.Marshal(requestData)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
- "success": false,
|
|
|
|
|
- "message": "无效的参数",
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ common.ApiErrorI18n(c, i18n.MsgInvalidParams)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
err = json.Unmarshal(requestDataBytes, &user)
|
|
err = json.Unmarshal(requestDataBytes, &user)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
- "success": false,
|
|
|
|
|
- "message": "无效的参数",
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ common.ApiErrorI18n(c, i18n.MsgInvalidParams)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -677,10 +663,7 @@ func UpdateSelf(c *gin.Context) {
|
|
|
user.Password = "$I_LOVE_U" // make Validator happy :)
|
|
user.Password = "$I_LOVE_U" // make Validator happy :)
|
|
|
}
|
|
}
|
|
|
if err := common.Validate.Struct(&user); err != nil {
|
|
if err := common.Validate.Struct(&user); err != nil {
|
|
|
- c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
- "success": false,
|
|
|
|
|
- "message": "输入不合法 " + err.Error(),
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ common.ApiErrorI18n(c, i18n.MsgInvalidInput)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -996,6 +979,10 @@ func TopUp(c *gin.Context) {
|
|
|
}
|
|
}
|
|
|
quota, err := model.Redeem(req.Key, id)
|
|
quota, err := model.Redeem(req.Key, id)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
|
|
+ if errors.Is(err, model.ErrRedeemFailed) {
|
|
|
|
|
+ common.ApiErrorI18n(c, i18n.MsgRedeemFailed)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
common.ApiError(c, err)
|
|
common.ApiError(c, err)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -1149,10 +1136,7 @@ func UpdateUserSetting(c *gin.Context) {
|
|
|
// 更新用户设置
|
|
// 更新用户设置
|
|
|
user.SetSetting(settings)
|
|
user.SetSetting(settings)
|
|
|
if err := user.Update(false); err != nil {
|
|
if err := user.Update(false); err != nil {
|
|
|
- c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
- "success": false,
|
|
|
|
|
- "message": "更新设置失败: " + err.Error(),
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ common.ApiErrorI18n(c, i18n.MsgUpdateFailed)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|