zhou.xiaoning 4 лет назад
Родитель
Сommit
432bfe5b68
4 измененных файлов с 156 добавлено и 0 удалено
  1. 1 0
      .gitignore
  2. 17 0
      permission_generator/.vscode/launch.json
  3. 3 0
      permission_generator/go.mod
  4. 135 0
      permission_generator/main.go

+ 1 - 0
.gitignore

@@ -24,3 +24,4 @@ _testmain.go
 *.test
 *.prof
 
+permission_generator/out

+ 17 - 0
permission_generator/.vscode/launch.json

@@ -0,0 +1,17 @@
+{
+    // 使用 IntelliSense 了解相关属性。 
+    // 悬停以查看现有属性的描述。
+    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "Launch",
+            "type": "go",
+            "request": "launch",
+            "mode": "auto",
+            "program": "${workspaceFolder}/",
+            "env": {},
+            "args": []
+        }
+    ]
+}

+ 3 - 0
permission_generator/go.mod

@@ -0,0 +1,3 @@
+module permission_generator
+
+go 1.17

+ 135 - 0
permission_generator/main.go

@@ -0,0 +1,135 @@
+package main
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"os"
+	"time"
+)
+
+// ClientMenu 客户端菜单
+type ClientMenu struct {
+	Code     string       `json:"code"`     // ID
+	Title    string       `json:"title"`    // 标题
+	Sort     int          `json:"sort"`     // 排序
+	Type     int          `json:"type"`     // 类型,1:菜单 2:按钮
+	RuleKey  string       `json:"rulekey"`  // 对应权限主键,对应FUNCMENULIST表RESOURCECODE
+	IsShow   bool         `json:"isshow"`   // 是否显示
+	URL      string       `json:"url"`      // URL
+	Remark   string       `json:"remark"`   // 备注
+	Children []ClientMenu `json:"children"` // 子菜单列表
+}
+
+// 主要用于风管云平台,将以PC菜单json文件来生成权限SQL权限脚本
+func main() {
+	var jsonFile string
+
+	// fmt.Println("请输入菜单JSON文件地址:")
+	// fmt.Scanln(&jsonFile)
+	// fmt.Println("正在处理,请稍后……")
+
+	jsonFile = "D:\\pc_menu_pingan.json"
+
+	// 读取Json文件
+	filePtr, err := os.Open(jsonFile)
+	if err != nil {
+		// 读取文件失败
+		defer filePtr.Close()
+		fmt.Printf("文件读取失败,错误:%s", err.Error())
+		return
+	}
+	defer filePtr.Close()
+
+	// 创建Json编码器
+	var datas []interface{}
+	decoder := json.NewDecoder(filePtr)
+	if err = decoder.Decode(&datas); err != nil {
+		// Json解码失败
+		fmt.Printf("Json解码失败,错误:%s", err.Error())
+		return
+	}
+
+	// 构建菜单数组
+	// clientMenus := buildMenu(datas)
+
+	// 创建导出目录
+	// err = os.MkdirAll("", os.ModePerm)
+	// if err != nil {
+	// 	fmt.Printf("创建目录失败,错误:%s", err.Error())
+	// 	return
+	// }
+
+	s := "-- 企业风管权限控制\n"
+	s += "-- 说明:MENUTYPE = 5 为企业风管PC WEB专用的终端权限\n"
+	s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=5;\n\n"
+	s += buildSQL(datas, "", 1)
+
+	// 写入SQL文件
+	outFile := fmt.Sprintf("out/%s%s", time.Now().Format("20060102150405"), ".sql")
+	if ioutil.WriteFile(outFile, []byte(s), 0644) == nil {
+		fmt.Println("写入文件成功")
+	}
+}
+
+// buildMenu 创建菜单数据
+func buildMenu(datas []interface{}) []ClientMenu {
+	clientMenus := make([]ClientMenu, 0)
+	for _, v := range datas {
+		data := v.(map[string]interface{})
+		if data["isshow"].(bool) {
+			item := ClientMenu{
+				Code:     data["code"].(string),
+				Title:    data["title"].(string),
+				Sort:     int(data["sort"].(float64)),
+				Type:     int(data["type"].(float64)),
+				RuleKey:  data["rulekey"].(string),
+				IsShow:   data["isshow"].(bool),
+				Children: buildMenu(data["children"].([]interface{})),
+			}
+			if data["url"] != nil {
+				item.URL = data["url"].(string)
+			}
+			if data["remark"] != nil {
+				item.Remark = data["remark"].(string)
+			}
+			clientMenus = append(clientMenus, item)
+		}
+	}
+
+	return clientMenus
+}
+
+func buildSQL(datas []interface{}, parentCode string, level int) string {
+	var sql string
+
+	for i, v := range datas {
+		data := v.(map[string]interface{})
+		var (
+			remark string
+		)
+
+		if data["remark"] != nil {
+			remark = data["remark"].(string)
+		}
+
+		sql += "INSERT INTO FUNCMENULIST (RESOURCECODE, RESOURCENAME, RESOURCELEVEL, MENUTYPE, PARENTCODE, URL, SORT, ICONAME, REMARK)\n"
+		if parentCode == "" {
+			sql += fmt.Sprintf("VALUES('%s', '%s', %d, 5, NULL, '', %d, '', '%s');\n", data["code"].(string), data["title"].(string), level, i+1, remark)
+		} else {
+			sql += fmt.Sprintf("VALUES('%s', '%s', %d, 5, '%s', '', %d, '', '%s');\n", data["code"].(string), data["title"].(string), level, parentCode, i+1, remark)
+		}
+
+		// 递归
+		sub := data["children"].([]interface{})
+		if len(sub) > 0 {
+			sql += buildSQL(data["children"].([]interface{}), data["code"].(string), level+1)
+		}
+
+		if level == 1 {
+			sql += "\n"
+		}
+	}
+
+	return sql
+}