index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <div class="sy-content">
  3. <div>
  4. <el-row v-if="$route.meta.showRole">
  5. <el-col :span="12">
  6. <!-- 搜索信息 -->
  7. <el-form
  8. v-show="showSearch"
  9. ref="queryForm"
  10. :model="queryParams"
  11. size="small"
  12. :inline="true"
  13. label-width="80px"
  14. >
  15. <el-form-item label="" prop="roleName">
  16. <el-input
  17. v-model="queryParams.roleName"
  18. placeholder="请输入名称"
  19. prefix-icon="el-icon-search"
  20. clearable
  21. @keyup.enter.native="handleQuery"
  22. />
  23. </el-form-item>
  24. <el-form-item>
  25. <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
  26. <el-button type="info" icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
  27. </el-form-item>
  28. </el-form>
  29. </el-col>
  30. <el-col :span="12">
  31. <!-- 操作按钮 -->
  32. <el-row class="czBtns" style="float: right;">
  33. <el-col :span="1.5">
  34. <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
  35. </el-col>
  36. <el-col :span="1.5" style="margin-left: 15px;">
  37. <el-button
  38. type="danger"
  39. size="small"
  40. :disabled="multiple"
  41. icon="el-icon-delete-solid"
  42. @click="handleDelete"
  43. >
  44. 批量删除
  45. </el-button>
  46. </el-col>
  47. </el-row>
  48. </el-col>
  49. </el-row>
  50. <el-row v-if="$route.meta.showRole">
  51. <el-col>
  52. <!-- 表格数据信息 -->
  53. <el-table
  54. ref="roleTable"
  55. v-loading="loading"
  56. :data="roleList"
  57. stripe
  58. border
  59. header-row-class-name="headBackground"
  60. @selection-change="handleSelectionChange"
  61. >
  62. <el-table-column type="selection" width="50" align="left" />
  63. <el-table-column v-if="false" key="id" label="编号" align="left" prop="id" />
  64. <el-table-column key="roleName" label="名称" align="left" prop="roleName" :show-overflow-tooltip="true" />
  65. <el-table-column key="roleCode" label="权限字符" align="left" prop="roleCode" :show-overflow-tooltip="true" />
  66. <el-table-column label="操作" align="left" width="400" class-name="small-padding fixed-width">
  67. <template v-if="scope.row.userId !== 1" slot-scope="scope">
  68. <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
  69. <el-button
  70. size="mini"
  71. type="text"
  72. icon="el-icon-delete"
  73. :disabled="!multiple"
  74. @click="handleDelete(scope.row)"
  75. >删除</el-button>
  76. <el-button
  77. size="mini"
  78. type="text"
  79. icon="el-icon-user"
  80. @click="handleAuthUser(scope.row)"
  81. >分配用户</el-button>
  82. <el-button
  83. size="mini"
  84. type="text"
  85. icon="el-icon-menu"
  86. @click="handleAuthMenu(scope.row)"
  87. >分配菜单</el-button>
  88. </template>
  89. </el-table-column>
  90. </el-table>
  91. <!-- 分页信息 -->
  92. <pagination
  93. v-show="total > 0"
  94. :total="total"
  95. :page.sync="queryParams.page"
  96. :limit.sync="queryParams.limit"
  97. align="right"
  98. @pagination="getRoleList"
  99. />
  100. </el-col>
  101. </el-row>
  102. <!-- 添加或修改对话框 -->
  103. <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
  104. <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  105. <el-row>
  106. <el-col>
  107. <el-form-item label="角色名称" prop="roleName">
  108. <el-input v-model="form.roleName" placeholder="请输入名称" maxlength="20" />
  109. </el-form-item>
  110. </el-col>
  111. </el-row>
  112. <el-row>
  113. <el-col>
  114. <el-form-item label="权限字符" prop="roleCode">
  115. <el-input v-model="form.roleCode" placeholder="请输入权限字符" maxlength="20" />
  116. </el-form-item>
  117. </el-col>
  118. </el-row>
  119. </el-form>
  120. <div slot="footer" class="dialog-footer">
  121. <el-button type="primary" size="mini" @click="submitForm">确定</el-button>
  122. <el-button size="mini" @click="cancel">取消</el-button>
  123. </div>
  124. </el-dialog>
  125. </div>
  126. <router-view />
  127. </div>
  128. </template>
  129. <script>
  130. import { getRoleList, addRoleInfo, updateRoleInfo, delRolesById } from '@/api/system/role'
  131. export default {
  132. name: 'Index',
  133. data() {
  134. // 添加角色名称验证
  135. const validatePass = (rule, value, callback) => {
  136. var reg = /^[^\s]{2,20}$/
  137. if (!(reg.test(value))) {
  138. callback(new Error('名称长度必须介于 2 和 20 之间 不能有空格'))
  139. }
  140. callback()
  141. }
  142. return {
  143. // 遮罩层
  144. loading: true,
  145. // 显示搜索条件
  146. showSearch: true,
  147. // 非多个禁用
  148. multiple: true,
  149. // 查询参数
  150. queryParams: {
  151. page: 1,
  152. limit: 10,
  153. roleName: undefined,
  154. roleCode: undefined
  155. },
  156. // 总条数
  157. total: 0,
  158. // 用户表格数据
  159. roleList: null,
  160. // 弹出层标题
  161. title: '',
  162. // 是否显示弹出层
  163. open: false,
  164. // 表单参数
  165. form: {
  166. roleName: null,
  167. roleCode: null
  168. },
  169. // 表单校验
  170. rules: {
  171. roleName: [
  172. { required: true, message: '名称不能为空', trigger: 'blur' },
  173. { validator: validatePass, trigger: 'blur' }
  174. ],
  175. roleCode: [
  176. { required: true, message: '权限字符不能为空', trigger: 'blur' },
  177. { min: 5, max: 20, message: '权限字符长度必须介于 5 和 20 之间', trigger: 'blur' },
  178. { validator: this.testKey }
  179. ]
  180. }
  181. }
  182. },
  183. created() {
  184. if (this.$route.meta.showRole) {
  185. this.getRoleList()
  186. }
  187. },
  188. methods: {
  189. /** 表单验证判断只能输入数字和字母 */
  190. testKey(rule, value, callback) {
  191. const key = /^[\a-\z\A-\Z0-9]+$/
  192. if (!key.test(value)) {
  193. callback(new Error('只能输入数字和字母'))
  194. } else {
  195. callback()
  196. }
  197. },
  198. /** 搜索按钮操作 */
  199. handleQuery(event) {
  200. this.$resetBtn(event)
  201. this.queryParams.page = 1
  202. this.getRoleList()
  203. },
  204. /** 重置按钮操作 */
  205. resetQuery(event) {
  206. this.$refs['queryForm'].resetFields()
  207. this.handleQuery(event)
  208. },
  209. /** 多选框选中数据 */
  210. handleSelectionChange(selection) {
  211. this.ids = selection.map(item => item.id)
  212. this.multiple = !selection.length
  213. },
  214. /** 查询用户列表 */
  215. getRoleList() {
  216. this.loading = true
  217. getRoleList(this.queryParams).then(response => {
  218. const data = response.data
  219. this.roleList = data.roleList
  220. this.total = data.count
  221. this.loading = false
  222. })
  223. },
  224. /** 删除按钮操作 */
  225. handleDelete(row) {
  226. const roleId = row.id
  227. if (!roleId) {
  228. const roleIdList = this.ids
  229. if (!roleIdList || roleIdList.length === 0) {
  230. this.$message({
  231. message: '请选择您要删除的角色',
  232. type: 'warning'
  233. })
  234. return
  235. }
  236. this.$confirm('您确定要删除选中的角色吗?', '系统提示', {
  237. confirmButtonText: '确定',
  238. cancelButtonText: '取消',
  239. cancelButtonClass: 'btn_custom_cancel',
  240. closeOnClickModal: false,
  241. type: 'warning'
  242. }).then(() => {
  243. if (this.roleList.length === roleIdList.length && this.queryParams.page > 1) {
  244. this.queryParams.page -= 1
  245. }
  246. this.delRolesById(roleIdList)
  247. }).catch(() => {
  248. this.$refs.roleTable.clearSelection()
  249. })
  250. return
  251. }
  252. this.$confirm('您确定要删除角色【' + row.roleName + '】吗?', '系统提示', {
  253. confirmButtonText: '确定',
  254. cancelButtonText: '取消',
  255. cancelButtonClass: 'btn_custom_cancel',
  256. closeOnClickModal: false,
  257. type: 'warning'
  258. }).then(() => {
  259. if (this.roleList.length === 1 && this.queryParams.page > 1) {
  260. this.queryParams.page -= 1
  261. }
  262. this.delRolesById([roleId])
  263. }).catch(() => {
  264. })
  265. },
  266. /** 删除角色请求 */
  267. delRolesById(ids) {
  268. delRolesById(ids).then(res => {
  269. if (res.code === 200) {
  270. this.$message({
  271. message: '删除角色成功!',
  272. type: 'success'
  273. })
  274. this.getRoleList()
  275. }
  276. }).catch(() => {
  277. this.$refs.roleTable.clearSelection()
  278. })
  279. },
  280. /** 修改按钮操作 */
  281. handleUpdate(row) {
  282. this.reset()
  283. this.form = {
  284. id: row.id,
  285. roleName: row.roleName,
  286. roleCode: row.roleCode
  287. }
  288. this.open = true
  289. this.title = '修改角色'
  290. },
  291. /** 分配角色人员 */
  292. handleAuthUser(row) {
  293. const roleId = row.id
  294. this.$router.push({
  295. path: '/system/role/role-auth',
  296. query: {
  297. id: roleId
  298. }
  299. })
  300. },
  301. /** 分配角色菜单 */
  302. handleAuthMenu(row) {
  303. const roleId = row.id
  304. this.$router.push({
  305. path: '/system/role/role-menu',
  306. query: {
  307. id: roleId
  308. }
  309. })
  310. },
  311. /** 新增按钮操作 */
  312. handleAdd() {
  313. this.reset()
  314. this.open = true
  315. this.title = '新增角色'
  316. },
  317. /** 表单提交 */
  318. submitForm() {
  319. this.$refs['form'].validate(valid => {
  320. if (valid) {
  321. if (this.form.id !== undefined) {
  322. updateRoleInfo(this.form).then(response => {
  323. this.$message({
  324. message: '修改成功',
  325. type: 'success'
  326. })
  327. this.open = false
  328. this.getRoleList()
  329. })
  330. } else {
  331. addRoleInfo(this.form).then(response => {
  332. this.$message({
  333. message: '新增成功',
  334. type: 'success'
  335. })
  336. this.open = false
  337. this.getRoleList()
  338. })
  339. }
  340. }
  341. })
  342. },
  343. /** 表单重置 */
  344. cancel() {
  345. this.open = false
  346. this.reset()
  347. },
  348. /** 表单重置 */
  349. reset() {
  350. this.form = {
  351. id: undefined,
  352. roleName: undefined,
  353. roleCode: undefined
  354. }
  355. if (this.$refs['form']) {
  356. this.$refs['form'].resetFields()
  357. }
  358. }
  359. }
  360. }
  361. </script>
  362. <style rel="stylesheet/scss" lang="scss">
  363. </style>