selectPersonnel.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <template>
  2. <el-select v-model="selectPersonnelValue" filterable @visible-change="toggleSelectPrefix" remote
  3. :remote-method="userListRemotemethod" :multiple="multiple" @focus="userFocus" :size="size" collapse-tags
  4. @change="updateValue" clearable :ref="`select${timeRef}`" :style="`width: ${width}`" :class="`custom-select ${user.userNameNeedTranslate == 1 && selectPrefixFlg ? 'setUpInput' : ''}`" :disabled="disabled">
  5. <template #prefix>
  6. <div style="height: 100%;display: flex;align-items: center;">
  7. <!-- 单选 -->
  8. <template v-if="!multiple">
  9. <div v-if="selectPrefixFlg" class="selectSingleChoice" :style="sizeStyle[size]">
  10. <template v-if="getSelectedLabel == '请选择'">
  11. <span class="pleaseChoose">请选择</span>
  12. </template>
  13. <template v-else>
  14. <!-- {{ getSelectedLabel }} -->
  15. <TranslationOpenDataText type='userName' :openid="getSelectedLabel"></TranslationOpenDataText>
  16. </template>
  17. </div>
  18. </template>
  19. <!-- 多选 -->
  20. <template v-if="multiple">
  21. <div class="selectMultiple" :style="sizeStyle[size]">
  22. <template v-if="value.length == 0">
  23. <span class="textSpan pleaseChoose">请选择</span>
  24. </template>
  25. <template v-else>
  26. <el-tag closable type="info" :size="size" class="narrow" style="margin: 0;" @close="userTagClose">
  27. {{
  28. getSelectedLabel
  29. }}
  30. <TranslationOpenDataText type='userName' :openid="getSelectedLabel"></TranslationOpenDataText>
  31. </el-tag>
  32. <el-tag type="info" :size="size" v-if="value.length > 1" class="narrow" style="margin: 0;">+ {{
  33. value.length - 1 }}</el-tag>
  34. </template>
  35. </div>
  36. </template>
  37. <!-- 占位符 -->
  38. <span></span>
  39. </div>
  40. </template>
  41. <!-- 主体显示内容 -->
  42. <div :ref="`mySelectUser${timeRef}`" class="select-user-class">
  43. <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
  44. <span class="floatLeft">
  45. <TranslationOpenDataText type='userName' :openid='item.name'></TranslationOpenDataText>
  46. </span>
  47. <span class="jonumberUser">{{ item.jobNumber }}</span>
  48. </el-option>
  49. <!-- 下滚加载提示 -->
  50. <div class="itemsLoading" v-if="loadingInProgress">
  51. 加载中...
  52. </div>
  53. </div>
  54. </el-select>
  55. </template>
  56. <script>
  57. export default {
  58. name: 'HelloWorld',
  59. props: {
  60. value: { // 双向数据绑定
  61. type: [String, Number, Array],
  62. required: true
  63. },
  64. size: { // 尺寸
  65. type: String,
  66. default: 'small'
  67. },
  68. multiple: { // 是否多选
  69. type: Boolean,
  70. default: () => false
  71. },
  72. width: { // 宽度
  73. type: String,
  74. default: '200px'
  75. },
  76. disabled: {
  77. type: Boolean,
  78. default: () => false
  79. }
  80. },
  81. data() {
  82. return {
  83. user: JSON.parse(sessionStorage.getItem("user")),
  84. options: [],
  85. selectPersonnelValue: this.value,
  86. selectPrefixFlg: true,
  87. timeRef: new Date().getTime(),
  88. updateTrigger: 0, // 用来强制触发计算属性
  89. sizeStyle: {
  90. mini: `height: 24px;`,
  91. small: `height: 28px`,
  92. medium: `height: 32px`,
  93. },
  94. keyword: '',
  95. loadingInProgress: false,
  96. userListloading: false,
  97. userPageIndex: 1,
  98. userPageSize: 50,
  99. userTotal: 0,
  100. cursor: 1,
  101. }
  102. },
  103. watch: {
  104. selectPersonnelValue: {
  105. handler() {
  106. this.$nextTick(() => {
  107. if (this.multiple) {
  108. this.getSelectedLabel
  109. }
  110. });
  111. },
  112. deep: true,
  113. immediate: true
  114. }
  115. },
  116. computed: {
  117. getSelectedLabel() {
  118. this.updateTrigger;
  119. if (!this.multiple) {
  120. const selectedOption = this.options.find(item => item.id === this.selectPersonnelValue)
  121. return selectedOption ? selectedOption.name : '请选择'
  122. } else if (this.multiple) {
  123. if (this.selectPersonnelValue.length === 0) {
  124. return '请选择'
  125. }
  126. const selectVal = this.selectPersonnelValue[0]
  127. const selectedOption = this.options.find(item => item.id === selectVal)
  128. return selectedOption ? selectedOption.name : '请选择'
  129. }
  130. return '请选择'
  131. }
  132. },
  133. methods: {
  134. userListRemotemethod: _.debounce(function (val) {
  135. this.newProjectListPage = 1
  136. this.getUserList(val)
  137. }, 500),
  138. // 随机取名字4-6个字
  139. getRandomName() {
  140. const firstNames = ['张', '李', '王', '刘', '陈', '杨', '黄', '赵'];
  141. const lastNames = ['伟', '芳', '娜', '敏', '军', '洋', '静', '磊'];
  142. const nameLength = Math.floor(Math.random() * 3) + 4; // 生成4到6之间的随机数
  143. let name = '';
  144. for (let i = 0; i < nameLength; i++) {
  145. if (i % 2 === 0) {
  146. name += firstNames[Math.floor(Math.random() * firstNames.length)];
  147. } else {
  148. name += lastNames[Math.floor(Math.random() * lastNames.length)];
  149. }
  150. }
  151. return name;
  152. },
  153. uniqueById(arr) {
  154. return arr.reduce((accumulator, current) => {
  155. if (!accumulator.some(item => item.id === current.id)) {
  156. accumulator.push(current);
  157. }
  158. return accumulator;
  159. }, []);
  160. },
  161. userTagClose() {
  162. this.selectPersonnelValue = this.selectPersonnelValue.splice(0, 1)
  163. },
  164. userFocus() {
  165. console.log('失去焦点了', this.selectPrefixFlg)
  166. if (this.selectPrefixFlg) {
  167. this.userPageIndex = 1
  168. this.getUserList('', false)
  169. }
  170. },
  171. addUserList() {
  172. this.loadingInProgress = true
  173. this.postData(`/user/getSimpleActiveUserListPage`, {
  174. pageIndex: this.userPageIndex,
  175. pageSize: this.userPageSize,
  176. departmentId: '',
  177. keyword: this.keyword,
  178. cursor: '',
  179. userIds: this.multiple ? this.selectPersonnelValue.join(',') : this.selectPersonnelValue
  180. }).then(res => {
  181. const { data = [], total = 0 } = res.data
  182. this.userTotal = total
  183. // this.options = [...new Set([...this.options, ...data].map(JSON.stringify))].map(JSON.parse);
  184. // this.options = this.uniqueById([...this.options, ...newData])
  185. const newData = data.map((item) => {
  186. return {
  187. ...item,
  188. // name: this.getRandomName()
  189. }
  190. })
  191. this.options = this.uniqueById([...this.options, ...newData])
  192. console.log(this.options.length, '<==== this.options')
  193. }).finally(() => {
  194. setTimeout(() => {
  195. this.loadingInProgress = false
  196. }, 500)
  197. })
  198. },
  199. getUserList(keyword = '', flag = true) {
  200. this.keyword = keyword
  201. this.userListloading = flag
  202. this.postData(`/user/getSimpleActiveUserListPage`, {
  203. pageIndex: this.userPageIndex,
  204. pageSize: this.userPageSize,
  205. departmentId: '',
  206. keyword,
  207. cursor: '',
  208. userIds: this.multiple ? this.selectPersonnelValue.join(',') : this.selectPersonnelValue
  209. }).then(res => {
  210. const { data = [], total = 0 } = res.data
  211. this.userTotal = total
  212. // this.options = [...new Set(data.map(JSON.stringify))].map(JSON.parse);
  213. // this.options = this.uniqueById(data);
  214. const dataVal = data.map((item) => {
  215. return {
  216. ...item,
  217. // name: this.getRandomName()
  218. }
  219. })
  220. this.options = this.uniqueById(dataVal);
  221. }).finally(() => {
  222. this.userListloading = false
  223. })
  224. },
  225. async postData(urls, param) { // 单独封装 post 请求 方法
  226. return new Promise((resolve, reject) => {
  227. this.http.post(urls, { ...param },
  228. res => {
  229. if (res.code == 'ok') {
  230. resolve(res)
  231. } else {
  232. this.$message({
  233. message: res.msg,
  234. type: 'error'
  235. })
  236. reject(res)
  237. }
  238. resolve(res)
  239. },
  240. error => {
  241. this.$message({
  242. message: error,
  243. type: "error"
  244. });
  245. reject(error)
  246. }
  247. )
  248. });
  249. },
  250. handleScroll(event) {
  251. const container = event.target;
  252. const { scrollTop, scrollHeight, clientHeight } = container;
  253. const totalPage = Math.ceil(this.userTotal / this.userPageSize);
  254. if (scrollTop + clientHeight >= scrollHeight - 20 && this.userPageIndex < totalPage && !this.loadingInProgress) {
  255. this.loadMoreData()
  256. }
  257. },
  258. loadMoreData() {
  259. this.userPageIndex += 1;
  260. this.addUserList();
  261. },
  262. toggleSelectPrefix(value) {
  263. this.selectPrefixFlg = !value
  264. console.log(this.selectPrefixFlg, '<===== selectPrefixFlg')
  265. if (value) {
  266. this.$nextTick(() => {
  267. this.addScrollListener();
  268. });
  269. } else {
  270. this.removeScrollListener();
  271. }
  272. },
  273. addScrollListener() {
  274. const container = this.$refs[`mySelectUser${this.timeRef}`];
  275. const scrollbar = container.closest('.el-scrollbar__wrap');
  276. if (scrollbar) {
  277. this.loadingInProgressHight = scrollbar.clientHeight - 40
  278. this.removeScrollListener();
  279. const debouncedHandleScroll = _.debounce(this.handleScroll, 200);
  280. scrollbar.addEventListener('scroll', debouncedHandleScroll);
  281. this.scrollListener = debouncedHandleScroll;
  282. }
  283. },
  284. removeScrollListener() {
  285. const container = this.$refs[`mySelectUser${this.timeRef}`];
  286. const scrollbar = container.closest('.el-scrollbar__wrap');
  287. if (scrollbar && this.scrollListener) {
  288. scrollbar.removeEventListener('scroll', this.scrollListener);
  289. this.scrollListener = null;
  290. }
  291. },
  292. updateValue(value) { // 更新数据
  293. if (this.multiple) {
  294. this.updateTrigger += 1 // 强行触发计算属性
  295. }
  296. this.$emit('input', value);
  297. this.$emit('change', value, this.getSelectedLabel);
  298. },
  299. },
  300. mounted() {
  301. this.$nextTick(() => {
  302. this.getUserList()
  303. });
  304. },
  305. beforeDestroy() {
  306. this.removeScrollListener();
  307. }
  308. }
  309. </script>
  310. <style>
  311. .custom-select input::placeholder {
  312. color: transparent !important;
  313. }
  314. .setUpInput input {
  315. color: #fff !important;
  316. }
  317. </style>
  318. <style scoped lang="scss">
  319. .select-base {
  320. position: absolute;
  321. width: auto;
  322. display: flex;
  323. align-items: center;
  324. background: #fff;
  325. box-sizing: border-box;
  326. color: #606266;
  327. white-space: nowrap;
  328. }
  329. .selectSingleChoice {
  330. @extend .select-base;
  331. left: 26px;
  332. transform-origin: left;
  333. }
  334. .selectMultiple {
  335. @extend .select-base;
  336. left: 0;
  337. z-index: 10;
  338. min-width: 40px;
  339. }
  340. .select-user-class {
  341. position: relative;
  342. .itemsLoading {
  343. position: absolute;
  344. left: 0;
  345. bottom: -2px;
  346. width: 100%;
  347. text-align: center;
  348. padding: 10px;
  349. background: #fff;
  350. box-shadow: 0px -4px 20px 0px #999;
  351. font-size: 12px;
  352. color: #999;
  353. z-index: 99;
  354. box-sizing: border-box;
  355. }
  356. }
  357. .narrow {
  358. transform: scale(0.9);
  359. transform-origin: left;
  360. }
  361. .selectMultiple .textSpan {
  362. position: relative;
  363. left: 26px;
  364. }
  365. .pleaseChoose {
  366. color: #C0C4CC;
  367. }
  368. .floatLeft {
  369. float: left;
  370. }
  371. .jonumberUser {
  372. float: right;
  373. color: #8492a6;
  374. }
  375. </style>