UserInput.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <template>
  2. <div v-clickoutside="handleClose" @click="handleClose">
  3. <div v-if="multipleLists.length > 0" class="user-id-multiple">
  4. <Tag v-for="(item, index) in multipleLists" :key="index" @on-close="() => { multipleLists.splice(index, 1); callMultipleLists() }" closable><UserView :username="item.username"/></Tag>
  5. </div>
  6. <div class="user-id-input" ref="reference">
  7. <Input v-model="nickName" :placeholder="placeholder" :disabled="disabled" icon="md-search" @on-click="searchEnter" @on-enter="searchEnter" @on-blur="searchEnter(true)">
  8. <div v-if="$slots.prepend !== undefined" slot="prepend"><slot name="prepend"></slot></div>
  9. <div v-if="$slots.append !== undefined" slot="append"><slot name="append"></slot></div>
  10. </Input>
  11. <div v-if="userName" class="user-id-subtitle">{{$L('用户名')}}: {{userName}}</div>
  12. <div v-if="spinShow" class="user-id-spin"><div><w-loading></w-loading></div></div>
  13. </div>
  14. <transition name="fade">
  15. <div
  16. v-show="!disabled && visible"
  17. ref="popper"
  18. class="user-id-input-body"
  19. :data-transfer="transfer"
  20. v-transfer-dom>
  21. <Table highlight-row
  22. v-if="searchShow"
  23. size="small"
  24. class="user-id-input-table"
  25. :style="tableStyle"
  26. :columns="columns"
  27. :data="userLists"
  28. @on-row-click="userChange"
  29. :no-data-text="noDataText"></Table>
  30. </div>
  31. </transition>
  32. </div>
  33. </template>
  34. <style lang="scss" scoped>
  35. .user-id-multiple {
  36. margin-bottom: 4px;
  37. overflow: auto;
  38. white-space: normal;
  39. }
  40. .user-id-input {
  41. display: inline-block;
  42. width: 100%;
  43. position: relative;
  44. vertical-align: middle;
  45. z-index: 5;
  46. .user-id-subtitle {
  47. position: absolute;
  48. top: 2px;
  49. right: 32px;
  50. height: 30px;
  51. line-height: 30px;
  52. color: #cccccc;
  53. z-index: 2;
  54. }
  55. .user-id-spin {
  56. position: absolute;
  57. top: 0;
  58. bottom: 0;
  59. left: 0;
  60. right: 0;
  61. border-radius: 4px;
  62. background-color: rgba(255, 255, 255, 0.26);
  63. > div {
  64. width: 18px;
  65. height: 18px;
  66. position: absolute;
  67. top: 50%;
  68. left: 6px;
  69. transform: translate(0, -50%);
  70. }
  71. }
  72. }
  73. </style>
  74. <style lang="scss">
  75. .user-id-input-body {
  76. z-index: 99999
  77. }
  78. .user-id-input-table {
  79. border-radius: 4px;
  80. overflow: hidden;
  81. box-shadow: 0 0 4px 2px rgba(0, 0, 0, 0.1);
  82. .ivu-table table {
  83. width: 100% !important;
  84. }
  85. .ivu-table:before, .ivu-table:after {
  86. display: none !important;
  87. }
  88. .ivu-table-body {
  89. max-height: 180px;
  90. overflow: auto;
  91. }
  92. .ivu-table-small td {
  93. cursor: pointer;
  94. }
  95. }
  96. </style>
  97. <script>
  98. import clickoutside from '../../_modules/directives/clickoutside';
  99. import TransferDom from '../../_modules/directives/transfer-dom';
  100. import Popper from '../../_modules/directives/popper-novalue';
  101. export default {
  102. name: 'UserInput',
  103. directives: {clickoutside, TransferDom},
  104. mixins: [Popper],
  105. props: {
  106. placement: {
  107. default: 'bottom'
  108. },
  109. value: {
  110. default: ''
  111. },
  112. identity: {
  113. default: ''
  114. },
  115. noidentity: {
  116. default: ''
  117. },
  118. nousername: {
  119. default: ''
  120. },
  121. noprojectid: {
  122. default: ''
  123. },
  124. projectid: {
  125. default: ''
  126. },
  127. placeholder: {
  128. default: ''
  129. },
  130. disabled: {
  131. type: Boolean,
  132. default: false
  133. },
  134. transfer: {
  135. type: Boolean,
  136. default () {
  137. return true;
  138. }
  139. },
  140. loadstatus: {
  141. default: false
  142. },
  143. multiple: {
  144. type: Boolean,
  145. default: false
  146. }
  147. },
  148. data () {
  149. return {
  150. multipleLists: [],
  151. userName: '',
  152. nickName: '',
  153. nickName__: '',
  154. seleName: '',
  155. searchShow: false,
  156. spinShow: false,
  157. skipSearch: false,
  158. winStyle: {},
  159. columns: [],
  160. userLists: [],
  161. noDataText: '',
  162. }
  163. },
  164. created() {
  165. this.columns = [
  166. {
  167. "title": this.$L("用户名"),
  168. "key": "username",
  169. "minWidth": 80,
  170. "ellipsis": true,
  171. "tooltip": true,
  172. render: (h, params) => {
  173. let arr = [];
  174. let username = params.row.username;
  175. let mLists = this.multipleLists.filter((item) => { return item.username == username; });
  176. if (mLists.length > 0) {
  177. arr.push(h('Icon', {
  178. props: { type: 'md-checkmark' },
  179. style: { marginRight: '6px', fontSize: '16px', color: '#FF5722' },
  180. }));
  181. }
  182. arr.push(h('span', username));
  183. return h('div', arr);
  184. }
  185. }, {
  186. "title": this.$L("昵称"),
  187. "key": "nickname",
  188. "minWidth": 80,
  189. "ellipsis": true,
  190. "tooltip": true,
  191. render: (h, params) => {
  192. return h('span', params.row.nickname || '-');
  193. }
  194. }
  195. ];
  196. this.noDataText = this.$L("数据加载中.....");
  197. },
  198. watch: {
  199. value (val) {
  200. if (this.multiple) {
  201. this.multipleLists = this.formatMultipleLists(val);
  202. return;
  203. }
  204. this.userName = $A.cloneData(val)
  205. },
  206. userName (val) {
  207. if (this.skipSearch === true) {
  208. this.skipSearch = false;
  209. }else{
  210. this.nickName = '';
  211. if (val) {
  212. let where = { usernameequal: val };
  213. if (typeof this.identity === "string") {
  214. where['identity'] = this.identity;
  215. }
  216. if (typeof this.noidentity === "string") {
  217. where['noidentity'] = this.noidentity;
  218. }
  219. if (typeof this.nousername === "string") {
  220. where['nousername'] = this.nousername;
  221. }
  222. if (this.noprojectid) {
  223. where['noprojectid'] = this.noprojectid;
  224. }
  225. if (this.projectid) {
  226. where['projectid'] = this.projectid;
  227. }
  228. this.noDataText = this.$L("数据加载中.....");
  229. $A.aAjax({
  230. url: window.location.origin + '/api/users/searchinfo',
  231. data: {
  232. where: where,
  233. pagesize: 1
  234. },
  235. beforeSend: () => {
  236. this.spinShow = true;
  237. },
  238. complete: () => {
  239. this.spinShow = false;
  240. this.noDataText = this.$L("没有相关的数据");
  241. },
  242. error: () => {
  243. this.noDataText = this.$L("数据加载失败!");
  244. },
  245. success: (res) => {
  246. if (res.ret === 1 && res.data.total > 0) {
  247. let tmpData = res.data[0];
  248. if (this.multiple) {
  249. this.addMultipleLists(tmpData);
  250. } else {
  251. this.userName = tmpData.username;
  252. this.seleName = tmpData.nickname || tmpData.username;
  253. this.nickName = tmpData.nickname || tmpData.username;
  254. this.nickName__ = tmpData.nickname || tmpData.username;
  255. this.$emit('input', this.userName);
  256. this.$emit('change', tmpData);
  257. }
  258. }
  259. }
  260. });
  261. }
  262. }
  263. },
  264. nickName(val) {
  265. if (val != this.seleName || val == '') {
  266. this.userName = '';
  267. if (!this.multiple) {
  268. this.$emit('input', this.userName);
  269. this.$emit('change', {});
  270. }
  271. }
  272. },
  273. spinShow(val) {
  274. if (typeof this.loadstatus === 'number') {
  275. this.$emit('update:loadstatus', val ? this.loadstatus + 1 : this.loadstatus - 1);
  276. }else if (typeof this.loadstatus === 'boolean') {
  277. this.$emit('update:loadstatus', val);
  278. }
  279. },
  280. searchShow(val) {
  281. if (val) {
  282. this.handleShowPopper();
  283. } else {
  284. this.handleClosePopper();
  285. }
  286. }
  287. },
  288. computed: {
  289. tableStyle() {
  290. return this.winStyle;
  291. }
  292. },
  293. methods: {
  294. handleShowPopper() {
  295. if (this.timeout) clearTimeout(this.timeout);
  296. this.timeout = setTimeout(() => {
  297. this.visible = true;
  298. }, this.delay);
  299. },
  300. handleClosePopper() {
  301. if (this.timeout) {
  302. clearTimeout(this.timeout);
  303. if (!this.controlled) {
  304. this.timeout = setTimeout(() => {
  305. this.visible = false;
  306. }, 100);
  307. }
  308. }
  309. },
  310. updateStyle() {
  311. this.winStyle = {
  312. width: `${Math.max(this.$el.offsetWidth, 230)}px`,
  313. };
  314. },
  315. emptyAll() {
  316. this.userName = '';
  317. this.nickName = '';
  318. this.nickName__ = '';
  319. this.seleName = '';
  320. this.searchShow = false;
  321. this.spinShow = false;
  322. },
  323. searchEnter(verify) {
  324. if (this.disabled === true) {
  325. return;
  326. }
  327. if (this.spinShow === true) {
  328. return;
  329. }
  330. if (verify === true) {
  331. if (this.nickName === '') {
  332. this.nickName__ = this.nickName;
  333. }
  334. if (this.nickName__ === this.nickName) {
  335. return;
  336. }
  337. }
  338. this.updateStyle();
  339. this.nickName__ = this.nickName;
  340. //
  341. let where = {username: this.nickName};
  342. if (typeof this.identity === "string") {
  343. where['identity'] = this.identity;
  344. }
  345. if (typeof this.noidentity === "string") {
  346. where['noidentity'] = this.noidentity;
  347. }
  348. if (typeof this.nousername === "string") {
  349. where['nousername'] = this.nousername;
  350. }
  351. if (this.noprojectid) {
  352. where['noprojectid'] = this.noprojectid;
  353. }
  354. if (this.projectid) {
  355. where['projectid'] = this.projectid;
  356. }
  357. this.noDataText = this.$L("数据加载中.....");
  358. $A.aAjax({
  359. url: window.location.origin + '/api/users/searchinfo',
  360. data: {
  361. where: where,
  362. pagesize: 30
  363. },
  364. beforeSend: () => {
  365. this.spinShow = true;
  366. },
  367. complete: () => {
  368. this.spinShow = false;
  369. this.noDataText = this.$L("没有相关的数据");
  370. },
  371. error: () => {
  372. this.noDataText = this.$L("数据加载失败!");
  373. },
  374. success: (res) => {
  375. if (res.ret === 1) {
  376. this.userLists = res.data;
  377. for (let i = 0; i < this.userLists.length; i++) {
  378. if (this.userLists[i].id == this.userName) {
  379. this.userLists[i]['_highlight'] = true;
  380. }
  381. }
  382. this.searchShow = true;
  383. } else {
  384. this.$Message.warning(res.msg);
  385. this.emptyAll();
  386. }
  387. }
  388. });
  389. },
  390. userChange(item) {
  391. if (this.multiple) {
  392. let tempLists = this.multipleLists.filter((res) => { return res.username == item.username });
  393. if (tempLists.length > 0) {
  394. this.multipleLists = this.multipleLists.filter((res) => { return res.username != item.username });
  395. } else {
  396. this.addMultipleLists(item);
  397. }
  398. } else {
  399. this.userName = item.username;
  400. this.seleName = item.nickname || item.username;
  401. this.nickName = item.nickname || item.username;
  402. this.nickName__ = item.nickname || item.username;
  403. this.skipSearch = true;
  404. this.searchShow = false;
  405. this.$emit('input', this.userName);
  406. this.$emit('change', item);
  407. }
  408. },
  409. handleClose(e) {
  410. if (this.multiple && $A(e.target).parents('.user-id-input-table').length > 0) {
  411. return;
  412. }
  413. if (this.searchShow === true) {
  414. this.searchShow = false;
  415. }
  416. },
  417. addMultipleLists(item) {
  418. let inn = false;
  419. this.multipleLists.some((tmp) => {
  420. if (tmp.username == item.username) {
  421. return inn = true;
  422. }
  423. })
  424. if (!inn) {
  425. this.multipleLists.push(item);
  426. this.callMultipleLists();
  427. }
  428. },
  429. callMultipleLists() {
  430. let val = '';
  431. this.multipleLists.forEach((tmp) => {
  432. if (val) {
  433. val+= ",";
  434. }
  435. val+= tmp.username;
  436. });
  437. this.$emit('input', val);
  438. },
  439. formatMultipleLists(val) {
  440. let arr = (val + ",").split(",");
  441. let narr = [];
  442. arr.forEach((uname) => {
  443. if (uname) {
  444. let inn = false;
  445. narr.some((tmp) => {
  446. if (tmp.username == uname) {
  447. return inn = true;
  448. }
  449. })
  450. if (!inn) {
  451. narr.push({
  452. username: uname,
  453. });
  454. }
  455. }
  456. });
  457. return narr;
  458. }
  459. },
  460. mounted() {
  461. this.updatePopper();
  462. //
  463. if ($A.runNum(this.value) > 0) {
  464. this.userName = this.value;
  465. }
  466. if (this.multiple) {
  467. this.multipleLists = this.formatMultipleLists(this.value);
  468. }
  469. }
  470. };
  471. </script>