From 82fb0128505c4916d5726fe1ed5322e605cc8046 Mon Sep 17 00:00:00 2001 From: TianJun Date: Fri, 30 Apr 2021 11:21:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=9F=E7=94=9F=E6=9F=A5=E8=AF=A2+1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/base_do.go | 2 +- src/main.go | 15 +++++-- src/modules/system/dao/role_dao.go | 49 ++++++++++++++++++++++ src/modules/system/entity/sys_dict_type.go | 4 +- src/util/aes_utils.go | 30 +++++++------ src/util/raw_query_utils.go | 17 ++++++++ 6 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 src/modules/system/dao/role_dao.go create mode 100644 src/util/raw_query_utils.go diff --git a/src/base/base_do.go b/src/base/base_do.go index c7e2c55..c7681f9 100644 --- a/src/base/base_do.go +++ b/src/base/base_do.go @@ -6,7 +6,7 @@ import ( type LingYeDO struct { // 自增ID - ID uint64 `gorm:"column:id;type:bigint(20);not null;primary_key;AUTO_INCREMENT;comment:'自增ID'"` + ID uint64 `gorm:"primary_key"` // 创建时间 CreatedAt time.Time `gorm:"comment:'创建时间'"` // 更新时间 diff --git a/src/main.go b/src/main.go index 37f1b5e..395cbda 100644 --- a/src/main.go +++ b/src/main.go @@ -1,17 +1,16 @@ package main import ( + "fmt" "lingye-gin/src/config" "lingye-gin/src/middleware" - v1 "lingye-gin/src/test/v1" + "lingye-gin/src/modules/system/entity" + "lingye-gin/src/util" ) func main() { // 初始化yaml配置 new(config.ApplicationProperties).Init() - - v1.DescribeStudents(nil) - // 初始化redis new(middleware.RedisPool).Init() // 初始化gorm, 注册表 @@ -23,3 +22,11 @@ func main() { // 初始化gin new(middleware.GinEngine).Start() } + +func Test() { + // 通过ID查询 + var role entity.Role + util.SelectOne(role, "select * from sys_role where id = ?", 1) + fmt.Printf("SelectOne=%v", role) + // 查询列表 +} diff --git a/src/modules/system/dao/role_dao.go b/src/modules/system/dao/role_dao.go new file mode 100644 index 0000000..2516ffd --- /dev/null +++ b/src/modules/system/dao/role_dao.go @@ -0,0 +1,49 @@ +package dao + +import ( + "lingye-gin/src/config" + "lingye-gin/src/modules/system/entity" + "lingye-gin/src/modules/system/service/query" + "lingye-gin/src/util" +) + +type RoleDAO struct{} + +// 新增 +func (dao RoleDAO) Insert(resource *entity.Role) { + config.SqlExcutor.Create(resource) +} + +// 删除 +func (RoleDAO) DeleteById(id uint64) { + role := entity.Role{} + role.ID = id + config.SqlExcutor.Delete(&role) +} + +// 更新 +func (RoleDAO) UpdateById(role *entity.Role) { + model := &entity.Role{} + model.ID = role.ID + config.SqlExcutor.Model(model).Updates(map[string]interface{}{ + "role_key": role.RoleKey, + "role_name": role.RoleName, + "role_sort": role.RoleSort, + }) +} + +// 条件分页查询 +func (RoleDAO) SelectPage(condition query.UserQuery) ([]entity.Role, int) { + var users []entity.Role + var total int + + page, pageSize := util.FixPage(condition.Page, condition.PageSize) + // 分页条件 + pageConfig := config.SqlExcutor.Where("username LIKE ?", "%"+condition.Username+"%") + // 分页参数 + pageConfig = pageConfig.Limit((page - 1) * pageSize).Offset(pageSize) + pageConfig = pageConfig.Find(&users) + // 统计数量 + pageConfig.Count(&total) + return users, total +} diff --git a/src/modules/system/entity/sys_dict_type.go b/src/modules/system/entity/sys_dict_type.go index e8a8d3f..ea93711 100644 --- a/src/modules/system/entity/sys_dict_type.go +++ b/src/modules/system/entity/sys_dict_type.go @@ -7,8 +7,8 @@ import ( // 字典类型 type DictType struct { base.LingYeDO - DictName string `gorm:"column:dict_name;type:varchar(255);comment:'字典名称'"` - DictType string `gorm:"column:dict_type;type:varchar(255);unique_index;comment:'字典类型'"` + DictName string `gorm:"column:dict_name;type:varchar(255);index:idx_name_type;comment:'字典名称'"` + DictType string `gorm:"column:dict_type;type:varchar(255);index:idx_name_type;comment:'字典类型'"` Status string `gorm:"column:status;type:char(1);default:'0';comment:'状态(0正常 1停用)'"` Remark string `gorm:"column:remark;comment:'备注'"` } diff --git a/src/util/aes_utils.go b/src/util/aes_utils.go index fb3ca5e..4a47ce2 100644 --- a/src/util/aes_utils.go +++ b/src/util/aes_utils.go @@ -13,17 +13,15 @@ import ( ) // 高级加密标准(Advanced Encryption Standard, AES) -type AESUtils struct{} - // 16,24,32位字符串的话, 分别对应AES-128, AES-192, AES-256 加密方法 // key不能泄露 -func (AESUtils) GetRandomPwdKey() string { +func GetRandomPwdKey() string { u2 := uuid.NewV4() return strings.Replace(u2.String(), "-", "", -1) } // PKCS7 填充模式 -func (AESUtils) PKCS7Padding(ciphertext []byte, blockSize int) []byte { +func PKCS7Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize // Repeat()函数的功能是把切片[]byte{byte(padding)}复制padding个, 然后合并成新的字节切片返回 padText := bytes.Repeat([]byte{byte(padding)}, padding) @@ -31,7 +29,7 @@ func (AESUtils) PKCS7Padding(ciphertext []byte, blockSize int) []byte { } // 填充的反向操作, 删除填充字符串 -func (AESUtils) PKCS7UnPadding(origData []byte) ([]byte, error) { +func PKCS7UnPadding(origData []byte) ([]byte, error) { // 获取数据长度 length := len(origData) if length == 0 { @@ -45,7 +43,7 @@ func (AESUtils) PKCS7UnPadding(origData []byte) ([]byte, error) { } // 实现加密 -func (util AESUtils) AesEncrypt(origData []byte, key []byte) ([]byte, error) { +func AesEncrypt(origData []byte, key []byte) ([]byte, error) { // 创建加密算法实例 block, err := aes.NewCipher(key) if err != nil { @@ -54,7 +52,7 @@ func (util AESUtils) AesEncrypt(origData []byte, key []byte) ([]byte, error) { // 获取块的大小 blockSize := block.BlockSize() // 对数据进行填充,让数据长度满足需求 - origData = util.PKCS7Padding(origData, blockSize) + origData = PKCS7Padding(origData, blockSize) // 采用AES加密方法中CBC加密模式 blocMode := cipher.NewCBCEncrypter(block, key[:blockSize]) result := make([]byte, len(origData)) @@ -64,7 +62,7 @@ func (util AESUtils) AesEncrypt(origData []byte, key []byte) ([]byte, error) { } // 实现解密 -func (util AESUtils) AesDeCrypt(cypted []byte, key []byte) ([]byte, error) { +func AesDeCrypt(cypted []byte, key []byte) ([]byte, error) { // 创建加密算法实例 block, err := aes.NewCipher(key) if err != nil { @@ -78,7 +76,7 @@ func (util AESUtils) AesDeCrypt(cypted []byte, key []byte) ([]byte, error) { // 这个函数也可以用来解密 blockMode.CryptBlocks(origData, cypted) // 去除填充字符串 - origData, err = util.PKCS7UnPadding(origData) + origData, err = PKCS7UnPadding(origData) if err != nil { return nil, err } @@ -86,8 +84,8 @@ func (util AESUtils) AesDeCrypt(cypted []byte, key []byte) ([]byte, error) { } // 加密base64 -func (util AESUtils) EnPwdCode(pwd []byte) (string, error) { - result, err := util.AesEncrypt(pwd, []byte(config.AppProps.Jwt.Secret)) +func EnPwdCode(pwd []byte) (string, error) { + result, err := AesEncrypt(pwd, []byte(config.AppProps.Jwt.Secret)) if err != nil { return "", err } @@ -95,19 +93,19 @@ func (util AESUtils) EnPwdCode(pwd []byte) (string, error) { } // 解密 -func (util AESUtils) DePwdCode(pwd string) ([]byte, error) { +func DePwdCode(pwd string) ([]byte, error) { // 解密base64字符串 pwdByte, err := base64.StdEncoding.DecodeString(pwd) if err != nil { return nil, err } // 执行AES解密 - return util.AesDeCrypt(pwdByte, []byte(config.AppProps.Jwt.Secret)) + return AesDeCrypt(pwdByte, []byte(config.AppProps.Jwt.Secret)) } -func (util AESUtils) Test() { +func AesTest() { str := []byte("加密测试") - pwd, _ := util.EnPwdCode(str) - bt, _ := util.DePwdCode(pwd) + pwd, _ := EnPwdCode(str) + bt, _ := DePwdCode(pwd) fmt.Println(string(bt)) } diff --git a/src/util/raw_query_utils.go b/src/util/raw_query_utils.go new file mode 100644 index 0000000..f9a83ef --- /dev/null +++ b/src/util/raw_query_utils.go @@ -0,0 +1,17 @@ +package util + +import ( + "lingye-gin/src/config" +) + +func SelectOne(target interface{}, sql string, values ...interface{}) { + config.SqlExcutor.Raw(sql, values).Scan(target) +} + +func SelectList(target interface{}, sql string, values ...interface{}) { + config.SqlExcutor.Raw(sql, values).Scan(target) +} + +func Create() { + +}