Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ghjkg546 committed Sep 18, 2024
1 parent 34f4d47 commit 30b25b3
Show file tree
Hide file tree
Showing 117 changed files with 7,996 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.vscode
/vendor/
/storage/
6 changes: 6 additions & 0 deletions app/common/request/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package request

type BatchSave struct {
Content string `form:"content" json:"content"`
CategoryId uint `form:"category_id" json:"category_id"`
}
15 changes: 15 additions & 0 deletions app/common/request/upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package request

import "mime/multipart"

type ImageUpload struct {
Business string `form:"business" json:"business" binding:"required"`
Image *multipart.FileHeader `form:"image" json:"image" binding:"required"`
}

func (imageUpload ImageUpload) GetMessages() ValidatorMessages {
return ValidatorMessages{
"business.required": "业务类型不能为空",
"image.required": "请选择图片",
}
}
42 changes: 42 additions & 0 deletions app/common/request/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package request

type Register struct {
UserName string `form:"username" json:"username" binding:"required"`
Password string `form:"password" json:"password" binding:"required"`
}

func (register Register) GetMessages() ValidatorMessages {
return ValidatorMessages{
"username.required": "用户名不能为空",
"password.required": "用户密码不能为空",
}
}

//type Login struct {
// UserName string `form:"username" json:"username" binding:"required,username"`
// Password string `form:"password" json:"password" binding:"required"`
//}

type Login struct {
UserName string `form:"username" json:"username"`
Password string `form:"password" json:"password"`
}

type PostComment struct {
Content string `form:"content" json:"content"`
ResItemId int32 `form:"resource_item_id" json:"resource_item_id"`
}

type ChangePass struct {
ConfirmPassword string `form:"confirmPassword" json:"confirmPassword"`
NewPassword string `form:"newPassword" json:"newPassword"`
OldPassword string `form:"oldPassword" json:"oldPassword"`
}

type EditUser struct {
Id int `form:"id" json:"id"`
UserName string `form:"username" json:"username"`
Password string `form:"password" json:"password"`
Mobile string `form:"mobile" json:"mobile"` // 添加手机号码字段
Status int `form:"status" json:"status"` // 添加状态字段
}
30 changes: 30 additions & 0 deletions app/common/request/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package request

import (
"github.com/go-playground/validator/v10"
)

type Validator interface {
GetMessages() ValidatorMessages
}

type ValidatorMessages map[string]string

// GetErrorMsg 获取错误信息
func GetErrorMsg(request interface{}, err error) string {
if _, isValidatorErrors := err.(validator.ValidationErrors); isValidatorErrors {
_, isValidator := request.(Validator)

for _, v := range err.(validator.ValidationErrors) {
// 若 request 结构体实现 Validator 接口即可实现自定义错误信息
if isValidator {
if message, exist := request.(Validator).GetMessages()[v.Field() + "." + v.Tag()]; exist {
return message
}
}
return v.Error()
}
}

return "Parameter error"
}
61 changes: 61 additions & 0 deletions app/common/response/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package response

import (
"github.com/gin-gonic/gin"
"github.com/jassue/jassue-gin/global"
"net/http"
"os"
)

type Response struct {
ErrorCode int `json:"code"`
Data interface{} `json:"data"`
Message string `json:"msg"`
}

func ServerError(c *gin.Context, err interface{}) {
msg := "Internal Server Error"
if global.App.Config.App.Env != "production" && os.Getenv(gin.EnvGinMode) != gin.ReleaseMode {
if _, ok := err.(error); ok {
msg = err.(error).Error()
}
}
c.JSON(http.StatusInternalServerError, Response{
http.StatusInternalServerError,
nil,
msg,
})
c.Abort()
}

func Success(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, Response{
0,
data,
"ok",
})
}

func Fail(c *gin.Context, errorCode int, msg string) {
c.JSON(http.StatusOK, Response{
errorCode,
nil,
msg,
})
}

func FailByError(c *gin.Context, error global.CustomError) {
Fail(c, error.ErrorCode, error.ErrorMsg)
}

func ValidateFail(c *gin.Context, msg string) {
Fail(c, global.Errors.ValidateError.ErrorCode, msg)
}

func BusinessFail(c *gin.Context, msg string) {
Fail(c, global.Errors.BusinessError.ErrorCode, msg)
}

func TokenFail(c *gin.Context) {
FailByError(c, global.Errors.TokenError)
}
182 changes: 182 additions & 0 deletions app/controllers/adminapi/appuser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package adminapi

import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jassue/jassue-gin/app/common/request"
"github.com/jassue/jassue-gin/app/common/response"
"github.com/jassue/jassue-gin/app/models"
"github.com/jassue/jassue-gin/app/services"
"github.com/jassue/jassue-gin/global"
"github.com/jassue/jassue-gin/utils"
"net/http"
"strconv"
)

type AppUserController struct{}

func (t *AppUserController) RoleList(c *gin.Context) {
// 执行数据库操作
db := global.App.DB

// Fetch all routes
var roles []models.SysRoleMenu
db.Where("role_id = ?", 2).Find(&roles)
var ids []uint
for i := range roles {
route := &roles[i]
ids = append(ids, route.MenuID)

}
var menus []models.SysMenu

db.Where("id in ?", ids).Order("parent_id asc").Find(&menus)

routeMap := make(map[uint]*models.Route)
var rootRoutes []*models.Route
for i := range menus {
menu := &menus[i]
route := &models.Route{
ID: menu.ID,
Path: menu.RoutePath,
Component: menu.Component,
Redirect: menu.Redirect,
Name: menu.Name,
ParentID: menu.ParentID,

MetaStr: "",
}
meta := models.Meta{
Title: menu.Name,
Icon: menu.Icon,
AlwaysShow: true,
}
route.Meta = meta
route.Children = []models.Route{}
routeMap[menu.ID] = route

if route.ParentID == 0 {

rootRoutes = append(rootRoutes, route)
}

}
for _, route := range routeMap {
if route.ParentID != 0 {
parent, exists := routeMap[route.ParentID]
if exists {
parent.Children = append(parent.Children, *route)
}
}
}
//

c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": rootRoutes,
})
}

func (uc *AppUserController) ChangePass(c *gin.Context) {
var input request.ChangePass
if err := c.BindJSON(&input); err != nil {
response.Fail(c, 500, "参数出错")
return
}
uid := services.JwtService.GetUserId(c)
var user models.User
fmt.Println("ui:d")
fmt.Println(uid)
global.App.DB.First(&user, uid)
fmt.Println(user)
if !utils.BcryptMakeCheck([]byte(input.OldPassword), user.Password) {
response.Fail(c, 500, "原密码错误")
return
}
//user.Password = utils.BcryptMake([]byte(input.NewPassword))
global.App.DB.Model(&user).Where("id = ?", uid).Update("password", utils.BcryptMake([]byte(input.NewPassword)))

response.Success(c, nil)
}

// GetList handles GET requests for user details
func (uc *AppUserController) GetList(c *gin.Context) {
var users []models.User
var totalUsers int64
pageStr := c.DefaultQuery("page", "1")
pageSizeStr := c.DefaultQuery("pageSize", "10")
keyword := c.DefaultQuery("keyword", "")

page, err := strconv.Atoi(pageStr)
if err != nil || page < 1 {
page = 1
}

pageSize, err := strconv.Atoi(pageSizeStr)
if err != nil || pageSize < 1 {
pageSize = 10
}
// Calculate offset and limit
offset := (page - 1) * pageSize
limit := pageSize
db := global.App.DB

query := db.Model(models.User{})
if keyword != "" {
query.Where("mobile LIKE ?", "%"+keyword+"%").Or("name LIKE ?", "%"+keyword+"%")
}

query.Find(&users).Count(&totalUsers).Limit(limit).Offset(offset)
//for _, route := range routeMap
for i := range users {
user := &users[i]
user.Password = ""
}
var res = gin.H{
"list": users,
"total": totalUsers,
}
response.Success(c, res)
}

// GetDetail handles GET requests for user details
func (uc *AppUserController) GetDetail(c *gin.Context) {
id := c.Param("id")
var user models.User

db := global.App.DB
db.Model(models.User{}).First(&user, id)
user.Password = ""
response.Success(c, user)
}

// Create handles POST requests to create a new user
func (uc *AppUserController) Create(c *gin.Context) {
// Example response, replace with actual logic
c.JSON(201, gin.H{"message": "User created"})
}

// Update handles PUT requests to update a user
func (uc *AppUserController) Update(c *gin.Context) {
var input request.EditUser
// Bind JSON payload to input
if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if input.Password != "" {
global.App.DB.Model(&models.User{}).Where("id = ?", input.Id).Update("password", utils.BcryptMake([]byte(input.Password)))
}

global.App.DB.Where("id = ?", input.Id).Updates(models.User{Mobile: input.Mobile})

response.Success(c, nil)

}

// Delete handles DELETE requests to delete a user
func (uc *AppUserController) Delete(c *gin.Context) {
id := c.Param("id")
global.App.DB.Delete(&models.User{}, id)
c.JSON(200, gin.H{"message": "User deleted", "id": id})
}
Loading

0 comments on commit 30b25b3

Please sign in to comment.