editDialogBtn.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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">
  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 label="状态:" prop="pstat">
  44. <el-select
  45. v-model="menuForm.pstat"
  46. placeholder="请选择状态"
  47. style="width: 100%"
  48. >
  49. <el-option label="启用" :value="1"></el-option>
  50. <el-option label="停用" :value="0"></el-option>
  51. </el-select>
  52. </el-form-item>
  53. </el-form>
  54. </div>
  55. </template>
  56. <script>
  57. import Treeselect from "@riophae/vue-treeselect";
  58. import { addMenuFn, updateMenuFn } from "@/api/system";
  59. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  60. export default {
  61. components: {
  62. Treeselect,
  63. },
  64. props: {
  65. activeName: {
  66. type: String,
  67. },
  68. tableData: {
  69. type: Array,
  70. },
  71. checkId: {
  72. type: Number,
  73. },
  74. title: {
  75. type: String,
  76. },
  77. row: {
  78. type: Object,
  79. },
  80. },
  81. data() {
  82. return {
  83. name: "",
  84. menuForm: {
  85. parentId: null,
  86. pname: null,
  87. purl: null,
  88. pstat: null,
  89. pcode: null,
  90. permissionShow: 2,
  91. piconUrl: "",
  92. psort: 0,
  93. },
  94. addMenuRules: {
  95. // permissionShow: {
  96. // required: true,
  97. // message: "请选择",
  98. // trigger: "change",
  99. // },
  100. psort: [{ required: true, message: "请填写显示排序", trigger: "blur" }],
  101. pcode: [{ required: true, message: "请填写权限标识", trigger: "blur" }],
  102. pname: [{ required: true, message: "请填写菜单名称", trigger: "blur" }],
  103. // purl: [{ required: true, message: "请填写路由地址", trigger: "blur" }],
  104. parentId: [
  105. { required: true, message: "请选择上级目录", trigger: "change" },
  106. ],
  107. pstat: [{ required: true, message: "请选择状态", trigger: "change" }],
  108. },
  109. stateOptions: [],
  110. menuOptions: [
  111. { permissionId: 0, permissionName: "主类目", children: [] },
  112. ],
  113. };
  114. },
  115. created() {
  116. this.initializeForm();
  117. },
  118. watch: {
  119. row: {
  120. handler(newVal) {
  121. if (newVal) {
  122. this.initializeForm();
  123. }
  124. },
  125. immediate: true,
  126. deep: true,
  127. },
  128. checkId(newVal) {
  129. if (newVal !== null && newVal !== undefined) {
  130. this.menuForm.parentId = newVal;
  131. this.initializeForm();
  132. }
  133. },
  134. tableData: {
  135. handler(newData) {
  136. this.$nextTick(() => {
  137. this.menuOptions[0].children = [];
  138. this.menuOptions[0].children.push(...newData);
  139. });
  140. },
  141. immediate: true,
  142. deep: true,
  143. },
  144. },
  145. methods: {
  146. // 选择图标
  147. selected(name) {
  148. this.menuForm = {
  149. ...this.menuForm,
  150. piconUrl: name,
  151. };
  152. },
  153. initializeForm() {
  154. this.menuOptions[0].children.push(...this.tableData);
  155. if (this.row && this.title === "编辑") {
  156. this.menuForm = {
  157. parentId: this.row.parentId,
  158. pname: this.row.permissionName,
  159. purl: this.row.permissionUrl,
  160. pstat: this.row.permissionState,
  161. pcode: this.row.permissionCode,
  162. psort: this.row.permissionSort,
  163. permissionShow: 2,
  164. };
  165. } else if (this.title === "新增") {
  166. this.menuForm.parentId = this.checkId;
  167. } else {
  168. this.menuForm.parentId = 0;
  169. }
  170. },
  171. normalizer(node) {
  172. const children = Array.isArray(node.children) ? node.children : [];
  173. if (children.length === 0) {
  174. delete node.children;
  175. }
  176. return {
  177. id: node.permissionId,
  178. label: node.permissionName,
  179. children: children.length > 0 ? [...children] : undefined,
  180. };
  181. },
  182. submit() {
  183. this.$refs["addUserForm"].validate((valid) => {
  184. if (valid) {
  185. if (this.title === "编辑" && this.row) {
  186. updateMenuFn({
  187. ...this.menuForm,
  188. ptype: this.activeName,
  189. permissionId: this.row.permissionId,
  190. })
  191. .then((res) => {
  192. this.$message({
  193. type: "success",
  194. message: "编辑成功",
  195. });
  196. this.cancel();
  197. this.$emit("updateList");
  198. })
  199. .catch((error) => {
  200. this.$message({
  201. type: "error",
  202. message: "编辑失败",
  203. });
  204. });
  205. } else if (this.title === "新增") {
  206. addMenuFn({
  207. ...this.menuForm,
  208. purl:
  209. this.menuForm.purl !== null ? this.menuForm.purl : undefined,
  210. ptype: this.activeName,
  211. parentId: this.menuForm.parentId,
  212. })
  213. .then((res) => {
  214. this.$message({
  215. type: "success",
  216. message: "新增成功",
  217. });
  218. this.cancel();
  219. this.$emit("updateList");
  220. })
  221. .catch((error) => {
  222. this.$message({
  223. type: "error",
  224. message: "新增失败",
  225. });
  226. });
  227. }
  228. } else {
  229. return false;
  230. }
  231. });
  232. },
  233. cancel() {
  234. this.$refs["addUserForm"].resetFields();
  235. this.menuForm = {
  236. parentId: null,
  237. pname: null,
  238. purl: null,
  239. pstat: null,
  240. pcode: null,
  241. permissionShow: 2,
  242. psort: 0,
  243. };
  244. },
  245. },
  246. };
  247. </script>
  248. <style scoped lang="scss">
  249. ::v-deep .el-input__prefix {
  250. line-height: 50px !important;
  251. }
  252. .demo-ruleForm {
  253. .el-form-item {
  254. margin-bottom: 25px;
  255. }
  256. }
  257. </style>