UserView.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div class="user-view-inline">
  3. <Tooltip :disabled="nickname === null" :delay="delay" :transfer="transfer" :placement="placement" @on-popper-show="popperShow">
  4. {{nickname || username}}
  5. <div slot="content">
  6. <div>{{$L('用户名')}}: {{username}}</div>
  7. <div>{{$L('职位/职称')}}: {{profession || '-'}}</div>
  8. </div>
  9. </Tooltip>
  10. </div>
  11. </template>
  12. <style lang="scss" scoped>
  13. .user-view-inline {
  14. display: inline-block;
  15. }
  16. </style>
  17. <script>
  18. export default {
  19. name: 'UserView',
  20. props: {
  21. username: {
  22. default: ''
  23. },
  24. delay: {
  25. type: Number,
  26. default: 600
  27. },
  28. transfer: {
  29. type: Boolean,
  30. default: true
  31. },
  32. placement: {
  33. default: 'bottom'
  34. },
  35. },
  36. data() {
  37. return {
  38. nickname: null,
  39. profession: ''
  40. }
  41. },
  42. mounted() {
  43. this.getUserData(0, 300);
  44. },
  45. watch: {
  46. username() {
  47. this.getUserData(0, 300);
  48. },
  49. },
  50. methods: {
  51. popperShow() {
  52. this.getUserData(0, 60)
  53. },
  54. getUserData(num, cacheTime) {
  55. let keyName = '__name:' + this.username.substring(0, 1) + '__';
  56. let localData = $A.jsonParse(window.localStorage[keyName]);
  57. if (window.__userViewNetworking === true) {
  58. if (num < 100) {
  59. setTimeout(() => {
  60. this.getUserData(num + 1, cacheTime)
  61. }, 500);
  62. }
  63. return;
  64. }
  65. //
  66. if (typeof localData[this.username] !== "object") {
  67. localData[this.username] = {};
  68. }
  69. //
  70. if (localData[this.username].success === true) {
  71. this.nickname = localData[this.username].data.nickname;
  72. this.profession = localData[this.username].data.profession;
  73. this.$emit("on-result", localData[this.username].data);
  74. if (localData[this.username].update + cacheTime > Math.round(new Date().getTime() / 1000)) {
  75. return;
  76. }
  77. }
  78. //
  79. window.__userViewNetworking = true;
  80. $A.aAjax({
  81. url: 'users/basic',
  82. data: {
  83. username: this.username,
  84. },
  85. error: () => {
  86. localData[this.username].success = false;
  87. localData[this.username].update = 0;
  88. localData[this.username].data = {};
  89. window.localStorage[keyName] = $A.jsonStringify(localData);
  90. window.__userViewNetworking = false;
  91. },
  92. success: (res) => {
  93. if (res.ret === 1) {
  94. this.nickname = res.data.nickname;
  95. this.profession = res.data.profession;
  96. localData[this.username].success = true;
  97. localData[this.username].update = Math.round(new Date().getTime() / 1000);
  98. localData[this.username].data = res.data;
  99. } else {
  100. this.nickname = '';
  101. this.profession = '';
  102. localData[this.username].success = false;
  103. localData[this.username].update = 0;
  104. localData[this.username].data = {};
  105. }
  106. this.$emit("on-result", localData[this.username].data);
  107. window.localStorage[keyName] = $A.jsonStringify(localData);
  108. window.__userViewNetworking = false;
  109. }
  110. });
  111. }
  112. }
  113. }
  114. </script>