tm-avatar.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view
  3. @click="onclick"
  4. :style="configStyle"
  5. class="tm--avatar d-inline-block "
  6. :class="[titl ? 'round-2' : 'rounded', text ? '' : `shadow-${color_tmeme}-${shadow}`, customClass]"
  7. >
  8. <view class="tm--avatar--dot" :class="[dotPos == 'top' ? 'top' : '', dotPos == 'bottom' ? 'bottom' : '']">
  9. <slot name="dot">
  10. <view v-if="dotPos == 'bottom'" style="width: 100%;"><tm-badges :offset="[0, -10]" v-if="dot" :color="dotColor"></tm-badges></view>
  11. <tm-badges :offset="[2, -2]" v-if="dot && dotPos == 'top'" :color="dotColor"></tm-badges>
  12. </slot>
  13. </view>
  14. <view
  15. class="flex-center overflow text-align-center tm--avatar--conter"
  16. :class="[
  17. titl ? `round-${round}` : 'rounded',
  18. !label && !src ? color_tmeme : '',
  19. label ? color_tmeme : '',
  20. black_tmeme ? 'bk' : '',
  21. text ? 'text' : '',
  22. outlined ? 'outlined' : '',
  23. `border-${color_tmeme}-a-${border}`
  24. ]"
  25. :style="{ width: imgstyle.width, height: imgstyle.height }"
  26. >
  27. <slot name="default" :src="src">
  28. <image v-if="!label" :class="[titl ? 'round-0' : 'rounded']" :style="{ width: imgstyle.width, height: imgstyle.height }" :src="src"></image>
  29. <text v-if="label" :style="{ fontSize: fontsize }">{{ label }}</text>
  30. </slot>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. /**
  36. * 头像框
  37. * @property {Number | String} size = [98|80|64] 默认:98,头像的宽高,单位upx
  38. * @property {String} color = [primary] 默认:primary,主题背景色
  39. * @property {Number|String} shadow = [] 默认:0,投影
  40. * @property {Number} round = [] 默认:0,圆角,只有在titl下起作用。
  41. * @property {String} label = [] 默认:'',当填入信息时,文本头像,禁用img模式。
  42. * @property {String} font-size = [] 默认:'36',文字大小,单位upx,label时启用。
  43. * @property {String} src = [] 默认:'https://picsum.photos/200',头像图片地址,label时禁用用。
  44. * @property {Boolean} titl = [true|false] 默认:false,开户titl模式即正常的正方形而非圆形。
  45. * @property {Boolean} text = [true|false] 默认:false,文本模式
  46. * @property {Boolean} outlined = [true|false] 默认:false,边框模式
  47. * @property {Boolean} dot = [true|false] 默认:false,显示头像点。建议自行通过slot dot 自行设置。
  48. * @property {String} dot-color = [] 默认:primary,角标颜色
  49. * @property {String} dot-pos = [top|bottom] 默认:top,解析的位置
  50. * @property {Number|String} border = [] 默认:0,边框,边框颜色为你的color颜色
  51. * @property {String | Boolean} black = [true|false] 默认:false,是否开启暗黑模式
  52. * @property {String} custom-class = [] 默认:'',自定义类。
  53. * @property {Function} click 返回:{event,src,label})。
  54. * @example <tm-avatar ></tm-avatar>
  55. */
  56. import tmBadges from '@/tm-vuetify/components/tm-badges/tm-badges.vue';
  57. export default {
  58. components: { tmBadges },
  59. name: 'tm-avatar',
  60. props: {
  61. // 头像的宽高upx
  62. size: {
  63. type: Number | String,
  64. default: 98
  65. },
  66. // 主题背景色
  67. color: {
  68. type: String,
  69. default: 'primary'
  70. },
  71. dotColor: {
  72. type: String,
  73. default: 'red'
  74. },
  75. // 自定义类
  76. customClass: {
  77. type: String,
  78. default: ''
  79. },
  80. // 投影
  81. shadow: {
  82. type: Number | String,
  83. default: 0
  84. },
  85. // 当填入信息时,禁用img模式。
  86. label: {
  87. type: String,
  88. default: ''
  89. },
  90. // 单位upx
  91. fontSize: {
  92. type: String | Number,
  93. default: 36
  94. },
  95. // 注意,只有当label没有填写时才会启用。
  96. src: {
  97. type: String,
  98. default: 'https://picsum.photos/200'
  99. },
  100. // 开户til模式即正常的正方形而非圆形。
  101. titl: {
  102. type: Boolean,
  103. default: false
  104. },
  105. black: {
  106. type: Boolean | String,
  107. default: null
  108. },
  109. round: {
  110. type: Number,
  111. default: 0
  112. },
  113. text: {
  114. type: Boolean,
  115. default: false
  116. },
  117. outlined: {
  118. type: Boolean,
  119. default: false
  120. },
  121. dot: {
  122. type: Boolean,
  123. default: false
  124. },
  125. dotPos: {
  126. type: String,
  127. default: 'top'
  128. },
  129. border: {
  130. type: Number | String,
  131. default: 0
  132. },
  133. // 跟随主题色的改变而改变。
  134. fllowTheme: {
  135. type: Boolean | String,
  136. default: true
  137. }
  138. },
  139. data() {
  140. return {
  141. imgstyle: { width: 0, height: 0 },
  142. wkstyle: {}
  143. };
  144. },
  145. computed: {
  146. fontsize: function() {
  147. return uni.upx2px(this.fontSize) + 'px';
  148. },
  149. black_tmeme: function() {
  150. if (this.black !== null) return this.black;
  151. return this.$tm.vx.state().tmVuetify.black;
  152. },
  153. color_tmeme: function() {
  154. if (this.$tm.vx.state().tmVuetify.color !== null && this.$tm.vx.state().tmVuetify.color && this.fllowTheme) {
  155. return this.$tm.vx.state().tmVuetify.color;
  156. }
  157. return this.color;
  158. },
  159. configStyle: {
  160. get: function() {
  161. return this.wkstyle;
  162. },
  163. set: function(obj) {
  164. this.wkstyle = uni.$tm.objToString(obj);
  165. }
  166. }
  167. },
  168. mounted() {
  169. this.imgstyle = {
  170. width: uni.upx2px(parseInt(this.size)) + 'px',
  171. height: uni.upx2px(parseInt(this.size)) + 'px'
  172. };
  173. },
  174. methods: {
  175. setConfigStyle(val) {
  176. this.configStyle = val;
  177. },
  178. onclick(e) {
  179. this.$emit('click', { event: e, src: this.src, label: this.label });
  180. }
  181. }
  182. };
  183. </script>
  184. <style lang="scss" scoped>
  185. .tm--avatar {
  186. position: relative;
  187. line-height: 0;
  188. vertical-align: middle;
  189. .tm--avatar--dot {
  190. position: absolute;
  191. z-index: 10;
  192. width: 100%;
  193. &.bottom {
  194. bottom: 0upx;
  195. }
  196. }
  197. .tm--avatar--conter {
  198. line-height: 0;
  199. }
  200. }
  201. </style>