addEdit.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <template>
  2. <el-dialog
  3. v-loading="loading"
  4. :title="title"
  5. :center="true"
  6. align="left"
  7. top="10vh"
  8. width="750px"
  9. :close-on-click-modal="false"
  10. :visible.sync="showModelThis"
  11. element-loading-text="拼命加载中"
  12. element-loading-spinner="el-icon-loading"
  13. element-loading-background="rgba(0, 0, 0, 0.8)"
  14. append-to-body
  15. @close="showModelThis = false"
  16. >
  17. <el-card style="margin-top: -20px">
  18. <el-form
  19. ref="ruleForm"
  20. :model="ruleForm"
  21. is_show-icon
  22. :rules="rulesThis"
  23. label-width="100px"
  24. class="demo-ruleForm"
  25. >
  26. <el-row>
  27. <el-col :span="24">
  28. <el-form-item v-if="level < 3" label="名称:" prop="name">
  29. <el-input
  30. v-model="ruleForm.name"
  31. :disabled="isDetail"
  32. maxlength="10"
  33. placeholder="例如:用户管理"
  34. />
  35. </el-form-item>
  36. <el-form-item v-if="level === 3" label="按钮标识:" prop="code">
  37. <el-select
  38. v-model="ruleForm.code"
  39. placeholder="请选择按钮标识"
  40. clearable
  41. filterable
  42. :disabled="isDetail"
  43. @change="codeChange"
  44. >
  45. <el-option
  46. v-for="item in btnList"
  47. :key="item.code"
  48. :label="item.name"
  49. :value="item.code"
  50. />
  51. </el-select>
  52. </el-form-item>
  53. <el-row>
  54. <el-col v-if="level < 3" :span="12">
  55. <el-form-item label="路由:" prop="route">
  56. <el-input
  57. v-model="ruleForm.route"
  58. :disabled="isDetail"
  59. maxlength="100"
  60. placeholder="例如:user"
  61. />
  62. </el-form-item>
  63. </el-col>
  64. <el-col v-if="level < 3" :span="12">
  65. <el-form-item label="图标:" prop="img">
  66. <el-row :gutter="10">
  67. <el-col v-if="ruleForm.img" :span="3" style="width: 45px">
  68. <div class="g-block" @click="showIconModel = true">
  69. <i :class="ruleForm.img" />
  70. </div>
  71. </el-col>
  72. <el-col v-if="!ruleForm.img" :span="7">
  73. <el-button
  74. type="warning"
  75. size="mini"
  76. @click="showIconModel = true"
  77. >
  78. 选择图标
  79. </el-button>
  80. </el-col>
  81. </el-row>
  82. <iconDialog
  83. :show-model="showIconModel"
  84. @setVal="setVal"
  85. @cancel="showIconModel = false"
  86. />
  87. </el-form-item>
  88. </el-col>
  89. </el-row>
  90. <el-form-item
  91. v-if="level === 2"
  92. label="页面路径:"
  93. prop="component"
  94. >
  95. <el-input
  96. v-model="ruleForm.component"
  97. :disabled="isDetail"
  98. maxlength="100"
  99. placeholder="例如:park/user"
  100. >
  101. <template slot="prepend">views/</template>
  102. </el-input>
  103. </el-form-item>
  104. <el-row>
  105. <el-col :span="12">
  106. <el-form-item label="状态:" prop="is_show">
  107. <el-radio
  108. v-model="ruleForm.is_show"
  109. :label="'1'"
  110. :disabled="isDetail"
  111. >
  112. 启用
  113. </el-radio>
  114. <el-radio
  115. v-model="ruleForm.is_show"
  116. :label="'0'"
  117. :disabled="isDetail"
  118. >
  119. 禁用
  120. </el-radio>
  121. </el-form-item>
  122. </el-col>
  123. <el-col :span="12">
  124. <el-form-item
  125. v-if="level < 3"
  126. label="页面状态:"
  127. prop="is_display"
  128. >
  129. <el-radio
  130. v-model="ruleForm.is_display"
  131. :label="'1'"
  132. :disabled="isDetail"
  133. >
  134. 显示
  135. </el-radio>
  136. <el-radio
  137. v-model="ruleForm.is_display"
  138. :label="'0'"
  139. :disabled="isDetail"
  140. >
  141. 隐藏
  142. </el-radio>
  143. </el-form-item>
  144. </el-col>
  145. </el-row>
  146. <el-form-item v-if="level < 3" label="排序权重:" prop="weight">
  147. <el-row :gutter="10">
  148. <el-col :span="16" style="padding: 0">
  149. <el-input
  150. v-model.number="ruleForm.weight"
  151. type="number"
  152. maxlength="10"
  153. :disabled="isDetail"
  154. />
  155. </el-col>
  156. <el-col :span="7">
  157. <el-tag type="danger">数值越大排名越靠前</el-tag>
  158. </el-col>
  159. </el-row>
  160. </el-form-item>
  161. <el-form-item v-if="level < 3" label="菜单等级" prop="level">
  162. <el-select v-model="ruleForm.level">
  163. <el-option label="超管" value="1" />
  164. <el-option label="运营" value="2" />
  165. <el-option label="其他" value="3" />
  166. </el-select>
  167. </el-form-item>
  168. </el-col>
  169. </el-row>
  170. </el-form>
  171. <el-col :span="24" style="text-align: right; padding: 0 0 10px 0">
  172. <el-button v-if="!isDetail" type="primary" @click="submitForm">
  173. 保 存
  174. </el-button>
  175. <el-button @click="showModelThis = false">{{
  176. isDetail ? "关 闭" : "取 消"
  177. }}</el-button>
  178. </el-col>
  179. </el-card>
  180. </el-dialog>
  181. </template>
  182. <script>
  183. import asyncRequest from '@/apis/service/system/menu'
  184. import btnList from '@/assets/js/btnList'
  185. import iconDialog from '@/views/system/menuOperator/iconDialog'
  186. import resToken from '@/mixins/resToken'
  187. export default {
  188. components: {
  189. iconDialog
  190. },
  191. mixins: [resToken],
  192. props: [
  193. 'showModel',
  194. 'id',
  195. 'isDetail',
  196. 'parentIds',
  197. 'level',
  198. 'isAdd',
  199. 'type',
  200. 'formData'
  201. ],
  202. data() {
  203. return {
  204. loading: false,
  205. showIconModel: false,
  206. title: '',
  207. btnList,
  208. showModelThis: this.showModel,
  209. ruleForm: {},
  210. rulesThis: this.rules,
  211. rules: {
  212. name: [
  213. {
  214. required: true,
  215. message: '请输入名称',
  216. trigger: 'blur'
  217. },
  218. {
  219. min: 1,
  220. max: 50,
  221. message: '长度在 1 到 50 个字符',
  222. trigger: 'blur'
  223. }
  224. ],
  225. code: [
  226. {
  227. required: true,
  228. message: '请输入按钮标识编码(用于判断按钮是否显示)',
  229. trigger: 'change'
  230. }
  231. ],
  232. level: [
  233. {
  234. required: true,
  235. message: '请选择菜单等级',
  236. trigger: 'change'
  237. }
  238. ],
  239. img: [
  240. {
  241. required: true,
  242. message: '请选择图标',
  243. trigger: 'blur'
  244. }
  245. ],
  246. route: [
  247. {
  248. required: true,
  249. message: '请输入路由',
  250. trigger: 'blur'
  251. },
  252. {
  253. min: 1,
  254. max: 50,
  255. message: '长度在 1 到 50 个字符',
  256. trigger: 'blur'
  257. }
  258. ],
  259. component: [
  260. {
  261. required: true,
  262. message: '请输入页面组件路径',
  263. trigger: 'blur'
  264. },
  265. {
  266. min: 1,
  267. max: 100,
  268. message: '长度在 1 到 100 个字符',
  269. trigger: 'blur'
  270. }
  271. ],
  272. weight: [
  273. {
  274. required: true,
  275. message: '请输入权重,数值越大排名越靠前',
  276. trigger: 'blur'
  277. }
  278. ],
  279. is_show: [
  280. {
  281. required: true,
  282. message: '请选择状态',
  283. trigger: 'change'
  284. }
  285. ],
  286. is_display: [
  287. {
  288. required: true,
  289. message: '请选择页面状态',
  290. trigger: 'change'
  291. }
  292. ]
  293. }
  294. }
  295. },
  296. watch: {
  297. showModel: function(val) {
  298. this.showModelThis = val
  299. if (val) {
  300. this.initForm()
  301. }
  302. },
  303. showModelThis(val) {
  304. if (!val) {
  305. this.$emit('cancel')
  306. }
  307. }
  308. },
  309. methods: {
  310. codeChange(code) {
  311. for (let i = 0; i < this.btnList.length; i++) {
  312. if (this.btnList[i].code === code) {
  313. this.ruleForm.name = this.btnList[i].name
  314. break
  315. }
  316. }
  317. },
  318. setVal(val) {
  319. this.ruleForm.img = val
  320. this.$refs.ruleForm.validateField('img')
  321. },
  322. async initForm() {
  323. if (this.isAdd) {
  324. if (this.level === 1) {
  325. this.title = '添加一级菜单'
  326. } else if (this.level === 2) {
  327. this.title = '添加二级菜单'
  328. } else {
  329. this.title = '添加按钮'
  330. }
  331. this.loading = false
  332. this.rulesThis = this.rules
  333. await this.resetForm()
  334. } else {
  335. if (this.isDetail) {
  336. if (this.level === 1) {
  337. this.title = '一级菜单详情'
  338. } else if (this.level === 2) {
  339. this.title = '二级菜单详情'
  340. } else {
  341. this.title = '按钮详情'
  342. }
  343. this.rulesThis = {}
  344. } else {
  345. if (this.level === 1) {
  346. this.title = '修改一级菜单'
  347. } else if (this.level === 2) {
  348. this.title = '修改二级菜单'
  349. } else {
  350. this.title = '修改按钮'
  351. }
  352. this.rulesThis = this.rules
  353. }
  354. await this.initData()
  355. }
  356. },
  357. async initData() {
  358. this.loading = true
  359. let parentIds = ''
  360. if (this.isAdd) {
  361. if (this.id === 0) {
  362. parentIds = '0'
  363. } else {
  364. parentIds = this.parentIds
  365. }
  366. } else {
  367. parentIds = this.parentIds
  368. }
  369. const {
  370. menu_name,
  371. menu_img,
  372. menu_route,
  373. is_show,
  374. weight,
  375. status,
  376. menu_url,
  377. menuid,
  378. action_name,
  379. action_code,
  380. is_display,
  381. level
  382. } = this.formData
  383. if (this.level !== 3) {
  384. // 重置
  385. this.ruleForm = {
  386. parentId: this.id + '', // 父级ID(一级为0)
  387. parentIds: parentIds + '', // 所有父级编号(用逗号分隔)
  388. name: menu_name + '', // 名称
  389. code: '', // 编码(用于按钮是否显示)
  390. img: menu_img + '', // 图标(导航图标)
  391. route: menu_route + '', // 路由
  392. component: this.level === 1 ? '' : menu_url + '', // 页面组件
  393. menuCategory: 1 + '', // 菜单类别(1.导航菜单 2.按钮)
  394. type: this.type + '', // 类型(1.运营菜单 2.物业菜单)
  395. is_show: is_show + '', // 状态(1启用 0禁用)
  396. is_display: is_display + '',
  397. weight: parseInt(weight + ''), // 权重,越大越靠前
  398. level: String(level)
  399. }
  400. if (this.level === 1) {
  401. delete this.ruleForm['parentIds']
  402. }
  403. } else {
  404. this.ruleForm = {
  405. parentId: this.id + '', // 父级ID(一级为0)
  406. parentIds: menuid + '', // 所有父级编号(用逗号分隔)
  407. name: action_name + '', // 名称
  408. code: action_code + '', // 编码(用于按钮是否显示)
  409. img: '', // 图标(导航图标)
  410. route: '', // 路由
  411. component: '', // 页面组件
  412. menuCategory: 2 + '', // 菜单类别(1.导航菜单 2.按钮)
  413. type: this.type + '', // 类型(1.运营菜单 2.物业菜单)
  414. is_show: status + '', // 状态(1启用 0禁用)
  415. weight: 1 + '' // 权重,越大越靠前
  416. }
  417. }
  418. this.loading = false
  419. await this.$nextTick(() => {
  420. if (this.$refs.ruleForm) {
  421. this.$refs.ruleForm.clearValidate()
  422. }
  423. })
  424. },
  425. async resetForm() {
  426. let parentIds = ''
  427. if (this.isAdd) {
  428. if (this.id === 0) {
  429. parentIds = '0'
  430. } else {
  431. parentIds = this.parentIds
  432. }
  433. } else {
  434. parentIds = this.parentIds
  435. }
  436. // 重置
  437. this.ruleForm = {
  438. parentId: this.id, // 父级ID(一级为0)
  439. parentIds, // 所有父级编号(用逗号分隔)
  440. name: '', // 名称
  441. code: '', // 编码(用于按钮是否显示)
  442. img: '', // 图标(导航图标)
  443. route: '', // 路由
  444. component: '', // 页面组件
  445. menuCategory: this.level < 3 ? 1 : 2, // 菜单类别(1.导航菜单 2.按钮)
  446. type: this.type + '', // 类型(1.运营菜单 2.物业菜单)
  447. is_show: '1', // 状态(1启用 0禁用)
  448. is_display: '1', // 页面状态(1显示 0隐藏)
  449. weight: 1 // 权重,越大越靠前
  450. }
  451. await this.$nextTick(() => {
  452. if (this.$refs.ruleForm) {
  453. this.$refs.ruleForm.clearValidate()
  454. }
  455. })
  456. },
  457. async submitForm() {
  458. if (this.loading) {
  459. return false
  460. }
  461. this.loading = true
  462. await this.$refs.ruleForm.validate(async(valid) => {
  463. if (valid) {
  464. let model = {}
  465. let res = {}
  466. const {
  467. name,
  468. img,
  469. route,
  470. is_show,
  471. is_display,
  472. weight,
  473. component,
  474. code,
  475. level
  476. } = this.ruleForm
  477. model = {
  478. id: this.id,
  479. pid: this.parentIds,
  480. menuid: this.parentIds,
  481. action_name: name,
  482. name,
  483. img: img,
  484. is_show: is_show,
  485. weight: weight,
  486. route: route,
  487. is_display: is_display,
  488. url: component,
  489. action_code: code,
  490. status: is_show,
  491. level
  492. }
  493. if (this.level === 1) {
  494. delete model['pid']
  495. delete model['menuid']
  496. delete model['url']
  497. delete model['action_code']
  498. delete model['action_name']
  499. delete model['status']
  500. } else if (this.level === 2) {
  501. delete model['menuid']
  502. delete model['action_code']
  503. delete model['action_name']
  504. delete model['status']
  505. } else {
  506. delete model['pid']
  507. delete model['img']
  508. delete model['is_show']
  509. delete model['weight']
  510. delete model['route']
  511. delete model['is_display']
  512. delete model['url']
  513. delete model['name']
  514. }
  515. if (this.isAdd) {
  516. delete model['id']
  517. }
  518. if (this.level < 3) {
  519. res = await asyncRequest.update(model)
  520. } else {
  521. if (this.isAdd) {
  522. res = await asyncRequest.actionadd(model)
  523. } else {
  524. res = await asyncRequest.actionedit(model)
  525. }
  526. }
  527. this.loading = false
  528. if (res && res.code === 0) {
  529. this.ruleForm = {}
  530. const title = this.isAdd ? '添加成功' : '修改成功'
  531. this.$notify.success({
  532. title,
  533. message: ''
  534. })
  535. this.showModelThis = false
  536. let pids = this.parentIds
  537. if (this.id === 0) {
  538. pids = '0'
  539. }
  540. this.$emit('refresh', this.id, pids, this.level)
  541. } else if (res && res.code >= 100 && res.code <= 104) {
  542. await this.logout()
  543. } else {
  544. this.$message.warning(res.message)
  545. }
  546. } else {
  547. this.loading = false
  548. return false
  549. }
  550. })
  551. }
  552. }
  553. }
  554. </script>
  555. <style lang="scss" scoped>
  556. .g-block {
  557. display: inline-block;
  558. padding: 10px 5px;
  559. text-align: center;
  560. border: 1px #ccc dashed;
  561. margin: 1px;
  562. font-size: 0px;
  563. cursor: pointer;
  564. i {
  565. font-size: 30px;
  566. line-height: 30px;
  567. display: block;
  568. margin: 0 auto;
  569. width: 30px;
  570. }
  571. }
  572. </style>