main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "time"
  8. )
  9. // ClientMenu 客户端菜单
  10. type ClientMenu struct {
  11. Code string `json:"code"` // ID
  12. Title string `json:"title"` // 标题
  13. Sort int `json:"sort"` // 排序
  14. Type int `json:"type"` // 类型,1:菜单 2:按钮
  15. RuleKey string `json:"rulekey"` // 对应权限主键,对应FUNCMENULIST表RESOURCECODE
  16. IsShow bool `json:"isshow"` // 是否显示
  17. URL string `json:"url"` // URL
  18. Remark string `json:"remark"` // 备注
  19. Children []ClientMenu `json:"children"` // 子菜单列表
  20. }
  21. // 主要用于风管云平台,将以PC菜单json文件来生成权限SQL权限脚本
  22. func main() {
  23. var jsonFile string
  24. // fmt.Println("请输入菜单JSON文件地址:")
  25. // fmt.Scanln(&jsonFile)
  26. // fmt.Println("正在处理,请稍后……")
  27. jsonFile = "D:\\pc_menu_pingan.json"
  28. // 读取Json文件
  29. filePtr, err := os.Open(jsonFile)
  30. if err != nil {
  31. // 读取文件失败
  32. defer filePtr.Close()
  33. fmt.Printf("文件读取失败,错误:%s", err.Error())
  34. return
  35. }
  36. defer filePtr.Close()
  37. // 创建Json编码器
  38. var datas []interface{}
  39. decoder := json.NewDecoder(filePtr)
  40. if err = decoder.Decode(&datas); err != nil {
  41. // Json解码失败
  42. fmt.Printf("Json解码失败,错误:%s", err.Error())
  43. return
  44. }
  45. // 构建菜单数组
  46. // clientMenus := buildMenu(datas)
  47. // 创建导出目录
  48. // err = os.MkdirAll("", os.ModePerm)
  49. // if err != nil {
  50. // fmt.Printf("创建目录失败,错误:%s", err.Error())
  51. // return
  52. // }
  53. s := "-- 企业风管权限控制\n"
  54. s += "-- 说明:MENUTYPE = 5 为企业风管PC WEB专用的终端权限\n"
  55. s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=5;\n\n"
  56. s += buildSQL(datas, "", 1)
  57. // 写入SQL文件
  58. outFile := fmt.Sprintf("out/%s%s", time.Now().Format("20060102150405"), ".sql")
  59. if ioutil.WriteFile(outFile, []byte(s), 0644) == nil {
  60. fmt.Println("写入文件成功")
  61. }
  62. }
  63. // buildMenu 创建菜单数据
  64. func buildMenu(datas []interface{}) []ClientMenu {
  65. clientMenus := make([]ClientMenu, 0)
  66. for _, v := range datas {
  67. data := v.(map[string]interface{})
  68. if data["isshow"].(bool) {
  69. item := ClientMenu{
  70. Code: data["code"].(string),
  71. Title: data["title"].(string),
  72. Sort: int(data["sort"].(float64)),
  73. Type: int(data["type"].(float64)),
  74. RuleKey: data["rulekey"].(string),
  75. IsShow: data["isshow"].(bool),
  76. Children: buildMenu(data["children"].([]interface{})),
  77. }
  78. if data["url"] != nil {
  79. item.URL = data["url"].(string)
  80. }
  81. if data["remark"] != nil {
  82. item.Remark = data["remark"].(string)
  83. }
  84. clientMenus = append(clientMenus, item)
  85. }
  86. }
  87. return clientMenus
  88. }
  89. func buildSQL(datas []interface{}, parentCode string, level int) string {
  90. var sql string
  91. for i, v := range datas {
  92. data := v.(map[string]interface{})
  93. var (
  94. remark string
  95. )
  96. if data["remark"] != nil {
  97. remark = data["remark"].(string)
  98. }
  99. sql += "INSERT INTO FUNCMENULIST (RESOURCECODE, RESOURCENAME, RESOURCELEVEL, MENUTYPE, PARENTCODE, URL, SORT, ICONAME, REMARK)\n"
  100. if parentCode == "" {
  101. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 5, NULL, '', %d, '', '%s');\n", data["code"].(string), data["title"].(string), level, i+1, remark)
  102. } else {
  103. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 5, '%s', '', %d, '', '%s');\n", data["code"].(string), data["title"].(string), level, parentCode, i+1, remark)
  104. }
  105. // 递归
  106. sub := data["children"].([]interface{})
  107. if len(sub) > 0 {
  108. sql += buildSQL(data["children"].([]interface{}), data["code"].(string), level+1)
  109. }
  110. if level == 1 {
  111. sql += "\n"
  112. }
  113. }
  114. return sql
  115. }