UseridInput.vue 17 KB

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