uv-text.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view
  3. class="uv-text"
  4. :class="[]"
  5. v-if="show"
  6. :style="{
  7. margin: margin,
  8. justifyContent: align === 'left' ? 'flex-start' : align === 'center' ? 'center' : 'flex-end'
  9. }"
  10. @tap="clickHandler"
  11. >
  12. <text
  13. :class="['uv-text__price', type && `uv-text__value--${type}`]"
  14. v-if="mode === 'price'"
  15. :style="[valueStyle]"
  16. >¥</text
  17. >
  18. <view class="uv-text__prefix-icon" v-if="prefixIcon">
  19. <uv-icon
  20. :name="prefixIcon"
  21. :customStyle="$uv.addStyle(iconStyle)"
  22. ></uv-icon>
  23. </view>
  24. <uv-link
  25. v-if="mode === 'link'"
  26. :text="value"
  27. :href="href"
  28. underLine
  29. ></uv-link>
  30. <template v-else-if="openType && isMp">
  31. <button
  32. class="uv-reset-button uv-text__value"
  33. :style="[valueStyle]"
  34. :openType="openType"
  35. @getuserinfo="onGetUserInfo"
  36. @contact="onContact"
  37. @getphonenumber="onGetPhoneNumber"
  38. @error="onError"
  39. @launchapp="onLaunchApp"
  40. @opensetting="onOpenSetting"
  41. :lang="lang"
  42. :session-from="sessionFrom"
  43. :send-message-title="sendMessageTitle"
  44. :send-message-path="sendMessagePath"
  45. :send-message-img="sendMessageImg"
  46. :show-message-card="showMessageCard"
  47. :app-parameter="appParameter"
  48. >
  49. {{ value }}
  50. </button>
  51. </template>
  52. <text
  53. v-else
  54. class="uv-text__value"
  55. :style="[valueStyle]"
  56. :class="[
  57. type && `uv-text__value--${type}`,
  58. lines && `uv-line-${lines}`
  59. ]"
  60. >{{ value }}</text
  61. >
  62. <view class="uv-text__suffix-icon" v-if="suffixIcon">
  63. <uv-icon
  64. :name="suffixIcon"
  65. :customStyle="$uv.addStyle(iconStyle)"
  66. ></uv-icon>
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. import value from './value.js'
  72. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  73. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  74. import button from '@/uni_modules/uv-ui-tools/libs/mixin/button.js'
  75. import openType from '@/uni_modules/uv-ui-tools/libs/mixin/openType.js'
  76. import props from './props.js'
  77. import uvIcon from '@/uni_modules/uv-icon/components/uv-icon/uv-icon.vue'
  78. import uvLink from '@/uni_modules/uv-link/components/uv-link/uv-link.vue'
  79. /**
  80. * Text 文本
  81. * @description 此组件集成了文本类在项目中的常用功能,包括状态,拨打电话,格式化日期,*替换,超链接...等功能。 您大可不必在使用特殊文本时自己定义,text组件几乎涵盖您能使用的大部分场景。
  82. * @tutorial https://www.uvui.cn/components/loading.html
  83. * @property {String} type 主题颜色
  84. * @property {Boolean} show 是否显示(默认 true )
  85. * @property {String | Number} text 显示的值
  86. * @property {String} prefixIcon 前置图标
  87. * @property {String} suffixIcon 后置图标
  88. * @property {String} mode 文本处理的匹配模式 text-普通文本,price-价格,phone-手机号,name-姓名,date-日期,link-超链接
  89. * @property {String} href mode=link下,配置的链接
  90. * @property {String | Function} format 格式化规则
  91. * @property {Boolean} call mode=phone时,点击文本是否拨打电话(默认 false )
  92. * @property {String} openType 小程序的打开方式
  93. * @property {Boolean} bold 是否粗体,默认normal(默认 false )
  94. * @property {Boolean} block 是否块状(默认 false )
  95. * @property {String | Number} lines 文本显示的行数,如果设置,超出此行数,将会显示省略号
  96. * @property {String} color 文本颜色(默认 '#303133' )
  97. * @property {String | Number} size 字体大小(默认 15 )
  98. * @property {Object | String} iconStyle 图标的样式 (默认 {fontSize: '15px'} )
  99. * @property {String} decoration 文字装饰,下划线,中划线等,可选值 none|underline|line-through(默认 'none' )
  100. * @property {Object | String | Number} margin 外边距,对象、字符串,数值形式均可(默认 0 )
  101. * @property {String | Number} lineHeight 文本行高
  102. * @property {String} align 文本对齐方式,可选值left|center|right(默认 'left' )
  103. * @property {String} wordWrap 文字换行,可选值break-word|normal|anywhere(默认 'normal' )
  104. * @event {Function} click 点击触发事件
  105. * @example <uv-text text="我用十年青春,赴你最后之约"></uv-text>
  106. */
  107. export default {
  108. name: 'uv-text',
  109. emits: ['click'],
  110. // #ifdef MP
  111. mixins: [mpMixin, mixin, value, button, openType, props],
  112. // #endif
  113. // #ifndef MP
  114. mixins: [mpMixin, mixin, value, props],
  115. // #endif
  116. computed: {
  117. valueStyle() {
  118. const style = {
  119. textDecoration: this.decoration,
  120. fontWeight: this.bold ? 'bold' : 'normal',
  121. wordWrap: this.wordWrap,
  122. fontSize: this.$uv.addUnit(this.size)
  123. };
  124. !this.type && (style.color = this.color);
  125. this.isNvue && this.lines && (style.lines = this.lines);
  126. if(this.isNvue && this.mode != 'price' && !this.prefixIcon && !this.suffixIcon) {
  127. style.flex = 1;
  128. style.textAlign = this.align === 'left' ? 'flex-start' : this.align === 'center' ? 'center' : 'right';
  129. }
  130. this.lineHeight && (style.lineHeight = this.$uv.addUnit(this.lineHeight));
  131. !this.isNvue && this.block && (style.display = 'block');
  132. return this.$uv.deepMerge(style, this.$uv.addStyle(this.customStyle));
  133. },
  134. isNvue() {
  135. let nvue = false
  136. // #ifdef APP-NVUE
  137. nvue = true
  138. // #endif
  139. return nvue
  140. },
  141. isMp() {
  142. let mp = false
  143. // #ifdef MP
  144. mp = true
  145. // #endif
  146. return mp
  147. }
  148. },
  149. components:{
  150. uvIcon,
  151. uvLink
  152. },
  153. data() {
  154. return {}
  155. },
  156. methods: {
  157. clickHandler() {
  158. // 如果为手机号模式,拨打电话
  159. if (this.call && this.mode === 'phone') {
  160. uni.makePhoneCall({
  161. phoneNumber: this.text
  162. })
  163. }
  164. this.$emit('click')
  165. }
  166. }
  167. }
  168. </script>
  169. <style lang="scss" scoped>
  170. $show-lines: 1;
  171. $show-reset-button: 1;
  172. @import '@/uni_modules/uv-ui-tools/libs/css/variable.scss';
  173. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  174. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  175. .uv-text {
  176. @include flex(row);
  177. align-items: center;
  178. flex-wrap: nowrap;
  179. flex: 1;
  180. /* #ifndef APP-NVUE */
  181. width: 100%;
  182. /* #endif */
  183. &__price {
  184. font-size: 14px;
  185. color: $uv-content-color;
  186. }
  187. &__value {
  188. font-size: 14px;
  189. @include flex;
  190. color: $uv-content-color;
  191. flex-wrap: wrap;
  192. // flex: 1;
  193. text-overflow: ellipsis;
  194. align-items: center;
  195. &--primary {
  196. color: $uv-primary;
  197. }
  198. &--warning {
  199. color: $uv-warning;
  200. }
  201. &--success {
  202. color: $uv-success;
  203. }
  204. &--info {
  205. color: $uv-info;
  206. }
  207. &--error {
  208. color: $uv-error;
  209. }
  210. &--main {
  211. color: $uv-main-color;
  212. }
  213. &--content {
  214. color: $uv-content-color;
  215. }
  216. &--tips {
  217. color: $uv-tips-color;
  218. }
  219. &--light {
  220. color: $uv-light-color;
  221. }
  222. }
  223. }
  224. </style>