UserView.vue 3.7 KB

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