UserView.vue 4.7 KB

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