| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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
- }
|