main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. )
  103. if data["remark"] != nil {
  104. remark = data["remark"].(string)
  105. }
  106. sql += "INSERT INTO FUNCMENULIST (RESOURCECODE, RESOURCENAME, RESOURCELEVEL, MENUTYPE, PARENTCODE, URL, SORT, ICONAME, REMARK)\n"
  107. if parentCode == "" {
  108. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 3, NULL, '', %d, '', '%s');\n", data["code"].(string), data["title"].(string), level, i+1, remark)
  109. } else {
  110. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 3, '%s', '', %d, '', '%s');\n", data["code"].(string), data["title"].(string), level, parentCode, i+1, remark)
  111. }
  112. // 递归
  113. sub := data["children"].([]interface{})
  114. if len(sub) > 0 {
  115. sql += buildSQL(data["children"].([]interface{}), data["code"].(string), level+1)
  116. }
  117. if level == 1 {
  118. sql += "\n"
  119. }
  120. }
  121. return sql
  122. }
  123. // 构建PC WEB SQL
  124. func buildPCWEBSQL(datas []interface{}, parentCode string, level int) string {
  125. var sql string
  126. for i, v := range datas {
  127. data := v.(map[string]interface{})
  128. var (
  129. remark string
  130. )
  131. if data["remark"] != nil {
  132. remark = data["remark"].(string)
  133. }
  134. title := ""
  135. if data["title"] != nil {
  136. title = data["title"].(string)
  137. } else {
  138. title = data["label"].(string)
  139. }
  140. sql += "INSERT INTO FUNCMENULIST (RESOURCECODE, RESOURCENAME, RESOURCELEVEL, MENUTYPE, PARENTCODE, URL, SORT, ICONAME, REMARK)\n"
  141. if parentCode == "" {
  142. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 5, NULL, '', %d, '', '%s');\n", data["code"].(string), title, level, i+1, remark)
  143. } else {
  144. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 5, '%s', '', %d, '', '%s');\n", data["code"].(string), title, level, parentCode, i+1, remark)
  145. }
  146. // 递归
  147. if data["auth"] != nil {
  148. sql += buildPCWEBSQL(data["auth"].([]interface{}), data["code"].(string), level+1)
  149. }
  150. if data["children"] != nil {
  151. sql += buildPCWEBSQL(data["children"].([]interface{}), data["code"].(string), level+1)
  152. }
  153. if level == 1 {
  154. sql += "\n"
  155. }
  156. }
  157. return sql
  158. }