فهرست منبع

优化列表筛选

sun-pc 4 ماه پیش
والد
کامیت
a65b551c23
8فایلهای تغییر یافته به همراه51 افزوده شده و 424 حذف شده
  1. 1 31
      src/api/ar.go
  2. 46 0
      src/api/comm.go
  3. 1 31
      src/api/image.go
  4. 1 31
      src/api/relic.go
  5. 0 62
      src/api/role.go
  6. 0 207
      src/api/user.go
  7. 1 31
      src/api/video.go
  8. 1 31
      src/api/vr.go

+ 1 - 31
src/api/ar.go

@@ -5,11 +5,9 @@ import (
 	"moutai/db/model"
 	"moutai/db/repo"
 	"moutai/log"
-	"strconv"
 	"time"
 
 	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
@@ -41,35 +39,7 @@ func DeleteAr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 func ArList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	page, size, query := UtilQueryPageSize(c)
-	_uploadTime := c.Query("uploadTime")
-	_cid := c.Query("cid")
-	cid, _ := primitive.ObjectIDFromHex(_cid)
-	name := c.Query("name")
-	_sort := c.Query("sort")
-	sortInt, _ := strconv.Atoi(_sort)
-	sort := repo.Map{"createTime": -1}
-	if sortInt > 0 {
-		sort = repo.Map{"sort": -1}
-	}
-
-	if !cid.IsZero() {
-		query["cid"] = cid
-	}
-	if len(name) > 0 {
-		query["name"] = bson.M{"$regex": name, "$options": "$i"}
-	}
-	if len(_uploadTime) > 0 {
-		uploadTime, err := time.Parse("2006-01-02", _uploadTime)
-		if err == nil {
-			startTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 0, 0, 0, 0, uploadTime.Location())
-			endTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 23, 59, 59, 0, uploadTime.Location())
-			query["createTime"] = bson.M{
-				"$gte": startTime,
-				"$lte": endTime,
-			}
-		}
-	}
-
+	query, sort := getQuery(query)
 	result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		CollectName: repo.CollectionAr,
 		Page:        page,

+ 46 - 0
src/api/comm.go

@@ -2,7 +2,9 @@ package api
 
 import (
 	"errors"
+	"fmt"
 	"moutai/db/repo"
+	"time"
 
 	"github.com/gin-gonic/gin"
 	"go.mongodb.org/mongo-driver/bson"
@@ -30,3 +32,47 @@ func Clicks(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	return false, errors.New("参数错误!")
 
 }
+
+func getQuery(query map[string]interface{}) (map[string]interface{}, map[string]interface{}) {
+	// query name查询
+	if _name, ok := query["name"]; ok {
+		name := _name.(string)
+		delete(query, "name")
+		if len(name) > 0 {
+			query["name"] = bson.M{"$regex": name, "$options": "$i"}
+		}
+	}
+	// query uploadTime查询
+	if _uploadTime, ok := query["uploadTime"]; ok {
+		uploadTime := _uploadTime.(string)
+		delete(query, "uploadTime")
+		if len(uploadTime) > 0 {
+			uploadTime, err := time.Parse("2006-01-02", uploadTime)
+			if err == nil {
+				startTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 0, 0, 0, 0, uploadTime.Location())
+				endTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 23, 59, 59, 0, uploadTime.Location())
+				query["createTime"] = bson.M{
+					"$gte": startTime,
+					"$lte": endTime,
+				}
+			}
+		}
+	}
+
+	sortMap := repo.Map{"createTime": -1}
+	// query sort查询
+	if _sort, ok := query["sort"]; ok {
+		// clicks 排序
+		if _sort.(string) == "clicks" {
+			sortMap = repo.Map{"clicks": -1}
+		}
+		// sort int自定义排序
+		if _sort.(string) == "sort" {
+			sortMap = repo.Map{"sort": -1}
+
+		}
+		delete(query, "sort")
+	}
+	fmt.Printf("查询条件:\nquery:%#v\nsortMap: %#v\n", query, sortMap)
+	return query, sortMap
+}

+ 1 - 31
src/api/image.go

@@ -5,11 +5,9 @@ import (
 	"moutai/db/model"
 	"moutai/db/repo"
 	"moutai/log"
-	"strconv"
 	"time"
 
 	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
@@ -41,35 +39,7 @@ func DeleteImage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 func ImageList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	page, size, query := UtilQueryPageSize(c)
-	_uploadTime := c.Query("uploadTime")
-	_cid := c.Query("cid")
-	cid, _ := primitive.ObjectIDFromHex(_cid)
-	name := c.Query("name")
-	_sort := c.Query("sort")
-	sortInt, _ := strconv.Atoi(_sort)
-	sort := repo.Map{"createTime": -1}
-	if sortInt > 0 {
-		sort = repo.Map{"sort": -1}
-	}
-
-	if !cid.IsZero() {
-		query["cid"] = cid
-	}
-	if len(name) > 0 {
-		query["name"] = bson.M{"$regex": name, "$options": "$i"}
-	}
-	if len(_uploadTime) > 0 {
-		uploadTime, err := time.Parse("2006-01-02", _uploadTime)
-		if err == nil {
-			startTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 0, 0, 0, 0, uploadTime.Location())
-			endTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 23, 59, 59, 0, uploadTime.Location())
-			query["createTime"] = bson.M{
-				"$gte": startTime,
-				"$lte": endTime,
-			}
-		}
-	}
-
+	query, sort := getQuery(query)
 	result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		CollectName: repo.CollectionImages,
 		Page:        page,

+ 1 - 31
src/api/relic.go

@@ -5,11 +5,9 @@ import (
 	"moutai/db/model"
 	"moutai/db/repo"
 	"moutai/log"
-	"strconv"
 	"time"
 
 	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
@@ -40,35 +38,7 @@ func DeleteRelic(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 func RelicList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	page, size, query := UtilQueryPageSize(c)
-	_uploadTime := c.Query("uploadTime")
-	_cid := c.Query("cid")
-	cid, _ := primitive.ObjectIDFromHex(_cid)
-	name := c.Query("name")
-	_sort := c.Query("sort")
-	sortInt, _ := strconv.Atoi(_sort)
-	sort := repo.Map{"createTime": -1}
-	if sortInt > 0 {
-		sort = repo.Map{"sort": -1}
-	}
-
-	if !cid.IsZero() {
-		query["cid"] = cid
-	}
-	if len(name) > 0 {
-		query["name"] = bson.M{"$regex": name, "$options": "$i"}
-	}
-	if len(_uploadTime) > 0 {
-		uploadTime, err := time.Parse("2006-01-02", _uploadTime)
-		if err == nil {
-			startTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 0, 0, 0, 0, uploadTime.Location())
-			endTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 23, 59, 59, 0, uploadTime.Location())
-			query["createTime"] = bson.M{
-				"$gte": startTime,
-				"$lte": endTime,
-			}
-		}
-	}
-
+	query, sort := getQuery(query)
 	result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		CollectName: repo.CollectionRelics,
 		Page:        page,

+ 0 - 62
src/api/role.go

@@ -1,62 +0,0 @@
-package api
-
-import (
-	"errors"
-	"moutai/log"
-	"time"
-
-	"moutai/db/model"
-	"moutai/db/repo"
-
-	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson/primitive"
-)
-
-func RoleCreate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	var form model.Role
-	err := c.ShouldBindJSON(&form)
-	if err != nil {
-		return nil, err
-	}
-	form.CreateTime = time.Now()
-	form.UpdateTime = time.Now()
-	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionRoles, &form)
-}
-
-func DeleteRole(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	_id := c.Param("id")
-	id, _ := primitive.ObjectIDFromHex(_id)
-	if id.IsZero() {
-		return nil, errors.New("id错误")
-	}
-	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionRoles, _id)
-
-}
-
-func RoleList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	page, size, query := UtilQueryPageSize(c)
-
-	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
-		CollectName: repo.CollectionRoles,
-		Page:        page,
-		Size:        size,
-		Query:       query,
-	})
-
-}
-
-func UpdateRole(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-
-	role := &model.Role{}
-	err := c.ShouldBindJSON(&role)
-	if err != nil {
-		log.Error(err)
-		return nil, err
-	}
-	if role.Id.IsZero() {
-		return nil, errors.New("id错误")
-	}
-
-	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, role.Id.Hex(), role)
-
-}

+ 0 - 207
src/api/user.go

@@ -1,207 +0,0 @@
-package api
-
-import (
-	"errors"
-	"moutai/log"
-	"time"
-
-	"moutai/db/model"
-	"moutai/db/repo"
-	"moutai/utils"
-
-	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson"
-	"go.mongodb.org/mongo-driver/bson/primitive"
-)
-
-func UserLoginPassword(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	var form model.User
-	err := c.ShouldBindJSON(&form)
-	if err != nil {
-		return nil, err
-	}
-
-	// 正常用户登录
-	user := &model.User{}
-	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-		CollectName: repo.CollectionUser,
-		Query: repo.Map{
-			"loginName": form.LoginName,
-			"password":  utils.UtilMd5(form.Password),
-		},
-	}, user)
-	if err != nil {
-		return nil, err
-	}
-	if !found {
-		return nil, errors.New("账号/密码不正确!")
-	}
-	if *user.State == -1 {
-		return nil, errors.New("该账号已被禁用!")
-	}
-
-	jwtU := &JWTUser{ID: user.GetID()}
-	token, _, err := apictx.Svc.JWT.JwtCreateToken(jwtU)
-	if err != nil {
-		return nil, err
-	}
-
-	// 前端返回处理
-	user.Password = ""
-	out := map[string]interface{}{
-		"token": token,
-		"user":  user,
-	}
-	return out, nil
-}
-
-// 用户唯一
-// db.users.createIndex({ loginName: 1 }, { unique: true })
-func CreateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-
-	user := &model.User{}
-	err := c.ShouldBindJSON(&user)
-	if err != nil {
-		log.Error(err)
-		return nil, err
-	}
-
-	// 验证登录名是否存在
-	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-		CollectName: repo.CollectionUser,
-		Query:       repo.Map{"loginName": user.LoginName},
-	}, user)
-	if err != nil {
-		return nil, err
-	}
-	if found {
-		return nil, errors.New("该账号已存在")
-	}
-
-	// 正常状态
-	state := 1
-	user.State = &state
-	user.Password = UtilMd5(user.Password)
-	user.CreateTime = time.Now()
-	user.UpdateTime = time.Now()
-	return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionUser, &user)
-}
-
-func DeleteUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	_id := c.Param("id")
-	id, _ := primitive.ObjectIDFromHex(_id)
-	if id.IsZero() {
-		return nil, errors.New("id错误")
-	}
-	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionUser, _id)
-}
-
-// 用户列表
-// /user/list?roleId=xxx&name=xxx
-func UserList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	page, size, query := UtilQueryPageSize(c)
-	_roleId := c.Query("roleId")
-	roleId, _ := primitive.ObjectIDFromHex(_roleId)
-	name := c.Query("name")
-
-	if !roleId.IsZero() {
-		query["roleId"] = roleId
-	}
-	if len(name) > 0 {
-		query["name"] = bson.M{"$regex": name, "$options": "$i"}
-	}
-
-	result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
-		CollectName: repo.CollectionUser,
-		Page:        page,
-		Size:        size,
-		Query:       query,
-		Sort:        bson.D{bson.E{Key: "createTime", Value: -1}, bson.E{Key: "_id", Value: -1}},
-		Project:     []string{"roleId", "name", "loginName", "avatar", "state", "createTime", "updateTime"},
-	})
-
-	role := model.Role{}
-	if len(result.List) > 0 {
-		for i, u := range result.List {
-			if v, ok := u["roleId"]; ok {
-				// 查询角色信息
-				roleId := v.(primitive.ObjectID)
-				repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-					CollectName: repo.CollectionRoles,
-					Query:       repo.Map{"_id": roleId},
-				}, &role)
-			}
-			// 拼接角色信息
-			result.List[i]["roleInfo"] = &role
-		}
-	}
-	return result, err
-}
-
-func UpdateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-
-	user := &model.User{}
-	err := c.ShouldBindJSON(&user)
-	if err != nil {
-		log.Error(err)
-		return nil, err
-	}
-	if user.Id.IsZero() {
-		return nil, errors.New("id错误")
-	}
-
-	if len(user.Password) > 0 {
-		user.Password = UtilMd5(user.Password)
-	}
-	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, user.Id.Hex(), user)
-}
-
-func DisableUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	_id := c.Param("id")
-	id, _ := primitive.ObjectIDFromHex(_id)
-	if id.IsZero() {
-		return nil, errors.New("id错误")
-	}
-	state := -1
-	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, _id, model.User{State: &state})
-}
-
-func EnableUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	_id := c.Param("id")
-	id, _ := primitive.ObjectIDFromHex(_id)
-	if id.IsZero() {
-		return nil, errors.New("id错误")
-	}
-	state := 1
-	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, _id, model.User{State: &state})
-}
-
-// 获取自己的信息
-func UserProfile(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	return GetUserById(apictx, apictx.User.ID)
-}
-
-// 根据id获取用户信息
-func GetUserById(apictx *ApiSession, id string) (*model.User, error) {
-	user := &model.User{}
-	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-		CollectName: repo.CollectionUser,
-		Query:       repo.Map{"_id": id},
-	}, user)
-	if err != nil {
-		log.Error(err)
-		return nil, err
-	}
-
-	if !found {
-		return nil, errors.New("未找到该数据")
-	}
-	role := model.Role{}
-	repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-		CollectName: repo.CollectionRoles,
-		Query:       repo.Map{"_id": user.RoleId},
-	}, &role)
-	user.RoleInfo = &role
-	user.Password = ""
-	return user, nil
-}

+ 1 - 31
src/api/video.go

@@ -5,11 +5,9 @@ import (
 	"moutai/db/model"
 	"moutai/db/repo"
 	"moutai/log"
-	"strconv"
 	"time"
 
 	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
@@ -41,35 +39,7 @@ func DeleteVideo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 func VideoList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	page, size, query := UtilQueryPageSize(c)
-	_uploadTime := c.Query("uploadTime")
-	_cid := c.Query("cid")
-	cid, _ := primitive.ObjectIDFromHex(_cid)
-	name := c.Query("name")
-	_sort := c.Query("sort")
-	sortInt, _ := strconv.Atoi(_sort)
-	sort := repo.Map{"createTime": -1}
-	if sortInt > 0 {
-		sort = repo.Map{"sort": -1}
-	}
-
-	if !cid.IsZero() {
-		query["cid"] = cid
-	}
-	if len(name) > 0 {
-		query["name"] = bson.M{"$regex": name, "$options": "$i"}
-	}
-	if len(_uploadTime) > 0 {
-		uploadTime, err := time.Parse("2006-01-02", _uploadTime)
-		if err == nil {
-			startTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 0, 0, 0, 0, uploadTime.Location())
-			endTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 23, 59, 59, 0, uploadTime.Location())
-			query["createTime"] = bson.M{
-				"$gte": startTime,
-				"$lte": endTime,
-			}
-		}
-	}
-
+	query, sort := getQuery(query)
 	result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		CollectName: repo.CollectionVideos,
 		Page:        page,

+ 1 - 31
src/api/vr.go

@@ -5,11 +5,9 @@ import (
 	"moutai/db/model"
 	"moutai/db/repo"
 	"moutai/log"
-	"strconv"
 	"time"
 
 	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
@@ -41,35 +39,7 @@ func DeleteVr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 func VrList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	page, size, query := UtilQueryPageSize(c)
-	_uploadTime := c.Query("uploadTime")
-	_cid := c.Query("cid")
-	cid, _ := primitive.ObjectIDFromHex(_cid)
-	name := c.Query("name")
-	_sort := c.Query("sort")
-	sortInt, _ := strconv.Atoi(_sort)
-	sort := repo.Map{"createTime": -1}
-	if sortInt > 0 {
-		sort = repo.Map{"sort": -1}
-	}
-
-	if !cid.IsZero() {
-		query["cid"] = cid
-	}
-	if len(name) > 0 {
-		query["name"] = bson.M{"$regex": name, "$options": "$i"}
-	}
-	if len(_uploadTime) > 0 {
-		uploadTime, err := time.Parse("2006-01-02", _uploadTime)
-		if err == nil {
-			startTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 0, 0, 0, 0, uploadTime.Location())
-			endTime := time.Date(uploadTime.Year(), uploadTime.Month(), uploadTime.Day(), 23, 59, 59, 0, uploadTime.Location())
-			query["createTime"] = bson.M{
-				"$gte": startTime,
-				"$lte": endTime,
-			}
-		}
-	}
-
+	query, sort := getQuery(query)
 	result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		CollectName: repo.CollectionVr,
 		Page:        page,