From e108521f81026aa6eaa2483bce8148319d59c568 Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Wed, 26 Aug 2020 13:46:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=B5=8B=E8=AF=95=E6=96=87?= =?UTF-8?q?=E6=A1=A3=E5=8F=8A=E5=88=9D=E5=A7=8B=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF,=E4=BF=AE=E5=A4=8Din=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2,=20gormv2=E5=8F=96=E6=B6=88()=E7=9A=84?= =?UTF-8?q?=E5=8D=A0=E4=BD=8D=E7=AC=A6,=E6=9B=B4=E6=96=B0=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E6=96=87=E4=BB=B6=E7=9A=84()=E5=8D=A0=E4=BD=8D?= =?UTF-8?q?=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...13\350\257\225\346\226\207\346\241\243.md" | 128 ++++++++++++------ server/model/exa_customer.go | 2 +- server/resource/template/te/service.go.tpl | 2 +- server/service/exa_customer.go | 4 +- server/service/initialize.go | 16 +-- 5 files changed, 99 insertions(+), 53 deletions(-) diff --git "a/server/GORMV2\346\216\245\345\217\243\346\265\213\350\257\225\346\226\207\346\241\243.md" "b/server/GORMV2\346\216\245\345\217\243\346\265\213\350\257\225\346\226\207\346\241\243.md" index 2c3e7d5a..6e9a1a4a 100644 --- "a/server/GORMV2\346\216\245\345\217\243\346\265\213\350\257\225\346\226\207\346\241\243.md" +++ "b/server/GORMV2\346\216\245\345\217\243\346\265\213\350\257\225\346\226\207\346\241\243.md" @@ -1,7 +1,7 @@ | 路由path | 路由描述 | 测试情况 | 备注 | | ------------------------------------------------- | ----------------------- | :------: | ---------- | | /base/login | 用户登录 | √ | SliverHorn | -| /base/register | 用户注册 | | | +| /base/register | 用户注册 | √ | SliverHorn | | /api/createApi | 创建api | √ | SliverHorn | | /api/getApiList | 获取api详细信息 | √ | SliverHorn | | /api/deleteApi | 删除Api | √ | SliverHorn | @@ -12,57 +12,109 @@ | /authority/deleteAuthority | 删除角色 | √ | SliverHorn | | /authority/getAuthorityList | 获取角色列表 | √ | SliverHorn | | /menu/getMenu | 获取菜单树 | √ | SliverHorn | -| /menu/getMenuList | 分页获取基础menu列表 | | | +| /menu/getMenuList | 分页获取基础menu列表 | √ | SliverHorn | | /menu/addBaseMenu | 新增菜单 | | | -| /menu/getBaseMenuTree | 获取用户动态路由 | | | -| /menu/addMenuAuthority | 增加menu和角色关联关系 | | | -| /menu/getMenuAuthority | 获取指定角色menu | | | +| /menu/getBaseMenuTree | 获取用户动态路由 | √ | SliverHorn | +| /menu/addMenuAuthority | 增加menu和角色关联关系 | √ | SliverHorn | +| /menu/getMenuAuthority | 获取指定角色menu | √ | SliverHorn | | /menu/deleteBaseMenu | 删除菜单 | | | | /menu/updateBaseMenu | 更新菜单 | | | -| /menu/getBaseMenuById | 根据id获取菜单 | | | +| /menu/getBaseMenuById | 根据id获取菜单 | √ | SliverHorn | | /user/changePassword | 修改密码 | | | -| /user/uploadHeaderImg | 上传头像 | | | +| /user/uploadHeaderImg | 上传头像 | √ | SliverHorn | | /user/getInfoList | 分页获取用户列表 | | | -| /user/getUserList | 获取用户列表 | | | -| /user/setUserAuthority | 修改用户角色 | | | -| /fileUploadAndDownload/upload | 文件上传示例 | | | -| /fileUploadAndDownload/getFileList | 获取上传文件列表 | | | -| /casbin/updateCasbin | 更改角色api权限 | | | -| /casbin/getPolicyPathByAuthorityId | 获取权限列表 | | | -| /fileUploadAndDownload/deleteFile | 删除文件 | | | +| /user/getUserList | 获取用户列表 | √ | SliverHorn | +| /user/setUserAuthority | 修改用户角色 | √ | SliverHorn | +| /fileUploadAndDownload/upload | 文件上传示例 | √ | SliverHorn | +| /fileUploadAndDownload/getFileList | 获取上传文件列表 | √ | SliverHorn | +| /casbin/updateCasbin | 更改角色api权限 | √ | SliverHorn | +| /casbin/getPolicyPathByAuthorityId | 获取权限列表 | √ | SliverHorn | +| /fileUploadAndDownload/deleteFile | 删除文件 | √ | SliverHorn | | /jwt/jsonInBlacklist | jwt加入黑名单 | | | -| /authority/setDataAuthority | 设置角色资源权限 | | | +| /authority/setDataAuthority | 设置角色资源权限 | √ | SliverHorn | | /system/getSystemConfig | 获取配置文件内容 | | | | /system/setSystemConfig | 设置配置文件内容 | | | -| /customer/customer | 创建客户 | | | +| /customer/customer | 创建客户 | √ | SliverHorn | | /customer/customer | 更新客户 | | | -| /customer/customer | 删除客户 | | | +| /customer/customer | 删除客户 | √ | SliverHorn | | /customer/customer | 获取单一客户 | | | -| /customer/customerList | 获取客户列表 | | | +| /customer/customerList | 获取客户列表 | √ | SliverHorn | | /casbin/casbinTest/:pathParam | RESTFUL模式测试 | | | | /autoCode/createTemp | 自动化代码 | | | -| /authority/updateAuthority | 更新角色信息 | | | -| /authority/copyAuthority | 拷贝角色 | | | -| /user/deleteUser | 删除用户 | | | -| /sysDictionaryDetail/createSysDictionaryDetail | 新增字典内容 | | | -| /sysDictionaryDetail/deleteSysDictionaryDetail | 新增字典内容 | | | -| /sysDictionaryDetail/updateSysDictionaryDetail | 新增字典内容 | | | -| /sysDictionaryDetail/findSysDictionaryDetail | 根据ID获取字典内容 | | | -| /sysDictionaryDetail/getSysDictionaryDetailList | 获取字典内容列表 | | | -| /sysDictionary/createSysDictionary | 新增字典 | | | -| /sysDictionary/deleteSysDictionary | 删除字典 | | | -| /sysDictionary/updateSysDictionary | 更新字典 | | | -| /sysDictionary/findSysDictionary | 根据ID获取字典 | | | -| /sysDictionary/getSysDictionaryList | 获取字典列表 | | | -| /sysOperationRecord/createSysOperationRecord | 新增操作记录 | | | -| /sysOperationRecord/deleteSysOperationRecord | 删除操作记录 | | | +| /authority/updateAuthority | 更新角色信息 | √ | SliverHorn | +| /authority/copyAuthority | 拷贝角色 | √ | SliverHorn | +| /user/deleteUser | 删除用户 | √ | SliverHorn | +| /sysDictionaryDetail/createSysDictionaryDetail | 新增字典内容 | √ | SliverHorn | +| /sysDictionaryDetail/deleteSysDictionaryDetail | 删除字典内容 | √ | SliverHorn | +| /sysDictionaryDetail/updateSysDictionaryDetail | 更新字典内容 | √ | SliverHorn | +| /sysDictionaryDetail/findSysDictionaryDetail | 根据ID获取字典内容 | √ | SliverHorn | +| /sysDictionaryDetail/getSysDictionaryDetailList | 获取字典内容列表 | √ | SliverHorn | +| /sysDictionary/createSysDictionary | 新增字典 | √ | SliverHorn | +| /sysDictionary/deleteSysDictionary | 删除字典 | √ | SliverHorn | +| /sysDictionary/updateSysDictionary | 更新字典 | √ | SliverHorn | +| /sysDictionary/findSysDictionary | 根据ID获取字典 | √ | SliverHorn | +| /sysDictionary/getSysDictionaryList | 获取字典列表 | √ | SliverHorn | +| /sysOperationRecord/createSysOperationRecord | 新增操作记录 | √ | SliverHorn | +| /sysOperationRecord/deleteSysOperationRecord | 删除操作记录 | √ | SliverHorn | | /sysOperationRecord/updateSysOperationRecord | 更新操作记录 | | | -| /sysOperationRecord/findSysOperationRecord | 根据ID获取操作记录 | | | -| /sysOperationRecord/getSysOperationRecordList | 获取操作记录列表 | | | -| /autoCode/getTables | 获取数据库表 | √ | | -| /autoCode/getDB | 获取所有数据库 | √ | | -| /autoCode/getColume | 获取所选table的所有字段 | √ | | +| /sysOperationRecord/findSysOperationRecord | 根据ID获取操作记录 | √ | SliverHorn | +| /sysOperationRecord/getSysOperationRecordList | 获取操作记录列表 | √ | SliverHorn | +| /autoCode/getTables | 获取数据库表 | √ | SliverHorn | +| /autoCode/getDB | 获取所有数据库 | √ | SliverHorn | +| /autoCode/getColume | 获取所选table的所有字段 | √ | SliverHorn | | /sysOperationRecord/deleteSysOperationRecordByIds | 批量删除操作历史 | | | | /simpleUploader/upload | 插件版分片上传 | | | | /simpleUploader/checkFileMd5 | 文件完整度验证 | | | | /simpleUploader/mergeFileMd5 | 上传完成合并文件 | | | + +### Gorm V2 Vs Gorm V1 +- in查询,v2不需要括号了 +```go +// V2文档 +// IN +db.Where("name IN ?", []string{"jinzhu", "jinzhu 2"}).Find(&users) +// SELECT * FROM users WHERE name in ('jinzhu','jinzhu 2'); + +// V1文档 +// IN +db.Where("name IN (?)", []string{"jinzhu", "jinzhu 2"}).Find(&users) +//// SELECT * FROM users WHERE name in ('jinzhu','jinzhu 2'); +``` + +- v2将RecordNotFound链式调用,取而代之是errors.Is(error, gorm.ErrRecordNotFound) + +```go +// V2版本代码(https://github.com/flipped-aurora/gin-vue-admin/blob/gva_gormv2_dev/server/service/sys_user.go) +func Register(u model.SysUser) (err error, userInter model.SysUser) { + var user model.SysUser + // 判断用户名是否注册 + notRegister := errors.Is(global.GVA_DB.Where("username = ?", u.Username).First(&user).Error, gorm.ErrRecordNotFound) + // notRegister为false表明读取到了 不能注册 + if !notRegister { + return errors.New("用户名已注册"), userInter + } else { + // 否则 附加uuid 密码md5简单加密 注册 + u.Password = utils.MD5V([]byte(u.Password)) + u.UUID = uuid.NewV4() + err = global.GVA_DB.Create(&u).Error + } + return err, u +} +// V1版本Register(https://github.com/flipped-aurora/gin-vue-admin/blob/master/server/service/sys_user.go) +func Register(u model.SysUser) (err error, userInter model.SysUser) { + var user model.SysUser + // 判断用户名是否注册 + notRegister := global.GVA_DB.Where("username = ?", u.Username).First(&user).RecordNotFound() + // notRegister为false表明读取到了 不能注册 + if !notRegister { + return errors.New("用户名已注册"), userInter + } else { + // 否则 附加uuid 密码md5简单加密 注册 + u.Password = utils.MD5V([]byte(u.Password)) + u.UUID = uuid.NewV4() + err = global.GVA_DB.Create(&u).Error + } + return err, u +} +``` + diff --git a/server/model/exa_customer.go b/server/model/exa_customer.go index fefdbe79..d4610206 100644 --- a/server/model/exa_customer.go +++ b/server/model/exa_customer.go @@ -16,6 +16,6 @@ type ExaCustomer struct { func ExaCustomerData() []ExaCustomer { return []ExaCustomer{ - {Model: gorm.Model{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, CustomerName: "测试客户", CustomerPhoneData: "1761111111", SysUserID: 10, SysUserAuthorityID: "888"}, + {Model: gorm.Model{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, CustomerName: "测试客户", CustomerPhoneData: "1761111111", SysUserID: 1, SysUserAuthorityID: "888"}, } } diff --git a/server/resource/template/te/service.go.tpl b/server/resource/template/te/service.go.tpl index c05f0cbb..88e0a2e2 100644 --- a/server/resource/template/te/service.go.tpl +++ b/server/resource/template/te/service.go.tpl @@ -35,7 +35,7 @@ func Delete{{.StructName}}({{.Abbreviation}} model.{{.StructName}}) (err error) // @return error func Delete{{.StructName}}ByIds(ids request.IdsReq) (err error) { - err = global.GVA_DB.Delete(&[]model.{{.StructName}}{},"id in (?)",ids.Ids).Error + err = global.GVA_DB.Delete(&[]model.{{.StructName}}{},"id in ?",ids.Ids).Error return err } diff --git a/server/service/exa_customer.go b/server/service/exa_customer.go index 0f50774d..e255be7b 100644 --- a/server/service/exa_customer.go +++ b/server/service/exa_customer.go @@ -70,11 +70,11 @@ func GetCustomerInfoList(sysUserAuthorityID string, info request.PageInfo) (err dataId = append(dataId, v.AuthorityId) } var CustomerList []model.ExaCustomer - err = db.Where("sys_user_authority_id in (?)", dataId).Count(&total).Error + err = db.Where("sys_user_authority_id in ?", dataId).Count(&total).Error if err != nil { return err, CustomerList, total } else { - err = db.Limit(limit).Offset(offset).Preload("SysUser").Where("sys_user_authority_id in (?)", dataId).Find(&CustomerList).Error + err = db.Limit(limit).Offset(offset).Preload("SysUser").Where("sys_user_authority_id in ?", dataId).Find(&CustomerList).Error } return err, CustomerList, total } diff --git a/server/service/initialize.go b/server/service/initialize.go index c0943933..a9c796b9 100644 --- a/server/service/initialize.go +++ b/server/service/initialize.go @@ -12,8 +12,8 @@ type SysAuthorityMenus struct { } type SysDataAuthorityId struct { - SysAuthorityAuthorityId string - SysBaseMenuId string + SysAuthorityAuthorityId string + DataAuthorityIdAuthorityId string } func InitSysApi() (err error) { @@ -44,8 +44,8 @@ func InitExaCustomer() (err error) { } func InitCasbinModel() (err error) { - if !global.GVA_DB.Migrator().HasTable("casbin_rule"){ - if err := global.GVA_DB.Migrator().CreateTable(&gormadapter.CasbinRule{}); err != nil{ + if !global.GVA_DB.Migrator().HasTable("casbin_rule") { + if err := global.GVA_DB.Migrator().CreateTable(&gormadapter.CasbinRule{}); err != nil { return err } } @@ -162,16 +162,10 @@ func InitSysDataAuthorityId() (err error) { {"888", "888"}, {"888", "8881"}, {"888", "9528"}, - {"888222", "888"}, - {"888222", "8881"}, - {"888222", "9528"}, - {"8883", "888"}, - {"8883", "8881"}, - {"8883", "9528"}, {"9528", "8881"}, {"9528", "9528"}, } - if tx.Table("sys_authority_menus").Create(&insert).Error != nil { // 遇到错误时回滚事务 + if tx.Table("sys_data_authority_ids").Create(&insert).Error != nil { // 遇到错误时回滚事务 tx.Rollback() } return tx.Commit().Error -- GitLab