UseridInput.vue 14 KB

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