UserInput.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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-current-change="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. import WLoading from "./WLoading";
  102. export default {
  103. name: 'UserInput',
  104. components: {WLoading},
  105. directives: {clickoutside, TransferDom},
  106. mixins: [Popper],
  107. props: {
  108. placement: {
  109. default: 'bottom'
  110. },
  111. value: {
  112. default: ''
  113. },
  114. identity: {
  115. default: ''
  116. },
  117. noidentity: {
  118. default: ''
  119. },
  120. nousername: {
  121. default: ''
  122. },
  123. noprojectid: {
  124. default: ''
  125. },
  126. projectid: {
  127. default: ''
  128. },
  129. placeholder: {
  130. default: ''
  131. },
  132. disabled: {
  133. type: Boolean,
  134. default: false
  135. },
  136. transfer: {
  137. type: Boolean,
  138. default () {
  139. return true;
  140. }
  141. },
  142. loadstatus: {
  143. default: false
  144. },
  145. multiple: {
  146. type: Boolean,
  147. default: false
  148. }
  149. },
  150. data () {
  151. return {
  152. multipleLists: [],
  153. userName: '',
  154. nickName: '',
  155. nickName__: '',
  156. seleName: '',
  157. searchShow: false,
  158. spinShow: false,
  159. skipSearch: false,
  160. winStyle: {},
  161. columns: [],
  162. userLists: [],
  163. noDataText: '',
  164. }
  165. },
  166. created() {
  167. this.columns = [
  168. {
  169. "title": this.$L("昵称"),
  170. "key": "nickname",
  171. "minWidth": 80,
  172. "ellipsis": true,
  173. "tooltip": true,
  174. render: (h, params) => {
  175. let arr = [];
  176. let username = params.row.username;
  177. let mLists = this.multipleLists.filter((item) => { return item.username == username; });
  178. if (mLists.length > 0) {
  179. arr.push(h('Icon', {
  180. props: { type: 'md-checkmark' },
  181. style: { marginRight: '6px', fontSize: '16px', color: '#FF5722' },
  182. }));
  183. }
  184. arr.push(h('span', params.row.nickname || '-'));
  185. return h('div', arr);
  186. }
  187. }, {
  188. "title": this.$L("用户名"),
  189. "key": "username",
  190. "minWidth": 80,
  191. "ellipsis": true,
  192. "tooltip": true,
  193. },
  194. ];
  195. this.noDataText = this.$L("数据加载中.....");
  196. },
  197. watch: {
  198. value (val) {
  199. if (this.multiple) {
  200. this.multipleLists = this.formatMultipleLists(val);
  201. return;
  202. }
  203. this.userName = $A.cloneData(val)
  204. },
  205. userName (val) {
  206. if (this.skipSearch === true) {
  207. this.skipSearch = false;
  208. }else{
  209. this.nickName = '';
  210. if (val) {
  211. let where = { usernameequal: val };
  212. if (typeof this.identity === "string") {
  213. where['identity'] = this.identity;
  214. }
  215. if (typeof this.noidentity === "string") {
  216. where['noidentity'] = this.noidentity;
  217. }
  218. if (typeof this.nousername === "string") {
  219. where['nousername'] = this.nousername;
  220. }
  221. if (this.noprojectid) {
  222. where['noprojectid'] = this.noprojectid;
  223. }
  224. if (this.projectid) {
  225. where['projectid'] = this.projectid;
  226. }
  227. this.noDataText = this.$L("数据加载中.....");
  228. $A.aAjax({
  229. url: window.location.origin + '/api/users/searchinfo',
  230. data: {
  231. where: where,
  232. pagesize: 1
  233. },
  234. beforeSend: () => {
  235. this.spinShow = true;
  236. },
  237. complete: () => {
  238. this.spinShow = false;
  239. this.noDataText = this.$L("没有相关的数据");
  240. },
  241. error: () => {
  242. this.noDataText = this.$L("数据加载失败!");
  243. },
  244. success: (res) => {
  245. if (res.ret === 1 && res.data.total > 0) {
  246. let tmpData = res.data[0];
  247. if (this.multiple) {
  248. this.addMultipleLists(tmpData);
  249. } else {
  250. this.userName = tmpData.username;
  251. this.seleName = tmpData.nickname || tmpData.username;
  252. this.nickName = tmpData.nickname || tmpData.username;
  253. this.nickName__ = tmpData.nickname || tmpData.username;
  254. this.$emit('input', this.userName);
  255. this.$emit('change', tmpData);
  256. }
  257. }
  258. }
  259. });
  260. }
  261. }
  262. },
  263. nickName(val) {
  264. if (val != this.seleName || val == '') {
  265. this.userName = '';
  266. if (!this.multiple) {
  267. this.$emit('input', this.userName);
  268. this.$emit('change', {});
  269. }
  270. }
  271. },
  272. spinShow(val) {
  273. if (typeof this.loadstatus === 'number') {
  274. this.$emit('update:loadstatus', val ? this.loadstatus + 1 : this.loadstatus - 1);
  275. }else if (typeof this.loadstatus === 'boolean') {
  276. this.$emit('update:loadstatus', val);
  277. }
  278. },
  279. searchShow(val) {
  280. if (val) {
  281. this.handleShowPopper();
  282. } else {
  283. this.handleClosePopper();
  284. }
  285. }
  286. },
  287. computed: {
  288. tableStyle() {
  289. return this.winStyle;
  290. }
  291. },
  292. methods: {
  293. handleShowPopper() {
  294. if (this.timeout) clearTimeout(this.timeout);
  295. this.timeout = setTimeout(() => {
  296. this.visible = true;
  297. }, this.delay);
  298. },
  299. handleClosePopper() {
  300. if (this.timeout) {
  301. clearTimeout(this.timeout);
  302. if (!this.controlled) {
  303. this.timeout = setTimeout(() => {
  304. this.visible = false;
  305. }, 100);
  306. }
  307. }
  308. },
  309. updateStyle() {
  310. this.winStyle = {
  311. width: `${Math.max(this.$el.offsetWidth, 230)}px`,
  312. };
  313. },
  314. emptyAll() {
  315. this.userName = '';
  316. this.nickName = '';
  317. this.nickName__ = '';
  318. this.seleName = '';
  319. this.searchShow = false;
  320. this.spinShow = false;
  321. },
  322. searchEnter(verify) {
  323. if (this.disabled === true) {
  324. return;
  325. }
  326. if (this.spinShow === true) {
  327. return;
  328. }
  329. if (verify === true) {
  330. if (this.nickName === '') {
  331. this.nickName__ = this.nickName;
  332. }
  333. if (this.nickName__ === this.nickName) {
  334. return;
  335. }
  336. }
  337. this.updateStyle();
  338. this.nickName__ = this.nickName;
  339. //
  340. let where = {username: this.nickName};
  341. if (typeof this.identity === "string") {
  342. where['identity'] = this.identity;
  343. }
  344. if (typeof this.noidentity === "string") {
  345. where['noidentity'] = this.noidentity;
  346. }
  347. if (typeof this.nousername === "string") {
  348. where['nousername'] = this.nousername;
  349. }
  350. if (this.noprojectid) {
  351. where['noprojectid'] = this.noprojectid;
  352. }
  353. if (this.projectid) {
  354. where['projectid'] = this.projectid;
  355. }
  356. this.noDataText = this.$L("数据加载中.....");
  357. $A.aAjax({
  358. url: window.location.origin + '/api/users/searchinfo',
  359. data: {
  360. where: where,
  361. pagesize: 30
  362. },
  363. beforeSend: () => {
  364. this.spinShow = true;
  365. },
  366. complete: () => {
  367. this.spinShow = false;
  368. this.noDataText = this.$L("没有相关的数据");
  369. },
  370. error: () => {
  371. this.noDataText = this.$L("数据加载失败!");
  372. },
  373. success: (res) => {
  374. if (res.ret === 1) {
  375. this.userLists = res.data;
  376. for (let i = 0; i < this.userLists.length; i++) {
  377. if (this.userLists[i].id == this.userName) {
  378. this.userLists[i]['_highlight'] = true;
  379. }
  380. }
  381. this.searchShow = true;
  382. } else {
  383. this.$Message.warning(res.msg);
  384. this.emptyAll();
  385. }
  386. }
  387. });
  388. },
  389. userChange(item) {
  390. if (this.multiple) {
  391. this.addMultipleLists(item);
  392. } else {
  393. this.userName = item.username;
  394. this.seleName = item.nickname || item.username;
  395. this.nickName = item.nickname || item.username;
  396. this.nickName__ = item.nickname || item.username;
  397. this.skipSearch = true;
  398. this.searchShow = false;
  399. this.$emit('input', this.userName);
  400. this.$emit('change', item);
  401. }
  402. },
  403. handleClose(e) {
  404. if (this.multiple && $A(e.target).parents('.user-id-input-table').length > 0) {
  405. return;
  406. }
  407. if (this.searchShow === true) {
  408. this.searchShow = false;
  409. }
  410. },
  411. addMultipleLists(item) {
  412. let inn = false;
  413. this.multipleLists.some((tmp) => {
  414. if (tmp.username == item.username) {
  415. return inn = true;
  416. }
  417. })
  418. if (!inn) {
  419. this.multipleLists.push(item);
  420. this.callMultipleLists();
  421. }
  422. },
  423. callMultipleLists() {
  424. let val = '';
  425. this.multipleLists.forEach((tmp) => {
  426. if (val) {
  427. val+= ",";
  428. }
  429. val+= tmp.username;
  430. });
  431. this.$emit('input', val);
  432. },
  433. formatMultipleLists(val) {
  434. let arr = (val + ",").split(",");
  435. let narr = [];
  436. arr.forEach((uname) => {
  437. if (uname) {
  438. let inn = false;
  439. narr.some((tmp) => {
  440. if (tmp.username == uname) {
  441. return inn = true;
  442. }
  443. })
  444. if (!inn) {
  445. narr.push({
  446. username: uname,
  447. });
  448. }
  449. }
  450. });
  451. return narr;
  452. }
  453. },
  454. mounted() {
  455. this.updatePopper();
  456. //
  457. if ($A.runNum(this.value) > 0) {
  458. this.userName = this.value;
  459. }
  460. if (this.multiple) {
  461. this.multipleLists = this.formatMultipleLists(this.value);
  462. }
  463. }
  464. };
  465. </script>