menu.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package common
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "mtp2_if/global/app"
  6. "mtp2_if/global/e"
  7. "mtp2_if/logger"
  8. "mtp2_if/models"
  9. "net/http"
  10. "os"
  11. "github.com/gin-gonic/gin"
  12. )
  13. // ClientMenu 客户端菜单
  14. type ClientMenu struct {
  15. Code string `json:"code"` // ID
  16. Title string `json:"title"` // 标题
  17. Sort int `json:"sort"` // 排序
  18. Type int `json:"type"` // 类型,1:菜单 2:按钮
  19. RuleKey string `json:"rulekey"` // 对应权限主键,对应FUNCMENULIST表RESOURCECODE
  20. IsShow bool `json:"isshow"` // 是否显示
  21. URL string `json:"url"` // URL
  22. Remark string `json:"remark"` // 备注
  23. Children []ClientMenu `json:"children"` // 子菜单列表
  24. }
  25. // PCWebMenuAuth PC客户端页面(按钮)权限
  26. type PCWebMenuAuth struct {
  27. Label string `json:"label"` // 标题
  28. Code string `json:"code"` // ID,新版本中权限KEY也是这个字段
  29. Rulekey string `json:"rulekey"` // 权限主键,非空表示需要判断控制,非空值不可重复
  30. IsShow bool `json:"isshow"` // 是否显示
  31. Remark string `json:"remark"` // 备注
  32. }
  33. // PCWebMenu PC客户端菜单权限
  34. type PCWebMenu struct {
  35. Title string `json:"title"` // 标题
  36. Code string `json:"code"` // ID,新版本中权限KEY也是这个字段
  37. Path string `json:"path"` // 路由路径
  38. Component string `json:"component"` // 路由组件
  39. Sort int `json:"sort"` // 排序
  40. Rulekey string `json:"rulekey"` // 权限主键,非空表示需要判断控制,非空值不可重复
  41. IsShow bool `json:"isshow"` // 是否显示
  42. URL string `json:"url"` // URL
  43. Remark string `json:"remark"` // 备注
  44. Children []PCWebMenu `json:"children"` // 子菜单列表
  45. Auth []PCWebMenuAuth `json:"auth"` // 页面权限列表
  46. }
  47. // GetClientMenusReq 获取交易端菜单请求参数
  48. type GetClientMenusReq struct {
  49. LoginID int `form:"loginID" binding:"required"`
  50. ClientType int `form:"clientType"`
  51. Name string `form:"name"`
  52. }
  53. // GetClientMenus 获取交易端菜单
  54. // @Summary 获取交易端菜单
  55. // @Produce json
  56. // @Security ApiKeyAuth
  57. // @Param loginID query int true "登录账号"
  58. // @Param clientType query int false "终端类型,0:PCWEB 1:Mobile,不传默认返回PC端数据"
  59. // @Param name query string false "分支名称,ermcp-风管云平台;pingan-平安期货;trader-交易所;不传默认返回风管云平台"
  60. // @Success 200 {array} ClientMenu
  61. // @Failure 500 {object} app.Response
  62. // @Router /Common/GetClientMenus [get]
  63. // @Tags 通用服务
  64. func GetClientMenus(c *gin.Context) {
  65. appG := app.Gin{C: c}
  66. // 获取请求参数
  67. var req GetClientMenusReq
  68. if err := c.ShouldBind(&req); err != nil {
  69. logger.GetLogger().Errorf("GetClientMenus parse query req: %v", err)
  70. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  71. return
  72. }
  73. // 获取菜单数据
  74. jsonFile := "pc_menu.json"
  75. if req.ClientType == 1 {
  76. // Mobile
  77. jsonFile = "mobile_menu.json"
  78. }
  79. // 获取分支名称
  80. if len(req.Name) > 0 {
  81. if req.ClientType == 0 {
  82. // PCWEB
  83. jsonFile = fmt.Sprintf("pcweb_menu/pcweb_menu_%s.json", req.Name)
  84. } else {
  85. // 手机
  86. jsonFile = fmt.Sprintf("mobile_menu/mobile_menu_%s.json", req.Name)
  87. }
  88. }
  89. // 读取json文件
  90. filePtr, err := os.Open(fmt.Sprintf("config/%s", jsonFile))
  91. if err != nil {
  92. // 读取文件失败
  93. logger.GetLogger().Errorf("GetClientMenus failed: %s", err.Error())
  94. appG.Response(http.StatusBadRequest, e.ERROR_CFG_FILE_FAIL, nil)
  95. return
  96. }
  97. defer filePtr.Close()
  98. var datas []interface{}
  99. // 创建Json编码器
  100. decoder := json.NewDecoder(filePtr)
  101. if err = decoder.Decode(&datas); err != nil {
  102. // Json解码失败
  103. logger.GetLogger().Errorf("GetClientMenus failed: %s", err.Error())
  104. appG.Response(http.StatusBadRequest, e.ERROR_CFG_JSON_FAIL, nil)
  105. return
  106. }
  107. isTrade, err := models.IsTrader(req.LoginID)
  108. if err != nil {
  109. // 判断用户类型失败
  110. logger.GetLogger().Errorf("GetClientMenus failed: %s", err.Error())
  111. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  112. return
  113. }
  114. var rst []ClientMenu
  115. if isTrade && req.ClientType == 1 {
  116. // 交易所自营会员或投资者, 目前只支持手机
  117. rst = createTraderMenu(datas)
  118. } else {
  119. rst = createMenu(datas, req.LoginID)
  120. }
  121. r := make([]ClientMenu, 0)
  122. if req.ClientType == 0 {
  123. // PC 去掉第一层没有子项的
  124. for _, v := range rst {
  125. if len(v.Children) > 0 {
  126. r = append(r, v)
  127. }
  128. }
  129. } else {
  130. r = rst
  131. }
  132. // 查询成功
  133. logger.GetLogger().Debugln("GetPCMenus successed: %v", r)
  134. appG.Response(http.StatusOK, e.SUCCESS, r)
  135. }
  136. // createMenu 创建菜单数据
  137. func createMenu(datas []interface{}, loginID int) []ClientMenu {
  138. clientMenus := make([]ClientMenu, 0)
  139. for _, v := range datas {
  140. data := v.(map[string]interface{})
  141. if data["isshow"].(bool) {
  142. rulekey := data["rulekey"].(string)
  143. if len(rulekey) != 0 {
  144. // 判断是否有权限
  145. funcMenuList, err := models.GetErmcpRoleFuncMenuLists(loginID, rulekey)
  146. if err != nil {
  147. continue
  148. }
  149. if len(funcMenuList) == 0 {
  150. continue
  151. }
  152. }
  153. item := ClientMenu{
  154. Code: data["code"].(string),
  155. Title: data["title"].(string),
  156. Sort: int(data["sort"].(float64)),
  157. Type: int(data["type"].(float64)),
  158. RuleKey: data["rulekey"].(string),
  159. IsShow: data["isshow"].(bool),
  160. Children: createMenu(data["children"].([]interface{}), loginID), // TODO: - 这里为什么可以这样转?
  161. }
  162. if data["url"] != nil {
  163. item.URL = data["url"].(string)
  164. }
  165. if data["remark"] != nil {
  166. item.Remark = data["remark"].(string)
  167. }
  168. clientMenus = append(clientMenus, item)
  169. }
  170. }
  171. return clientMenus
  172. }
  173. // createTraderMenu 创建交易所版自营会员和投资者菜单
  174. func createTraderMenu(datas []interface{}) []ClientMenu {
  175. clientMenus := make([]ClientMenu, 0)
  176. for _, v := range datas {
  177. data := v.(map[string]interface{})
  178. if data["isshow"].(bool) {
  179. item := ClientMenu{
  180. Code: data["code"].(string),
  181. Title: data["title"].(string),
  182. Sort: int(data["sort"].(float64)),
  183. Type: int(data["type"].(float64)),
  184. RuleKey: data["rulekey"].(string),
  185. IsShow: data["isshow"].(bool),
  186. Children: createTraderMenu(data["children"].([]interface{})),
  187. }
  188. if data["url"] != nil {
  189. item.URL = data["url"].(string)
  190. }
  191. if data["remark"] != nil {
  192. item.Remark = data["remark"].(string)
  193. }
  194. clientMenus = append(clientMenus, item)
  195. }
  196. }
  197. return clientMenus
  198. }
  199. // GetPCWebMenusReq 获取PCWeb交易端菜单请求参数
  200. type GetPCWebMenusReq struct {
  201. LoginID int `form:"loginID" binding:"required"`
  202. Name string `form:"name"`
  203. }
  204. // GetPCWebMenus 获取PCWeb交易端菜单
  205. // @Summary 获取PCWeb交易端菜单
  206. // @Produce json
  207. // @Security ApiKeyAuth
  208. // @Param loginID query int true "登录账号"
  209. // @Param name query string false "分支名称,ermcp:风管云平台,不传默认返回ermcp"
  210. // @Success 200 {array} PCWebMenu
  211. // @Failure 500 {object} app.Response
  212. // @Router /Common/GetPCWebMenus [get]
  213. // @Tags 通用服务
  214. func GetPCWebMenus(c *gin.Context) {
  215. appG := app.Gin{C: c}
  216. // 获取请求参数
  217. var req GetPCWebMenusReq
  218. if err := c.ShouldBind(&req); err != nil {
  219. logger.GetLogger().Errorf("GetPCWebMenus parse query req: %v", err)
  220. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  221. return
  222. }
  223. // 获取菜单数据
  224. jsonFile := "pcweb_menu/pcweb_menu_ermcp.json"
  225. // 获取分支名称
  226. if len(req.Name) > 0 {
  227. jsonFile = fmt.Sprintf("pcweb_menu/pcweb_menu_%s.json", req.Name)
  228. }
  229. // 读取json文件
  230. filePtr, err := os.Open(fmt.Sprintf("config/%s", jsonFile))
  231. if err != nil {
  232. // 读取文件失败
  233. logger.GetLogger().Errorf("GetPCWebMenus failed: %s", err.Error())
  234. appG.Response(http.StatusBadRequest, e.ERROR_CFG_FILE_FAIL, nil)
  235. return
  236. }
  237. defer filePtr.Close()
  238. var datas []interface{}
  239. // 创建Json编码器
  240. decoder := json.NewDecoder(filePtr)
  241. if err = decoder.Decode(&datas); err != nil {
  242. // Json解码失败
  243. logger.GetLogger().Errorf("GetPCWebMenus failed: %s", err.Error())
  244. appG.Response(http.StatusBadRequest, e.ERROR_CFG_JSON_FAIL, nil)
  245. return
  246. }
  247. rst := createPCWebMenu(datas, req.LoginID)
  248. // 查询成功
  249. logger.GetLogger().Debugln("GetPCWebMenus successed: %v", rst)
  250. appG.Response(http.StatusOK, e.SUCCESS, rst)
  251. }
  252. // createPCWebMenu 创建菜单数据
  253. func createPCWebMenu(datas []interface{}, loginID int) []PCWebMenu {
  254. clientMenus := make([]PCWebMenu, 0)
  255. for _, v := range datas {
  256. data := v.(map[string]interface{})
  257. if data["isshow"].(bool) {
  258. // 判断权限
  259. rulekey := data["rulekey"].(string)
  260. // FIXME: - 暂时代码
  261. requireAuth := false // rulekey != ""
  262. // FIXME: - 暂时代码
  263. requireAuth = false
  264. if requireAuth {
  265. // 判断是否有权限
  266. funcMenuList, err := models.GetErmcpRoleFuncMenuLists(loginID, rulekey)
  267. if err != nil {
  268. continue
  269. }
  270. if len(funcMenuList) == 0 {
  271. continue
  272. }
  273. }
  274. // 构建页面(按钮)权限
  275. auths := make([]PCWebMenuAuth, 0)
  276. if data["auth"] != nil {
  277. list := data["auth"].([]interface{})
  278. for _, a := range list {
  279. am := a.(map[string]interface{})
  280. if am["isshow"].(bool) {
  281. // 判断权限
  282. rulekey := am["rulekey"].(string)
  283. // FIXME: - 暂时代码
  284. requireAuth := false // rulekey != ""
  285. if requireAuth {
  286. // 判断是否有权限
  287. funcMenuList, err := models.GetErmcpRoleFuncMenuLists(loginID, rulekey)
  288. if err != nil {
  289. continue
  290. }
  291. if len(funcMenuList) == 0 {
  292. continue
  293. }
  294. }
  295. auth := PCWebMenuAuth{
  296. Label: am["label"].(string),
  297. Code: am["code"].(string),
  298. Rulekey: am["rulekey"].(string),
  299. IsShow: am["isshow"].(bool),
  300. }
  301. if am["remark"] != nil {
  302. auth.Remark = am["remark"].(string)
  303. }
  304. auths = append(auths, auth)
  305. }
  306. }
  307. }
  308. item := PCWebMenu{
  309. Title: data["title"].(string),
  310. Code: data["code"].(string),
  311. Path: data["path"].(string),
  312. Component: data["component"].(string),
  313. Sort: int(data["sort"].(float64)),
  314. Rulekey: data["rulekey"].(string),
  315. IsShow: data["isshow"].(bool),
  316. }
  317. if data["url"] != nil {
  318. item.URL = data["url"].(string)
  319. }
  320. if data["remark"] != nil {
  321. item.Remark = data["remark"].(string)
  322. }
  323. if data["children"] != nil {
  324. item.Children = createPCWebMenu(data["children"].([]interface{}), loginID)
  325. }
  326. if len(auths) > 0 {
  327. item.Auth = auths
  328. }
  329. clientMenus = append(clientMenus, item)
  330. }
  331. }
  332. return clientMenus
  333. }