UserImage.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div>
  3. <img v-if="info.img_url" class="logo_img" :src="info.img_url" :width="width" :height="height" />
  4. <div v-else class="user_name_div" :style="{
  5. width: width,
  6. height: height,
  7. lineHeight: height,
  8. fontSize: fontSize + 'rem'}">
  9. {{name}}
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'userImage',
  16. props: {
  17. width: {
  18. type: String,
  19. default: '0.8rem'
  20. },
  21. height: {
  22. type: String,
  23. default: '0.8rem'
  24. },
  25. id:{
  26. type: Number,
  27. default: 0
  28. },
  29. img_url: {
  30. type: String,
  31. default: ''
  32. },
  33. user_name: {
  34. type: String,
  35. default: ''
  36. },
  37. fontSize:{
  38. type: String,
  39. default: '0.32'
  40. }
  41. },
  42. watch:{
  43. id: function (val) {
  44. this.set_info()
  45. this.name_no()
  46. },
  47. img_url: function (val) {
  48. this.set_info()
  49. this.name_no()
  50. },
  51. user_name: function (val) {
  52. this.set_info()
  53. this.name_no()
  54. }
  55. },
  56. // 数据
  57. data(){
  58. let info ={name:'',img_url: '',id:0}
  59. if(this.img_url != ''){
  60. info.img_url = this.img_url
  61. }
  62. if(this.user_name != ''){
  63. info.name = this.user_name
  64. }
  65. return {
  66. info: info,
  67. name: '',
  68. }
  69. },
  70. computed:{
  71. },
  72. // 方法
  73. methods:{
  74. set_info(){
  75. let info ={name:'',img_url: '',id:0}
  76. if(this.img_url != ''){
  77. info.img_url = this.img_url
  78. }
  79. if(this.user_name != ''){
  80. info.name = this.user_name
  81. }
  82. this.info = info
  83. },
  84. // 加载
  85. name_no(){
  86. if(!this.info.img_url){
  87. let pattern = new RegExp("^[\u4E00-\u9FA5]+");
  88. if(this.info.name.length > 2){
  89. if(pattern.test(this.info.name)){
  90. this.name = this.info.name.substring(this.info.name.length-2,this.info.name.length)
  91. }else{
  92. this.name = this.info.name.substring(0,2)
  93. }
  94. }else{
  95. this.name = this.info.name
  96. }
  97. }
  98. }
  99. },
  100. // 组件挂载完成
  101. mounted() {
  102. this.name_no()
  103. },
  104. }
  105. </script>
  106. <style scoped>
  107. .user_name_div{
  108. border-radius: 50%;
  109. background: #238dfa;
  110. text-align: center;
  111. color: #fff;
  112. }
  113. .logo_img{
  114. border-radius: 50%;
  115. }
  116. </style>