diff --git a/app/api/user.go b/app/api/user.go index b8ca9e9..9d66b60 100644 --- a/app/api/user.go +++ b/app/api/user.go @@ -14,13 +14,16 @@ import ( type User struct{} type userForm struct { - Account string `json:"account" form:"account" binding:"required"` - Name string `json:"name" form:"name" binding:"required"` - Avatar string `json:"avatar" form:"avatar"` - Mobile string `json:"mobile" form:"mobile" binding:"required"` - Password string `json:"password" form:"password"` - Gender int `json:"gender" form:"gender"` - Remark string `json:"remark" form:"remark"` + TenantID string `json:"tenant_id" form:"tenant_id"` + Account string `json:"account" form:"account" binding:"required"` + Name string `json:"name" form:"name" binding:"required"` + Avatar string `json:"avatar" form:"avatar"` + Mobile string `json:"mobile" form:"mobile" binding:"required"` + Password string `json:"password" form:"password"` + Gender int `json:"gender" form:"gender" binding:"required"` + Status int `json:"status" form:"status" binding:"required"` + Remark string `json:"remark" form:"remark"` + RoleIDs []string `json:"role_ids" form:"role_ids"` } /** @@ -138,6 +141,16 @@ func (*User) List(c *gin.Context) { APIResponse(err, data)(c) } +func (*User) Detail(c *gin.Context) { + form := new(IDStringForm) + if err := bind(form)(c); err != nil { + APIFailure(err.(error))(c) + return + } + data, err := user.NewInstance()(getSession()(c).(*service.Session)).Detail(form.Convert()) + APIResponse(err, data)(c) +} + /** * @api {get} /api/v1/user/add 用户信息添加 * @apiVersion 1.0.0 @@ -146,13 +159,16 @@ func (*User) List(c *gin.Context) { * * @apiHeader {string} x-token token * +* @apiParam {String} tenant_id 租户单位信息 * @apiParam {String} account 登陆账号 * @apiParam {String} name 姓名 * @apiParam {String} [avatar="''"] 头像 * @apiParam {String} mobile 手机号 * @apiParam {String} password 密码 * @apiParam {Number} gender 性别,1:男,2:女 +* @apiParam {Number} status 状态,1:启用,2:禁用 * @apiParam {String} [remark="''"] 备注信息 +* @apiParam {String[]} [role_ids="[]"] 角色信息 * * @apiSuccess (200) {Object} data 数据信息 * @apiSuccess (200) {String} data.name 用户名 @@ -176,10 +192,11 @@ func (*User) Add(c *gin.Context) { return } obj := &ImageForm{Image: form.Avatar} + IDObj := &IDStringForm{ID: form.TenantID} err := user.NewInstance()(getSession()(c).(*service.Session)).Form(&user.InstanceParams{ - Account: form.Account, Name: form.Name, Avatar: obj.FilterImageURL(), Mobile: form.Mobile, - Password: form.Password, Remark: form.Remark, Gender: form.Gender, + TenantID: IDObj.Convert(), Account: form.Account, Name: form.Name, Avatar: obj.FilterImageURL(), + Mobile: form.Mobile, Password: form.Password, Remark: form.Remark, Gender: form.Gender, Status: form.Status, }) APIResponse(err)(c) } @@ -193,12 +210,15 @@ func (*User) Add(c *gin.Context) { * @apiHeader {string} x-token token * * @apiParam {String} id ID +* @apiParam {String} tenant_id 租户单位信息 * @apiParam {String} account 登陆账号 * @apiParam {String} name 姓名 * @apiParam {String} [avatar="''"] 头像 * @apiParam {String} mobile 手机号 * @apiParam {Number} gender 性别,1:男,2:女 +* @apiParam {Number} status 状态,1:启用,2:禁用 * @apiParam {String} [remark="''"] 备注信息 +* @apiParam {String[]} [role_ids="[]"] 角色信息 * * @apiSuccess (200) {Object} data 数据信息 * @apiSuccess (200) {String} data.name 用户名 @@ -225,10 +245,11 @@ func (*User) Edit(c *gin.Context) { return } obj := &ImageForm{Image: form.Avatar} + IDObj := &IDStringForm{ID: form.TenantID} err := user.NewInstance()(getSession()(c).(*service.Session)).Form(&user.InstanceParams{ - ID: form.Convert(), Account: form.Account, Name: form.Name, Avatar: obj.FilterImageURL(), - Mobile: form.Mobile, Password: form.Password, Remark: form.Remark, Gender: form.Gender, + ID: form.Convert(), TenantID: IDObj.Convert(), Account: form.Account, Name: form.Name, Avatar: obj.FilterImageURL(), + Mobile: form.Mobile, Password: form.Password, Remark: form.Remark, Gender: form.Gender, Status: form.Status, }) APIResponse(err)(c) } diff --git a/app/common/model/manage_notice.go b/app/common/model/manage_notice.go new file mode 100644 index 0000000..5ab67a1 --- /dev/null +++ b/app/common/model/manage_notice.go @@ -0,0 +1,19 @@ +package model + +// ManageNotice 公告管理数据模型 +type ManageNotice struct { + Model + ModelTenant + Title string `gorm:"column:title;type:varchar(100);default:null;comment:标题" json:"title"` + Content string `gorm:"column:content;type:text;default:null;comment:内容" json:"content"` + ModelDeleted + ModelAt +} + +func (m *ManageNotice) TableName() string { + return "manage_notice" +} + +func NewManageNotice() *ManageNotice { + return &ManageNotice{} +} diff --git a/app/common/model/sys_user.go b/app/common/model/sys_user.go index 5cb4896..ef52ae7 100644 --- a/app/common/model/sys_user.go +++ b/app/common/model/sys_user.go @@ -20,8 +20,8 @@ type SysUser struct { Password string `gorm:"column:password;type:varchar(100);default:null;comment:密码" json:"-"` Salt string `gorm:"column:salt;type:varchar(10);default:null;comment:盐值" json:"-"` IsAdmin SysUserAdministrator `gorm:"column:is_admin;type:tinyint(1);default:0;comment:管理员(0:普通用户,1:管理员)" json:"-"` - Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:备注" json:"-"` - Status SysUserStatus `gorm:"column:status;type:tinyint(1);default:1;comment:账号状态" json:"-"` + Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:备注" json:"remark"` + Status SysUserStatus `gorm:"column:status;type:tinyint(1);default:1;comment:账号状态" json:"status"` ModelDeleted ModelAt } diff --git a/app/controller/manage/notice.go b/app/controller/manage/notice.go new file mode 100644 index 0000000..4164343 --- /dev/null +++ b/app/controller/manage/notice.go @@ -0,0 +1,87 @@ +package manage + +import ( + model2 "ArmedPolice/app/common/model" + "ArmedPolice/app/controller/basic" + "ArmedPolice/app/model" + "ArmedPolice/app/service" + "errors" + "time" +) + +type Notice struct{ *service.Session } + +type NoticeHandle func(session *service.Session) *Notice + +type NoticeInfo struct { + ID string `json:"id"` + *model2.ManageNotice +} + +// List 列表信息 +func (c *Notice) List(page, pageSize int) (*basic.PageDataResponse, error) { + mManageNotice := model.NewManageNotice() + + out := make([]*model2.ManageNotice, 0) + + var count int64 + + if err := model2.Pages(mManageNotice.ManageNotice, &out, page, pageSize, &count, &model2.ModelWhereOrder{ + Where: model2.NewWhere("tenant_id", c.TenantID), + Order: model2.NewOrder("id", model2.OrderModeToDesc), + }); err != nil { + return nil, err + } + list := make([]*NoticeInfo, 0) + + for _, v := range out { + list = append(list, &NoticeInfo{ + ID: v.GetEncodeID(), + ManageNotice: v, + }) + } + return &basic.PageDataResponse{Data: list, Count: count}, nil +} + +// Detail 详细信息 +func (c *Notice) Detail(id uint64) (*model2.ManageNotice, error) { + mManageNotice := model.NewManageNotice() + mManageNotice.ID = id + + isExist, err := model2.FirstWhere(mManageNotice.ManageNotice) + + if err != nil { + return nil, err + } else if !isExist { + return nil, errors.New("操作错误,公告信息不存在或已被删除") + } + return mManageNotice.ManageNotice, nil +} + +// Form 数据操作 +func (c *Notice) Form(id uint64, title, content string) error { + mManageNotice := model.NewManageNotice() + + if id > 0 { + mManageNotice.ID = id + + return model2.Updates(mManageNotice.ManageNotice, map[string]interface{}{ + "title": title, "content": content, "updated_at": time.Now(), + }) + } + mManageNotice.Title = title + mManageNotice.Content = content + return model2.Create(mManageNotice) +} + +func (c *Notice) Delete(id uint64) error { + mManageNotice := model.NewManageNotice() + mManageNotice.ID = id + return model2.Delete(mManageNotice.ManageNotice) +} + +func NewNotice() NoticeHandle { + return func(session *service.Session) *Notice { + return &Notice{session} + } +} diff --git a/app/controller/user/instance.go b/app/controller/user/instance.go index 98a312f..b4c754c 100644 --- a/app/controller/user/instance.go +++ b/app/controller/user/instance.go @@ -5,7 +5,11 @@ import ( "ArmedPolice/app/controller/basic" "ArmedPolice/app/model" "ArmedPolice/app/service" + "ArmedPolice/config" + "ArmedPolice/serve/orm" + "ArmedPolice/utils" "errors" + "gorm.io/gorm" "time" ) @@ -25,11 +29,23 @@ type ( ID string `json:"id"` *model.SysUserInfo } + // InstanceDetailInfo 详细信息 + InstanceDetailInfo struct { + ID string `json:"id"` + *model2.SysUser + Roles []*InstanceRoleInfo `json:"roles"` + } + // InstanceRoleInfo 角色信息 + InstanceRoleInfo struct { + ID string `json:"id"` + *model.SysUserRoleInfo + } // InstanceParams 参数信息 InstanceParams struct { - ID uint64 + ID, TenantID uint64 Account, Name, Avatar, Mobile, Password, Remark string - Gender int + Gender, Status int + RoleIDs []uint64 } // InstanceBasicInfo 基本信息 InstanceBasicInfo struct { @@ -38,6 +54,37 @@ type ( } ) +// roleHandle 角色处理 +func (c *InstanceParams) roleHandle(tx *gorm.DB, uid uint64) error { + mSysUserRole := model.NewSysUserRole() + + err := model2.DeleteWhere(mSysUserRole.SysUserRole, + []*model2.ModelWhere{model2.NewWhere("uid", uid)}, tx) + if err != nil { + return err + } + roles := make([]*model2.SysUserRole, 0) + obj := make([]string, 0) + + mark := make(map[uint64]uint64, 0) + + for _, v := range c.RoleIDs { + if _, has := mark[v]; has { + continue + } + obj = append(obj, utils.UintToString(v)) + + roles = append(roles, &model2.SysUserRole{ + UID: uid, RoleID: v, + }) + mark[v] = v + } + if err = model2.Creates(mSysUserRole.SysUserRole, roles, tx); err != nil { + return err + } + return nil +} + // Info 基本信息 func (c *Instance) Info() *InstanceBasic { out := &InstanceBasic{Name: c.Name, Avatar: c.Avatar} @@ -88,12 +135,50 @@ func (c *Instance) List(name, mobile string, tenantID uint64, page, pageSize int list := make([]*InstanceInfo, 0) + mImage := new(model2.Image) + for _, v := range out { + mImage.Image = v.Avatar + v.Avatar = mImage.Analysis(config.SystemConfig[config.Domain].(string)) list = append(list, &InstanceInfo{ID: v.GetEncodeID(), SysUserInfo: v}) } return &basic.PageDataResponse{Data: list, Count: count}, nil } +// Detail 详细信息 +func (*Instance) Detail(id uint64) (*InstanceDetailInfo, error) { + mSysUser := model.NewSysUser() + mSysUser.ID = id + + isExist, err := model2.FirstWhere(mSysUser.SysUser) + + if err != nil { + return nil, err + } else if !isExist { + return nil, err + } + mSysUserRole := model.NewSysUserRole() + + roles := make([]*model.SysUserRoleInfo, 0) + + if roles, err = mSysUserRole.Roles(mSysUser.UUID); err != nil { + return nil, err + } + out := &InstanceDetailInfo{ + ID: mSysUser.GetEncodeID(), + SysUser: mSysUser.SysUser, + Roles: make([]*InstanceRoleInfo, 0), + } + for _, v := range roles { + mSysUserRole.ID = v.ID + out.Roles = append(out.Roles, &InstanceRoleInfo{ + ID: mSysUserRole.GetEncodeID(), + SysUserRoleInfo: v, + }) + } + return out, nil +} + // Form 数据操作 func (c *Instance) Form(params *InstanceParams) error { mSysUser := model.NewSysUser() @@ -117,9 +202,17 @@ func (c *Instance) Form(params *InstanceParams) error { return errors.New("操作错误,该手机号已被注册") } } - return model2.Updates(mSysUser.SysUser, map[string]interface{}{ - "name": params.Name, "mobile": params.Mobile, "avatar": params.Avatar, "gender": params.Gender, - "remark": params.Remark, "updated_at": time.Now(), + return orm.GetDB().Transaction(func(tx *gorm.DB) error { + if err = model2.Updates(mSysUser.SysUser, map[string]interface{}{ + "tenant_id": params.TenantID, "name": params.Name, "mobile": params.Mobile, "avatar": params.Avatar, "gender": params.Gender, + "status": params.Status, "remark": params.Remark, "updated_at": time.Now(), + }, tx); err != nil { + return err + } + if err = params.roleHandle(tx, mSysUser.UUID); err != nil { + return err + } + return nil }) } if params.Password == "" { @@ -139,15 +232,25 @@ func (c *Instance) Form(params *InstanceParams) error { } else if count > 0 { return errors.New("操作错误,该手机号已被注册") } + mSysUser.TenantID = params.TenantID mSysUser.Account = params.Account mSysUser.Name = params.Name mSysUser.Mobile = params.Mobile mSysUser.Avatar = params.Avatar mSysUser.Password = params.Password mSysUser.Gender.Gender = model2.GenderKind(params.Gender) + mSysUser.Status = model2.SysUserStatus(params.Status) mSysUser.Remark = params.Remark - return model2.Create(mSysUser.SysUser) + return orm.GetDB().Transaction(func(tx *gorm.DB) error { + if err = model2.Create(mSysUser.SysUser, tx); err != nil { + return err + } + if err = params.roleHandle(tx, mSysUser.UUID); err != nil { + return err + } + return nil + }) } // Delete 删除信息 diff --git a/app/model/sys_role.go b/app/model/sys_role.go index 8ac26df..bda68a9 100644 --- a/app/model/sys_role.go +++ b/app/model/sys_role.go @@ -28,7 +28,7 @@ func (m *SysRole) UserRole(uid uint64) ([]*SysRoleUserInfo, error) { mSysUserRole := NewSysUserRole() db := orm.GetDB().Table(m.TableName()+" AS r"). - Select("r.is", "r.parent_id", "r.name", "u_r.id AS user_role_id"). + Select("r.id", "r.parent_id", "r.name", "u_r.id AS user_role_id"). Joins(fmt.Sprintf("LEFT JOIN %s AS u_r ON r.id = u_r.role_id AND u_r.uid = %d AND u_r.is_deleted = %d", mSysUserRole.TableName(), uid, model.DeleteStatusForNot)). Where("r.is_deleted = ?", model.DeleteStatusForNot) diff --git a/app/model/sys_user.go b/app/model/sys_user.go index 57a5752..8cfb644 100644 --- a/app/model/sys_user.go +++ b/app/model/sys_user.go @@ -47,7 +47,8 @@ func (m *SysUser) GetByAccountOrMobile(param string) (bool, error) { func (m *SysUser) Users(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*SysUserInfo, error) { mSysTenant := model.NewSysTenant() - db := orm.GetDB().Table(m.TableName()+" As u").Select("u.*", "t.name AS tenant_name"). + db := orm.GetDB().Table(m.TableName()+" As u").Select("u.id", "u.uuid", "u.name", "u.avatar", + "u.mobile", "u.email", "u.status", "t.name AS tenant_name"). Joins(fmt.Sprintf("LEFT JOIN %s AS t ON u.tenant_id = t.id", mSysTenant.TableName())). Where("u.is_deleted = ?", model.DeleteStatusForNot) diff --git a/app/model/sys_user_role.go b/app/model/sys_user_role.go index eedec53..e3d1de4 100644 --- a/app/model/sys_user_role.go +++ b/app/model/sys_user_role.go @@ -2,12 +2,36 @@ package model import ( "ArmedPolice/app/common/model" + "ArmedPolice/serve/orm" + "fmt" ) type SysUserRole struct { *model.SysUserRole } +// SysUserRoleInfo 用户角色信息 +type SysUserRoleInfo struct { + ID uint64 `json:"id"` + Name string `json:"name"` +} + +// Roles 用户角色信息 +func (m *SysUserRole) Roles(uid uint64) ([]*SysUserRoleInfo, error) { + db := orm.GetDB().Table(m.TableName()+" AS u_r"). + Select("r.id", "r.name"). + Joins(fmt.Sprintf("LEFT JOIN %s AS r ON u_r.role_id = r.id", + model.NewSysRole().TableName())). + Where("u_r.uid = ? AND u_r.is_deleted = ?", uid, model.DeleteStatusForNot) + + out := make([]*SysUserRoleInfo, 0) + + if err := db.Order("r.sort " + model.OrderModeToDesc).Order("r.id " + model.OrderModeToDesc).Scan(&out).Error; err != nil { + return nil, err + } + return out, nil +} + func NewSysUserRole() *SysUserRole { return &SysUserRole{SysUserRole: model.NewSysUserRole()} } diff --git a/router/router.go b/router/router.go index f323f02..666cdc6 100644 --- a/router/router.go +++ b/router/router.go @@ -89,6 +89,7 @@ func (this *Router) registerAPI() { userV1.GET("/select", _api.Select) userV1.GET("/menu", _api.Menu) userV1.POST("/list", _api.List) + userV1.POST("/detail", _api.Detail) userV1.POST("/add", _api.Add) userV1.POST("/edit", _api.Edit) userV1.POST("/delete", _api.Delete)