index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <template>
  2. <div class="app-container">
  3. <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
  4. <el-form-item label="菜单名称" prop="menuName">
  5. <el-input
  6. v-model="queryParams.menuName"
  7. placeholder="请输入菜单名称"
  8. clearable
  9. size="small"
  10. @keyup.enter.native="handleQuery"
  11. />
  12. </el-form-item>
  13. <el-form-item label="状态" prop="status">
  14. <el-select v-model="queryParams.status" placeholder="菜单状态" clearable size="small">
  15. <el-option
  16. v-for="dict in statusOptions"
  17. :key="dict.dictValue"
  18. :label="dict.dictLabel"
  19. :value="dict.dictValue"
  20. />
  21. </el-select>
  22. </el-form-item>
  23. <el-form-item>
  24. <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  25. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  26. </el-form-item>
  27. </el-form>
  28. <el-row :gutter="10" class="mb8">
  29. <el-col :span="1.5">
  30. <el-button
  31. type="primary"
  32. icon="el-icon-plus"
  33. size="mini"
  34. @click="handleAdd"
  35. v-hasPermi="['system:menu:add']"
  36. >新增</el-button>
  37. </el-col>
  38. <div class="top-right-btn">
  39. <el-tooltip class="item" effect="dark" content="刷新" placement="top">
  40. <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
  41. </el-tooltip>
  42. <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
  43. <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
  44. </el-tooltip>
  45. </div>
  46. </el-row>
  47. <el-table
  48. v-loading="loading"
  49. :data="menuList"
  50. row-key="menuId"
  51. :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
  52. >
  53. <el-table-column prop="menuName" label="菜单名称" :show-overflow-tooltip="true" width="160"></el-table-column>
  54. <el-table-column prop="icon" label="图标" align="center" width="100">
  55. <template slot-scope="scope">
  56. <svg-icon :icon-class="scope.row.icon" />
  57. </template>
  58. </el-table-column>
  59. <el-table-column prop="orderNum" label="排序" width="60"></el-table-column>
  60. <el-table-column prop="perms" label="权限标识" :show-overflow-tooltip="true"></el-table-column>
  61. <el-table-column prop="component" label="组件路径" :show-overflow-tooltip="true"></el-table-column>
  62. <el-table-column prop="status" label="状态" :formatter="statusFormat" width="80"></el-table-column>
  63. <el-table-column label="创建时间" align="center" prop="createTime">
  64. <template slot-scope="scope">
  65. <span>{{ parseTime(scope.row.createTime) }}</span>
  66. </template>
  67. </el-table-column>
  68. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  69. <template slot-scope="scope">
  70. <el-button size="mini"
  71. type="text"
  72. icon="el-icon-edit"
  73. @click="handleUpdate(scope.row)"
  74. v-hasPermi="['system:menu:edit']"
  75. >修改</el-button>
  76. <el-button
  77. size="mini"
  78. type="text"
  79. icon="el-icon-plus"
  80. @click="handleAdd(scope.row)"
  81. v-hasPermi="['system:menu:add']"
  82. >新增</el-button>
  83. <el-button
  84. size="mini"
  85. type="text"
  86. icon="el-icon-delete"
  87. @click="handleDelete(scope.row)"
  88. v-hasPermi="['system:menu:remove']"
  89. >删除</el-button>
  90. </template>
  91. </el-table-column>
  92. </el-table>
  93. <!-- 添加或修改菜单对话框 -->
  94. <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
  95. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  96. <el-row>
  97. <el-col :span="24">
  98. <el-form-item label="上级菜单">
  99. <treeselect
  100. v-model="form.parentId"
  101. :options="menuOptions"
  102. :normalizer="normalizer"
  103. :show-count="true"
  104. placeholder="选择上级菜单"
  105. />
  106. </el-form-item>
  107. </el-col>
  108. <el-col :span="24">
  109. <el-form-item label="菜单类型" prop="menuType">
  110. <el-radio-group v-model="form.menuType">
  111. <el-radio label="M">目录</el-radio>
  112. <el-radio label="C">菜单</el-radio>
  113. <el-radio label="F">按钮</el-radio>
  114. </el-radio-group>
  115. </el-form-item>
  116. </el-col>
  117. <el-col :span="24">
  118. <el-form-item v-if="form.menuType != 'F'" label="菜单图标">
  119. <el-popover
  120. placement="bottom-start"
  121. width="460"
  122. trigger="click"
  123. @show="$refs['iconSelect'].reset()"
  124. >
  125. <IconSelect ref="iconSelect" @selected="selected" />
  126. <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" readonly>
  127. <svg-icon
  128. v-if="form.icon"
  129. slot="prefix"
  130. :icon-class="form.icon"
  131. class="el-input__icon"
  132. style="height: 32px;width: 16px;"
  133. />
  134. <i v-else slot="prefix" class="el-icon-search el-input__icon" />
  135. </el-input>
  136. </el-popover>
  137. </el-form-item>
  138. </el-col>
  139. <el-col :span="12">
  140. <el-form-item label="菜单名称" prop="menuName">
  141. <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
  142. </el-form-item>
  143. </el-col>
  144. <el-col :span="12">
  145. <el-form-item label="显示排序" prop="orderNum">
  146. <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
  147. </el-form-item>
  148. </el-col>
  149. <el-col :span="12">
  150. <el-form-item v-if="form.menuType != 'F'" label="是否外链">
  151. <el-radio-group v-model="form.isFrame">
  152. <el-radio label="0">是</el-radio>
  153. <el-radio label="1">否</el-radio>
  154. </el-radio-group>
  155. </el-form-item>
  156. </el-col>
  157. <el-col :span="12">
  158. <el-form-item v-if="form.menuType != 'F'" label="路由地址" prop="path">
  159. <el-input v-model="form.path" placeholder="请输入路由地址" />
  160. </el-form-item>
  161. </el-col>
  162. <el-col :span="12" v-if="form.menuType == 'C'">
  163. <el-form-item label="组件路径" prop="component">
  164. <el-input v-model="form.component" placeholder="请输入组件路径" />
  165. </el-form-item>
  166. </el-col>
  167. <el-col :span="12">
  168. <el-form-item v-if="form.menuType != 'M'" label="权限标识">
  169. <el-input v-model="form.perms" placeholder="请权限标识" maxlength="50" />
  170. </el-form-item>
  171. </el-col>
  172. <el-col :span="12">
  173. <el-form-item v-if="form.menuType != 'F'" label="显示状态">
  174. <el-radio-group v-model="form.visible">
  175. <el-radio
  176. v-for="dict in visibleOptions"
  177. :key="dict.dictValue"
  178. :label="dict.dictValue"
  179. >{{dict.dictLabel}}</el-radio>
  180. </el-radio-group>
  181. </el-form-item>
  182. </el-col>
  183. <el-col :span="12">
  184. <el-form-item v-if="form.menuType != 'F'" label="菜单状态">
  185. <el-radio-group v-model="form.status">
  186. <el-radio
  187. v-for="dict in statusOptions"
  188. :key="dict.dictValue"
  189. :label="dict.dictValue"
  190. >{{dict.dictLabel}}</el-radio>
  191. </el-radio-group>
  192. </el-form-item>
  193. </el-col>
  194. </el-row>
  195. </el-form>
  196. <div slot="footer" class="dialog-footer">
  197. <el-button type="primary" @click="submitForm">确 定</el-button>
  198. <el-button @click="cancel">取 消</el-button>
  199. </div>
  200. </el-dialog>
  201. </div>
  202. </template>
  203. <script>
  204. import { listMenu, getMenu, delMenu, addMenu, updateMenu } from "@/api/system/menu";
  205. import Treeselect from "@riophae/vue-treeselect";
  206. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  207. import IconSelect from "@/components/IconSelect";
  208. export default {
  209. name: "Menu",
  210. components: { Treeselect, IconSelect },
  211. data() {
  212. return {
  213. // 遮罩层
  214. loading: true,
  215. // 显示搜索条件
  216. showSearch: true,
  217. // 菜单表格树数据
  218. menuList: [],
  219. // 菜单树选项
  220. menuOptions: [],
  221. // 弹出层标题
  222. title: "",
  223. // 是否显示弹出层
  224. open: false,
  225. // 显示状态数据字典
  226. visibleOptions: [],
  227. // 菜单状态数据字典
  228. statusOptions: [],
  229. // 查询参数
  230. queryParams: {
  231. menuName: undefined,
  232. visible: undefined
  233. },
  234. // 表单参数
  235. form: {},
  236. // 表单校验
  237. rules: {
  238. menuName: [
  239. { required: true, message: "菜单名称不能为空", trigger: "blur" }
  240. ],
  241. orderNum: [
  242. { required: true, message: "菜单顺序不能为空", trigger: "blur" }
  243. ],
  244. path: [
  245. { required: true, message: "路由地址不能为空", trigger: "blur" }
  246. ]
  247. }
  248. };
  249. },
  250. created() {
  251. this.getList();
  252. this.getDicts("sys_show_hide").then(response => {
  253. this.visibleOptions = response.data;
  254. });
  255. this.getDicts("sys_normal_disable").then(response => {
  256. this.statusOptions = response.data;
  257. });
  258. },
  259. methods: {
  260. // 选择图标
  261. selected(name) {
  262. this.form.icon = name;
  263. },
  264. /** 查询菜单列表 */
  265. getList() {
  266. this.loading = true;
  267. listMenu(this.queryParams).then(response => {
  268. this.menuList = this.handleTree(response.data, "menuId");
  269. this.loading = false;
  270. });
  271. },
  272. /** 转换菜单数据结构 */
  273. normalizer(node) {
  274. if (node.children && !node.children.length) {
  275. delete node.children;
  276. }
  277. return {
  278. id: node.menuId,
  279. label: node.menuName,
  280. children: node.children
  281. };
  282. },
  283. /** 查询菜单下拉树结构 */
  284. getTreeselect() {
  285. listMenu().then(response => {
  286. this.menuOptions = [];
  287. const menu = { menuId: 0, menuName: '主类目', children: [] };
  288. menu.children = this.handleTree(response.data, "menuId");
  289. this.menuOptions.push(menu);
  290. });
  291. },
  292. // 显示状态字典翻译
  293. visibleFormat(row, column) {
  294. if (row.menuType == "F") {
  295. return "";
  296. }
  297. return this.selectDictLabel(this.visibleOptions, row.visible);
  298. },
  299. // 菜单状态字典翻译
  300. statusFormat(row, column) {
  301. if (row.menuType == "F") {
  302. return "";
  303. }
  304. return this.selectDictLabel(this.statusOptions, row.status);
  305. },
  306. // 取消按钮
  307. cancel() {
  308. this.open = false;
  309. this.reset();
  310. },
  311. // 表单重置
  312. reset() {
  313. this.form = {
  314. menuId: undefined,
  315. parentId: 0,
  316. menuName: undefined,
  317. icon: undefined,
  318. menuType: "M",
  319. orderNum: undefined,
  320. isFrame: "1",
  321. visible: "0",
  322. status: "0"
  323. };
  324. this.resetForm("form");
  325. },
  326. /** 搜索按钮操作 */
  327. handleQuery() {
  328. this.getList();
  329. },
  330. /** 重置按钮操作 */
  331. resetQuery() {
  332. this.resetForm("queryForm");
  333. this.handleQuery();
  334. },
  335. /** 新增按钮操作 */
  336. handleAdd(row) {
  337. this.reset();
  338. this.getTreeselect();
  339. if (row != null && row.menuId) {
  340. this.form.parentId = row.menuId;
  341. } else {
  342. this.form.parentId = 0;
  343. }
  344. this.open = true;
  345. this.title = "添加菜单";
  346. },
  347. /** 修改按钮操作 */
  348. handleUpdate(row) {
  349. this.reset();
  350. this.getTreeselect();
  351. getMenu(row.menuId).then(response => {
  352. this.form = response.data;
  353. this.open = true;
  354. this.title = "修改菜单";
  355. });
  356. },
  357. /** 提交按钮 */
  358. submitForm: function() {
  359. this.$refs["form"].validate(valid => {
  360. if (valid) {
  361. if (this.form.menuId != undefined) {
  362. updateMenu(this.form).then(response => {
  363. if (response.code === 200) {
  364. this.msgSuccess("修改成功");
  365. this.open = false;
  366. this.getList();
  367. }
  368. });
  369. } else {
  370. addMenu(this.form).then(response => {
  371. if (response.code === 200) {
  372. this.msgSuccess("新增成功");
  373. this.open = false;
  374. this.getList();
  375. }
  376. });
  377. }
  378. }
  379. });
  380. },
  381. /** 删除按钮操作 */
  382. handleDelete(row) {
  383. this.$confirm('是否确认删除名称为"' + row.menuName + '"的数据项?', "警告", {
  384. confirmButtonText: "确定",
  385. cancelButtonText: "取消",
  386. type: "warning"
  387. }).then(function() {
  388. return delMenu(row.menuId);
  389. }).then(() => {
  390. this.getList();
  391. this.msgSuccess("删除成功");
  392. }).catch(function() {});
  393. }
  394. }
  395. };
  396. </script>