UseridInput.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <div v-clickoutside="handleClose">
  3. <div class="user-id-input" ref="reference">
  4. <Input v-model="nickName" :placeholder="placeholder" :disabled="disabled" icon="md-search" @on-click="searchEnter" @on-enter="searchEnter" @on-blur="searchEnter(true)">
  5. <div v-if="$slots.prepend !== undefined" slot="prepend"><slot name="prepend"></slot></div>
  6. <div v-if="$slots.append !== undefined" slot="append"><slot name="append"></slot></div>
  7. </Input>
  8. <div v-if="userName" class="user-id-subtitle">用户名: {{userName}}</div>
  9. <div v-if="spinShow" class="user-id-spin"><div><w-loading></w-loading></div></div>
  10. </div>
  11. <transition name="fade">
  12. <div
  13. v-show="!disabled && visible"
  14. ref="popper"
  15. class="user-id-input-body"
  16. :data-transfer="transfer"
  17. v-transfer-dom>
  18. <Table highlight-row
  19. v-if="searchShow"
  20. size="small"
  21. class="user-id-input-table"
  22. :style="tableStyle"
  23. :columns="columns"
  24. :data="userLists"
  25. @on-current-change="userChange"
  26. :no-data-text="nodatatext"></Table>
  27. </div>
  28. </transition>
  29. </div>
  30. </template>
  31. <style lang="scss" scoped>
  32. .user-id-input {
  33. display: inline-block;
  34. width: 100%;
  35. position: relative;
  36. vertical-align: middle;
  37. z-index: 5;
  38. .user-id-subtitle {
  39. position: absolute;
  40. top: 2px;
  41. right: 32px;
  42. height: 30px;
  43. line-height: 30px;
  44. color: #cccccc;
  45. z-index: 2;
  46. }
  47. .user-id-spin {
  48. position: absolute;
  49. top: 0;
  50. bottom: 0;
  51. left: 0;
  52. right: 0;
  53. border-radius: 4px;
  54. background-color: rgba(255, 255, 255, 0.26);
  55. > div {
  56. width: 18px;
  57. height: 18px;
  58. position: absolute;
  59. top: 50%;
  60. left: 6px;
  61. transform: translate(0, -50%);
  62. }
  63. }
  64. }
  65. </style>
  66. <style lang="scss">
  67. .user-id-input-body {
  68. z-index: 99999
  69. }
  70. .user-id-input-table {
  71. border-radius: 4px;
  72. overflow: hidden;
  73. box-shadow: 0 0 4px 2px rgba(0, 0, 0, 0.1);
  74. .ivu-table table {
  75. width: 100% !important;
  76. }
  77. .ivu-table:before, .ivu-table:after {
  78. display: none !important;
  79. }
  80. .ivu-table-body {
  81. max-height: 180px;
  82. overflow: auto;
  83. }
  84. .ivu-table-small td {
  85. cursor: pointer;
  86. }
  87. }
  88. </style>
  89. <script>
  90. import clickoutside from './directives/clickoutside';
  91. import TransferDom from './directives/transfer-dom';
  92. import Popper from './directives/popper-novalue';
  93. import WLoading from "./WLoading";
  94. export default {
  95. name: 'UseridInput',
  96. components: {WLoading},
  97. directives: {clickoutside, TransferDom},
  98. mixins: [Popper],
  99. props: {
  100. placement: {
  101. default: 'bottom'
  102. },
  103. value: {
  104. default: ''
  105. },
  106. identity: {
  107. default: ''
  108. },
  109. noidentity: {
  110. default: ''
  111. },
  112. placeholder: {
  113. default: ''
  114. },
  115. disabled: {
  116. type: Boolean,
  117. default: false
  118. },
  119. transfer: {
  120. type: Boolean,
  121. default () {
  122. return true;
  123. }
  124. },
  125. loadstatus: {
  126. default: false
  127. }
  128. },
  129. data () {
  130. return {
  131. userName: '',
  132. nickName: '',
  133. nickName__: '',
  134. seleName: '',
  135. searchShow: false,
  136. spinShow: false,
  137. skipSearch: false,
  138. winStyle: {},
  139. columns: [
  140. {
  141. "title": "用户名",
  142. "key": "username",
  143. "minWidth": 80,
  144. "ellipsis": true,
  145. "tooltip": true,
  146. }, {
  147. "title": "昵称",
  148. "key": "nickname",
  149. "minWidth": 80,
  150. "ellipsis": true,
  151. "tooltip": true,
  152. render: (h, params) => {
  153. return h('span', params.row.nickname || '-');
  154. }
  155. },
  156. ],
  157. userLists: [],
  158. nodatatext: "数据加载中.....",
  159. }
  160. },
  161. watch: {
  162. value (val) {
  163. this.userName = $A.clone(val)
  164. },
  165. userName (val) {
  166. if (this.skipSearch === true) {
  167. this.skipSearch = false;
  168. }else{
  169. this.nickName = '';
  170. if (val) {
  171. let where = { usernameequal: val };
  172. if (typeof this.identity === "string") {
  173. where['identity'] = this.identity;
  174. }
  175. if (typeof this.noidentity === "string") {
  176. where['noidentity'] = this.noidentity;
  177. }
  178. $A.aAjax({
  179. url: window.location.origin + '/api/users/searchinfo',
  180. data: {
  181. where: where,
  182. pagesize: 1
  183. },
  184. beforeSend: () => {
  185. this.spinShow = true;
  186. },
  187. complete: () => {
  188. this.spinShow = false;
  189. },
  190. success: (res) => {
  191. if (res.ret === 1 && res.data.total > 0) {
  192. let tmpData = res.data.lists[0];
  193. this.userName = tmpData.username;
  194. this.seleName = tmpData.nickname || tmpData.username;
  195. this.nickName = tmpData.nickname || tmpData.username;
  196. this.nickName__ = tmpData.nickname || tmpData.username;
  197. this.$emit('input', this.userName);
  198. this.$emit('change', tmpData);
  199. }
  200. }
  201. });
  202. }
  203. }
  204. },
  205. nickName(val) {
  206. if (val != this.seleName || val == '') {
  207. this.userName = '';
  208. this.$emit('input', this.userName);
  209. this.$emit('change', {});
  210. }
  211. },
  212. spinShow(val) {
  213. if (typeof this.loadstatus === 'number') {
  214. this.$emit('update:loadstatus', val ? this.loadstatus + 1 : this.loadstatus - 1);
  215. }else if (typeof this.loadstatus === 'boolean') {
  216. this.$emit('update:loadstatus', val);
  217. }
  218. },
  219. searchShow(val) {
  220. if (val) {
  221. this.handleShowPopper();
  222. } else {
  223. this.handleClosePopper();
  224. }
  225. }
  226. },
  227. computed: {
  228. tableStyle() {
  229. return this.winStyle;
  230. }
  231. },
  232. methods: {
  233. handleShowPopper() {
  234. if (this.timeout) clearTimeout(this.timeout);
  235. this.timeout = setTimeout(() => {
  236. this.visible = true;
  237. }, this.delay);
  238. },
  239. handleClosePopper() {
  240. if (this.timeout) {
  241. clearTimeout(this.timeout);
  242. if (!this.controlled) {
  243. this.timeout = setTimeout(() => {
  244. this.visible = false;
  245. }, 100);
  246. }
  247. }
  248. },
  249. updateStyle() {
  250. this.winStyle = {
  251. width: `${Math.max(this.$el.offsetWidth, 230)}px`,
  252. };
  253. },
  254. emptyAll() {
  255. this.userName = '';
  256. this.nickName = '';
  257. this.nickName__ = '';
  258. this.seleName = '';
  259. this.searchShow = false;
  260. this.spinShow = false;
  261. },
  262. searchEnter(verify) {
  263. if (this.disabled === true) {
  264. return;
  265. }
  266. if (this.spinShow === true) {
  267. return;
  268. }
  269. if (verify === true) {
  270. if (this.nickName === '') {
  271. this.nickName__ = this.nickName;
  272. }
  273. if (this.nickName__ === this.nickName) {
  274. return;
  275. }
  276. }
  277. this.updateStyle();
  278. this.nickName__ = this.nickName;
  279. //
  280. let where = {username: this.nickName};
  281. if (typeof this.identity === "string") {
  282. where['identity'] = this.identity;
  283. }
  284. if (typeof this.noidentity === "string") {
  285. where['noidentity'] = this.noidentity;
  286. }
  287. $A.aAjax({
  288. url: window.location.origin + '/api/users/searchinfo',
  289. data: {
  290. where: where,
  291. pagesize: 30
  292. },
  293. beforeSend: () => {
  294. this.spinShow = true;
  295. },
  296. complete: () => {
  297. this.spinShow = false;
  298. },
  299. success: (res) => {
  300. if (res.ret === 1) {
  301. this.userLists = res.data.lists;
  302. for (let i = 0; i < this.userLists.length; i++) {
  303. if (this.userLists[i].id == this.userName) {
  304. this.userLists[i]['_highlight'] = true;
  305. }
  306. }
  307. this.searchShow = true;
  308. } else {
  309. this.$Message.warning(res.msg);
  310. this.emptyAll();
  311. }
  312. }
  313. });
  314. },
  315. userChange(item) {
  316. this.userName = item.username;
  317. this.seleName = item.nickname || item.username;
  318. this.nickName = item.nickname || item.username;
  319. this.nickName__ = item.nickname || item.username;
  320. this.skipSearch = true;
  321. this.searchShow = false;
  322. this.$emit('input', this.userName);
  323. this.$emit('change', item);
  324. },
  325. handleClose() {
  326. if (this.searchShow === true) {
  327. this.searchShow = false;
  328. }
  329. }
  330. },
  331. mounted() {
  332. this.updatePopper();
  333. //
  334. if ($A.runNum(this.value) > 0) {
  335. this.userName = this.value;
  336. }
  337. }
  338. };
  339. </script>