main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 (
  24. jsonFile string // JSON文件地址
  25. clientType string // 终端类型: 1-PCWEB 2-手机
  26. )
  27. fmt.Println("请输入终端类型: (1-PCWEB 2-手机)")
  28. fmt.Scanln(&clientType)
  29. fmt.Println("请输入菜单JSON文件地址:")
  30. fmt.Scanln(&jsonFile)
  31. fmt.Println("正在处理,请稍等……")
  32. // 读取Json文件
  33. filePtr, err := os.Open(jsonFile)
  34. if err != nil {
  35. // 读取文件失败
  36. defer filePtr.Close()
  37. fmt.Printf("文件读取失败,错误:%s", err.Error())
  38. return
  39. }
  40. defer filePtr.Close()
  41. // 创建Json编码器
  42. var datas []interface{}
  43. decoder := json.NewDecoder(filePtr)
  44. if err = decoder.Decode(&datas); err != nil {
  45. // Json解码失败
  46. fmt.Printf("Json解码失败,错误:%s", err.Error())
  47. return
  48. }
  49. s := "-- 企业风管权限控制\n"
  50. if clientType == "1" {
  51. s += "-- 说明:MENUTYPE = 5 为企业风管PC WEB专用的终端权限\n"
  52. s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=5;\n\n"
  53. } else {
  54. s += "-- 说明:MENUTYPE = 3 为企业风管手机专用的终端权限\n"
  55. s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=3;\n\n"
  56. }
  57. if clientType == "1" {
  58. s += buildPCWEBSQL(datas, "", 1)
  59. } else {
  60. s += buildSQL(datas, "", 1)
  61. }
  62. // 判断out文件夹是否存在
  63. isExists, err := PathExists("out")
  64. if !isExists || err != nil {
  65. os.Mkdir("out", os.ModePerm)
  66. }
  67. // 写入SQL文件
  68. outFile := fmt.Sprintf("out/%s%s", time.Now().Format("20060102150405"), ".sql")
  69. if ioutil.WriteFile(outFile, []byte(s), 0644) == nil {
  70. fmt.Printf("写入文件成功: %s\n", outFile)
  71. }
  72. }
  73. func PathExists(path string) (bool, error) {
  74. _, err := os.Stat(path)
  75. if err == nil {
  76. return true, nil
  77. }
  78. if os.IsNotExist(err) {
  79. return false, nil
  80. }
  81. return false, err
  82. }
  83. // buildSQL 构建手机SQL
  84. func buildSQL(datas []interface{}, parentCode string, level int) string {
  85. var sql string
  86. for i, v := range datas {
  87. data := v.(map[string]interface{})
  88. var (
  89. remark string
  90. )
  91. rulekey, hasRuleKey := data["rulekey"]
  92. if hasRuleKey && rulekey != "" {
  93. rulekey = data["rulekey"].(string)
  94. if data["remark"] != nil {
  95. remark = data["remark"].(string)
  96. }
  97. sql += "INSERT INTO FUNCMENULIST (RESOURCECODE, RESOURCENAME, RESOURCELEVEL, MENUTYPE, PARENTCODE, URL, SORT, ICONAME, REMARK)\n"
  98. if parentCode == "" {
  99. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 3, NULL, '', %d, '', '%s');\n", rulekey, data["title"].(string), level, i+1, remark)
  100. } else {
  101. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 3, '%s', '', %d, '', '%s');\n", rulekey, data["title"].(string), level, parentCode, i+1, remark)
  102. }
  103. // 递归
  104. sub := data["children"].([]interface{})
  105. if len(sub) > 0 {
  106. sql += buildSQL(data["children"].([]interface{}), data["rulekey"].(string), level+1)
  107. }
  108. if level == 1 {
  109. sql += "\n"
  110. }
  111. }
  112. }
  113. return sql
  114. }
  115. // 构建PC WEB SQL
  116. func buildPCWEBSQL(datas []interface{}, parentCode string, level int) string {
  117. var sql string
  118. for i, v := range datas {
  119. data := v.(map[string]interface{})
  120. var (
  121. remark string
  122. )
  123. rulekey, hasRuleKey := data["rulekey"]
  124. if hasRuleKey && rulekey != "" {
  125. rulekey = data["rulekey"].(string)
  126. if data["remark"] != nil {
  127. remark = data["remark"].(string)
  128. }
  129. title := ""
  130. if data["title"] != nil {
  131. title = data["title"].(string)
  132. } else {
  133. title = data["label"].(string)
  134. }
  135. sql += "INSERT INTO FUNCMENULIST (RESOURCECODE, RESOURCENAME, RESOURCELEVEL, MENUTYPE, PARENTCODE, URL, SORT, ICONAME, REMARK)\n"
  136. if parentCode == "" {
  137. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 5, NULL, '', %d, '', '%s');\n", rulekey, title, level, i+1, remark)
  138. } else {
  139. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 5, '%s', '', %d, '', '%s');\n", rulekey, title, level, parentCode, i+1, remark)
  140. }
  141. // 递归
  142. if data["auth"] != nil {
  143. sql += buildPCWEBSQL(data["auth"].([]interface{}), data["rulekey"].(string), level+1)
  144. }
  145. if data["children"] != nil {
  146. sql += buildPCWEBSQL(data["children"].([]interface{}), data["rulekey"].(string), level+1)
  147. }
  148. if level == 1 {
  149. sql += "\n"
  150. }
  151. }
  152. }
  153. return sql
  154. }