package permisgen import ( "context" "errors" "fmt" "strconv" "strings" "unicode/utf8" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/os/gfile" "github.com/xuri/excelize/v2" ) type PermisFiled struct { Id int // id Name string `json:"name"` // 权限名称 Code string `json:"code"` // 权限唯一编码 Path string `json:"path"` // 前端路径 ParentCode string `json:"parentCode"` // 父code Type int `json:"type"` // 1-菜单权限;2-功能权限;3-数据权限 Sort int `json:"sort"` // 排序号 SystemType int `json:"systemType"` // 1-云帆管理平台;2-机构数字病例库 MenuCategory string `json:"menuCategory"` // 菜单分类 } type RolesFiled struct { Name string `json:"name"` // 角色名称 PermisCodes []string `json:"permisIds"` // id PermisCodesStr string `json:"permisIdsStr"` // id DataPermisCode string `json:"dataPermisCode"` // id SystemType int `json:"systemType"` // 1-云帆管理平台;2-机构数字病例库 } type permisGen struct { FileNames []string rolesFiled []RolesFiled } func NewPermisGen(fileNames []string) *permisGen { return &permisGen{ FileNames: fileNames, } } func (p *permisGen) Run() error { if len(p.FileNames) == 0 { fmt.Println("file not found") return nil } for _, fileName := range p.FileNames { arr := strings.Split(fileName, "-") systemType, err := strconv.Atoi(arr[0]) if err != nil { return errors.New("文件名定义错误,文件名格式必须是'系统类型-文件名'") } res, err := p.getPermisFileds(fileName, systemType) if err != nil { return err } p.permisDataGen(res, systemType) p.roleDataGen(systemType) } return nil } var SYSTEM_ID_INTERVAL = 10000 func (p *permisGen) getPermisFileds(file string, systemType int) ([]PermisFiled, error) { f, err := excelize.OpenFile(file) if err != nil { fmt.Println(err) return nil, err } defer func() { // 关闭文件 if err := f.Close(); err != nil { fmt.Println(err) } }() // 从sheet中获取行数据 rows, err := f.GetRows("Sheet1") if err != nil { return nil, err } res := make([]PermisFiled, 0) menuCodeMap := make(map[string]string) parentName := "" sortNum := 1 roleIndexMap := make(map[int]string) rolePermisMap := make(map[string][]string) for i, row := range rows { if i == 0 { for j := 5; j < len(row); j++ { roleIndexMap[j] = row[j] } continue } if len(row) < 5 { continue } if len(strings.TrimSpace(row[0])) > 0 { parentName = strings.TrimSpace(row[0]) sortNum = 1 } name := strings.TrimSpace(row[1]) menuCategory := strings.TrimSpace(row[2]) code := strings.TrimSpace(row[3]) path := strings.TrimSpace(row[4]) sortN, _ := strconv.Atoi(strings.TrimSpace(row[5])) if sortN > 0 { sortNum = sortN } if len(name) > 0 { permisType := 2 if strings.HasPrefix(code, "menu") { permisType = 1 menuCodeMap[name] = code } else if strings.HasPrefix(code, "data") { permisType = 3 } filed := PermisFiled{ Name: name, Code: code, Path: path, ParentCode: menuCodeMap[parentName], Type: permisType, SystemType: systemType, Sort: sortNum, MenuCategory: menuCategory, } res = append(res, filed) // 角色拥有的权限 for k, v := range roleIndexMap { if len(row) > k && strings.TrimSpace(row[k]) == "Y" { permisCodes := rolePermisMap[v] if permisCodes == nil { permisCodes = make([]string, 0) } permisCodes = append(permisCodes, code) rolePermisMap[v] = permisCodes } } sortNum++ } } rolesFiled := make([]RolesFiled, 0) for k, v := range rolePermisMap { permisCodesStr := "[" dataPermisCode := "" for _, code := range v { if strings.HasPrefix(code, "data") { dataPermisCode = code } else { permisCodesStr += "\"" + code + "\"," } } if utf8.RuneCountInString(permisCodesStr) > 1 { permisCodesStr = permisCodesStr[:len(permisCodesStr)-1] } permisCodesStr += "]" filed := RolesFiled{ Name: k, PermisCodes: v, PermisCodesStr: permisCodesStr, DataPermisCode: dataPermisCode, SystemType: systemType, } rolesFiled = append(rolesFiled, filed) } p.rolesFiled = rolesFiled return res, nil } func (p *permisGen) permisDataGen(permis []PermisFiled, systemType int) (err error) { if len(permis) == 0 { return } permisContent, err := g.View().Parse(context.Background(), "permis.template", map[string]interface{}{ "param": permis, }) path := "./" + strconv.Itoa(systemType) + "-permis_data.sql" if err := gfile.PutContents(path, strings.TrimSpace(permisContent)); err != nil { fmt.Printf("writing permis content to '%s' failed: %v", path, err) } return } func (p *permisGen) roleDataGen(systemType int) (err error) { if len(p.rolesFiled) == 0 { return } roleContent, err := g.View().Parse(context.Background(), "role.template", map[string]interface{}{ "param": p.rolesFiled, }) path := "./" + strconv.Itoa(systemType) + "-role_data.sql" if err := gfile.PutContents(path, strings.TrimSpace(roleContent)); err != nil { fmt.Printf("writing permis content to '%s' failed: %v", path, err) } return }