u-text.vue 6.2 KB

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