UserView.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. nickname(val) {
  50. this.$emit("on-result", val);
  51. }
  52. },
  53. methods: {
  54. popperShow() {
  55. this.getUserData(0, 60)
  56. },
  57. getUserData(num, cacheTime) {
  58. let keyName = '__name:' + this.username.substring(0, 1) + '__';
  59. let localData = $A.jsonParse(window.localStorage[keyName]);
  60. if (window.__userViewNetworking === true) {
  61. if (num < 100) {
  62. setTimeout(() => {
  63. this.getUserData(num + 1, cacheTime)
  64. }, 500);
  65. }
  66. return;
  67. }
  68. //
  69. if (typeof localData[this.username] !== "object") {
  70. localData[this.username] = {};
  71. }
  72. //
  73. if (localData[this.username].success === true) {
  74. this.nickname = localData[this.username].data.nickname;
  75. this.profession = localData[this.username].data.profession;
  76. if (localData[this.username].update + cacheTime > Math.round(new Date().getTime() / 1000)) {
  77. return;
  78. }
  79. }
  80. //
  81. window.__userViewNetworking = true;
  82. $A.aAjax({
  83. url: 'users/basic',
  84. data: {
  85. username: this.username,
  86. },
  87. error: () => {
  88. localData[this.username].success = false;
  89. localData[this.username].update = 0;
  90. localData[this.username].data = {};
  91. window.localStorage[keyName] = $A.jsonStringify(localData);
  92. window.__userViewNetworking = false;
  93. },
  94. success: (res) => {
  95. if (res.ret === 1) {
  96. this.nickname = res.data.nickname;
  97. this.profession = res.data.profession;
  98. localData[this.username].success = true;
  99. localData[this.username].update = Math.round(new Date().getTime() / 1000);
  100. localData[this.username].data = res.data;
  101. } else {
  102. this.nickname = '';
  103. this.profession = '';
  104. localData[this.username].success = false;
  105. localData[this.username].update = 0;
  106. localData[this.username].data = {};
  107. }
  108. window.localStorage[keyName] = $A.jsonStringify(localData);
  109. window.__userViewNetworking = false;
  110. }
  111. });
  112. }
  113. }
  114. }
  115. </script>