list.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <view>
  3. <view class="uni-header">
  4. <uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
  5. </view>
  6. <view class="uni-tabs__header">
  7. <view class="uni-tabs__nav-wrap">
  8. <view class="uni-tabs__nav-scroll">
  9. <view class="uni-tabs__nav">
  10. <view @click="switchTab('menus')" :class="{'is-active':currentTab==='menus'}"
  11. class="uni-tabs__item">
  12. {{$t('menu.text.menuManager')}}
  13. </view>
  14. <view @click="switchTab('pluginMenus')" v-if="pluginMenus.length"
  15. :class="{'is-active':currentTab==='pluginMenus'}" class="uni-tabs__item">
  16. {{$t('menu.text.additiveMenu')}}
  17. <uni-badge class="menu-badge" :text="pluginMenus.length" type="error"></uni-badge>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. </view>
  23. <view v-show="currentTab==='menus'">
  24. <view class="uni-header" style="border-bottom: 0;margin-bottom: -15px;">
  25. <view class="uni-group">
  26. <button @click="navigateTo('./add')" size="mini" plain="true"
  27. type="primary">{{$t('menu.button.addFirstLevelMenu')}}</button>
  28. </view>
  29. <view class="uni-group">
  30. </view>
  31. </view>
  32. <view class="uni-container">
  33. <unicloud-db ref="udb" @load="onqueryload" collection="opendb-admin-menus" :options="options"
  34. :where="where" page-data="replace" :orderby="orderby" :getcount="true" :page-size="options.pageSize"
  35. :page-current="options.pageCurrent" v-slot:default="{data,pagination,loading,error}">
  36. <uni-table :loading="loading" class="table-pc" :emptyText="errMsg || $t('common.empty')" border
  37. stripe>
  38. <uni-tr>
  39. <uni-th align="center">排序</uni-th>
  40. <uni-th width="200" align="center">名称</uni-th>
  41. <uni-th align="center">标识</uni-th>
  42. <uni-th align="center">URL</uni-th>
  43. <uni-th width="100" align="center">是否启用</uni-th>
  44. <uni-th align="center">操作</uni-th>
  45. </uni-tr>
  46. <uni-tr v-for="(item,index) in data" :key="index">
  47. <uni-td align="center">{{item.sort}}</uni-td>
  48. <uni-td>{{item.name}}</uni-td>
  49. <uni-td>{{item.menu_id}}</uni-td>
  50. <uni-td>{{item.url}}</uni-td>
  51. <uni-td align="center" :class="{'menu-disable':!item.enable}">{{item.enable?'已启用':'未启用'}}
  52. </uni-td>
  53. <uni-td align="center">
  54. <view class="uni-group" style="justify-content: left;">
  55. <button @click="navigateTo('./edit?id='+item._id, false)" class="uni-button"
  56. size="mini" type="primary">{{$t('common.button.edit')}}</button>
  57. <button
  58. v-if="item.menu_id !== 'system_menu' && item.menu_id !== 'system_management'"
  59. @click="confirmDelete(item)" class="uni-button" size="mini"
  60. type="warn">{{$t('common.button.delete')}}</button>
  61. <button v-if="!item.url" @click="navigateTo('./add?parent_id='+item.menu_id, false)"
  62. class="uni-button" size="mini"
  63. type="primary">{{$t('menu.button.addChildMenu')}}</button>
  64. </view>
  65. </uni-td>
  66. </uni-tr>
  67. </uni-table>
  68. </unicloud-db>
  69. </view>
  70. </view>
  71. <view v-show="currentTab==='pluginMenus'">
  72. <view class="uni-header" style="border-bottom: 0;margin-bottom: -15px;">
  73. <view class="uni-group">
  74. <button style="width: 130px;" @click="addPluginMenus" size="mini" type="primary">添加选中的菜单</button>
  75. </view>
  76. <view class="uni-group"></view>
  77. </view>
  78. <view class="uni-container">
  79. <uni-table ref="pluginMenusTable" type="selection" border stripe
  80. @selection-change="pluginMenuSelectChange">
  81. <uni-tr>
  82. <uni-th align="center">名称(标识)</uni-th>
  83. <uni-th align="center">URL</uni-th>
  84. <uni-th align="center">插件菜单 json 文件</uni-th>
  85. </uni-tr>
  86. <uni-tr v-for="(item,index) in pluginMenus" :key="index">
  87. <uni-td>{{item.name}}({{item.menu_id}})</uni-td>
  88. <uni-td>{{item.url}}</uni-td>
  89. <uni-td>{{item.json}}</uni-td>
  90. </uni-tr>
  91. </uni-table>
  92. <view class="uni-sub-title" style="margin-top: 15px;">
  93. 以上待添加菜单来自于三方插件,添加后,将显示在菜单管理中,若不希望显示在上述表格中时,可手动删除项目中对应的`插件id-menu.json`文件。
  94. </view>
  95. </view>
  96. </view>
  97. <!-- #ifndef H5 -->
  98. <fix-window />
  99. <!-- #endif -->
  100. </view>
  101. </template>
  102. <script>
  103. import {
  104. buildMenus
  105. } from '../../../components/uni-data-menu/util.js'
  106. const db = uniCloud.database()
  107. // 表查询配置
  108. const dbOrderBy = 'create_date asc'
  109. // 分页配置
  110. const pageSize = 20000
  111. const pageCurrent = 1
  112. // 查找插件注册的菜单列表(目前仅在开发模式启用,仅限 admin 角色)
  113. const pluginMenuJsons = []
  114. // #ifndef VUE3
  115. if (process.env.NODE_ENV === 'development') {
  116. const rootModules = require.context(
  117. '../../../',
  118. false,
  119. /-menu.json$/
  120. )
  121. rootModules.keys().forEach(function(key) {
  122. const json = key.substr(2)
  123. rootModules(key).forEach(item => {
  124. item.json = json
  125. pluginMenuJsons.push(item)
  126. })
  127. })
  128. const pluginModules = require.context(
  129. '../../../uni_modules/',
  130. true,
  131. /menu.json$/
  132. )
  133. pluginModules.keys().forEach(function(key) {
  134. const json = 'uni_modules' + key.substr(1)
  135. pluginModules(key).forEach(item => {
  136. item.json = json
  137. pluginMenuJsons.push(item)
  138. })
  139. })
  140. }
  141. // #endif
  142. // 获取父的个数
  143. function getParents(menus, id, depth = 0) {
  144. menus.forEach(menu => {
  145. if (menu.menu_id === id && menu.parent_id) {
  146. depth = depth + 1 + getParents(menus, menu.parent_id, depth)
  147. }
  148. })
  149. return depth
  150. }
  151. // 获取子的 _id
  152. function getChildren(menus, id, childrenIds = []) {
  153. if (menus.find(menu => menu.parent_id === id)) {
  154. menus.forEach(item => {
  155. if (item.parent_id === id) {
  156. childrenIds.push(item._id)
  157. getChildren(menus, item.menu_id, childrenIds)
  158. }
  159. })
  160. }
  161. return childrenIds
  162. }
  163. export default {
  164. data() {
  165. return {
  166. query: '',
  167. where: '',
  168. orderby: dbOrderBy,
  169. options: {
  170. pageSize,
  171. pageCurrent
  172. },
  173. selectedIndexs: [], //批量选中的项
  174. loading: true,
  175. menus: [],
  176. errMsg: '',
  177. currentTab: 'menus',
  178. selectedPluginMenuIndexs: []
  179. }
  180. },
  181. computed: {
  182. pluginMenus() {
  183. const menus = []
  184. if (!this.$hasRole('admin')) {
  185. return menus
  186. }
  187. const dbMenus = this.menus
  188. if (!dbMenus.length) {
  189. return menus
  190. }
  191. pluginMenuJsons.forEach(menu => {
  192. // 查找尚未被注册到数据库中的菜单
  193. if (!dbMenus.find(item => item.menu_id === menu.menu_id)) {
  194. menus.push(menu)
  195. }
  196. })
  197. return menus
  198. },
  199. },
  200. watch: {
  201. pluginMenus(val) {
  202. if (!val.length) {
  203. this.currentTab = 'menus'
  204. }
  205. }
  206. },
  207. methods: {
  208. getSortMenu(menuList) {
  209. // 标记叶子节点
  210. menuList.map(item => {
  211. if (!menuList.some(subMenuItem => subMenuItem.parent_id === item.menu_id)) {
  212. item.isLeafNode = true
  213. }
  214. })
  215. return buildMenus(menuList)
  216. },
  217. onqueryload(data) {
  218. for (var i = 0; i < data.length; i++) {
  219. let item = data[i]
  220. const depth = getParents(data, item.menu_id)
  221. item.name = (depth ? ' '.repeat(depth) + '|-' : '') + item.name
  222. }
  223. const menuTree = this.getSortMenu(data)
  224. const sortMenus = []
  225. this.patTree(menuTree, sortMenus)
  226. data.length = 0;
  227. data.push(...sortMenus)
  228. this.menus = data //仅导出当前页
  229. },
  230. patTree(tree, sortMenus) {
  231. tree.forEach(item => {
  232. sortMenus.push(item)
  233. if (item.children.length) {
  234. this.patTree(item.children, sortMenus)
  235. }
  236. })
  237. return sortMenus
  238. },
  239. switchTab(tab) {
  240. this.currentTab = tab
  241. },
  242. loadData(clear = true) {
  243. this.$refs.udb.loadData({
  244. clear
  245. })
  246. },
  247. navigateTo(url, clear) { // clear 表示刷新列表时是否清除当前页码,true 表示刷新并回到列表第 1 页,默认为 true
  248. uni.navigateTo({
  249. url,
  250. events: {
  251. refreshData: () => {
  252. this.loadData(clear)
  253. }
  254. }
  255. })
  256. },
  257. confirmDelete(menu) {
  258. let ids = menu._id
  259. let content = '是否删除该菜单?'
  260. // 如有子菜单
  261. const children = getChildren(this.menus, menu.menu_id)
  262. if (children.length) content = '是否删除该菜单及其子菜单?'
  263. ids = [ids, ...children]
  264. uni.showModal({
  265. title: '提示',
  266. content,
  267. success: (res) => {
  268. if (!res.confirm) {
  269. return
  270. }
  271. this.$refs.udb.remove(ids, {
  272. needConfirm: false
  273. })
  274. }
  275. })
  276. },
  277. pluginMenuSelectChange(e) {
  278. this.selectedPluginMenuIndexs = e.detail.index
  279. },
  280. addPluginMenus(confirmContent) {
  281. if (!this.selectedPluginMenuIndexs.length) {
  282. return uni.showModal({
  283. title: '提示',
  284. content: '请选择要添加的菜单!',
  285. showCancel: false
  286. })
  287. }
  288. const pluginMenus = this.pluginMenus
  289. const menus = []
  290. this.selectedPluginMenuIndexs.forEach(i => {
  291. const menu = pluginMenus[i]
  292. if (menu) {
  293. // 拷贝一份,移除 json 字段
  294. const dbMenu = JSON.parse(JSON.stringify(menu))
  295. delete dbMenu.json
  296. menus.push(dbMenu)
  297. }
  298. })
  299. uni.showModal({
  300. title: '提示',
  301. content: '您确认要添加已选中的菜单吗?',
  302. success: (res) => {
  303. if (!res.confirm) {
  304. return
  305. }
  306. uni.showLoading({
  307. mask: true
  308. })
  309. const checkAll = menus.length === pluginMenus.length
  310. uniCloud.database().collection('opendb-admin-menus').add(menus).then(res => {
  311. this.init()
  312. uni.showModal({
  313. title: '提示',
  314. content: '添加菜单成功!',
  315. showCancel: false,
  316. success: () => {
  317. this.$refs.pluginMenusTable.clearSelection()
  318. if (checkAll) {
  319. this.currentTab = 'menus'
  320. }
  321. this.loadData()
  322. }
  323. })
  324. }).catch(err => {
  325. uni.showModal({
  326. title: '提示',
  327. content: err.message,
  328. showCancel: false
  329. })
  330. }).finally(() => {
  331. uni.hideLoading()
  332. })
  333. }
  334. })
  335. }
  336. }
  337. }
  338. </script>
  339. <style>
  340. /* #ifndef H5 */
  341. page {
  342. padding-top: 85px;
  343. }
  344. /* #endif */
  345. .menu-disable {
  346. color: red;
  347. }
  348. .menu-badge {
  349. position: absolute;
  350. top: 0;
  351. right: 5px;
  352. }
  353. </style>