permis_gen.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package permisgen
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "unicode/utf8"
  9. "github.com/gogf/gf/frame/g"
  10. "github.com/gogf/gf/os/gfile"
  11. "github.com/xuri/excelize/v2"
  12. )
  13. type PermisFiled struct {
  14. Id int // id
  15. Name string `json:"name"` // 权限名称
  16. Code string `json:"code"` // 权限唯一编码
  17. Path string `json:"path"` // 前端路径
  18. ParentCode string `json:"parentCode"` // 父code
  19. Type int `json:"type"` // 1-菜单权限;2-功能权限;3-数据权限
  20. Sort int `json:"sort"` // 排序号
  21. SystemType int `json:"systemType"` // 1-云帆管理平台;2-机构数字病例库
  22. MenuCategory string `json:"menuCategory"` // 菜单分类
  23. }
  24. type RolesFiled struct {
  25. Name string `json:"name"` // 角色名称
  26. PermisCodes []string `json:"permisIds"` // id
  27. PermisCodesStr string `json:"permisIdsStr"` // id
  28. DataPermisCode string `json:"dataPermisCode"` // id
  29. SystemType int `json:"systemType"` // 1-云帆管理平台;2-机构数字病例库
  30. }
  31. type permisGen struct {
  32. FileNames []string
  33. rolesFiled []RolesFiled
  34. }
  35. func NewPermisGen(fileNames []string) *permisGen {
  36. return &permisGen{
  37. FileNames: fileNames,
  38. }
  39. }
  40. func (p *permisGen) Run() error {
  41. if len(p.FileNames) == 0 {
  42. fmt.Println("file not found")
  43. return nil
  44. }
  45. for _, fileName := range p.FileNames {
  46. arr := strings.Split(fileName, "-")
  47. systemType, err := strconv.Atoi(arr[0])
  48. if err != nil {
  49. return errors.New("文件名定义错误,文件名格式必须是'系统类型-文件名'")
  50. }
  51. res, err := p.getPermisFileds(fileName, systemType)
  52. if err != nil {
  53. return err
  54. }
  55. p.permisDataGen(res, systemType)
  56. p.roleDataGen(systemType)
  57. }
  58. return nil
  59. }
  60. var SYSTEM_ID_INTERVAL = 10000
  61. func (p *permisGen) getPermisFileds(file string, systemType int) ([]PermisFiled, error) {
  62. f, err := excelize.OpenFile(file)
  63. if err != nil {
  64. fmt.Println(err)
  65. return nil, err
  66. }
  67. defer func() {
  68. // 关闭文件
  69. if err := f.Close(); err != nil {
  70. fmt.Println(err)
  71. }
  72. }()
  73. // 从sheet中获取行数据
  74. rows, err := f.GetRows("Sheet1")
  75. if err != nil {
  76. return nil, err
  77. }
  78. res := make([]PermisFiled, 0)
  79. menuCodeMap := make(map[string]string)
  80. parentName := ""
  81. sortNum := 1
  82. roleIndexMap := make(map[int]string)
  83. rolePermisMap := make(map[string][]string)
  84. for i, row := range rows {
  85. if i == 0 {
  86. for j := 5; j < len(row); j++ {
  87. roleIndexMap[j] = row[j]
  88. }
  89. continue
  90. }
  91. if len(row) < 5 {
  92. continue
  93. }
  94. if len(strings.TrimSpace(row[0])) > 0 {
  95. parentName = strings.TrimSpace(row[0])
  96. sortNum = 1
  97. }
  98. name := strings.TrimSpace(row[1])
  99. menuCategory := strings.TrimSpace(row[2])
  100. code := strings.TrimSpace(row[3])
  101. path := strings.TrimSpace(row[4])
  102. sortN, _ := strconv.Atoi(strings.TrimSpace(row[5]))
  103. if sortN > 0 {
  104. sortNum = sortN
  105. }
  106. if len(name) > 0 {
  107. permisType := 2
  108. if strings.HasPrefix(code, "menu") {
  109. permisType = 1
  110. menuCodeMap[name] = code
  111. } else if strings.HasPrefix(code, "data") {
  112. permisType = 3
  113. }
  114. filed := PermisFiled{
  115. Name: name,
  116. Code: code,
  117. Path: path,
  118. ParentCode: menuCodeMap[parentName],
  119. Type: permisType,
  120. SystemType: systemType,
  121. Sort: sortNum,
  122. MenuCategory: menuCategory,
  123. }
  124. res = append(res, filed)
  125. // 角色拥有的权限
  126. for k, v := range roleIndexMap {
  127. if len(row) > k && strings.TrimSpace(row[k]) == "Y" {
  128. permisCodes := rolePermisMap[v]
  129. if permisCodes == nil {
  130. permisCodes = make([]string, 0)
  131. }
  132. permisCodes = append(permisCodes, code)
  133. rolePermisMap[v] = permisCodes
  134. }
  135. }
  136. sortNum++
  137. }
  138. }
  139. rolesFiled := make([]RolesFiled, 0)
  140. for k, v := range rolePermisMap {
  141. permisCodesStr := "["
  142. dataPermisCode := ""
  143. for _, code := range v {
  144. if strings.HasPrefix(code, "data") {
  145. dataPermisCode = code
  146. } else {
  147. permisCodesStr += "\"" + code + "\","
  148. }
  149. }
  150. if utf8.RuneCountInString(permisCodesStr) > 1 {
  151. permisCodesStr = permisCodesStr[:len(permisCodesStr)-1]
  152. }
  153. permisCodesStr += "]"
  154. filed := RolesFiled{
  155. Name: k,
  156. PermisCodes: v,
  157. PermisCodesStr: permisCodesStr,
  158. DataPermisCode: dataPermisCode,
  159. SystemType: systemType,
  160. }
  161. rolesFiled = append(rolesFiled, filed)
  162. }
  163. p.rolesFiled = rolesFiled
  164. return res, nil
  165. }
  166. func (p *permisGen) permisDataGen(permis []PermisFiled, systemType int) (err error) {
  167. if len(permis) == 0 {
  168. return
  169. }
  170. permisContent, err := g.View().Parse(context.Background(), "permis.template", map[string]interface{}{
  171. "param": permis,
  172. })
  173. path := "./" + strconv.Itoa(systemType) + "-permis_data.sql"
  174. if err := gfile.PutContents(path, strings.TrimSpace(permisContent)); err != nil {
  175. fmt.Printf("writing permis content to '%s' failed: %v", path, err)
  176. }
  177. return
  178. }
  179. func (p *permisGen) roleDataGen(systemType int) (err error) {
  180. if len(p.rolesFiled) == 0 {
  181. return
  182. }
  183. roleContent, err := g.View().Parse(context.Background(), "role.template", map[string]interface{}{
  184. "param": p.rolesFiled,
  185. })
  186. path := "./" + strconv.Itoa(systemType) + "-role_data.sql"
  187. if err := gfile.PutContents(path, strings.TrimSpace(roleContent)); err != nil {
  188. fmt.Printf("writing permis content to '%s' failed: %v", path, err)
  189. }
  190. return
  191. }