user_image.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <div>
  3. <el-image
  4. v-if="info.img_url"
  5. :src="info.img_url == defaultImg() ? replaceImg() : info.img_url"
  6. :style="{width: width, height: height}"
  7. style="border-radius: 50%"
  8. fit="fill"
  9. >
  10. <div slot="error" class="image-slot">
  11. <i class="el-icon-picture"></i>
  12. </div>
  13. </el-image>
  14. <span class="img_round" v-else>
  15. <div class="user_name_div" :style="{width: width, height: height,lineHeight: height, fontSize: fontSize + 'px'}"><WWOpenData type="userName" :openid="user_name"></WWOpenData></div>
  16. <div class="user_img_bg" :style="{width: width, height: height}" ></div>
  17. </span>
  18. </div>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'userImage',
  23. props: {
  24. width: {
  25. type: String,
  26. default: '0.8rem'
  27. },
  28. height: {
  29. type: String,
  30. default: '0.8rem'
  31. },
  32. id:{
  33. type: Number,
  34. default: 0
  35. },
  36. img_url: {
  37. type: String,
  38. default: ''
  39. },
  40. user_name: {
  41. type: String,
  42. default: ''
  43. },
  44. fontSize:{
  45. type: String,
  46. default: '14'
  47. }
  48. },
  49. watch:{
  50. id: function (val) {
  51. this.set_info()
  52. this.name_no()
  53. },
  54. img_url: function (val) {
  55. this.set_info()
  56. this.name_no()
  57. },
  58. user_name: function (val) {
  59. this.set_info()
  60. this.name_no()
  61. }
  62. },
  63. // 数据
  64. data(){
  65. let info = this.$store.getters.employee_map[this.id] || {name:'',img_url: '',id:0}
  66. console.log("q撒旦轻松的"+JSON.stringify(info))
  67. if(this.img_url != ''){
  68. info.img_url = this.img_url
  69. }
  70. if(this.user_name != ''){
  71. info.name = this.user_name
  72. }
  73. return {
  74. info: info,
  75. name: '',
  76. }
  77. },
  78. computed:{
  79. },
  80. // 方法
  81. methods:{
  82. set_info(){
  83. let info = this.$store.getters.employee_map[this.id] || {name:'',img_url: '',id:0}
  84. if(this.img_url != ''){
  85. info.img_url = this.img_url
  86. }
  87. if(this.user_name != ''){
  88. info.name = this.user_name
  89. }
  90. this.info = info
  91. },
  92. // 加载
  93. name_no(){
  94. if(!this.info.img_url){
  95. let pattern = new RegExp("^[\u4E00-\u9FA5]+");
  96. if(this.info.name.length > 2){
  97. if(pattern.test(this.info.name)){
  98. this.name = this.info.name.substring(this.info.name.length-2,this.info.name.length)
  99. }else{
  100. this.name = this.info.name.substring(0,2)
  101. }
  102. }else{
  103. this.name = this.info.name
  104. }
  105. }
  106. },
  107. defaultImg(){
  108. return "https://rescdn.qqmail.com/node/wwmng/wwmng/style/images/independent/DefaultAvatar$73ba92b5.png"
  109. },
  110. replaceImg(){
  111. // return 'static/images/gttx'+ Math.round(Math.random()*5) +'.jpg'
  112. return 'static/images/default5.png'
  113. },
  114. },
  115. // 组件挂载完成
  116. mounted() {
  117. this.name_no()
  118. },
  119. }
  120. </script>
  121. <style scoped>
  122. .img_round{
  123. position: relative;
  124. display: inline-block;
  125. vertical-align: top;
  126. }
  127. .img_round .user_img_bg{
  128. border-radius: 50%;
  129. background: #238dfa;
  130. }
  131. .user_name_div{
  132. position: absolute;
  133. z-index: 1;
  134. text-align: center;
  135. color: #fff;
  136. white-space:nowrap;
  137. overflow: hidden;
  138. }
  139. .logo_img{
  140. border-radius: 50%;
  141. }
  142. /deep/ .image-slot{
  143. font-size: 30px;
  144. line-height: 50px;
  145. text-align: center;
  146. background: #f1f1f1;
  147. }
  148. /deep/ .van-image{vertical-align: top;}
  149. </style>