main.go 5.1 KB

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