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. .ivu-table-cell {
  126. padding-left: 12px;
  127. padding-right: 12px;
  128. &.ivu-table-cell-with-selection {
  129. padding-right: 2px;
  130. }
  131. }
  132. .user-id-input-bottom {
  133. padding: 8px;
  134. display: flex;
  135. align-items: center;
  136. justify-content: flex-end;
  137. > button {
  138. font-size: 13px;
  139. }
  140. }
  141. }
  142. </style>
  143. <script>
  144. import clickoutside from '../../_modules/directives/clickoutside';
  145. import TransferDom from '../../_modules/directives/transfer-dom';
  146. import Popper from '../../_modules/directives/popper-novalue';
  147. export default {
  148. name: 'UserInput',
  149. directives: {clickoutside, TransferDom},
  150. mixins: [Popper],
  151. props: {
  152. placement: {
  153. default: 'bottom'
  154. },
  155. value: {
  156. default: ''
  157. },
  158. identity: {
  159. default: ''
  160. },
  161. noidentity: {
  162. default: ''
  163. },
  164. nousername: {
  165. default: ''
  166. },
  167. noprojectid: {
  168. default: ''
  169. },
  170. projectid: {
  171. default: ''
  172. },
  173. nobookid: {
  174. default: ''
  175. },
  176. placeholder: {
  177. default: ''
  178. },
  179. disabled: {
  180. type: Boolean,
  181. default: false
  182. },
  183. transfer: {
  184. type: Boolean,
  185. default () {
  186. return true;
  187. }
  188. },
  189. loadstatus: {
  190. default: false
  191. },
  192. multiple: {
  193. type: Boolean,
  194. default: false
  195. },
  196. multipleDisabled: {
  197. default: ''
  198. }
  199. },
  200. data () {
  201. return {
  202. multipleLists: [],
  203. userName: '',
  204. nickName: '',
  205. nickName__: '',
  206. seleName: '',
  207. searchShow: false,
  208. spinShow: false,
  209. skipSearch: false,
  210. isConfirm: false,
  211. tempName: '',
  212. winStyle: {},
  213. columns: [],
  214. userLists: [],
  215. noDataText: '',
  216. }
  217. },
  218. created() {
  219. this.columns = [
  220. {
  221. "title": this.$L("头像"),
  222. "width": 60,
  223. "align": 'center',
  224. render: (h, params) => {
  225. return h('UserImg', {
  226. props: {
  227. info: params.row,
  228. },
  229. style: {
  230. width: "28px",
  231. height: "28px",
  232. fontSize: "14px",
  233. lineHeight: "28px",
  234. borderRadius: "14px",
  235. verticalAlign: "middle"
  236. },
  237. });
  238. }
  239. }, {
  240. "title": this.$L("用户名"),
  241. "key": "username",
  242. "minWidth": 80,
  243. "ellipsis": true,
  244. }, {
  245. "title": this.$L("昵称"),
  246. "key": "nickname",
  247. "minWidth": 80,
  248. "ellipsis": true,
  249. render: (h, params) => {
  250. return h('span', params.row.nickname || '-');
  251. }
  252. }
  253. ];
  254. if (this.multiple) {
  255. this.columns.unshift({
  256. type: 'selection',
  257. width: 30,
  258. align: 'center'
  259. });
  260. }
  261. this.noDataText = this.$L("数据加载中.....");
  262. this.isConfirm = this.$listeners['on-confirm'];
  263. },
  264. watch: {
  265. value (val) {
  266. if (this.multiple) {
  267. this.multipleLists = this.formatMultipleLists(val);
  268. return;
  269. }
  270. this.userName = $A.cloneData(val)
  271. },
  272. userName (val) {
  273. if (this.skipSearch === true) {
  274. this.skipSearch = false;
  275. }else{
  276. this.nickName = '';
  277. if (val) {
  278. let where = { usernameequal: val };
  279. if (typeof this.identity === "string") {
  280. where['identity'] = this.identity;
  281. }
  282. if (typeof this.noidentity === "string") {
  283. where['noidentity'] = this.noidentity;
  284. }
  285. if (typeof this.nousername === "string") {
  286. where['nousername'] = this.nousername;
  287. }
  288. if (this.noprojectid) {
  289. where['noprojectid'] = this.noprojectid;
  290. }
  291. if (this.projectid) {
  292. where['projectid'] = this.projectid;
  293. }
  294. if (this.nobookid) {
  295. where['nobookid'] = this.nobookid;
  296. }
  297. this.noDataText = this.$L("数据加载中.....");
  298. $A.apiAjax({
  299. url: 'users/searchinfo',
  300. data: {
  301. where: where,
  302. take: 1
  303. },
  304. beforeSend: () => {
  305. this.spinShow = true;
  306. },
  307. complete: () => {
  308. this.spinShow = false;
  309. this.noDataText = this.$L("没有相关的数据");
  310. },
  311. error: () => {
  312. this.noDataText = this.$L("数据加载失败!");
  313. },
  314. success: (res) => {
  315. if (res.ret === 1 && $A.count(res.data) > 0) {
  316. let tmpData = res.data[0];
  317. if (this.multiple) {
  318. this.addMultipleLists(tmpData);
  319. } else {
  320. this.userName = tmpData.username;
  321. this.seleName = tmpData.nickname || tmpData.username;
  322. this.nickName = tmpData.nickname || tmpData.username;
  323. this.nickName__ = tmpData.nickname || tmpData.username;
  324. this.$emit('input', this.userName);
  325. this.$emit('change', tmpData);
  326. }
  327. }
  328. }
  329. });
  330. }
  331. }
  332. },
  333. nickName(val) {
  334. if (val != this.seleName || val == '') {
  335. this.userName = '';
  336. if (!this.multiple) {
  337. this.$emit('input', this.userName);
  338. this.$emit('change', {});
  339. }
  340. }
  341. },
  342. spinShow(val) {
  343. if (typeof this.loadstatus === 'number') {
  344. this.$emit('update:loadstatus', val ? this.loadstatus + 1 : this.loadstatus - 1);
  345. }else if (typeof this.loadstatus === 'boolean') {
  346. this.$emit('update:loadstatus', val);
  347. }
  348. },
  349. searchShow(val) {
  350. if (val) {
  351. this.handleShowPopper();
  352. this.updateMultipleLists();
  353. } else {
  354. this.handleClosePopper();
  355. }
  356. },
  357. multipleLists: {
  358. handler() {
  359. if (this.searchShow) {
  360. this.updateMultipleLists();
  361. }
  362. this.emitMultipleLists();
  363. },
  364. deep: true
  365. }
  366. },
  367. computed: {
  368. tableStyle() {
  369. return this.winStyle;
  370. }
  371. },
  372. methods: {
  373. inputChange() {
  374. const val = this.nickName;
  375. this.spinShow = false;
  376. this.tempName = '';
  377. if (val != '') {
  378. setTimeout(() => {
  379. if (val == this.nickName && val != this.tempName) {
  380. this.searchEnter(false, (res) => {
  381. return val == this.nickName;
  382. });
  383. }
  384. }, 500)
  385. } else {
  386. this.searchShow = false;
  387. }
  388. },
  389. handleShowPopper() {
  390. if (this.timeout) clearTimeout(this.timeout);
  391. this.timeout = setTimeout(() => {
  392. this.visible = true;
  393. }, this.delay);
  394. },
  395. handleClosePopper() {
  396. if (this.timeout) {
  397. clearTimeout(this.timeout);
  398. if (!this.controlled) {
  399. this.timeout = setTimeout(() => {
  400. this.visible = false;
  401. }, 100);
  402. }
  403. }
  404. },
  405. updateStyle() {
  406. this.winStyle = {
  407. width: `${Math.max(this.$el.offsetWidth, 230)}px`,
  408. };
  409. },
  410. emptyAll() {
  411. this.userName = '';
  412. this.nickName = '';
  413. this.nickName__ = '';
  414. this.seleName = '';
  415. this.searchShow = false;
  416. this.spinShow = false;
  417. },
  418. searchEnter(verify, callback) {
  419. if (this.disabled === true) {
  420. return;
  421. }
  422. if (this.spinShow === true) {
  423. return;
  424. }
  425. if (verify === true) {
  426. if (this.nickName === '') {
  427. this.nickName__ = this.nickName;
  428. }
  429. if (this.nickName__ === this.nickName) {
  430. return;
  431. }
  432. }
  433. this.updateStyle();
  434. this.nickName__ = this.nickName;
  435. //
  436. let where = {username: this.nickName};
  437. if (typeof this.identity === "string") {
  438. where['identity'] = this.identity;
  439. }
  440. if (typeof this.noidentity === "string") {
  441. where['noidentity'] = this.noidentity;
  442. }
  443. if (typeof this.nousername === "string") {
  444. where['nousername'] = this.nousername;
  445. }
  446. if (this.noprojectid) {
  447. where['noprojectid'] = this.noprojectid;
  448. }
  449. if (this.projectid) {
  450. where['projectid'] = this.projectid;
  451. }
  452. if (this.nobookid) {
  453. where['nobookid'] = this.nobookid;
  454. }
  455. this.tempName = where.username;
  456. this.noDataText = this.$L("数据加载中.....");
  457. $A.apiAjax({
  458. url: 'users/searchinfo',
  459. data: {
  460. where: where,
  461. take: 30
  462. },
  463. beforeSend: () => {
  464. this.spinShow = true;
  465. },
  466. complete: () => {
  467. this.spinShow = false;
  468. this.noDataText = this.$L("没有相关的数据");
  469. },
  470. error: () => {
  471. this.noDataText = this.$L("数据加载失败!");
  472. },
  473. success: (res) => {
  474. if (typeof callback === "function" && callback(res) === false) {
  475. return;
  476. }
  477. if (res.ret === 1) {
  478. this.userLists = res.data;
  479. this.userLists.forEach((item) => {
  480. if (this.multiple) {
  481. if (this.existMultipleDisabled(item.username)) {
  482. item._disabled = true;
  483. }
  484. } else {
  485. if (item.username == this.userName) {
  486. item._highlight = true;
  487. }
  488. }
  489. });
  490. this.searchShow = true;
  491. } else {
  492. this.$Message.warning(res.msg);
  493. this.emptyAll();
  494. }
  495. }
  496. });
  497. },
  498. userChange(item) {
  499. if (this.multiple) {
  500. if (this.existMultipleDisabled(item.username)) {
  501. return;
  502. }
  503. let tempLists = this.multipleLists.filter(({username}) => username == item.username);
  504. if (tempLists.length > 0) {
  505. this.multipleLists = this.multipleLists.filter(({username}) => username != item.username);
  506. } else {
  507. this.addMultipleLists(item);
  508. }
  509. } else {
  510. this.userName = item.username;
  511. this.seleName = item.nickname || item.username;
  512. this.nickName = item.nickname || item.username;
  513. this.nickName__ = item.nickname || item.username;
  514. this.skipSearch = true;
  515. this.searchShow = false;
  516. this.$emit('input', this.userName);
  517. this.$emit('change', item);
  518. }
  519. },
  520. userSelect() {
  521. if (this.multiple) {
  522. let lists = this.$refs.myTable.objData, item, inThe;
  523. for (let index in lists) {
  524. if (lists.hasOwnProperty(index)) {
  525. item = lists[index];
  526. inThe = this.multipleLists.find(({username}) => username == item.username);
  527. if (item._isChecked) {
  528. !inThe && this.multipleLists.push(item);
  529. } else {
  530. inThe && (this.multipleLists = this.multipleLists.filter(({username}) => username != item.username));
  531. }
  532. }
  533. }
  534. }
  535. },
  536. handleClose(e) {
  537. if (this.multiple && $A(e.target).parents('.user-id-input-table').length > 0) {
  538. return;
  539. }
  540. if (this.searchShow === true) {
  541. this.searchShow = false;
  542. }
  543. },
  544. existMultipleDisabled(username) {
  545. return this.multipleDisabled && $A.strExists(`,${this.multipleDisabled},`, `,${username},`)
  546. },
  547. addMultipleLists(item) {
  548. let inThe = this.multipleLists.find(({username}) => username == item.username);
  549. if (!inThe) {
  550. this.multipleLists.push(item);
  551. }
  552. },
  553. updateMultipleLists() {
  554. this.$nextTick(() => {
  555. let lists = this.$refs.myTable.objData, item, inThe;
  556. for (let index in lists) {
  557. if (lists.hasOwnProperty(index)) {
  558. item = lists[index];
  559. inThe = this.multipleLists.find(({username}) => username == item.username);
  560. this.$set(item, "_isChecked", !!inThe);
  561. }
  562. }
  563. });
  564. },
  565. emitMultipleLists() {
  566. let val = '';
  567. this.multipleLists.forEach((tmp) => {
  568. if (val) {
  569. val+= ",";
  570. }
  571. val+= tmp.username;
  572. });
  573. this.$emit('input', val);
  574. },
  575. formatMultipleLists(val) {
  576. let arr = (val + ",").split(",");
  577. let narr = [];
  578. arr.forEach((uname) => {
  579. if (uname) {
  580. let inn = false;
  581. narr.some((tmp) => {
  582. if (tmp.username == uname) {
  583. return inn = true;
  584. }
  585. })
  586. if (!inn) {
  587. narr.push({
  588. username: uname,
  589. });
  590. }
  591. }
  592. });
  593. return narr;
  594. },
  595. onConfirm(e) {
  596. this.searchShow = false;
  597. this.$emit('on-confirm', e);
  598. }
  599. },
  600. mounted() {
  601. this.updatePopper();
  602. //
  603. if (this.multiple) {
  604. this.multipleLists = this.formatMultipleLists(this.value);
  605. } else if ($A.count(this.value) > 0) {
  606. this.userName = this.value;
  607. }
  608. }
  609. };
  610. </script>