UserImage.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div style="box-sizing: border-box;">
  3. <img v-if="imgUrl" class="logo_img" :src="imgUrl" :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.8'
  40. }
  41. },
  42. // 数据
  43. data(){
  44. return {
  45. imgUrl: "",
  46. name: "",
  47. employeeMap:this.$getCache("userList")
  48. }
  49. },
  50. watch:{
  51. id: function(val) {
  52. this.name_no()
  53. },
  54. img_url: function(val) {
  55. this.name_no()
  56. },
  57. user_name: function(val) {
  58. this.name_no()
  59. },
  60. },
  61. // 组件挂载完成
  62. mounted() {
  63. this.name_no()
  64. },
  65. // 方法
  66. methods:{
  67. name_no(){
  68. this.imgUrl=this.img_url
  69. this.name=this.user_name
  70. let pattern = new RegExp("^[\u4E00-\u9FA5]+");
  71. if(this.name.length > 2){
  72. if(pattern.test(this.name)){
  73. this.name = this.name.substring(this.name.length-2,this.name.length)
  74. }else{
  75. this.name = this.name.substring(0,2)
  76. }
  77. }
  78. if(!this.imgUrl&&this.id&&this.employeeMap){
  79. this.imgUrl=this.employeeMap[this.id]? this.employeeMap[this.id].img_url:""
  80. }
  81. }
  82. },
  83. }
  84. </script>
  85. <style scoped>
  86. .user_name_div{
  87. border-radius: 50%;
  88. background: #409eff;
  89. text-align: center;
  90. color: #fff;
  91. margin: 0 auto;
  92. box-sizing: border-box;
  93. }
  94. .logo_img{
  95. border-radius: 50%;
  96. }
  97. </style>