u-text.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view
  3. class="u-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="['u-text__price', type && `u-text__value--${type}`]"
  14. v-if="mode === 'price'"
  15. :style="[valueStyle]"
  16. >¥</text
  17. >
  18. <view class="u-text__prefix-icon" v-if="prefixIcon">
  19. <u-icon
  20. :name="prefixIcon"
  21. :customStyle="$u.addStyle(iconStyle)"
  22. ></u-icon>
  23. </view>
  24. <u-link
  25. v-if="mode === 'link'"
  26. :text="value"
  27. :href="href"
  28. underLine
  29. ></u-link>
  30. <template v-else-if="openType && isMp">
  31. <button
  32. class="u-reset-button u-text__value"
  33. :style="[valueStyle]"
  34. :data-index="index"
  35. :openType="openType"
  36. @getuserinfo="onGetUserInfo"
  37. @contact="onContact"
  38. @getphonenumber="onGetPhoneNumber"
  39. @chooseavatar="onChooseAvatar"
  40. @error="onError"
  41. @launchapp="onLaunchApp"
  42. @opensetting="onOpenSetting"
  43. :lang="lang"
  44. :session-from="sessionFrom"
  45. :send-message-title="sendMessageTitle"
  46. :send-message-path="sendMessagePath"
  47. :send-message-img="sendMessageImg"
  48. :show-message-card="showMessageCard"
  49. :app-parameter="appParameter"
  50. >
  51. {{ value }}
  52. </button>
  53. </template>
  54. <text
  55. v-else
  56. class="u-text__value"
  57. :style="[valueStyle]"
  58. :class="[
  59. type && `u-text__value--${type}`,
  60. lines && `u-line-${lines}`
  61. ]"
  62. >{{ value }}</text
  63. >
  64. <view class="u-text__suffix-icon" v-if="suffixIcon">
  65. <u-icon
  66. :name="suffixIcon"
  67. :customStyle="$u.addStyle(iconStyle)"
  68. ></u-icon>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. import value from './value.js'
  74. import button from '../../libs/mixin/button.js'
  75. import openType from '../../libs/mixin/openType.js'
  76. import props from './props.js'
  77. import mixin from '../../libs/mixin/mixin'
  78. import mpMixin from '../../libs/mixin/mpMixin'
  79. /**
  80. * Text 文本
  81. * @description 此组件集成了文本类在项目中的常用功能,包括状态,拨打电话,格式化日期,*替换,超链接...等功能。 您大可不必在使用特殊文本时自己定义,text组件几乎涵盖您能使用的大部分场景。
  82. * @tutorial https://uview.d3u.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 <u--text text="我用十年青春,赴你最后之约"></u--text>
  106. */
  107. export default {
  108. name: 'u--text',
  109. mixins: [
  110. mpMixin,
  111. mixin,
  112. value,
  113. props,
  114. // #ifdef MP
  115. button,
  116. openType
  117. // #endif
  118. ],
  119. computed: {
  120. valueStyle() {
  121. const style = {
  122. textDecoration: this.decoration,
  123. fontWeight: this.bold ? 'bold' : 'normal',
  124. wordWrap: this.wordWrap,
  125. fontSize: uni.$u.addUnit(this.size)
  126. }
  127. !this.type && (style.color = this.color)
  128. this.isNvue && this.lines && (style.lines = this.lines)
  129. this.lineHeight &&
  130. (style.lineHeight = uni.$u.addUnit(this.lineHeight))
  131. !this.isNvue && this.block && (style.display = 'block')
  132. return uni.$u.deepMerge(style, uni.$u.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. data() {
  150. return {}
  151. },
  152. // #ifdef VUE3
  153. emits: ["click"],
  154. // #endif
  155. methods: {
  156. clickHandler(e) {
  157. // 如果为手机号模式,拨打电话
  158. if (this.call && this.mode === 'phone') {
  159. uni.makePhoneCall({
  160. phoneNumber: this.text
  161. })
  162. }
  163. this.$emit('click', e)
  164. }
  165. }
  166. }
  167. </script>
  168. <style lang="scss" scoped>
  169. @import '../../libs/css/components.scss';
  170. .u-text {
  171. @include flex(row);
  172. align-items: center;
  173. flex-wrap: nowrap;
  174. flex: 1;
  175. /* #ifndef APP-NVUE */
  176. width: 100%;
  177. /* #endif */
  178. &__price {
  179. font-size: 14px;
  180. color: $u-content-color;
  181. }
  182. &__value {
  183. font-size: 14px;
  184. @include flex;
  185. color: $u-content-color;
  186. flex-wrap: wrap;
  187. // flex: 1;
  188. text-overflow: ellipsis;
  189. align-items: center;
  190. &--primary {
  191. color: $u-primary;
  192. }
  193. &--warning {
  194. color: $u-warning;
  195. }
  196. &--success {
  197. color: $u-success;
  198. }
  199. &--info {
  200. color: $u-info;
  201. }
  202. &--error {
  203. color: $u-error;
  204. }
  205. &--main {
  206. color: $u-main-color;
  207. }
  208. &--content {
  209. color: $u-content-color;
  210. }
  211. &--tips {
  212. color: $u-tips-color;
  213. }
  214. &--light {
  215. color: $u-light-color;
  216. }
  217. }
  218. }
  219. </style>