UseridInput.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. projectid: {
  119. default: ''
  120. },
  121. placeholder: {
  122. default: ''
  123. },
  124. disabled: {
  125. type: Boolean,
  126. default: false
  127. },
  128. transfer: {
  129. type: Boolean,
  130. default () {
  131. return true;
  132. }
  133. },
  134. loadstatus: {
  135. default: false
  136. },
  137. multiple: {
  138. type: Boolean,
  139. default: false
  140. },
  141. },
  142. data () {
  143. return {
  144. multipleLists: [],
  145. userName: '',
  146. nickName: '',
  147. nickName__: '',
  148. seleName: '',
  149. searchShow: false,
  150. spinShow: false,
  151. skipSearch: false,
  152. winStyle: {},
  153. columns: [
  154. {
  155. "title": "昵称",
  156. "key": "nickname",
  157. "minWidth": 80,
  158. "ellipsis": true,
  159. "tooltip": true,
  160. render: (h, params) => {
  161. return h('span', params.row.nickname || '-');
  162. }
  163. }, {
  164. "title": "用户名",
  165. "key": "username",
  166. "minWidth": 80,
  167. "ellipsis": true,
  168. "tooltip": true,
  169. },
  170. ],
  171. userLists: [],
  172. nodatatext: "数据加载中.....",
  173. }
  174. },
  175. watch: {
  176. value (val) {
  177. if (this.multiple) {
  178. return;
  179. }
  180. this.userName = $A.cloneData(val)
  181. },
  182. userName (val) {
  183. if (this.skipSearch === true) {
  184. this.skipSearch = false;
  185. }else{
  186. this.nickName = '';
  187. if (val) {
  188. let where = { usernameequal: val };
  189. if (typeof this.identity === "string") {
  190. where['identity'] = this.identity;
  191. }
  192. if (typeof this.noidentity === "string") {
  193. where['noidentity'] = this.noidentity;
  194. }
  195. if (this.projectid) {
  196. where['projectid'] = this.projectid;
  197. }
  198. $A.aAjax({
  199. url: window.location.origin + '/api/users/searchinfo',
  200. data: {
  201. where: where,
  202. pagesize: 1
  203. },
  204. beforeSend: () => {
  205. this.spinShow = true;
  206. },
  207. complete: () => {
  208. this.spinShow = false;
  209. },
  210. success: (res) => {
  211. if (res.ret === 1 && res.data.total > 0) {
  212. let tmpData = res.data[0];
  213. if (this.multiple) {
  214. this.addMultipleLists(tmpData);
  215. } else {
  216. this.userName = tmpData.username;
  217. this.seleName = tmpData.nickname || tmpData.username;
  218. this.nickName = tmpData.nickname || tmpData.username;
  219. this.nickName__ = tmpData.nickname || tmpData.username;
  220. this.$emit('input', this.userName);
  221. this.$emit('change', tmpData);
  222. }
  223. }
  224. }
  225. });
  226. }
  227. }
  228. },
  229. nickName(val) {
  230. if (val != this.seleName || val == '') {
  231. this.userName = '';
  232. if (!this.multiple) {
  233. this.$emit('input', this.userName);
  234. this.$emit('change', {});
  235. }
  236. }
  237. },
  238. spinShow(val) {
  239. if (typeof this.loadstatus === 'number') {
  240. this.$emit('update:loadstatus', val ? this.loadstatus + 1 : this.loadstatus - 1);
  241. }else if (typeof this.loadstatus === 'boolean') {
  242. this.$emit('update:loadstatus', val);
  243. }
  244. },
  245. searchShow(val) {
  246. if (val) {
  247. this.handleShowPopper();
  248. } else {
  249. this.handleClosePopper();
  250. }
  251. }
  252. },
  253. computed: {
  254. tableStyle() {
  255. return this.winStyle;
  256. }
  257. },
  258. methods: {
  259. handleShowPopper() {
  260. if (this.timeout) clearTimeout(this.timeout);
  261. this.timeout = setTimeout(() => {
  262. this.visible = true;
  263. }, this.delay);
  264. },
  265. handleClosePopper() {
  266. if (this.timeout) {
  267. clearTimeout(this.timeout);
  268. if (!this.controlled) {
  269. this.timeout = setTimeout(() => {
  270. this.visible = false;
  271. }, 100);
  272. }
  273. }
  274. },
  275. updateStyle() {
  276. this.winStyle = {
  277. width: `${Math.max(this.$el.offsetWidth, 230)}px`,
  278. };
  279. },
  280. emptyAll() {
  281. this.userName = '';
  282. this.nickName = '';
  283. this.nickName__ = '';
  284. this.seleName = '';
  285. this.searchShow = false;
  286. this.spinShow = false;
  287. },
  288. searchEnter(verify) {
  289. if (this.disabled === true) {
  290. return;
  291. }
  292. if (this.spinShow === true) {
  293. return;
  294. }
  295. if (verify === true) {
  296. if (this.nickName === '') {
  297. this.nickName__ = this.nickName;
  298. }
  299. if (this.nickName__ === this.nickName) {
  300. return;
  301. }
  302. }
  303. this.updateStyle();
  304. this.nickName__ = this.nickName;
  305. //
  306. let where = {username: this.nickName};
  307. if (typeof this.identity === "string") {
  308. where['identity'] = this.identity;
  309. }
  310. if (typeof this.noidentity === "string") {
  311. where['noidentity'] = this.noidentity;
  312. }
  313. if (this.projectid) {
  314. where['projectid'] = this.projectid;
  315. }
  316. $A.aAjax({
  317. url: window.location.origin + '/api/users/searchinfo',
  318. data: {
  319. where: where,
  320. pagesize: 30
  321. },
  322. beforeSend: () => {
  323. this.spinShow = true;
  324. },
  325. complete: () => {
  326. this.spinShow = false;
  327. },
  328. success: (res) => {
  329. if (res.ret === 1) {
  330. this.userLists = res.data;
  331. for (let i = 0; i < this.userLists.length; i++) {
  332. if (this.userLists[i].id == this.userName) {
  333. this.userLists[i]['_highlight'] = true;
  334. }
  335. }
  336. this.searchShow = true;
  337. } else {
  338. this.$Message.warning(res.msg);
  339. this.emptyAll();
  340. }
  341. }
  342. });
  343. },
  344. userChange(item) {
  345. if (this.multiple) {
  346. this.addMultipleLists(item);
  347. } else {
  348. this.userName = item.username;
  349. this.seleName = item.nickname || item.username;
  350. this.nickName = item.nickname || item.username;
  351. this.nickName__ = item.nickname || item.username;
  352. this.skipSearch = true;
  353. this.searchShow = false;
  354. this.$emit('input', this.userName);
  355. this.$emit('change', item);
  356. }
  357. },
  358. handleClose(e) {
  359. if (this.multiple && $A(e.target).parents('.user-id-input-table').length > 0) {
  360. return;
  361. }
  362. if (this.searchShow === true) {
  363. this.searchShow = false;
  364. }
  365. },
  366. addMultipleLists(item) {
  367. let inn = false;
  368. this.multipleLists.some((tmp) => {
  369. if (tmp.username == item.username) {
  370. return inn = true;
  371. }
  372. })
  373. if (!inn) {
  374. this.multipleLists.push(item);
  375. this.callMultipleLists();
  376. }
  377. },
  378. callMultipleLists() {
  379. let val = '';
  380. this.multipleLists.forEach((tmp) => {
  381. if (val) {
  382. val+= ",";
  383. }
  384. val+= tmp.username;
  385. });
  386. this.$emit('input', val);
  387. }
  388. },
  389. mounted() {
  390. this.updatePopper();
  391. //
  392. if ($A.runNum(this.value) > 0) {
  393. this.userName = this.value;
  394. }
  395. }
  396. };
  397. </script>