user-component.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!-- 用户组件(单选) -->
  2. <template>
  3. <div>
  4. <el-select
  5. v-model="value"
  6. ref="select"
  7. placeholder="请选择"
  8. clearable
  9. filterable
  10. remote
  11. :remote-method="remoteMethod"
  12. @change = "onChange"
  13. @focus="cancelReadOnly"
  14. @hook:mounted="cancelReadOnly"
  15. @visible-change="cancelReadOnly">
  16. <el-option
  17. v-for="item in options"
  18. :key="item.value"
  19. :label="item.label"
  20. :value="item.value">
  21. </el-option>
  22. <el-option v-if="options.length > 0" label="加载更多" style="font-style: italic; color: #8a979e" value="undefined" @click.native="handleClick()"></el-option>
  23. </el-select>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. name: 'user-component',
  29. props: {
  30. userId: {
  31. type: String,
  32. default: ''
  33. }
  34. },
  35. model: {
  36. prop: 'userId',
  37. event: 'userSelected'
  38. },
  39. watch: {
  40. userId (value) {
  41. this.value = value
  42. // 检查缺失item
  43. this.checkItem(value)
  44. }
  45. },
  46. data () {
  47. return {
  48. value: '',
  49. current: 1,
  50. size: 10,
  51. name: '',
  52. options: [],
  53. loading: false,
  54. noMore: false
  55. }
  56. },
  57. mounted () {
  58. this.init()
  59. },
  60. methods: {
  61. async init () {
  62. this.getList()
  63. },
  64. remoteMethod (query) {
  65. this.options = []
  66. this.current = 1
  67. this.name = query
  68. this.getList()
  69. },
  70. getList () {
  71. this.$http({
  72. url: this.$http.adornUrl(`/user-service/user/list`),
  73. method: 'get',
  74. params: this.$http.adornParams({
  75. 'current': this.current,
  76. 'size': this.size,
  77. 'name': this.name
  78. })
  79. }).then(({data}) => {
  80. if (data && data.code === '200') {
  81. if (this.current > data.data.pages) {
  82. return
  83. }
  84. this.noMore = data.data.records.length >= 10
  85. data.data.records.forEach(item => {
  86. let i = this.options.findIndex(a => a.value === item.userId)
  87. if (i < 0) {
  88. this.options.push({
  89. label: item.name + ' (' + item.orgName + ')',
  90. value: item.userId,
  91. name: item.name,
  92. phone: item.mobile
  93. })
  94. }
  95. })
  96. } else {
  97. this.options = []
  98. }
  99. })
  100. },
  101. handleClick () {
  102. this.loadMore()
  103. },
  104. loadMore () {
  105. this.current ++
  106. this.value = null
  107. this.getList()
  108. },
  109. onChange (item) {
  110. if (item === 'undefined') {
  111. this.value = null
  112. }
  113. this.$emit('userSelected', item)
  114. },
  115. checkItem (code) {
  116. if (!code || !this.options) return
  117. let i = this.options.findIndex(item => item.value === code)
  118. if (i > -1) return
  119. // info
  120. this.$http({
  121. url: this.$http.adornUrl(`/user-service/user/info/${code}`),
  122. method: 'get'
  123. }).then(({data}) => {
  124. if (data && data.code === '200' && data.data) {
  125. this.options.push({
  126. label: data.data.name + ' (' + data.data.orgName + ')',
  127. value: data.data.userId
  128. })
  129. }
  130. })
  131. },
  132. cancelReadOnly (onOff) {
  133. this.$nextTick(() => {
  134. if (!onOff) {
  135. const input = this.$refs.select.$el.querySelector('.el-input__inner')
  136. const timer = setTimeout(() => {
  137. input.removeAttribute('readonly')
  138. clearTimeout(timer)
  139. }, 200)
  140. }
  141. })
  142. }
  143. }
  144. }
  145. </script>
  146. <style scoped>
  147. </style>