123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- package system
- import (
- "errors"
- "go-nc/configs/global"
- "go-nc/internal/utils"
- "go-nc/model"
- "go-nc/model/common"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- )
- // 登录
- func SysLogin(c *gin.Context) {
- var param SysLoginType
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- pwd := utils.DecryptDES_ECB(param.Password, "")
- db_pwd := utils.EncryptDES_ECB(pwd, "limingYa")
- param.Password = db_pwd
- var data model.Sys_user
- global.App.DB.Where("username = ? AND password = ?", param.Username, param.Password).First(&data)
- if data.Id == 0 {
- c.Error(errors.New("用户名或密码错误!"))
- return
- }
- data.Password = ""
- token, err := utils.GenerateToken(utils.UserInfo{
- Id: data.Id,
- UserName: data.Username,
- UserType: data.UserType,
- })
- if err != nil {
- c.Error(err)
- return
- }
- type Res struct {
- model.Sys_user
- Token string `json:"token"`
- }
- c.Set("res_data", Res{data, token})
- }
- // 创建用户
- func SysRegister(c *gin.Context) {
- var param SysUserType
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- if len(param.RoleIds) == 0 {
- c.Error(errors.New("请选择角色!"))
- return
- }
- // 事物操作
- tx := global.App.DB.Begin()
- // 判断用户名是否存在
- var user model.Sys_user
- if err := tx.Where("username = ?", param.Username).First(&user).Error; err == nil {
- tx.Rollback()
- c.Error(errors.New("该用户名已存在!!"))
- return
- }
- if user.Id != 0 {
- tx.Rollback()
- c.Error(errors.New("该用户名已存在!"))
- return
- }
- // 创建用户&密码加密
- pwd := utils.EncryptDES_ECB(param.Password, "limingYa")
- param.Password = pwd
- param.UserType = "1"
- copier.Copy(&user, ¶m)
- if err := tx.Create(&user).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 批量创建用户角色关系。不要循环插入
- var userRoleList []model.Sys_user_role
- for _, v := range param.RoleIds {
- userRoleList = append(userRoleList, model.Sys_user_role{
- UserId: user.Id,
- RoleId: v,
- })
- }
- if err := tx.Create(&userRoleList).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- tx.Commit()
- c.Set("res_data", "添加成功")
- }
- // 数据换加密后内容api
- func EncryptDataAPi(c *gin.Context) {
- pwd := c.Query("pwd")
- data := utils.EncryptDES_ECB(pwd, "")
- c.Set("res_data", data)
- }
- // 获取用户列表
- func GetUserList(c *gin.Context) {
- type Param struct {
- Name string `json:"username"`
- common.Pagination
- }
- var param Param
- param.Pagination = common.NewPagination()
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- // 先查询总记录数
- var total int64
- var userList []model.Sys_user
- if err := global.App.DB.Model(&model.Sys_user{}).Where("name LIKE ? AND user_type = 1", "%"+param.Name+"%").Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- // 使用分页查询
- if err := global.App.DB.Where("name LIKE ? AND user_type = 1", "%"+param.Name+"%").
- Limit(param.PageSize).
- Offset((param.Current - 1) * param.PageSize).
- Find(&userList).Error; err != nil {
- c.Error(err)
- return
- }
- // 查询用户角色
- type User struct {
- model.Sys_user
- RoleList []uint `json:"roleIds"`
- }
- var userRoleList []User
- // 复制数据
- copier.Copy(&userRoleList, &userList)
- // 查询角色
- for i, user := range userRoleList {
- var roleList []model.Sys_user_role
- if err := global.App.DB.Where("user_id = ?", user.Id).Find(&roleList).Error; err != nil {
- c.Error(err)
- return
- }
- // userIds
- var userRoleIds []uint
- for _, v := range roleList {
- userRoleIds = append(userRoleIds, v.RoleId)
- }
- userRoleList[i].Password = ""
- userRoleList[i].RoleList = userRoleIds
- }
- records := make([]interface{}, len(userRoleList))
- for i, user := range userRoleList {
- records[i] = user
- }
- // 返回数据
- data := common.Pagination{
- Records: records,
- Current: param.Current,
- PageSize: param.PageSize,
- Total: total,
- }
- c.Set("res_data", data)
- }
- // 删除用户
- func DeleteUser(c *gin.Context) {
- type Param struct {
- Id uint `json:"id" validate:"required"`
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数!"))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- // 事物操作
- tx := global.App.DB.Begin()
- // 删除用户
- var user model.Sys_user
- user.Id = param.Id
- if err := tx.Unscoped().Delete(&user).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 删除用户角色关系
- var userRoleList model.Sys_user_role
- if err := tx.Where("user_id = ?", param.Id).Unscoped().Delete(&userRoleList).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- tx.Commit()
- c.Set("res_data", "删除成功!")
- }
- // 更新用户
- func UpdateUser(c *gin.Context) {
- type Param struct {
- Id uint `json:"id" validate:"required"`
- Username string `json:"username" validate:"required"`
- State string `json:"state" validate:"required"`
- Name string `json:"name" validate:"required"`
- Password string `json:"password" `
- RoleIds []uint `json:"roleIds" validate:"required"`
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- if len(param.RoleIds) == 0 {
- c.Error(errors.New("请选择角色!"))
- return
- }
- if param.Password != "" {
- pwd := utils.DecryptDES_ECB(param.Password, "")
- param.Password = utils.EncryptDES_ECB(pwd, "limingYa")
- }
- // 更新用户
- tx := global.App.DB.Begin()
- user := model.Sys_user{}
- copier.Copy(&user, ¶m)
- if err := tx.Updates(&user).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 更新用户和角色关系
- // 删除用户角色关系
- if err := tx.Where("user_id = ?", param.Id).Unscoped().Delete(&model.Sys_user_role{}).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- // 批量创建用户角色关系。不要循环插入
- var userRoleList []model.Sys_user_role
- for _, v := range param.RoleIds {
- userRoleList = append(userRoleList, model.Sys_user_role{
- UserId: user.Id,
- RoleId: v,
- })
- }
- if err := tx.Create(&userRoleList).Error; err != nil {
- tx.Rollback()
- c.Error(err)
- return
- }
- tx.Commit()
- c.Set("res_data", "更新成功!")
- }
|