userRole.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <MDialog ref="dialog" title="角色分配" @sure="handleSaveRole">
  3. <MForm ref="form" :items="roleFormItems" v-model="roleFormValue">
  4. <template #username>
  5. <el-tag>{{ roleFormValue.username }}</el-tag>
  6. </template>
  7. <template #name>
  8. <el-tag>{{ roleFormValue.name }}</el-tag>
  9. </template>
  10. </MForm>
  11. </MDialog>
  12. </template>
  13. <script>
  14. import {
  15. saveUser
  16. } from '@/api/user'
  17. import { getRoleList } from '@/api/menu'
  18. export default {
  19. name: 'user-role',
  20. data () {
  21. return {
  22. roleFormValue: {
  23. roleId: null
  24. },
  25. itemData: {},
  26. loading: false
  27. }
  28. },
  29. computed: {
  30. roleFormItems () {
  31. return [
  32. {
  33. label: '用户账号',
  34. prop: 'username'
  35. },
  36. {
  37. label: '用户昵称',
  38. prop: 'name'
  39. },
  40. {
  41. label: '角色',
  42. prop: 'roleId',
  43. type: 'select',
  44. options: {
  45. multiple: true,
  46. placeholder: '请选择角色',
  47. filterable: true,
  48. remote: true,
  49. labelText: 'roleName',
  50. labelValue: 'id',
  51. remoteMethod: this.remoteMethod,
  52. valueKey: 'id',
  53. defaultFirstOption: true,
  54. loading: this.loading,
  55. items: this.items
  56. },
  57. rules: [
  58. { required: true, message: '请选择角色', trigger: 'change' }
  59. ]
  60. }
  61. ]
  62. }
  63. },
  64. methods: {
  65. async open (item) {
  66. this.itemData = item
  67. this.roleFormValue = {
  68. username: item.username,
  69. name: item.name,
  70. roleId: item.roleId ? item.roleId.split(',').map(e => +e) : []
  71. }
  72. if (this.roleFormValue.roleId.length > 0) {
  73. try {
  74. const { data } = await getRoleList({ ids: this.roleFormValue.roleId })
  75. this.items = data.records
  76. } catch (error) {
  77. this.items = []
  78. this.$message.error(error)
  79. }
  80. }
  81. this.$refs.dialog.open()
  82. },
  83. async remoteMethod (str) {
  84. this.loading = true
  85. try {
  86. const { data } = await getRoleList({ size: 50, roleName: str })
  87. this.items = data.records
  88. } catch (error) {
  89. this.items = []
  90. this.$message.error(error)
  91. } finally {
  92. this.loading = false
  93. }
  94. },
  95. async handleSaveRole () {
  96. this.$refs.form.validate(async valid => {
  97. if (!valid) {
  98. return
  99. }
  100. try {
  101. await saveUser({
  102. id: this.itemData?.id,
  103. tenantCode: this.itemData.tenantCode,
  104. roleId: this.roleFormValue.roleId.toString()
  105. })
  106. this.$message.success('保存成功')
  107. this.$refs.dialog.close()
  108. this.$emit('refresh')
  109. } catch (error) {
  110. this.$message.error(error)
  111. }
  112. })
  113. }
  114. }
  115. }
  116. </script>
  117. <style lang="scss" scoped>
  118. </style>