zhou.xiaoning 4 年之前
父節點
當前提交
702807e65d
共有 3 個文件被更改,包括 95 次插入36 次删除
  1. 4 0
      permission_generator/build_macos.bat
  2. 1 0
      permission_generator/build_win.bat
  3. 90 36
      permission_generator/main.go

+ 4 - 0
permission_generator/build_macos.bat

@@ -0,0 +1,4 @@
+SET CGO_ENABLED=0
+SET GOOS=darwin
+SET GOARCH=amd64
+go build -o buildsql

+ 1 - 0
permission_generator/build_win.bat

@@ -0,0 +1 @@
+go build -o buildsql.exe

+ 90 - 36
permission_generator/main.go

@@ -23,13 +23,23 @@ type ClientMenu struct {
 
 // 主要用于风管云平台,将以PC菜单json文件来生成权限SQL权限脚本
 func main() {
-	var jsonFile string
+	var (
+		jsonFile   string // JSON文件地址
+		clientType string // 终端类型: 1-PCWEB 2-手机
+	)
 
-	// fmt.Println("请输入菜单JSON文件地址:")
-	// fmt.Scanln(&jsonFile)
-	// fmt.Println("正在处理,请稍后……")
+	fmt.Println("请输入终端类型: (1-PCWEB 2-手机)")
+	fmt.Scanln(&clientType)
 
-	jsonFile = "D:\\pc_menu_pingan.json"
+	fmt.Println("请输入菜单JSON文件地址:")
+	fmt.Scanln(&jsonFile)
+	fmt.Println("正在处理,请稍等……")
+
+	//jsonFile = "D:\\pc_menu_pingan.json"
+	// jsonFile = "D:\\pc_menu_ermcp.json"
+
+	// clientType = "1"
+	// jsonFile = "D:\\mobile_menu.json"
 
 	// 读取Json文件
 	filePtr, err := os.Open(jsonFile)
@@ -61,45 +71,45 @@ func main() {
 	// }
 
 	s := "-- 企业风管权限控制\n"
-	s += "-- 说明:MENUTYPE = 5 为企业风管PC WEB专用的终端权限\n"
-	s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=5;\n\n"
-	s += buildSQL(datas, "", 1)
+	if clientType == "1" {
+		s += "-- 说明:MENUTYPE = 5 为企业风管PC WEB专用的终端权限\n"
+		s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=5;\n\n"
+	} else {
+		s += "-- 说明:MENUTYPE = 3 为企业风管手机专用的终端权限\n"
+		s += "DELETE FROM FUNCMENULIST WHERE MENUTYPE=3;\n\n"
+	}
+
+	if clientType == "1" {
+		s += buildPCWEBSQL(datas, "", 1)
+	} else {
+		s += buildSQL(datas, "", 1)
+	}
+
+	// 判断out文件夹是否存在
+	isExists, err := PathExists("out")
+	if !isExists || err != nil {
+		os.Mkdir("out", os.ModePerm)
+	}
 
 	// 写入SQL文件
 	outFile := fmt.Sprintf("out/%s%s", time.Now().Format("20060102150405"), ".sql")
 	if ioutil.WriteFile(outFile, []byte(s), 0644) == nil {
-		fmt.Println("写入文件成功")
+		fmt.Printf("写入文件成功: %s\n", outFile)
 	}
 }
 
-// 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)
-		}
+func PathExists(path string) (bool, error) {
+	_, err := os.Stat(path)
+	if err == nil {
+		return true, nil
 	}
-
-	return clientMenus
+	if os.IsNotExist(err) {
+		return false, nil
+	}
+	return false, err
 }
 
+// buildSQL 构建手机SQL
 func buildSQL(datas []interface{}, parentCode string, level int) string {
 	var sql string
 
@@ -115,9 +125,9 @@ func buildSQL(datas []interface{}, parentCode string, level int) 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)
+			sql += fmt.Sprintf("VALUES('%s', '%s', %d, 3, 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)
+			sql += fmt.Sprintf("VALUES('%s', '%s', %d, 3, '%s', '', %d, '', '%s');\n", data["code"].(string), data["title"].(string), level, parentCode, i+1, remark)
 		}
 
 		// 递归
@@ -133,3 +143,47 @@ func buildSQL(datas []interface{}, parentCode string, level int) string {
 
 	return sql
 }
+
+// 构建PC WEB SQL
+func buildPCWEBSQL(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)
+		}
+
+		title := ""
+		if data["title"] != nil {
+			title = data["title"].(string)
+		} else {
+			title = data["label"].(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), title, level, i+1, remark)
+		} else {
+			sql += fmt.Sprintf("VALUES('%s', '%s', %d, 5, '%s', '', %d, '', '%s');\n", data["code"].(string), title, level, parentCode, i+1, remark)
+		}
+
+		// 递归
+		if data["auth"] != nil {
+			sql += buildPCWEBSQL(data["auth"].([]interface{}), data["code"].(string), level+1)
+		}
+		if data["children"] != nil {
+			sql += buildPCWEBSQL(data["children"].([]interface{}), data["code"].(string), level+1)
+		}
+
+		if level == 1 {
+			sql += "\n"
+		}
+	}
+
+	return sql
+}