index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <template>
  2. <div class="sy-content">
  3. <el-row>
  4. <el-col :span="24">
  5. <!-- 表单查询 -->
  6. <el-form ref="queryForm" :model="queryParams" :inline="true" size="small">
  7. <el-form-item label="部门名称" prop="departmentName">
  8. <el-input v-model="queryParams.departmentName" placeholder="请输入部门名称" clear />
  9. </el-form-item>
  10. <el-form-item>
  11. <el-button type="primary" icon="el-icon-search" size="mini" @click="handelSearch">搜索</el-button>
  12. <el-button icon="el-icon-refresh" size="mini" @click="handelReset">重置</el-button>
  13. </el-form-item>
  14. </el-form>
  15. <el-button size="mini" icon="el-icon-plus" type="primary" plain @click="handelAdd">新增</el-button>
  16. <el-button
  17. type="danger"
  18. plain
  19. icon="el-icon-circle-close"
  20. size="mini"
  21. :disabled="multiple"
  22. @click="handelDelAll"
  23. >批量删除</el-button>
  24. <!-- 表格数据信息 -->
  25. <el-table
  26. ref="deptTable"
  27. v-loading="loading"
  28. :data="deptList"
  29. row-key="id"
  30. @selection-change="handleSelectionChange"
  31. >
  32. <el-table-column type="selection" width="50" align="center" />
  33. <el-table-column
  34. key="departmentName"
  35. label="部门名称"
  36. align="center"
  37. prop="departmentName"
  38. show-overflow-tooltip
  39. />
  40. <el-table-column
  41. key="departmentDescribe"
  42. label="部门描述"
  43. align="center"
  44. prop="departmentDescribe"
  45. show-overflow-tooltip
  46. />
  47. <el-table-column key="departmentNum" label="排序号" align="center" prop="departmentNum" />
  48. <el-table-column key="updateTime" label="更新时间" align="center" prop="updateTime" />
  49. <el-table-column key="createTime" label="创建时间" align="center" prop="createTime" />
  50. <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
  51. <template slot-scope="scope">
  52. <el-button size="mini" type="text" icon="el-icon-edit" @click="handelEdit(scope.row.id)">修改
  53. </el-button>
  54. <el-button
  55. size="mini"
  56. type="text"
  57. icon="el-icon-delete"
  58. :disabled="!multiple"
  59. @click="handelDel(scope.row)"
  60. >删除
  61. </el-button>
  62. </template>
  63. </el-table-column>
  64. </el-table>
  65. <!-- 分页信息 -->
  66. <pagination
  67. v-show="total > 0"
  68. :total="total"
  69. :page.sync="queryParams.page"
  70. :limit.sync="queryParams.limit"
  71. align="right"
  72. @pagination="getDeptList"
  73. />
  74. </el-col>
  75. </el-row>
  76. <!-- 弹出框 -->
  77. <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" :close-on-click-modal="false">
  78. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  79. <el-row>
  80. <el-col :span="24">
  81. <el-form-item label="上级部门" prop="parentId">
  82. <selectTree v-model="form.parentId" :options="options" placeholder="请选择上级部门" :key-name="keyName" />
  83. </el-form-item>
  84. </el-col>
  85. </el-row>
  86. <el-row>
  87. <el-col :span="24">
  88. <el-form-item label="部门名称" prop="departmentName">
  89. <el-input v-model.trim="form.departmentName" clearable placeholder="请输入部门名称" />
  90. </el-form-item>
  91. </el-col>
  92. </el-row>
  93. <el-row>
  94. <el-col :span="24">
  95. <el-form-item label="部门描述" prop="departmentDescribe">
  96. <el-input v-model.trim="form.departmentDescribe" clearable placeholder="请输入部门描述" />
  97. </el-form-item>
  98. </el-col>
  99. </el-row>
  100. <el-row>
  101. <el-col :span="24">
  102. <el-form-item label="排序号" prop="departmentNum">
  103. <el-input
  104. v-model.trim="form.departmentNum"
  105. placeholder="请输入排序号"
  106. controls-position="right"
  107. :minlength="1"
  108. :maxlength="5"
  109. />
  110. </el-form-item>
  111. </el-col>
  112. </el-row>
  113. </el-form>
  114. <span slot="footer" class="dialog-footer">
  115. <el-button type="primary" size="mini" @click="handelSubmit('form')">确定</el-button>
  116. <el-button size="mini" @click="handelCancel('form')">取消</el-button>
  117. </span>
  118. </el-dialog>
  119. </div>
  120. </template>
  121. <script>
  122. import { getDeptList, addDept, getDeptById, editDept, delDept, getAllSonMenuList } from '@/api/system/dept.js'
  123. import selectTree from '@/components/SelectTree'
  124. export default {
  125. components: { selectTree },
  126. data() {
  127. const departmentNum = (rule, value, callback) => {
  128. var reg = /^[1-9][0-9]*$/
  129. if (!value) {
  130. callback(new Error('排序号不能为空'))
  131. }
  132. if (!(reg.test(value))) {
  133. callback(new Error('只能输入正整数'))
  134. }
  135. callback()
  136. }
  137. return {
  138. // 遮罩层
  139. loading: true,
  140. // 查询参数
  141. queryParams: {
  142. page: 1,
  143. limit: 10
  144. },
  145. // 总条数
  146. total: 0,
  147. // 表格数据
  148. deptList: [],
  149. // 弹出框
  150. dialogVisible: false,
  151. // 弹出框标题
  152. title: '',
  153. // 弹出框表单
  154. form: {
  155. parentId: 0
  156. },
  157. // 部门树数据
  158. options: [],
  159. // 表单验证
  160. rules: {
  161. departmentName: [
  162. { required: true, message: '部门名称不能为空', trigger: 'blur' }
  163. ],
  164. departmentDescribe: [
  165. { required: true, message: '部门描述不能为空', trigger: 'blur' }
  166. ],
  167. departmentNum: [
  168. { required: true, validator: departmentNum, trigger: 'blur' }
  169. ]
  170. },
  171. // 修改菜单id
  172. id: null,
  173. // 控制批量删除按钮
  174. multiple: true,
  175. // 删除部门集合
  176. depts: [],
  177. // 自定义treeselect键名
  178. keyName: null
  179. }
  180. },
  181. created() {
  182. this.getDeptList()
  183. },
  184. methods: {
  185. /** 获取表格数据 */
  186. getDeptList() {
  187. getDeptList(this.queryParams).then(res => {
  188. if (res.code === 200) {
  189. const data = res.data
  190. this.deptList = data.departmentList
  191. this.total = data.count
  192. this.getOptionsList()
  193. this.loading = false
  194. }
  195. })
  196. },
  197. /** 获取上级部门 */
  198. getOptionsList() {
  199. getAllSonMenuList().then(res => {
  200. const data = res.data
  201. this.options = data
  202. this.keyName = 'departmentName'
  203. })
  204. },
  205. /** 搜索 */
  206. handelSearch(event) {
  207. this.$resetBtn(event)
  208. this.queryParams.page = 1
  209. this.getDeptList()
  210. },
  211. /** 重置按钮操作 */
  212. handelReset(event) {
  213. this.$refs['queryForm'].resetFields()
  214. this.handelSearch(event)
  215. },
  216. /** 新增按钮 */
  217. handelAdd() {
  218. this.dialogVisible = true
  219. this.title = '添加部门'
  220. this.reset()
  221. },
  222. /** 修改按钮 */
  223. handelEdit(id) {
  224. this.id = id
  225. getDeptById({ id }).then(res => {
  226. this.form = res.data
  227. })
  228. this.dialogVisible = true
  229. this.title = '修改部门'
  230. },
  231. /** 表格多选 */
  232. handleSelectionChange(val) {
  233. this.depts = val
  234. this.multiple = !val.length
  235. },
  236. /** 删除按钮 */
  237. handelDel(row) {
  238. this.$confirm('您确定要删除该部门吗?', '提示', {
  239. confirmButtonText: '确定',
  240. cancelButtonText: '取消',
  241. cancelButtonClass: 'btn_custom_cancel',
  242. closeOnClickModal: false,
  243. type: 'warning'
  244. }).then(() => {
  245. if (row.children.length) {
  246. this.$message({
  247. type: 'warning',
  248. message: '删除失败,请先删除下级部门'
  249. })
  250. } else {
  251. delDept([row.id]).then(res => {
  252. if (res.code === 200) {
  253. this.$message({
  254. type: 'success',
  255. message: '删除成功'
  256. })
  257. this.getDeptList()
  258. }
  259. })
  260. }
  261. }).catch(() => {
  262. this.$message({
  263. type: 'info',
  264. message: '已取消删除'
  265. })
  266. })
  267. },
  268. /** 批量删除 */
  269. handelDelAll() {
  270. this.$confirm('您确定要批量删除选中的部门吗?', '提示', {
  271. confirmButtonText: '确定',
  272. cancelButtonText: '取消',
  273. cancelButtonClass: 'btn_custom_cancel',
  274. closeOnClickModal: false,
  275. type: 'warning'
  276. }).then(() => {
  277. try {
  278. this.depts.forEach(o => {
  279. // 选中父级
  280. if (o.children.length) {
  281. this.$message({
  282. type: 'warning',
  283. message: '删除失败,请先删除下级部门'
  284. })
  285. throw Error()
  286. } else {
  287. const ids = this.depts.map(item => item.id)
  288. delDept(ids).then(res => {
  289. if (res.code === 200) {
  290. this.$message({
  291. type: 'success',
  292. message: '删除成功'
  293. })
  294. this.getDeptList()
  295. }
  296. })
  297. }
  298. })
  299. } catch (error) {
  300. console.log(error)
  301. }
  302. }).catch(() => {
  303. this.$refs.deptTable.clearSelection()
  304. this.$message({
  305. type: 'info',
  306. message: '已取消删除'
  307. })
  308. })
  309. },
  310. /** 表单提交 */
  311. handelSubmit(formName) {
  312. this.$refs[formName].validate((valid) => {
  313. if (valid) {
  314. if (this.title === '添加部门') {
  315. addDept(this.form).then(res => {
  316. if (res.code === 200) {
  317. this.$message({
  318. message: res.data,
  319. type: 'success'
  320. })
  321. this.getDeptList()
  322. this.dialogVisible = false
  323. }
  324. })
  325. } else {
  326. this.form.id = this.id
  327. editDept(this.form).then(res => {
  328. if (res.code === 200) {
  329. this.$message({
  330. message: res.data,
  331. type: 'success'
  332. })
  333. this.getDeptList()
  334. this.dialogVisible = false
  335. }
  336. })
  337. }
  338. }
  339. })
  340. },
  341. /** 表单取消 */
  342. handelCancel() {
  343. this.dialogVisible = false
  344. this.reset()
  345. },
  346. /** 表单重置 */
  347. reset() {
  348. if (this.$refs['form']) {
  349. this.$refs['form'].resetFields()
  350. }
  351. }
  352. }
  353. }
  354. </script>
  355. <style lang="scss" rel="stylesheet/scss" scoped>
  356. </style>