main.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strconv"
  8. "time"
  9. )
  10. // ClientMenu 客户端菜单
  11. type ClientMenu struct {
  12. Code string `json:"code"` // ID
  13. Title string `json:"title"` // 标题
  14. Sort int `json:"sort"` // 排序
  15. Type int `json:"type"` // 类型,1:菜单 2:按钮
  16. RuleKey string `json:"rulekey"` // 对应权限主键,对应FUNCMENULIST表RESOURCECODE
  17. IsShow bool `json:"isshow"` // 是否显示
  18. URL string `json:"url"` // URL
  19. Remark string `json:"remark"` // 备注
  20. Children []ClientMenu `json:"children"` // 子菜单列表
  21. }
  22. // 主要用于风管云平台,将以PC菜单json文件来生成权限SQL权限脚本
  23. func main() {
  24. var (
  25. jsonFile string // JSON文件地址
  26. clientType string // 终端类型: 1-PCWEB 2-手机
  27. )
  28. fmt.Println("请输入终端类型: (3-手机 4-PC大宗 5-PC风管)")
  29. fmt.Scanln(&clientType)
  30. fmt.Println("请输入菜单JSON文件地址:")
  31. fmt.Scanln(&jsonFile)
  32. fmt.Println("正在处理,请稍等……")
  33. // 读取Json文件
  34. filePtr, err := os.Open(jsonFile)
  35. if err != nil {
  36. // 读取文件失败
  37. defer filePtr.Close()
  38. fmt.Printf("文件读取失败,错误:%s", err.Error())
  39. return
  40. }
  41. defer filePtr.Close()
  42. // 创建Json编码器
  43. var datas []interface{}
  44. decoder := json.NewDecoder(filePtr)
  45. if err = decoder.Decode(&datas); err != nil {
  46. // Json解码失败
  47. fmt.Printf("Json解码失败,错误:%s", err.Error())
  48. return
  49. }
  50. s := "-- 权限控制\n"
  51. if clientType == "4" {
  52. s += "-- 说明:MENUTYPE = 4 为PC大宗专用的终端权限\n"
  53. s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=4;\n\n"
  54. } else if clientType == "5" {
  55. s += "-- 说明:MENUTYPE = 5 为PC风管专用的终端权限\n"
  56. s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=5;\n\n"
  57. } else if clientType == "3" {
  58. s += "-- 说明:MENUTYPE = 3 为手机专用的终端权限\n"
  59. s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=3;\n\n"
  60. } else {
  61. fmt.Printf("错误的终端类型。")
  62. return
  63. }
  64. t, _ := strconv.Atoi(clientType)
  65. if clientType == "4" || clientType == "5" {
  66. s += buildPCWEBSQL(datas, "", 1, t)
  67. } else {
  68. s += buildSQL(datas, "", 1)
  69. }
  70. // 判断out文件夹是否存在
  71. isExists, err := PathExists("out")
  72. if !isExists || err != nil {
  73. os.Mkdir("out", os.ModePerm)
  74. }
  75. // 写入SQL文件
  76. outFile := fmt.Sprintf("out/%s%s", time.Now().Format("20060102150405"), ".sql")
  77. if ioutil.WriteFile(outFile, []byte(s), 0644) == nil {
  78. fmt.Printf("写入文件成功: %s\n", outFile)
  79. }
  80. }
  81. func PathExists(path string) (bool, error) {
  82. _, err := os.Stat(path)
  83. if err == nil {
  84. return true, nil
  85. }
  86. if os.IsNotExist(err) {
  87. return false, nil
  88. }
  89. return false, err
  90. }
  91. // buildSQL 构建手机SQL
  92. func buildSQL(datas []interface{}, parentCode string, level int) string {
  93. var sql string
  94. for i, v := range datas {
  95. data := v.(map[string]interface{})
  96. var (
  97. remark string
  98. )
  99. rulekey, hasRuleKey := data["rulekey"]
  100. if hasRuleKey && rulekey != "" {
  101. rulekey = data["rulekey"].(string)
  102. if data["remark"] != nil {
  103. remark = data["remark"].(string)
  104. }
  105. sql += "INSERT INTO FUNCMENULIST (RESOURCECODE, RESOURCENAME, RESOURCELEVEL, MENUTYPE, PARENTCODE, URL, SORT, ICONAME, REMARK)\n"
  106. if parentCode == "" {
  107. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 3, NULL, '', %d, '', '%s');\n", rulekey, data["title"].(string), level, i+1, remark)
  108. } else {
  109. sql += fmt.Sprintf("VALUES('%s', '%s', %d, 3, '%s', '', %d, '', '%s');\n", rulekey, data["title"].(string), level, parentCode, i+1, remark)
  110. }
  111. // 递归
  112. sub := data["children"].([]interface{})
  113. if len(sub) > 0 {
  114. sql += buildSQL(data["children"].([]interface{}), data["rulekey"].(string), level+1)
  115. }
  116. if level == 1 {
  117. sql += "\n"
  118. }
  119. }
  120. }
  121. return sql
  122. }
  123. // 构建PC WEB SQL
  124. func buildPCWEBSQL(datas []interface{}, parentCode string, level int, clientType 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. rulekey, hasRuleKey := data["rulekey"]
  132. isshow := false
  133. if _, hasIsShow := data["isshow"]; hasIsShow {
  134. isshow = data["isshow"].(bool)
  135. }
  136. if (hasRuleKey && rulekey != "") && isshow {
  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, %d, NULL, '', %d, '', '%s');\n", rulekey, title, level, clientType, i+1, remark)
  150. } else {
  151. sql += fmt.Sprintf("VALUES('%s', '%s', %d, %d, '%s', '', %d, '', '%s');\n", rulekey, title, level, clientType, parentCode, i+1, remark)
  152. }
  153. // 递归
  154. if data["auth"] != nil {
  155. sql += buildPCWEBSQL(data["auth"].([]interface{}), data["rulekey"].(string), level+1, clientType)
  156. }
  157. if data["children"] != nil {
  158. sql += buildPCWEBSQL(data["children"].([]interface{}), data["rulekey"].(string), level+1, clientType)
  159. }
  160. if level == 1 {
  161. sql += "\n"
  162. }
  163. }
  164. }
  165. return sql
  166. }