UseridInput.vue 16 KB

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