index.vue 11 KB

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