UserView.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div class="user-view-inline">
  3. <Tooltip :disabled="nickname === null" :delay="delay" :transfer="transfer" @on-popper-show="popperShow">
  4. {{nickname || username}}
  5. <div slot="content">
  6. <div>用户名:{{username}}</div>
  7. <div>职位/职称:{{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. },
  33. data() {
  34. return {
  35. nickname: null,
  36. profession: ''
  37. }
  38. },
  39. mounted() {
  40. this.getUserData(0, 300);
  41. },
  42. watch: {
  43. username() {
  44. this.getUserData(0, 300);
  45. }
  46. },
  47. methods: {
  48. popperShow() {
  49. this.getUserData(0, 60)
  50. },
  51. getUserData(num, cacheTime) {
  52. let keyName = '__name:' + this.username.substring(0, 1) + '__';
  53. let localData = $A.jsonParse(window.localStorage[keyName]);
  54. if (localData.__loadIng === true) {
  55. if (num < 100) {
  56. setTimeout(() => {
  57. this.getUserData(num + 1, cacheTime)
  58. }, 500);
  59. }
  60. return;
  61. }
  62. //
  63. if (typeof localData[this.username] !== "object") {
  64. localData[this.username] = {};
  65. }
  66. //
  67. if (localData[this.username].success === true
  68. && localData[this.username].update + cacheTime > Math.round(new Date().getTime() / 1000)) {
  69. this.nickname = localData[this.username].data.nickname;
  70. this.profession = localData[this.username].data.profession;
  71. return;
  72. }
  73. //
  74. localData.__loadIng = true;
  75. $A.aAjax({
  76. url: 'users/basic',
  77. data: {
  78. username: this.username,
  79. },
  80. error: () => {
  81. localData[this.username].success = false;
  82. localData[this.username].update = 0;
  83. localData[this.username].data = {};
  84. localData.__loadIng = false;
  85. window.localStorage[keyName] = $A.jsonStringify(localData);
  86. },
  87. success: (res) => {
  88. if (res.ret === 1) {
  89. this.nickname = res.data.nickname;
  90. this.profession = res.data.profession;
  91. localData[this.username].success = true;
  92. localData[this.username].update = Math.round(new Date().getTime() / 1000);
  93. localData[this.username].data = res.data;
  94. } else {
  95. this.nickname = '';
  96. this.profession = '';
  97. localData[this.username].success = false;
  98. localData[this.username].update = 0;
  99. localData[this.username].data = {};
  100. }
  101. localData.__loadIng = false;
  102. window.localStorage[keyName] = $A.jsonStringify(localData);
  103. }
  104. });
  105. }
  106. }
  107. }
  108. </script>