|
@@ -2,9 +2,7 @@ package api
|
|
|
|
|
|
import (
|
|
import (
|
|
"errors"
|
|
"errors"
|
|
- "fmt"
|
|
|
|
"moutai/log"
|
|
"moutai/log"
|
|
- "net/url"
|
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"moutai/db/model"
|
|
"moutai/db/model"
|
|
@@ -12,39 +10,34 @@ import (
|
|
"moutai/utils"
|
|
"moutai/utils"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
- "github.com/xuri/excelize/v2"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
)
|
|
|
|
|
|
-type UserLoginPasswordReq struct {
|
|
|
|
- LoginName string `json:"loginName"`
|
|
|
|
- Password string `json:"password"`
|
|
|
|
- Role string `json:"role"`
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func UserLoginPassword(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
func UserLoginPassword(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
- var form UserLoginPasswordReq
|
|
|
|
|
|
+ var form model.User
|
|
err := c.ShouldBindJSON(&form)
|
|
err := c.ShouldBindJSON(&form)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- // 查找用户:根据longinName/password/role是否在roles中
|
|
|
|
|
|
+ // 正常用户登录
|
|
user := &model.User{}
|
|
user := &model.User{}
|
|
found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
|
|
CollectName: repo.CollectionUser,
|
|
CollectName: repo.CollectionUser,
|
|
Query: repo.Map{
|
|
Query: repo.Map{
|
|
"loginName": form.LoginName,
|
|
"loginName": form.LoginName,
|
|
"password": utils.UtilMd5(form.Password),
|
|
"password": utils.UtilMd5(form.Password),
|
|
- "roles": bson.M{"$elemMatch": bson.M{"$eq": form.Role}},
|
|
|
|
},
|
|
},
|
|
}, user)
|
|
}, user)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
if !found {
|
|
if !found {
|
|
- return nil, errors.New("账号/密码/角色不正确")
|
|
|
|
|
|
+ return nil, errors.New("账号/密码不正确!")
|
|
|
|
+ }
|
|
|
|
+ if *user.State == -1 {
|
|
|
|
+ return nil, errors.New("该账号已被禁用!")
|
|
}
|
|
}
|
|
|
|
|
|
jwtU := &JWTUser{ID: user.GetID()}
|
|
jwtU := &JWTUser{ID: user.GetID()}
|
|
@@ -63,19 +56,11 @@ func UserLoginPassword(c *gin.Context, apictx *ApiSession) (interface{}, error)
|
|
}
|
|
}
|
|
|
|
|
|
// 用户唯一
|
|
// 用户唯一
|
|
-// db.users.createIndex({ nid: 1 }, { unique: true })
|
|
|
|
|
|
+// db.users.createIndex({ loginName: 1 }, { unique: true })
|
|
func CreateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
func CreateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
- // 验证是否为管理员
|
|
|
|
- isAdmin, err := IsAdmin(c, apictx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if !isAdmin {
|
|
|
|
- return nil, errors.New("没有权限")
|
|
|
|
- }
|
|
|
|
|
|
|
|
user := &model.User{}
|
|
user := &model.User{}
|
|
- err = c.ShouldBindJSON(&user)
|
|
|
|
|
|
+ err := c.ShouldBindJSON(&user)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Error(err)
|
|
log.Error(err)
|
|
return nil, err
|
|
return nil, err
|
|
@@ -93,10 +78,9 @@ func CreateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
return nil, errors.New("该账号已存在")
|
|
return nil, errors.New("该账号已存在")
|
|
}
|
|
}
|
|
|
|
|
|
- // student,teacher,admin
|
|
|
|
- if len(user.Roles) < 1 {
|
|
|
|
- user.Roles = []string{"student"}
|
|
|
|
- }
|
|
|
|
|
|
+ // 正常状态
|
|
|
|
+ state := 1
|
|
|
|
+ user.State = &state
|
|
user.Password = UtilMd5(user.Password)
|
|
user.Password = UtilMd5(user.Password)
|
|
user.CreateTime = time.Now()
|
|
user.CreateTime = time.Now()
|
|
user.UpdateTime = time.Now()
|
|
user.UpdateTime = time.Now()
|
|
@@ -104,14 +88,6 @@ func CreateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
}
|
|
}
|
|
|
|
|
|
func DeleteUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
func DeleteUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
- // 验证是否为管理员
|
|
|
|
- isAdmin, err := IsAdmin(c, apictx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if !isAdmin {
|
|
|
|
- return nil, errors.New("没有权限")
|
|
|
|
- }
|
|
|
|
_id := c.Param("id")
|
|
_id := c.Param("id")
|
|
id, _ := primitive.ObjectIDFromHex(_id)
|
|
id, _ := primitive.ObjectIDFromHex(_id)
|
|
if id.IsZero() {
|
|
if id.IsZero() {
|
|
@@ -120,93 +96,52 @@ func DeleteUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionUser, _id)
|
|
return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionUser, _id)
|
|
}
|
|
}
|
|
|
|
|
|
-type BatchDeleteUserReq struct {
|
|
|
|
- Ids []string `json:"ids"`
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func BatchDeleteUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- // 验证是否为管理员
|
|
|
|
- isAdmin, err := IsAdmin(c, apictx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if !isAdmin {
|
|
|
|
- return nil, errors.New("没有权限")
|
|
|
|
- }
|
|
|
|
- var form BatchDeleteUserReq
|
|
|
|
- err = c.ShouldBindJSON(&form)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, errors.New("参数错误")
|
|
|
|
- }
|
|
|
|
- if len(form.Ids) > 0 {
|
|
|
|
- for _, id := range form.Ids {
|
|
|
|
- repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionUser, id)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return true, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// 用户列表
|
|
// 用户列表
|
|
-// /user/list?role=student&name=xxx&nid=xxx&page=1&size=10
|
|
|
|
|
|
+// /user/list?roleId=xxx&name=xxx
|
|
func UserList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
func UserList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
- // 验证是否为管理员
|
|
|
|
- isStudent, err := IsStudent(c, apictx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if isStudent {
|
|
|
|
- return nil, errors.New("没有权限")
|
|
|
|
- }
|
|
|
|
page, size, query := UtilQueryPageSize(c)
|
|
page, size, query := UtilQueryPageSize(c)
|
|
- role := c.Query("role")
|
|
|
|
|
|
+ _roleId := c.Query("roleId")
|
|
|
|
+ roleId, _ := primitive.ObjectIDFromHex(_roleId)
|
|
name := c.Query("name")
|
|
name := c.Query("name")
|
|
|
|
|
|
- if len(role) > 0 {
|
|
|
|
- query["roles"] = bson.M{"$elemMatch": bson.M{"$eq": role}}
|
|
|
|
|
|
+ if !roleId.IsZero() {
|
|
|
|
+ query["roleId"] = roleId
|
|
}
|
|
}
|
|
if len(name) > 0 {
|
|
if len(name) > 0 {
|
|
query["name"] = bson.M{"$regex": name, "$options": "$i"}
|
|
query["name"] = bson.M{"$regex": name, "$options": "$i"}
|
|
}
|
|
}
|
|
|
|
|
|
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
|
|
|
+ result, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
CollectName: repo.CollectionUser,
|
|
CollectName: repo.CollectionUser,
|
|
Page: page,
|
|
Page: page,
|
|
Size: size,
|
|
Size: size,
|
|
Query: query,
|
|
Query: query,
|
|
Sort: bson.D{bson.E{Key: "createTime", Value: -1}, bson.E{Key: "_id", Value: -1}},
|
|
Sort: bson.D{bson.E{Key: "createTime", Value: -1}, bson.E{Key: "_id", Value: -1}},
|
|
- Project: []string{"name", "loginName", "avatar", "roles", "createTime", "updateTime"},
|
|
|
|
|
|
+ Project: []string{"roleId", "name", "loginName", "avatar", "state", "createTime", "updateTime"},
|
|
})
|
|
})
|
|
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func UserDetail(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- // 验证是否为管理员
|
|
|
|
- isAdmin, err := IsAdmin(c, apictx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if !isAdmin {
|
|
|
|
- return nil, errors.New("没有权限")
|
|
|
|
- }
|
|
|
|
- _id := c.Param("id")
|
|
|
|
- id, _ := primitive.ObjectIDFromHex(_id)
|
|
|
|
- if id.IsZero() {
|
|
|
|
- return nil, errors.New("id错误")
|
|
|
|
|
|
+ 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 GetUserById(apictx, _id)
|
|
|
|
|
|
+ return result, err
|
|
}
|
|
}
|
|
|
|
|
|
func UpdateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
func UpdateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
- // 验证是否为管理员
|
|
|
|
- isAdmin, err := IsAdmin(c, apictx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if !isAdmin {
|
|
|
|
- return nil, errors.New("没有权限")
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
user := &model.User{}
|
|
user := &model.User{}
|
|
- err = c.ShouldBindJSON(&user)
|
|
|
|
|
|
+ err := c.ShouldBindJSON(&user)
|
|
if err != nil {
|
|
if err != nil {
|
|
log.Error(err)
|
|
log.Error(err)
|
|
return nil, err
|
|
return nil, err
|
|
@@ -218,11 +153,27 @@ func UpdateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
if len(user.Password) > 0 {
|
|
if len(user.Password) > 0 {
|
|
user.Password = UtilMd5(user.Password)
|
|
user.Password = UtilMd5(user.Password)
|
|
}
|
|
}
|
|
- result, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, user.Id.Hex(), user)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, errors.New("更新失败,请检查编码是否重复")
|
|
|
|
|
|
+ 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错误")
|
|
}
|
|
}
|
|
- return result, err
|
|
|
|
|
|
+ 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})
|
|
}
|
|
}
|
|
|
|
|
|
// 获取自己的信息
|
|
// 获取自己的信息
|
|
@@ -230,29 +181,6 @@ func UserProfile(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
return GetUserById(apictx, apictx.User.ID)
|
|
return GetUserById(apictx, apictx.User.ID)
|
|
}
|
|
}
|
|
|
|
|
|
-// func UserLearnInc(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
-// _time := c.Query("t")
|
|
|
|
-// increase, _ := strconv.Atoi(_time)
|
|
|
|
-// if increase < 1 {
|
|
|
|
-
|
|
|
|
-// return false, fmt.Errorf("增加学习时长小于1: %d", increase)
|
|
|
|
-// }
|
|
|
|
-// userInfo, err := GetUserById(apictx, apictx.User.ID)
|
|
|
|
-// if err != nil {
|
|
|
|
-// return false, errors.New("未找到该用户信息")
|
|
|
|
-// }
|
|
|
|
-// learnTime := userInfo.LearnTime
|
|
|
|
-// // 增加学习时长
|
|
|
|
-// newTime := learnTime + increase
|
|
|
|
-// _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, apictx.User.ID, &model.User{LearnTime: newTime})
|
|
|
|
-// if err != nil {
|
|
|
|
-// fmt.Println(err)
|
|
|
|
-// log.Error(err)
|
|
|
|
-// return false, errors.New("更新学习时长出错")
|
|
|
|
-// }
|
|
|
|
-// return true, nil
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
// 根据id获取用户信息
|
|
// 根据id获取用户信息
|
|
func GetUserById(apictx *ApiSession, id string) (*model.User, error) {
|
|
func GetUserById(apictx *ApiSession, id string) (*model.User, error) {
|
|
user := &model.User{}
|
|
user := &model.User{}
|
|
@@ -268,190 +196,12 @@ func GetUserById(apictx *ApiSession, id string) (*model.User, error) {
|
|
if !found {
|
|
if !found {
|
|
return nil, errors.New("未找到该数据")
|
|
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 = ""
|
|
user.Password = ""
|
|
return user, nil
|
|
return user, nil
|
|
}
|
|
}
|
|
-
|
|
|
|
-// 是否是管理员
|
|
|
|
-func IsAdmin(c *gin.Context, apictx *ApiSession) (bool, error) {
|
|
|
|
- user, err := GetUserById(apictx, apictx.User.ID)
|
|
|
|
- if err != nil {
|
|
|
|
- return false, err
|
|
|
|
- }
|
|
|
|
- for _, v := range user.Roles {
|
|
|
|
- if v == "admin" {
|
|
|
|
- return true, nil
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 是否是老师
|
|
|
|
-func IsTeacher(c *gin.Context, apictx *ApiSession) (bool, error) {
|
|
|
|
- user, err := GetUserById(apictx, apictx.User.ID)
|
|
|
|
- if err != nil {
|
|
|
|
- return false, err
|
|
|
|
- }
|
|
|
|
- for _, v := range user.Roles {
|
|
|
|
- if v == "teacher" {
|
|
|
|
- return true, nil
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 是否是学生
|
|
|
|
-func IsStudent(c *gin.Context, apictx *ApiSession) (bool, error) {
|
|
|
|
- user, err := GetUserById(apictx, apictx.User.ID)
|
|
|
|
- if err != nil {
|
|
|
|
- return false, err
|
|
|
|
- }
|
|
|
|
- for _, v := range user.Roles {
|
|
|
|
- if v == "student" {
|
|
|
|
- return true, nil
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 导入用户
|
|
|
|
-func ImportUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- isAdmin, err := IsAdmin(c, apictx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if !isAdmin {
|
|
|
|
- return nil, errors.New("没有权限")
|
|
|
|
- }
|
|
|
|
- file, _, err := c.Request.FormFile("file")
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, errors.New("文件错误")
|
|
|
|
- }
|
|
|
|
- excel, err := excelize.OpenReader(file)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- excelIndex := excel.GetActiveSheetIndex()
|
|
|
|
- sheetName := excel.GetSheetName(excelIndex)
|
|
|
|
- users, err := excel.GetRows(sheetName)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- errors := []string{}
|
|
|
|
-
|
|
|
|
- if len(users) > 0 {
|
|
|
|
- for index, us := range users {
|
|
|
|
- rowNum := index + 1
|
|
|
|
- // 标题行
|
|
|
|
- if index == 0 {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- // 表头
|
|
|
|
- if index == 1 {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- // 去除可能的空行
|
|
|
|
- if len(us) == 0 {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- // 用户名name 编号nid 密码password 角色roles
|
|
|
|
- user := &model.User{}
|
|
|
|
- user.LoginName = us[0]
|
|
|
|
- user.Nid = us[1]
|
|
|
|
- if len(us[0]) == 0 {
|
|
|
|
- errors = append(errors, fmt.Sprintf("第%d行错误: %s", rowNum, "账号不能为空"))
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
- // !查询用户,确保账号名唯一,用数据库唯一索引约束:db.users.createIndex({ loginName: 1 }, { unique: true })
|
|
|
|
-
|
|
|
|
- roles := []string{}
|
|
|
|
- if us[2] == "管理员" {
|
|
|
|
- roles = []string{"admin"}
|
|
|
|
- }
|
|
|
|
- if us[2] == "教员" {
|
|
|
|
- roles = []string{"teacher"}
|
|
|
|
- }
|
|
|
|
- if us[2] == "学员" {
|
|
|
|
- roles = []string{"student"}
|
|
|
|
- }
|
|
|
|
- user.Roles = roles
|
|
|
|
- user.Password = UtilMd5(us[3])
|
|
|
|
- if len(us[3]) == 0 {
|
|
|
|
- user.Password = UtilMd5("123456")
|
|
|
|
- } else if len(us[3]) == 32 {
|
|
|
|
- user.Password = us[3]
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- user.CreateTime = time.Now()
|
|
|
|
- user.UpdateTime = time.Now()
|
|
|
|
-
|
|
|
|
- _, err = repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionUser, user)
|
|
|
|
- if err != nil {
|
|
|
|
- errors = append(errors, fmt.Sprintf("第%d行错误: %s", rowNum, "保存数据失败, 请检查数据格式是否正确/账号是否重复"))
|
|
|
|
- log.Error(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return errors, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 导出用户
|
|
|
|
-func ExportUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- isAdmin, err := IsAdmin(c, apictx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- if !isAdmin {
|
|
|
|
- return nil, errors.New("没有权限")
|
|
|
|
- }
|
|
|
|
- f := excelize.NewFile()
|
|
|
|
- index, _ := f.NewSheet("Sheet1")
|
|
|
|
- f.SetActiveSheet(index)
|
|
|
|
- f.SetDefaultFont("宋体")
|
|
|
|
-
|
|
|
|
- testExcel := NewUserExcel(f)
|
|
|
|
- testExcel.Title = "用户信息"
|
|
|
|
-
|
|
|
|
- _, _, query := UtilQueryPageSize(c)
|
|
|
|
- // 获取试题列表
|
|
|
|
- users := make([]*model.User, 0)
|
|
|
|
- err = repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
|
|
|
|
- CollectName: repo.CollectionUser,
|
|
|
|
- Query: query,
|
|
|
|
- Sort: bson.M{"createTime": 1},
|
|
|
|
- }, &users)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- testExcel.Content = users
|
|
|
|
- testExcel.Draws()
|
|
|
|
- fileName := url.PathEscape("用户信息.xlsx")
|
|
|
|
- c.Header("Content-Type", "application/octet-stream")
|
|
|
|
- c.Header("Content-Disposition", "attachment; filename="+fileName)
|
|
|
|
- c.Header("Content-Transfer-Encoding", "binary")
|
|
|
|
-
|
|
|
|
- err = f.Write(c.Writer)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return nil, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 下载用户模板
|
|
|
|
-func UserTmplate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
|
|
|
|
- filename := "用户信息模板.xlsx"
|
|
|
|
- // url.PathEscape将字符串中的特殊字符进行编码,使其符合URL规范
|
|
|
|
- filename = url.PathEscape(filename)
|
|
|
|
-
|
|
|
|
- // 设置下载的文件名
|
|
|
|
- c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
|
|
|
|
-
|
|
|
|
- // 设置文件类型
|
|
|
|
- c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
|
|
-
|
|
|
|
- c.File("./template/用户信息模板.xlsx")
|
|
|
|
- return nil, nil
|
|
|
|
-}
|
|
|