UseridInput.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. $A.aAjax({
  211. url: window.location.origin + '/api/users/searchinfo',
  212. data: {
  213. where: where,
  214. pagesize: 1
  215. },
  216. beforeSend: () => {
  217. this.spinShow = true;
  218. },
  219. complete: () => {
  220. this.spinShow = false;
  221. },
  222. success: (res) => {
  223. if (res.ret === 1 && res.data.total > 0) {
  224. let tmpData = res.data[0];
  225. if (this.multiple) {
  226. this.addMultipleLists(tmpData);
  227. } else {
  228. this.userName = tmpData.username;
  229. this.seleName = tmpData.nickname || tmpData.username;
  230. this.nickName = tmpData.nickname || tmpData.username;
  231. this.nickName__ = tmpData.nickname || tmpData.username;
  232. this.$emit('input', this.userName);
  233. this.$emit('change', tmpData);
  234. }
  235. }
  236. }
  237. });
  238. }
  239. }
  240. },
  241. nickName(val) {
  242. if (val != this.seleName || val == '') {
  243. this.userName = '';
  244. if (!this.multiple) {
  245. this.$emit('input', this.userName);
  246. this.$emit('change', {});
  247. }
  248. }
  249. },
  250. spinShow(val) {
  251. if (typeof this.loadstatus === 'number') {
  252. this.$emit('update:loadstatus', val ? this.loadstatus + 1 : this.loadstatus - 1);
  253. }else if (typeof this.loadstatus === 'boolean') {
  254. this.$emit('update:loadstatus', val);
  255. }
  256. },
  257. searchShow(val) {
  258. if (val) {
  259. this.handleShowPopper();
  260. } else {
  261. this.handleClosePopper();
  262. }
  263. }
  264. },
  265. computed: {
  266. tableStyle() {
  267. return this.winStyle;
  268. }
  269. },
  270. methods: {
  271. handleShowPopper() {
  272. if (this.timeout) clearTimeout(this.timeout);
  273. this.timeout = setTimeout(() => {
  274. this.visible = true;
  275. }, this.delay);
  276. },
  277. handleClosePopper() {
  278. if (this.timeout) {
  279. clearTimeout(this.timeout);
  280. if (!this.controlled) {
  281. this.timeout = setTimeout(() => {
  282. this.visible = false;
  283. }, 100);
  284. }
  285. }
  286. },
  287. updateStyle() {
  288. this.winStyle = {
  289. width: `${Math.max(this.$el.offsetWidth, 230)}px`,
  290. };
  291. },
  292. emptyAll() {
  293. this.userName = '';
  294. this.nickName = '';
  295. this.nickName__ = '';
  296. this.seleName = '';
  297. this.searchShow = false;
  298. this.spinShow = false;
  299. },
  300. searchEnter(verify) {
  301. if (this.disabled === true) {
  302. return;
  303. }
  304. if (this.spinShow === true) {
  305. return;
  306. }
  307. if (verify === true) {
  308. if (this.nickName === '') {
  309. this.nickName__ = this.nickName;
  310. }
  311. if (this.nickName__ === this.nickName) {
  312. return;
  313. }
  314. }
  315. this.updateStyle();
  316. this.nickName__ = this.nickName;
  317. //
  318. let where = {username: this.nickName};
  319. if (typeof this.identity === "string") {
  320. where['identity'] = this.identity;
  321. }
  322. if (typeof this.noidentity === "string") {
  323. where['noidentity'] = this.noidentity;
  324. }
  325. if (typeof this.nousername === "string") {
  326. where['nousername'] = this.nousername;
  327. }
  328. if (this.noprojectid) {
  329. where['noprojectid'] = this.noprojectid;
  330. }
  331. if (this.projectid) {
  332. where['projectid'] = this.projectid;
  333. }
  334. $A.aAjax({
  335. url: window.location.origin + '/api/users/searchinfo',
  336. data: {
  337. where: where,
  338. pagesize: 30
  339. },
  340. beforeSend: () => {
  341. this.spinShow = true;
  342. },
  343. complete: () => {
  344. this.spinShow = false;
  345. },
  346. success: (res) => {
  347. if (res.ret === 1) {
  348. this.userLists = res.data;
  349. for (let i = 0; i < this.userLists.length; i++) {
  350. if (this.userLists[i].id == this.userName) {
  351. this.userLists[i]['_highlight'] = true;
  352. }
  353. }
  354. this.searchShow = true;
  355. } else {
  356. this.$Message.warning(res.msg);
  357. this.emptyAll();
  358. }
  359. }
  360. });
  361. },
  362. userChange(item) {
  363. if (this.multiple) {
  364. this.addMultipleLists(item);
  365. } else {
  366. this.userName = item.username;
  367. this.seleName = item.nickname || item.username;
  368. this.nickName = item.nickname || item.username;
  369. this.nickName__ = item.nickname || item.username;
  370. this.skipSearch = true;
  371. this.searchShow = false;
  372. this.$emit('input', this.userName);
  373. this.$emit('change', item);
  374. }
  375. },
  376. handleClose(e) {
  377. if (this.multiple && $A(e.target).parents('.user-id-input-table').length > 0) {
  378. return;
  379. }
  380. if (this.searchShow === true) {
  381. this.searchShow = false;
  382. }
  383. },
  384. addMultipleLists(item) {
  385. let inn = false;
  386. this.multipleLists.some((tmp) => {
  387. if (tmp.username == item.username) {
  388. return inn = true;
  389. }
  390. })
  391. if (!inn) {
  392. this.multipleLists.push(item);
  393. this.callMultipleLists();
  394. }
  395. },
  396. callMultipleLists() {
  397. let val = '';
  398. this.multipleLists.forEach((tmp) => {
  399. if (val) {
  400. val+= ",";
  401. }
  402. val+= tmp.username;
  403. });
  404. this.$emit('input', val);
  405. }
  406. },
  407. mounted() {
  408. this.updatePopper();
  409. //
  410. if ($A.runNum(this.value) > 0) {
  411. this.userName = this.value;
  412. }
  413. }
  414. };
  415. </script>