|
@@ -0,0 +1,289 @@
|
|
|
+package api
|
|
|
+
|
|
|
+import (
|
|
|
+ "cr-svc/log"
|
|
|
+ "errors"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "cr-svc/db/model"
|
|
|
+ "cr-svc/db/repo"
|
|
|
+ "cr-svc/utils"
|
|
|
+
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "go.mongodb.org/mongo-driver/bson"
|
|
|
+ "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) {
|
|
|
+ var form UserLoginPasswordReq
|
|
|
+ err := c.ShouldBindJSON(&form)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找用户:根据longinName/password/role是否在roles中
|
|
|
+ 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),
|
|
|
+ "roles": bson.M{"$elemMatch": bson.M{"$eq": form.Role}},
|
|
|
+ },
|
|
|
+ }, user)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if !found {
|
|
|
+ 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({ nid: 1 }, { unique: true })
|
|
|
+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{}
|
|
|
+ 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("该账号已存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ // student,teacher,admin
|
|
|
+ if len(user.Roles) < 1 {
|
|
|
+ user.Roles = []string{"student"}
|
|
|
+ }
|
|
|
+ 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) {
|
|
|
+ // 验证是否为管理员
|
|
|
+ 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错误")
|
|
|
+ }
|
|
|
+ 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
|
|
|
+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)
|
|
|
+ role := c.Query("role")
|
|
|
+ name := c.Query("name")
|
|
|
+
|
|
|
+ if len(role) > 0 {
|
|
|
+ query["roles"] = bson.M{"$elemMatch": bson.M{"$eq": role}}
|
|
|
+ }
|
|
|
+ if len(name) > 0 {
|
|
|
+ query["name"] = bson.M{"$regex": name, "$options": "$i"}
|
|
|
+ }
|
|
|
+
|
|
|
+ return 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{"name", "loginName", "avatar", "roles", "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错误")
|
|
|
+ }
|
|
|
+ return GetUserById(apictx, _id)
|
|
|
+}
|
|
|
+
|
|
|
+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{}
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ result, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, user.Id.Hex(), user)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New("更新失败,请检查编码是否重复")
|
|
|
+ }
|
|
|
+ return result, err
|
|
|
+}
|
|
|
+
|
|
|
+// 获取自己的信息
|
|
|
+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("未找到该数据")
|
|
|
+ }
|
|
|
+ user.Password = ""
|
|
|
+ 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
|
|
|
+}
|