UserInput.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <template>
  2. <div v-clickoutside="handleClose" @click="handleClose">
  3. <!--已选列表-->
  4. <div v-if="multipleLists.length > 0" class="user-id-multiple">
  5. <div v-for="(item, index) in multipleLists" :key="index" class="user-id-multiple-item">
  6. <Tag @on-close="multipleLists.splice(index,1)" :closable="!existMultipleDisabled(item.username)"><UserView :username="item.username" showimg/></Tag>
  7. </div>
  8. </div>
  9. <!--输入框区域-->
  10. <div class="user-id-input" ref="reference">
  11. <Input v-model="nickName"
  12. :placeholder="placeholder"
  13. :disabled="disabled"
  14. icon="md-search"
  15. @on-click="searchEnter"
  16. @on-enter="searchEnter"
  17. @on-blur="searchEnter(true)"
  18. @on-change="inputChange">
  19. <div v-if="$slots.prepend !== undefined" slot="prepend"><slot name="prepend"></slot></div>
  20. <div v-if="$slots.append !== undefined" slot="append"><slot name="append"></slot></div>
  21. </Input>
  22. <div v-if="userName" class="user-id-subtitle">{{$L('用户名')}}: {{userName}}</div>
  23. <div v-if="spinShow" class="user-id-spin"><div><w-loading></w-loading></div></div>
  24. </div>
  25. <!--弹出选择表-->
  26. <transition name="fade">
  27. <div
  28. v-show="!disabled && visible"
  29. ref="popper"
  30. class="user-id-input-body"
  31. :data-transfer="transfer"
  32. v-transfer-dom>
  33. <div class="user-id-input-table">
  34. <Table highlight-row
  35. v-if="searchShow"
  36. ref="myTable"
  37. size="small"
  38. class="tableSelection"
  39. :style="tableStyle"
  40. :columns="columns"
  41. :data="userLists"
  42. @on-row-click="userChange"
  43. @on-selection-change="userSelect"
  44. :no-data-text="noDataText"></Table>
  45. <div v-if="isConfirm&&searchShow" class="user-id-input-bottom">
  46. <Button type="primary" size="small" @click="onConfirm">{{$L('确定')}}</Button>
  47. </div>
  48. </div>
  49. </div>
  50. </transition>
  51. </div>
  52. </template>
  53. <style lang="scss" scoped>
  54. .user-id-multiple {
  55. margin-bottom: 6px;
  56. overflow: auto;
  57. white-space: normal;
  58. .ivu-tag {
  59. padding-left: 7px;
  60. }
  61. .user-id-multiple-item {
  62. display: inline-block;
  63. }
  64. .user-view-inline {
  65. height: 20px;
  66. line-height: 20px;
  67. vertical-align: top;
  68. }
  69. }
  70. .user-id-input {
  71. display: inline-block;
  72. width: 100%;
  73. position: relative;
  74. vertical-align: middle;
  75. z-index: 5;
  76. .user-id-subtitle {
  77. position: absolute;
  78. top: 2px;
  79. right: 32px;
  80. height: 30px;
  81. line-height: 30px;
  82. color: #cccccc;
  83. z-index: 2;
  84. }
  85. .user-id-spin {
  86. width: 14px;
  87. height: 14px;
  88. position: absolute;
  89. top: 50%;
  90. right: 34px;
  91. transform: translate(0, -50%);
  92. display: flex;
  93. align-items: center;
  94. > div {
  95. width: 100%;
  96. height: 100%;
  97. }
  98. }
  99. }
  100. </style>
  101. <style lang="scss">
  102. .user-id-input-body {
  103. z-index: 99999
  104. }
  105. .user-id-input-table {
  106. display: flex;
  107. flex-direction: column;
  108. border-radius: 4px;
  109. overflow: hidden;
  110. box-shadow: 0 0 4px 2px rgba(0, 0, 0, 0.1);
  111. background-color: #ffffff;
  112. .ivu-table table {
  113. width: 100% !important;
  114. }
  115. .ivu-table:before, .ivu-table:after {
  116. display: none !important;
  117. }
  118. .ivu-table-body {
  119. max-height: 180px;
  120. overflow: auto;
  121. }
  122. .ivu-table-small td {
  123. cursor: pointer;
  124. }
  125. .user-id-input-bottom {
  126. padding: 8px;
  127. display: flex;
  128. align-items: center;
  129. justify-content: flex-end;
  130. > button {
  131. font-size: 13px;
  132. }
  133. }
  134. }
  135. </style>
  136. <script>
  137. import clickoutside from '../../_modules/directives/clickoutside';
  138. import TransferDom from '../../_modules/directives/transfer-dom';
  139. import Popper from '../../_modules/directives/popper-novalue';
  140. export default {
  141. name: 'UserInput',
  142. directives: {clickoutside, TransferDom},
  143. mixins: [Popper],
  144. props: {
  145. placement: {
  146. default: 'bottom'
  147. },
  148. value: {
  149. default: ''
  150. },
  151. identity: {
  152. default: ''
  153. },
  154. noidentity: {
  155. default: ''
  156. },
  157. nousername: {
  158. default: ''
  159. },
  160. noprojectid: {
  161. default: ''
  162. },
  163. projectid: {
  164. default: ''
  165. },
  166. nobookid: {
  167. default: ''
  168. },
  169. nogroupid: {
  170. default: ''
  171. },
  172. placeholder: {
  173. default: ''
  174. },
  175. disabled: {
  176. type: Boolean,
  177. default: false
  178. },
  179. transfer: {
  180. type: Boolean,
  181. default () {
  182. return true;
  183. }
  184. },
  185. loadstatus: {
  186. default: false
  187. },
  188. multiple: {
  189. type: Boolean,
  190. default: false
  191. },
  192. multipleDisabled: {
  193. default: ''
  194. }
  195. },
  196. data () {
  197. return {
  198. multipleLists: [],
  199. userName: '',
  200. nickName: '',
  201. nickName__: '',
  202. seleName: '',
  203. searchShow: false,
  204. spinShow: false,
  205. skipSearch: false,
  206. isConfirm: false,
  207. tempName: '',
  208. winStyle: {},
  209. columns: [],
  210. userLists: [],
  211. noDataText: '',
  212. }
  213. },
  214. created() {
  215. this.columns = [
  216. {
  217. "title": this.$L("头像"),
  218. "width": 60,
  219. "align": 'center',
  220. render: (h, params) => {
  221. return h('UserImg', {
  222. props: {
  223. info: params.row,
  224. },
  225. style: {
  226. width: "28px",
  227. height: "28px",
  228. fontSize: "14px",
  229. lineHeight: "28px",
  230. borderRadius: "14px",
  231. verticalAlign: "middle"
  232. },
  233. });
  234. }
  235. }, {
  236. "title": this.$L("用户名"),
  237. "key": "username",
  238. "minWidth": 80,
  239. "ellipsis": true,
  240. }, {
  241. "title": this.$L("昵称"),
  242. "key": "nickname",
  243. "minWidth": 80,
  244. "ellipsis": true,
  245. render: (h, params) => {
  246. return h('span', params.row.nickname || '-');
  247. }
  248. }
  249. ];
  250. if (this.multiple) {
  251. this.columns.unshift({
  252. type: 'selection',
  253. width: 30,
  254. align: 'center'
  255. });
  256. }
  257. this.noDataText = this.$L("数据加载中.....");
  258. this.isConfirm = this.$listeners['on-confirm'];
  259. },
  260. watch: {
  261. value (val) {
  262. if (this.multiple) {
  263. this.multipleLists = this.formatMultipleLists(val);
  264. return;
  265. }
  266. this.userName = $A.cloneData(val)
  267. },
  268. userName (val) {
  269. if (this.skipSearch === true) {
  270. this.skipSearch = false;
  271. }else{
  272. this.nickName = '';
  273. if (val) {
  274. let where = { usernameequal: val };
  275. if (typeof this.identity === "string") {
  276. where['identity'] = this.identity;
  277. }
  278. if (typeof this.noidentity === "string") {
  279. where['noidentity'] = this.noidentity;
  280. }
  281. if (typeof this.nousername === "string") {
  282. where['nousername'] = this.nousername;
  283. }
  284. if (this.noprojectid) {
  285. where['noprojectid'] = this.noprojectid;
  286. }
  287. if (this.projectid) {
  288. where['projectid'] = this.projectid;
  289. }
  290. if (this.nobookid) {
  291. where['nobookid'] = this.nobookid;
  292. }
  293. if (this.nogroupid) {
  294. where['nogroupid'] = this.nogroupid;
  295. }
  296. this.noDataText = this.$L("数据加载中.....");
  297. $A.apiAjax({
  298. url: 'users/searchinfo',
  299. data: {
  300. where: where,
  301. take: 1
  302. },
  303. beforeSend: () => {
  304. this.spinShow = true;
  305. },
  306. complete: () => {
  307. this.spinShow = false;
  308. this.noDataText = this.$L("没有相关的数据");
  309. },
  310. error: () => {
  311. this.noDataText = this.$L("数据加载失败!");
  312. },
  313. success: (res) => {
  314. if (res.ret === 1 && $A.count(res.data) > 0) {
  315. let tmpData = res.data[0];
  316. if (this.multiple) {
  317. this.addMultipleLists(tmpData);
  318. } else {
  319. this.userName = tmpData.username;
  320. this.seleName = tmpData.nickname || tmpData.username;
  321. this.nickName = tmpData.nickname || tmpData.username;
  322. this.nickName__ = tmpData.nickname || tmpData.username;
  323. this.$emit('input', this.userName);
  324. this.$emit('change', tmpData);
  325. }
  326. }
  327. }
  328. });
  329. }
  330. }
  331. },
  332. nickName(val) {
  333. if (val != this.seleName || val == '') {
  334. this.userName = '';
  335. if (!this.multiple) {
  336. this.$emit('input', this.userName);
  337. this.$emit('change', {});
  338. }
  339. }
  340. },
  341. spinShow(val) {
  342. if (typeof this.loadstatus === 'number') {
  343. this.$emit('update:loadstatus', val ? this.loadstatus + 1 : this.loadstatus - 1);
  344. }else if (typeof this.loadstatus === 'boolean') {
  345. this.$emit('update:loadstatus', val);
  346. }
  347. },
  348. searchShow(val) {
  349. if (val) {
  350. this.handleShowPopper();
  351. this.updateMultipleLists();
  352. } else {
  353. this.handleClosePopper();
  354. }
  355. },
  356. multipleLists: {
  357. handler() {
  358. if (this.searchShow) {
  359. this.updateMultipleLists();
  360. }
  361. this.emitMultipleLists();
  362. },
  363. deep: true
  364. }
  365. },
  366. computed: {
  367. tableStyle() {
  368. return this.winStyle;
  369. }
  370. },
  371. methods: {
  372. inputChange() {
  373. const val = this.nickName;
  374. this.spinShow = false;
  375. this.tempName = '';
  376. if (val != '') {
  377. setTimeout(() => {
  378. if (val == this.nickName && val != this.tempName) {
  379. this.searchEnter(false, (res) => {
  380. return val == this.nickName;
  381. });
  382. }
  383. }, 500)
  384. } else {
  385. this.searchShow = false;
  386. }
  387. },
  388. handleShowPopper() {
  389. if (this.timeout) clearTimeout(this.timeout);
  390. this.timeout = setTimeout(() => {
  391. this.visible = true;
  392. }, this.delay);
  393. },
  394. handleClosePopper() {
  395. if (this.timeout) {
  396. clearTimeout(this.timeout);
  397. if (!this.controlled) {
  398. this.timeout = setTimeout(() => {
  399. this.visible = false;
  400. }, 100);
  401. }
  402. }
  403. },
  404. updateStyle() {
  405. this.winStyle = {
  406. width: `${Math.max(this.$el.offsetWidth, 230)}px`,
  407. };
  408. },
  409. emptyAll() {
  410. this.userName = '';
  411. this.nickName = '';
  412. this.nickName__ = '';
  413. this.seleName = '';
  414. this.searchShow = false;
  415. this.spinShow = false;
  416. },
  417. searchEnter(verify, callback) {
  418. if (this.disabled === true) {
  419. return;
  420. }
  421. if (this.spinShow === true) {
  422. return;
  423. }
  424. if (verify === true) {
  425. if (this.nickName === '') {
  426. this.nickName__ = this.nickName;
  427. }
  428. if (this.nickName__ === this.nickName) {
  429. return;
  430. }
  431. }
  432. this.updateStyle();
  433. this.nickName__ = this.nickName;
  434. //
  435. let where = {username: this.nickName};
  436. if (typeof this.identity === "string") {
  437. where['identity'] = this.identity;
  438. }
  439. if (typeof this.noidentity === "string") {
  440. where['noidentity'] = this.noidentity;
  441. }
  442. if (typeof this.nousername === "string") {
  443. where['nousername'] = this.nousername;
  444. }
  445. if (this.noprojectid) {
  446. where['noprojectid'] = this.noprojectid;
  447. }
  448. if (this.projectid) {
  449. where['projectid'] = this.projectid;
  450. }
  451. if (this.nobookid) {
  452. where['nobookid'] = this.nobookid;
  453. }
  454. if (this.nogroupid) {
  455. where['nogroupid'] = this.nogroupid;
  456. }
  457. this.tempName = where.username;
  458. this.noDataText = this.$L("数据加载中.....");
  459. $A.apiAjax({
  460. url: 'users/searchinfo',
  461. data: {
  462. where: where,
  463. take: 30
  464. },
  465. beforeSend: () => {
  466. this.spinShow = true;
  467. },
  468. complete: () => {
  469. this.spinShow = false;
  470. this.noDataText = this.$L("没有相关的数据");
  471. },
  472. error: () => {
  473. this.noDataText = this.$L("数据加载失败!");
  474. },
  475. success: (res) => {
  476. if (typeof callback === "function" && callback(res) === false) {
  477. return;
  478. }
  479. if (res.ret === 1) {
  480. this.userLists = res.data;
  481. this.userLists.forEach((item) => {
  482. if (this.multiple) {
  483. if (this.existMultipleDisabled(item.username)) {
  484. item._disabled = true;
  485. }
  486. } else {
  487. if (item.username == this.userName) {
  488. item._highlight = true;
  489. }
  490. }
  491. });
  492. this.searchShow = true;
  493. } else {
  494. this.$Message.warning(res.msg);
  495. this.emptyAll();
  496. }
  497. }
  498. });
  499. },
  500. userChange(item) {
  501. if (this.multiple) {
  502. if (this.existMultipleDisabled(item.username)) {
  503. return;
  504. }
  505. let tempLists = this.multipleLists.filter(({username}) => username == item.username);
  506. if (tempLists.length > 0) {
  507. this.multipleLists = this.multipleLists.filter(({username}) => username != item.username);
  508. } else {
  509. this.addMultipleLists(item);
  510. }
  511. } else {
  512. this.userName = item.username;
  513. this.seleName = item.nickname || item.username;
  514. this.nickName = item.nickname || item.username;
  515. this.nickName__ = item.nickname || item.username;
  516. this.skipSearch = true;
  517. this.searchShow = false;
  518. this.$emit('input', this.userName);
  519. this.$emit('change', item);
  520. }
  521. },
  522. userSelect() {
  523. if (this.multiple) {
  524. let lists = this.$refs.myTable.objData, item, inThe;
  525. for (let index in lists) {
  526. if (lists.hasOwnProperty(index)) {
  527. item = lists[index];
  528. inThe = this.multipleLists.find(({username}) => username == item.username);
  529. if (item._isChecked) {
  530. !inThe && this.multipleLists.push(item);
  531. } else {
  532. inThe && (this.multipleLists = this.multipleLists.filter(({username}) => username != item.username));
  533. }
  534. }
  535. }
  536. }
  537. },
  538. handleClose(e) {
  539. if (this.multiple && $A(e.target).parents('.user-id-input-table').length > 0) {
  540. return;
  541. }
  542. if (this.searchShow === true) {
  543. this.searchShow = false;
  544. }
  545. },
  546. existMultipleDisabled(username) {
  547. return this.multipleDisabled && $A.strExists(`,${this.multipleDisabled},`, `,${username},`)
  548. },
  549. addMultipleLists(item) {
  550. let inThe = this.multipleLists.find(({username}) => username == item.username);
  551. if (!inThe) {
  552. this.multipleLists.push(item);
  553. }
  554. },
  555. updateMultipleLists() {
  556. this.$nextTick(() => {
  557. let lists = this.$refs.myTable.objData, item, inThe;
  558. for (let index in lists) {
  559. if (lists.hasOwnProperty(index)) {
  560. item = lists[index];
  561. inThe = this.multipleLists.find(({username}) => username == item.username);
  562. this.$set(item, "_isChecked", !!inThe);
  563. }
  564. }
  565. });
  566. },
  567. emitMultipleLists() {
  568. let val = '';
  569. this.multipleLists.forEach((tmp) => {
  570. if (val) {
  571. val+= ",";
  572. }
  573. val+= tmp.username;
  574. });
  575. this.$emit('input', val);
  576. },
  577. formatMultipleLists(val) {
  578. let arr = (val + ",").split(",");
  579. let narr = [];
  580. arr.forEach((uname) => {
  581. if (uname) {
  582. let inn = false;
  583. narr.some((tmp) => {
  584. if (tmp.username == uname) {
  585. return inn = true;
  586. }
  587. })
  588. if (!inn) {
  589. narr.push({
  590. username: uname,
  591. });
  592. }
  593. }
  594. });
  595. return narr;
  596. },
  597. onConfirm(e) {
  598. this.searchShow = false;
  599. this.$emit('on-confirm', e);
  600. }
  601. },
  602. mounted() {
  603. this.updatePopper();
  604. //
  605. if (this.multiple) {
  606. this.multipleLists = this.formatMultipleLists(this.value);
  607. } else if ($A.count(this.value) > 0) {
  608. this.userName = this.value;
  609. }
  610. }
  611. };
  612. </script>