editDialog.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <div>
  3. <el-form
  4. :model="menuForm"
  5. :rules="addMenuRules"
  6. ref="addUserForm"
  7. label-width="100px"
  8. class="demo-ruleForm"
  9. >
  10. <el-form-item label="上级目录:" prop="parentId" v-if="activeName !== '1'">
  11. <treeselect
  12. :normalizer="normalizer"
  13. :noChildrenText="'没有子选项'"
  14. :noOptionsText="'没有可选项'"
  15. :noResultsText="'没有匹配的结果'"
  16. :placeholder="'请选择'"
  17. :searchable="true"
  18. v-model="menuForm.parentId"
  19. placeholder="请选择"
  20. :options="menuOptions"
  21. ></treeselect>
  22. </el-form-item>
  23. <el-form-item label="菜单名称:" prop="pname">
  24. <el-input
  25. v-model="menuForm.pname"
  26. placeholder="请输入菜单名称"
  27. ></el-input>
  28. </el-form-item>
  29. <el-form-item label="菜单标识:" prop="pcode">
  30. <el-input
  31. v-model="menuForm.pcode"
  32. placeholder="请输入菜单标识"
  33. ></el-input>
  34. </el-form-item>
  35. <el-form-item label="显示排序:" prop="psort">
  36. <el-input
  37. type="number"
  38. :min="0"
  39. v-model="menuForm.psort"
  40. placeholder="请输入菜单标识"
  41. ></el-input>
  42. </el-form-item>
  43. <el-form-item
  44. label="菜单图标"
  45. prop="piconUrl"
  46. v-if="activeName !== '3' && activeName !== '2'"
  47. >
  48. <el-popover
  49. placement="bottom-start"
  50. width="460"
  51. trigger="click"
  52. @show="$refs['iconSelect'].reset()"
  53. >
  54. <IconSelect
  55. ref="iconSelect"
  56. @selected="selected"
  57. :active-icon="menuForm.piconUrl"
  58. />
  59. <el-input
  60. slot="reference"
  61. v-model="menuForm.piconUrl"
  62. placeholder="点击选择图标"
  63. readonly
  64. >
  65. <svg-icon
  66. v-if="menuForm.piconUrl"
  67. slot="prefix"
  68. :icon-class="menuForm.piconUrl"
  69. style="width: 25px; height: 25px"
  70. />
  71. <i v-else slot="prefix" class="el-icon-search el-input__icon" />
  72. </el-input>
  73. </el-popover>
  74. </el-form-item>
  75. <el-form-item label="路由地址:" prop="purl" v-if="activeName !== '1'">
  76. <el-input
  77. v-model="menuForm.purl"
  78. placeholder="请输入路由地址"
  79. ></el-input>
  80. </el-form-item>
  81. <el-form-item label="是否显示:" prop="permissionShow">
  82. <el-select
  83. v-model="menuForm.permissionShow"
  84. placeholder="请选择状态"
  85. style="width: 100%"
  86. >
  87. <el-option label="显示" :value="1"></el-option>
  88. <el-option label="不显示" :value="2"></el-option>
  89. </el-select>
  90. </el-form-item>
  91. <el-form-item label="状态:" prop="pstat">
  92. <el-select
  93. v-model="menuForm.pstat"
  94. placeholder="请选择状态"
  95. style="width: 100%"
  96. >
  97. <el-option label="启用" :value="1"></el-option>
  98. <el-option label="停用" :value="0"></el-option>
  99. </el-select>
  100. </el-form-item>
  101. </el-form>
  102. </div>
  103. </template>
  104. <script>
  105. import Treeselect from "@riophae/vue-treeselect";
  106. import { addMenuFn, updateMenuFn } from "@/api/system";
  107. import IconSelect from "@/components/IconSelect";
  108. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  109. export default {
  110. components: {
  111. Treeselect,
  112. IconSelect,
  113. },
  114. props: {
  115. activeName: {
  116. type: String,
  117. },
  118. tableData: {
  119. type: Array,
  120. },
  121. checkId: {
  122. type: Number,
  123. },
  124. title: {
  125. type: String,
  126. },
  127. row: {
  128. type: Object,
  129. },
  130. },
  131. data() {
  132. return {
  133. name: "",
  134. menuForm: {
  135. psort: 0,
  136. parentId: null,
  137. pname: null,
  138. purl: null,
  139. pstat: null,
  140. pcode: null,
  141. permissionShow: null,
  142. piconUrl: "",
  143. },
  144. addMenuRules: {
  145. permissionShow: {
  146. required: true,
  147. message: "请选择",
  148. trigger: "change",
  149. },
  150. psort: [{ required: true, message: "请填写显示排序", trigger: "blur" }],
  151. pcode: [{ required: true, message: "请填写菜单标识", trigger: "blur" }],
  152. pname: [{ required: true, message: "请填写菜单名称", trigger: "blur" }],
  153. purl: [{ required: true, message: "请填写路由地址", trigger: "blur" }],
  154. parentId: [
  155. { required: true, message: "请选择上级目录", trigger: "change" },
  156. ],
  157. pstat: [{ required: true, message: "请选择状态", trigger: "change" }],
  158. },
  159. stateOptions: [],
  160. menuOptions: [
  161. { permissionId: 0, permissionName: "主类目", children: [] },
  162. ],
  163. };
  164. },
  165. created() {
  166. this.initializeForm();
  167. },
  168. watch: {
  169. row: {
  170. handler(newVal) {
  171. if (newVal) {
  172. this.initializeForm();
  173. }
  174. },
  175. immediate: true,
  176. deep: true,
  177. },
  178. checkId(newVal) {
  179. if (newVal !== null && newVal !== undefined) {
  180. this.menuForm.parentId = newVal;
  181. this.initializeForm();
  182. }
  183. },
  184. tableData: {
  185. handler(newData) {
  186. this.$nextTick(() => {
  187. this.menuOptions[0].children = [];
  188. this.menuOptions[0].children.push(...newData);
  189. });
  190. },
  191. immediate: true,
  192. deep: true,
  193. },
  194. },
  195. methods: {
  196. // 选择图标
  197. selected(name) {
  198. this.menuForm = {
  199. ...this.menuForm,
  200. piconUrl: name,
  201. };
  202. },
  203. initializeForm() {
  204. this.menuOptions[0].children.push(...this.tableData);
  205. if (this.row && this.title === "编辑" && this.activeName === "2") {
  206. this.menuForm = {
  207. parentId: this.row.parentId,
  208. pname: this.row.permissionName,
  209. purl: this.row.permissionUrl,
  210. pstat: this.row.permissionState,
  211. pcode: this.row.permissionCode,
  212. psort: this.row.permissionSort,
  213. permissionShow: this.row.permissionShow,
  214. };
  215. } else if (this.title === "新增" && this.activeName === "2") {
  216. this.menuForm.parentId = this.checkId;
  217. } else {
  218. this.menuForm.parentId = 0;
  219. }
  220. if (this.row && this.title === "编辑") {
  221. this.menuForm = {
  222. parentId: this.row.parentId,
  223. pname: this.row.permissionName,
  224. purl: this.row.permissionUrl,
  225. pstat: this.row.permissionState,
  226. pcode: this.row.permissionCode,
  227. permissionShow: this.row.permissionShow,
  228. psort: this.row.permissionSort,
  229. piconUrl: this.row.permissionIconUrl,
  230. };
  231. if (this.activeName === "1") {
  232. this.$nextTick(() => {
  233. this.menuForm = {
  234. pname: this.row.permissionName,
  235. pstat: this.row.permissionState,
  236. parentId: 0,
  237. pcode: this.row.permissionCode,
  238. permissionShow: this.row.permissionShow,
  239. psort: this.row.permissionSort,
  240. piconUrl: this.row.permissionIconUrl,
  241. };
  242. });
  243. }
  244. } else if (this.title === "新增" && this.activeName === "2") {
  245. this.menuForm.parentId = this.checkId;
  246. } else {
  247. this.menuForm.parentId = 0;
  248. }
  249. },
  250. normalizer(node) {
  251. const children = Array.isArray(node.children) ? node.children : [];
  252. if (children.length === 0) {
  253. delete node.children;
  254. }
  255. return {
  256. id: node.permissionId,
  257. label: node.permissionName,
  258. children: children.length > 0 ? [...children] : undefined,
  259. };
  260. },
  261. submit() {
  262. this.$refs["addUserForm"].validate((valid) => {
  263. if (valid) {
  264. if (this.title === "编辑" && this.row) {
  265. updateMenuFn({
  266. ...this.menuForm,
  267. ptype: this.activeName,
  268. permissionId: this.row.permissionId,
  269. })
  270. .then((res) => {
  271. this.$message({
  272. type: "success",
  273. message: "编辑成功",
  274. });
  275. this.cancel();
  276. this.$emit("updateList");
  277. })
  278. .catch((error) => {
  279. this.$message({
  280. type: "error",
  281. message: "编辑失败",
  282. });
  283. });
  284. } else if (this.title === "新增") {
  285. addMenuFn({
  286. ...this.menuForm,
  287. purl:
  288. this.menuForm.purl !== null ? this.menuForm.purl : undefined,
  289. ptype: this.activeName,
  290. parentId:
  291. this.activeName === "1" ? undefined : this.menuForm.parentId,
  292. })
  293. .then((res) => {
  294. this.$message({
  295. type: "success",
  296. message: "新增成功",
  297. });
  298. this.cancel();
  299. this.$emit("updateList");
  300. })
  301. .catch((error) => {
  302. this.$message({
  303. type: "error",
  304. message: "新增失败",
  305. });
  306. });
  307. }
  308. } else {
  309. return false;
  310. }
  311. });
  312. },
  313. cancel() {
  314. this.$refs["addUserForm"].resetFields();
  315. this.menuForm = {
  316. parentId: null,
  317. pname: null,
  318. purl: null,
  319. pstat: null,
  320. pcode: null,
  321. psort: 0,
  322. permissionShow: null,
  323. };
  324. },
  325. },
  326. };
  327. </script>
  328. <style scoped lang="scss">
  329. ::v-deep .el-input__prefix {
  330. line-height: 50px !important;
  331. }
  332. .demo-ruleForm {
  333. .el-form-item {
  334. margin-bottom: 25px;
  335. }
  336. }
  337. </style>