UseridInput.vue 15 KB

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