Account.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <schema-table
  3. ref="table"
  4. :schema="schema"
  5. >
  6. <confirm-dialog
  7. ref="editDialog"
  8. title="新增账号"
  9. @confirm="onSave"
  10. @cancel="onCancel"
  11. >
  12. <div
  13. v-if="account"
  14. class="c-grid-form u-align-self--center"
  15. >
  16. <span class="c-grid-form__label required">账号:</span>
  17. <div
  18. class="c-grid-form__info"
  19. data-info="仅可包含数字、字母,至少4位"
  20. >
  21. <el-input
  22. v-model.trim="account.username"
  23. placeholder="请填写用户账号"
  24. maxlength="20"
  25. show-word-limit
  26. />
  27. </div>
  28. <span class="c-grid-form__label c-grid-form__auto">分组:</span>
  29. <div class="c-grid-form__auto u-bold">
  30. {{ group.label }}
  31. </div>
  32. </div>
  33. </confirm-dialog>
  34. <el-dialog
  35. :visible.sync="showSettings"
  36. title="设置"
  37. custom-class="c-dialog medium"
  38. >
  39. <settings
  40. v-if="showSettings"
  41. :user="user"
  42. :groups="groups"
  43. @del="onDel"
  44. v-on="$listeners"
  45. />
  46. </el-dialog>
  47. </schema-table>
  48. </template>
  49. <script>
  50. import { mapGetters } from 'vuex'
  51. import {
  52. addUser,
  53. getUsersByGroup
  54. } from '@/api/user'
  55. import Settings from './Settings'
  56. export default {
  57. name: 'Account',
  58. components: {
  59. Settings
  60. },
  61. props: {
  62. groups: {
  63. type: Array,
  64. required: true
  65. },
  66. group: {
  67. type: Object,
  68. required: true
  69. }
  70. },
  71. data () {
  72. return {
  73. schema: {
  74. singlePage: true,
  75. condition: { id: this.group.id },
  76. list: getUsersByGroup,
  77. buttons: [
  78. { type: 'add', on: this.onAdd }
  79. ],
  80. cols: [
  81. { prop: 'username', label: '账号' },
  82. { prop: 'enabled', type: 'tag', render: ({ id, enabled }) => id === this.userId
  83. ? null
  84. : {
  85. type: enabled ? 'success' : 'danger',
  86. label: enabled ? '启用' : '禁用'
  87. } },
  88. { type: 'invoke', use: ({ id }) => id !== this.userId, render: [
  89. { label: '设置', on: this.onSettings }
  90. ] }
  91. ]
  92. },
  93. account: null,
  94. showSettings: false
  95. }
  96. },
  97. computed: {
  98. ...mapGetters(['userId', 'isSuperAdmin'])
  99. },
  100. watch: {
  101. group () {
  102. this.showSettings = false
  103. this.$refs.table.mergeCondition({ id: this.group.id })
  104. }
  105. },
  106. methods: {
  107. onSettings (user) {
  108. this.user = user
  109. this.showSettings = true
  110. },
  111. onDel () {
  112. this.$message({
  113. type: 'success',
  114. message: '注销成功'
  115. })
  116. this.showSettings = false
  117. this.$refs.table.decrease(1)
  118. },
  119. onAdd () {
  120. this.account = {
  121. username: '',
  122. group: this.group
  123. }
  124. this.$refs.editDialog.show()
  125. },
  126. onSave (done) {
  127. const { username, group } = this.account
  128. if (!username) {
  129. this.$message({
  130. type: 'warning',
  131. message: '请填写用户账号'
  132. })
  133. return
  134. }
  135. if (username.length < 4) {
  136. this.$message({
  137. type: 'warning',
  138. message: '用户账号过短'
  139. })
  140. return
  141. }
  142. if (!/^[\da-zA-Z]{4,}$/.test(username)) {
  143. this.$message({
  144. type: 'warning',
  145. message: '用户账号格式错误'
  146. })
  147. return
  148. }
  149. if (!group) {
  150. this.$message({
  151. type: 'warning',
  152. message: '请选择用户所属的分组'
  153. })
  154. return
  155. }
  156. addUser({
  157. username,
  158. groups: this.getGroups(group),
  159. enabled: true,
  160. credentials: [{ type: 'password', value: process.env.VUE_APP_USER_PASSWORD, temporary: true }]
  161. }).then(() => {
  162. done()
  163. this.onCancel()
  164. this.$refs.table.pageTo()
  165. })
  166. },
  167. getGroups (group) {
  168. const groups = [group.path]
  169. let target = group
  170. while (target.parentGroup) {
  171. target = group.parentGroup
  172. groups.unshift(target.path)
  173. }
  174. return groups
  175. },
  176. onCancel () {
  177. this.account = null
  178. }
  179. }
  180. }
  181. </script>