mirror of
https://github.com/KaySar12/NextZen-UserService.git
synced 2025-03-15 15:15:35 +07:00
786 lines
25 KiB
Go
786 lines
25 KiB
Go
package v1
|
||
|
||
import (
|
||
"crypto/ecdsa"
|
||
"encoding/base64"
|
||
json2 "encoding/json"
|
||
"image"
|
||
"image/png"
|
||
"io"
|
||
"log"
|
||
"net/http"
|
||
url2 "net/url"
|
||
"os"
|
||
"path"
|
||
"path/filepath"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/KaySar12/NextZen-Common/utils/common_err"
|
||
"github.com/KaySar12/NextZen-Common/utils/jwt"
|
||
"github.com/KaySar12/NextZen-Common/utils/logger"
|
||
"github.com/KaySar12/NextZen-UserService/model"
|
||
"github.com/KaySar12/NextZen-UserService/model/system_model"
|
||
"github.com/KaySar12/NextZen-UserService/pkg/config"
|
||
"github.com/KaySar12/NextZen-UserService/pkg/utils/encryption"
|
||
"github.com/KaySar12/NextZen-UserService/pkg/utils/file"
|
||
model2 "github.com/KaySar12/NextZen-UserService/service/model"
|
||
uuid "github.com/satori/go.uuid"
|
||
"github.com/tidwall/gjson"
|
||
"go.uber.org/zap"
|
||
|
||
"github.com/KaySar12/NextZen-UserService/service"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// @Summary register user
|
||
// @Router /user/register/ [post]
|
||
func PostUserRegister(c *gin.Context) {
|
||
json := make(map[string]string)
|
||
c.ShouldBind(&json)
|
||
|
||
username := json["username"]
|
||
pwd := json["password"]
|
||
key := json["key"]
|
||
if _, ok := service.UserRegisterHash[key]; !ok {
|
||
c.JSON(common_err.CLIENT_ERROR,
|
||
model.Result{Success: common_err.KEY_NOT_EXIST, Message: common_err.GetMsg(common_err.KEY_NOT_EXIST)})
|
||
return
|
||
}
|
||
|
||
if len(username) == 0 || len(pwd) == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR,
|
||
model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
if len(pwd) < 6 {
|
||
c.JSON(common_err.CLIENT_ERROR,
|
||
model.Result{Success: common_err.PWD_IS_TOO_SIMPLE, Message: common_err.GetMsg(common_err.PWD_IS_TOO_SIMPLE)})
|
||
return
|
||
}
|
||
oldUser := service.MyService.User().GetUserInfoByUserName(username)
|
||
if oldUser.Id > 0 {
|
||
c.JSON(common_err.CLIENT_ERROR,
|
||
model.Result{Success: common_err.USER_EXIST, Message: common_err.GetMsg(common_err.USER_EXIST)})
|
||
return
|
||
}
|
||
|
||
user := model2.UserDBModel{}
|
||
user.Username = username
|
||
user.Password = encryption.GetMD5ByStr(pwd)
|
||
user.Role = "admin"
|
||
|
||
user = service.MyService.User().CreateUser(user)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR)})
|
||
return
|
||
}
|
||
file.MkDir(config.AppInfo.UserDataPath + "/" + strconv.Itoa(user.Id))
|
||
delete(service.UserRegisterHash, key)
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
|
||
}
|
||
|
||
// @Summary login
|
||
// @Produce application/json
|
||
// @Accept application/json
|
||
// @Tags user
|
||
// @Param user_name query string true "User name"
|
||
// @Param pwd query string true "password"
|
||
// @Success 200 {string} string "ok"
|
||
// @Router /user/login [post]
|
||
func PostUserLogin(c *gin.Context) {
|
||
json := make(map[string]string)
|
||
c.ShouldBind(&json)
|
||
|
||
username := json["username"]
|
||
|
||
password := json["password"]
|
||
// check params is empty
|
||
if len(username) == 0 || len(password) == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR,
|
||
model.Result{
|
||
Success: common_err.CLIENT_ERROR,
|
||
Message: common_err.GetMsg(common_err.INVALID_PARAMS),
|
||
})
|
||
return
|
||
}
|
||
user := service.MyService.User().GetUserAllInfoByName(username)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
if user.Password != encryption.GetMD5ByStr(password) {
|
||
c.JSON(common_err.CLIENT_ERROR,
|
||
model.Result{Success: common_err.PWD_INVALID, Message: common_err.GetMsg(common_err.PWD_INVALID)})
|
||
return
|
||
}
|
||
|
||
privateKey, _ := service.MyService.User().GetKeyPair()
|
||
|
||
token := system_model.VerifyInformation{}
|
||
|
||
accessToken, err := jwt.GetAccessToken(user.Username, privateKey, user.Id)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
||
}
|
||
token.AccessToken = accessToken
|
||
|
||
refreshToken, err := jwt.GetRefreshToken(user.Username, privateKey, user.Id)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
||
}
|
||
token.RefreshToken = refreshToken
|
||
|
||
token.ExpiresAt = time.Now().Add(3 * time.Hour * time.Duration(1)).Unix()
|
||
data := make(map[string]interface{}, 2)
|
||
user.Password = ""
|
||
data["token"] = token
|
||
|
||
// TODO:1 Database fields cannot be external
|
||
data["user"] = user
|
||
|
||
c.JSON(common_err.SUCCESS,
|
||
model.Result{
|
||
Success: common_err.SUCCESS,
|
||
Message: common_err.GetMsg(common_err.SUCCESS),
|
||
Data: data,
|
||
})
|
||
}
|
||
|
||
// @Summary edit user head
|
||
// @Produce application/json
|
||
// @Accept multipart/form-data
|
||
// @Tags user
|
||
// @Param file formData file true "用户头像"
|
||
// @Security ApiKeyAuth
|
||
// @Success 200 {string} string "ok"
|
||
// @Router /users/avatar [put]
|
||
func PutUserAvatar(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
json := make(map[string]string)
|
||
c.ShouldBind(&json)
|
||
|
||
data := json["file"]
|
||
imgBase64 := strings.Replace(data, "data:image/png;base64,", "", 1)
|
||
decodeData, err := base64.StdEncoding.DecodeString(string(imgBase64))
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
||
return
|
||
}
|
||
|
||
// 将字节数组转为图片
|
||
img, _, err := image.Decode(strings.NewReader(string(decodeData)))
|
||
if err != nil {
|
||
log.Fatal(err)
|
||
}
|
||
|
||
ext := ".png"
|
||
avatarPath := config.AppInfo.UserDataPath + "/" + id + "/avatar" + ext
|
||
os.Remove(avatarPath)
|
||
outFile, err := os.Create(avatarPath)
|
||
if err != nil {
|
||
logger.Error("create file error", zap.Error(err))
|
||
}
|
||
defer outFile.Close()
|
||
|
||
err = png.Encode(outFile, img)
|
||
if err != nil {
|
||
logger.Error("encode error", zap.Error(err))
|
||
}
|
||
user.Avatar = avatarPath
|
||
service.MyService.User().UpdateUser(user)
|
||
c.JSON(http.StatusOK,
|
||
model.Result{
|
||
Success: common_err.SUCCESS,
|
||
Message: common_err.GetMsg(common_err.SUCCESS),
|
||
Data: user,
|
||
})
|
||
}
|
||
|
||
// @Summary get user head
|
||
// @Produce application/json
|
||
// @Tags user
|
||
// @Param file formData file true "用户头像"
|
||
// @Security ApiKeyAuth
|
||
// @Success 200 {string} string "ok"
|
||
// @Router /users/avatar [get]
|
||
func GetUserAvatar(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
|
||
if file.Exists(user.Avatar) {
|
||
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(path.Base(user.Avatar)))
|
||
c.Header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate, value")
|
||
c.File(user.Avatar)
|
||
return
|
||
}
|
||
user.Avatar = "/usr/share/casaos/www/avatar.svg"
|
||
if file.Exists(user.Avatar) {
|
||
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(path.Base(user.Avatar)))
|
||
c.Header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate, value")
|
||
c.File(user.Avatar)
|
||
return
|
||
}
|
||
user.Avatar = "/var/lib/casaos/www/avatar.svg"
|
||
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(path.Base(user.Avatar)))
|
||
c.Header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate, value")
|
||
c.File(user.Avatar)
|
||
}
|
||
|
||
// @Summary edit user name
|
||
// @Produce application/json
|
||
// @Accept application/json
|
||
// @Tags user
|
||
// @Param old_name query string true "Old user name"
|
||
// @Security ApiKeyAuth
|
||
// @Success 200 {string} string "ok"
|
||
// @Router /user/name/:id [put]
|
||
func PutUserInfo(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
json := model2.UserDBModel{}
|
||
c.ShouldBind(&json)
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
if len(json.Username) > 0 {
|
||
u := service.MyService.User().GetUserInfoByUserName(json.Username)
|
||
if u.Id > 0 {
|
||
c.JSON(common_err.CLIENT_ERROR,
|
||
model.Result{Success: common_err.USER_EXIST, Message: common_err.GetMsg(common_err.USER_EXIST)})
|
||
return
|
||
}
|
||
}
|
||
|
||
if len(json.Email) == 0 {
|
||
json.Email = user.Email
|
||
}
|
||
if len(json.Avatar) == 0 {
|
||
json.Avatar = user.Avatar
|
||
}
|
||
if len(json.Role) == 0 {
|
||
json.Role = user.Role
|
||
}
|
||
if len(json.Description) == 0 {
|
||
json.Description = user.Description
|
||
}
|
||
if len(json.Nickname) == 0 {
|
||
json.Nickname = user.Nickname
|
||
}
|
||
service.MyService.User().UpdateUser(json)
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: json})
|
||
}
|
||
|
||
// @Summary edit user password
|
||
// @Produce application/json
|
||
// @Accept application/json
|
||
// @Tags user
|
||
// @Security ApiKeyAuth
|
||
// @Success 200 {string} string "ok"
|
||
// @Router /user/password/:id [put]
|
||
func PutUserPassword(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
json := make(map[string]string)
|
||
c.ShouldBind(&json)
|
||
oldPwd := json["old_password"]
|
||
pwd := json["password"]
|
||
if len(oldPwd) == 0 || len(pwd) == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
user := service.MyService.User().GetUserAllInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
if user.Password != encryption.GetMD5ByStr(oldPwd) {
|
||
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.PWD_INVALID_OLD, Message: common_err.GetMsg(common_err.PWD_INVALID_OLD)})
|
||
return
|
||
}
|
||
user.Password = encryption.GetMD5ByStr(pwd)
|
||
service.MyService.User().UpdateUserPassword(user)
|
||
user.Password = ""
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: user})
|
||
}
|
||
|
||
// @Summary edit user nick
|
||
// @Produce application/json
|
||
// @Accept application/json
|
||
// @Tags user
|
||
// @Param nick_name query string false "nick name"
|
||
// @Security ApiKeyAuth
|
||
// @Success 200 {string} string "ok"
|
||
// @Router /user/nick [put]
|
||
func PutUserNick(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
json := make(map[string]string)
|
||
c.ShouldBind(&json)
|
||
Nickname := json["nick_name"]
|
||
if len(Nickname) == 0 {
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(http.StatusOK,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
user.Nickname = Nickname
|
||
service.MyService.User().UpdateUser(user)
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: user})
|
||
}
|
||
|
||
// @Summary edit user description
|
||
// @Produce application/json
|
||
// @Accept multipart/form-data
|
||
// @Tags user
|
||
// @Param description formData string false "Description"
|
||
// @Security ApiKeyAuth
|
||
// @Success 200 {string} string "ok"
|
||
// @Router /user/desc [put]
|
||
func PutUserDesc(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
json := make(map[string]string)
|
||
c.ShouldBind(&json)
|
||
desc := json["description"]
|
||
if len(desc) == 0 {
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(http.StatusOK,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
user.Description = desc
|
||
|
||
service.MyService.User().UpdateUser(user)
|
||
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: user})
|
||
}
|
||
|
||
// @Summary get user info
|
||
// @Produce application/json
|
||
// @Accept application/json
|
||
// @Tags user
|
||
// @Success 200 {string} string "ok"
|
||
// @Router /user/info/:id [get]
|
||
func GetUserInfo(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
|
||
c.JSON(common_err.SUCCESS,
|
||
model.Result{
|
||
Success: common_err.SUCCESS,
|
||
Message: common_err.GetMsg(common_err.SUCCESS),
|
||
Data: user,
|
||
})
|
||
}
|
||
|
||
/**
|
||
* @description:
|
||
* @param {*gin.Context} c
|
||
* @param {string} Username
|
||
* @return {*}
|
||
* @method:
|
||
* @router:
|
||
*/
|
||
func GetUserInfoByUsername(c *gin.Context) {
|
||
username := c.Param("username")
|
||
if len(username) == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
user := service.MyService.User().GetUserInfoByUserName(username)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
|
||
c.JSON(common_err.SUCCESS,
|
||
model.Result{
|
||
Success: common_err.SUCCESS,
|
||
Message: common_err.GetMsg(common_err.SUCCESS),
|
||
Data: user,
|
||
})
|
||
}
|
||
|
||
/**
|
||
* @description: get all Usernames
|
||
* @method:GET
|
||
* @router:/user/all/name
|
||
*/
|
||
func GetUserAllUsername(c *gin.Context) {
|
||
users := service.MyService.User().GetAllUserName()
|
||
names := []string{}
|
||
for _, v := range users {
|
||
names = append(names, v.Username)
|
||
}
|
||
c.JSON(common_err.SUCCESS,
|
||
model.Result{
|
||
Success: common_err.SUCCESS,
|
||
Message: common_err.GetMsg(common_err.SUCCESS),
|
||
Data: names,
|
||
})
|
||
}
|
||
|
||
/**
|
||
* @description:get custom file by user
|
||
* @param {path} name string "file name"
|
||
* @method: GET
|
||
* @router: /user/custom/:key
|
||
*/
|
||
func GetUserCustomConf(c *gin.Context) {
|
||
name := c.Param("key")
|
||
if len(name) == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
id := c.GetHeader("user_id")
|
||
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
// user := service.MyService.User().GetUserInfoByUsername(Username)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
filePath := config.AppInfo.UserDataPath + "/" + id + "/" + name + ".json"
|
||
|
||
data := file.ReadFullFile(filePath)
|
||
if !gjson.ValidBytes(data) {
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: string(data)})
|
||
return
|
||
}
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: json2.RawMessage(string(data))})
|
||
}
|
||
|
||
/**
|
||
* @description:create or update custom conf by user
|
||
* @param {path} name string "file name"
|
||
* @method:POST
|
||
* @router:/user/custom/:key
|
||
*/
|
||
func PostUserCustomConf(c *gin.Context) {
|
||
name := c.Param("key")
|
||
if len(name) == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
id := c.GetHeader("user_id")
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
data, _ := io.ReadAll(c.Request.Body)
|
||
filePath := config.AppInfo.UserDataPath + "/" + strconv.Itoa(user.Id)
|
||
|
||
if err := file.IsNotExistMkDir(filePath); err != nil {
|
||
c.JSON(common_err.SERVICE_ERROR,
|
||
model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR)})
|
||
return
|
||
}
|
||
|
||
if err := file.WriteToPath(data, filePath, name+".json"); err != nil {
|
||
c.JSON(common_err.SERVICE_ERROR,
|
||
model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR)})
|
||
return
|
||
}
|
||
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: json2.RawMessage(string(data))})
|
||
}
|
||
|
||
/**
|
||
* @description: delete user custom config
|
||
* @param {path} key string
|
||
* @method:delete
|
||
* @router:/user/custom/:key
|
||
*/
|
||
func DeleteUserCustomConf(c *gin.Context) {
|
||
name := c.Param("key")
|
||
if len(name) == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
id := c.GetHeader("user_id")
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR,
|
||
model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
filePath := config.AppInfo.UserDataPath + "/" + strconv.Itoa(user.Id) + "/" + name + ".json"
|
||
err := os.Remove(filePath)
|
||
if err != nil {
|
||
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR)})
|
||
return
|
||
}
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
|
||
}
|
||
|
||
/**
|
||
* @description:
|
||
* @param {path} id string "user id"
|
||
* @method:DELETE
|
||
* @router:/user/delete/:id
|
||
*/
|
||
func DeleteUser(c *gin.Context) {
|
||
id := c.Param("id")
|
||
service.MyService.User().DeleteUserById(id)
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: id})
|
||
}
|
||
|
||
/**
|
||
* @description:update user image
|
||
* @method:POST
|
||
* @router:/user/current/image/:key
|
||
*/
|
||
func PutUserImage(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
json := make(map[string]string)
|
||
c.ShouldBind(&json)
|
||
|
||
path := json["path"]
|
||
key := c.Param("key")
|
||
if len(path) == 0 || len(key) == 0 {
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
if !file.Exists(path) {
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.FILE_DOES_NOT_EXIST, Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST)})
|
||
return
|
||
}
|
||
|
||
_, err := file.GetImageExt(path)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.NOT_IMAGE, Message: common_err.GetMsg(common_err.NOT_IMAGE)})
|
||
return
|
||
}
|
||
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
fstat, _ := os.Stat(path)
|
||
if fstat.Size() > 10<<20 {
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.IMAGE_TOO_LARGE, Message: common_err.GetMsg(common_err.IMAGE_TOO_LARGE)})
|
||
return
|
||
}
|
||
ext := file.GetExt(path)
|
||
filePath := config.AppInfo.UserDataPath + "/" + strconv.Itoa(user.Id) + "/" + key + ext
|
||
file.CopySingleFile(path, filePath, "overwrite")
|
||
|
||
data := make(map[string]string, 3)
|
||
data["path"] = filePath
|
||
data["file_name"] = key + ext
|
||
data["online_path"] = "/v1/users/image?path=" + filePath
|
||
c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
|
||
}
|
||
|
||
/**
|
||
* @description:
|
||
* @param {*gin.Context} c
|
||
* @param {file} file
|
||
* @param {string} key
|
||
* @param {string} type:avatar,background
|
||
* @return {*}
|
||
* @method:
|
||
* @router:
|
||
*/
|
||
func PostUserUploadImage(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
f, err := c.FormFile("file")
|
||
key := c.Param("key")
|
||
t := c.PostForm("type")
|
||
if len(key) == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
if err != nil {
|
||
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.CLIENT_ERROR), Data: err.Error()})
|
||
return
|
||
}
|
||
|
||
_, err = file.GetImageExtByName(f.Filename)
|
||
if err != nil {
|
||
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.NOT_IMAGE, Message: common_err.GetMsg(common_err.NOT_IMAGE)})
|
||
return
|
||
}
|
||
ext := filepath.Ext(f.Filename)
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
if t == "avatar" {
|
||
key = "avatar"
|
||
}
|
||
path := config.AppInfo.UserDataPath + "/" + strconv.Itoa(user.Id) + "/" + key + ext
|
||
|
||
c.SaveUploadedFile(f, path)
|
||
data := make(map[string]string, 3)
|
||
data["path"] = path
|
||
data["file_name"] = key + ext
|
||
data["online_path"] = "/v1/users/image?path=" + path
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
|
||
}
|
||
|
||
/**
|
||
* @description: get current user's image
|
||
* @method:GET
|
||
* @router:/user/image/:id
|
||
*/
|
||
func GetUserImage(c *gin.Context) {
|
||
filePath := c.Query("path")
|
||
if len(filePath) == 0 {
|
||
c.JSON(http.StatusNotFound, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
if !file.Exists(filePath) {
|
||
c.JSON(http.StatusNotFound, model.Result{Success: common_err.FILE_DOES_NOT_EXIST, Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST)})
|
||
return
|
||
}
|
||
if !strings.Contains(filePath, config.AppInfo.UserDataPath) {
|
||
c.JSON(http.StatusNotFound, model.Result{Success: common_err.INSUFFICIENT_PERMISSIONS, Message: common_err.GetMsg(common_err.INSUFFICIENT_PERMISSIONS)})
|
||
return
|
||
}
|
||
|
||
fileTmp, _ := os.Open(filePath)
|
||
defer fileTmp.Close()
|
||
|
||
fileName := path.Base(filePath)
|
||
|
||
// @tiger - RESTful 规范下不应该返回文件本身内容,而是返回文件的静态URL,由前端去解析
|
||
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url2.PathEscape(fileName))
|
||
c.File(filePath)
|
||
}
|
||
|
||
func DeleteUserImage(c *gin.Context) {
|
||
id := c.GetHeader("user_id")
|
||
path := c.Query("path")
|
||
if len(path) == 0 {
|
||
c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
|
||
return
|
||
}
|
||
user := service.MyService.User().GetUserInfoById(id)
|
||
if user.Id == 0 {
|
||
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.USER_NOT_EXIST, Message: common_err.GetMsg(common_err.USER_NOT_EXIST)})
|
||
return
|
||
}
|
||
if !file.Exists(path) {
|
||
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.FILE_DOES_NOT_EXIST, Message: common_err.GetMsg(common_err.FILE_DOES_NOT_EXIST)})
|
||
return
|
||
}
|
||
if !strings.Contains(path, config.AppInfo.UserDataPath+"/"+strconv.Itoa(user.Id)) {
|
||
c.JSON(common_err.SERVICE_ERROR, model.Result{Success: common_err.INSUFFICIENT_PERMISSIONS, Message: common_err.GetMsg(common_err.INSUFFICIENT_PERMISSIONS)})
|
||
return
|
||
}
|
||
os.Remove(path)
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
|
||
}
|
||
|
||
/**
|
||
* @description:
|
||
* @param {*gin.Context} c
|
||
* @param {string} refresh_token
|
||
* @return {*}
|
||
* @method:
|
||
* @router:
|
||
*/
|
||
func PostUserRefreshToken(c *gin.Context) {
|
||
js := make(map[string]string)
|
||
c.ShouldBind(&js)
|
||
refresh := js["refresh_token"]
|
||
|
||
privateKey, _ := service.MyService.User().GetKeyPair()
|
||
|
||
claims, err := jwt.ParseToken(
|
||
refresh,
|
||
func() (*ecdsa.PublicKey, error) {
|
||
_, publicKey := service.MyService.User().GetKeyPair()
|
||
return publicKey, nil
|
||
})
|
||
if err != nil {
|
||
c.JSON(http.StatusUnauthorized, model.Result{Success: common_err.VERIFICATION_FAILURE, Message: common_err.GetMsg(common_err.VERIFICATION_FAILURE), Data: err.Error()})
|
||
return
|
||
}
|
||
if !claims.VerifyExpiresAt(time.Now(), true) || !claims.VerifyIssuer("refresh", true) {
|
||
c.JSON(http.StatusUnauthorized, model.Result{Success: common_err.VERIFICATION_FAILURE, Message: common_err.GetMsg(common_err.VERIFICATION_FAILURE)})
|
||
return
|
||
}
|
||
|
||
newAccessToken, err := jwt.GetAccessToken(claims.Username, privateKey, claims.ID)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
||
return
|
||
}
|
||
|
||
newRefreshToken, err := jwt.GetRefreshToken(claims.Username, privateKey, claims.ID)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, model.Result{Success: common_err.SERVICE_ERROR, Message: err.Error()})
|
||
return
|
||
}
|
||
|
||
verifyInfo := system_model.VerifyInformation{
|
||
AccessToken: newAccessToken,
|
||
RefreshToken: newRefreshToken,
|
||
ExpiresAt: time.Now().Add(3 * time.Hour).Unix(),
|
||
}
|
||
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: verifyInfo})
|
||
}
|
||
|
||
func DeleteUserAll(c *gin.Context) {
|
||
service.MyService.User().DeleteAllUser()
|
||
c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
|
||
}
|
||
|
||
// @Summary 检查是否进入引导状态
|
||
// @Produce application/json
|
||
// @Accept application/json
|
||
// @Tags sys
|
||
// @Security ApiKeyAuth
|
||
// @Success 200 {string} string "ok"
|
||
// @Router /sys/init/check [get]
|
||
func GetUserStatus(c *gin.Context) {
|
||
data := make(map[string]interface{}, 2)
|
||
|
||
if service.MyService.User().GetUserCount() > 0 {
|
||
data["initialized"] = true
|
||
data["key"] = ""
|
||
} else {
|
||
key := uuid.NewV4().String()
|
||
service.UserRegisterHash[key] = key
|
||
data["key"] = key
|
||
data["initialized"] = false
|
||
}
|
||
c.JSON(common_err.SUCCESS,
|
||
model.Result{
|
||
Success: common_err.SUCCESS,
|
||
Message: common_err.GetMsg(common_err.SUCCESS),
|
||
Data: data,
|
||
})
|
||
}
|