You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.5 KiB

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