|
@@ -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
|
|
|
|
-}
|
|
|