3
0

u-text.vue 7.7 KB

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