ActivityCard.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <view class="active-box">
  3. <!-- 热门标签 -->
  4. <view v-if="config.hot" class="btn-tag-star">
  5. <uni-icons type="star-filled" size="16" color="#ffd700"></uni-icons>
  6. </view>
  7. <!-- 图片区域 -->
  8. <view class="img crm-cursor" @click="handleClick">
  9. <image :src="imageSrc" mode="aspectFill" />
  10. <view v-if="config.imageTitle" class="imgTitle">{{ t(config.imageTitle) }}</view>
  11. </view>
  12. <!-- 内容区域 -->
  13. <view class="content">
  14. <view class="title">
  15. <span class="name crm-cursor crm-one-font" @click="handleClick">
  16. {{ t(config.title) }}
  17. </span>
  18. <span v-if="config.time" class="time">{{ config.time }}</span>
  19. </view>
  20. <view v-if="config.description" class="des crm-one-font">
  21. {{ t(config.description) }}
  22. </view>
  23. <!-- 按钮区域 -->
  24. <view class="bottom">
  25. <template v-for="(btn, index) in config.buttons" :key="index">
  26. <span v-if="shouldShowButton(btn)" :class="['btn', getButtonType(btn)]" @click="handleButtonClick(btn)">
  27. {{ t(btn.text) }}
  28. <template v-if="btn.suffix && state[btn.suffix]">
  29. {{ state[btn.suffix] }}
  30. </template>
  31. <template v-if="btn.suffixText">
  32. {{ t(btn.suffixText) }}
  33. </template>
  34. </span>
  35. <span v-else-if="btn.elseType && btn.condition" :class="['btn', btn.elseType]">
  36. {{ t(btn.text) }}
  37. </span>
  38. </template>
  39. </view>
  40. </view>
  41. </view>
  42. </template>
  43. <script setup lang="ts">
  44. import { computed } from 'vue'
  45. import { useI18n } from 'vue-i18n'
  46. const { t, locale} = useI18n()
  47. const props = defineProps<{
  48. config: any
  49. state?: Record<string, any>
  50. }>()
  51. const emit = defineEmits(['action'])
  52. // 图片源
  53. const imageSrc = computed(() => {
  54. if (typeof props.config.image === 'object') {
  55. const lang = locale.value || 'en'
  56. return props.config.image[lang] || props.config.image.default
  57. }
  58. return props.config.image
  59. })
  60. // 判断按钮是否显示
  61. const shouldShowButton = (btn: any): boolean => {
  62. if (!btn.condition) return true
  63. if (!props.state) return false
  64. try {
  65. const conditionStr = btn.condition
  66. const evalStr = conditionStr.replace(/([a-zA-Z_][a-zA-Z0-9_.]*)/g, (match) => {
  67. if (match.includes('.')) {
  68. const parts = match.split('.')
  69. let value: any = props.state
  70. for (const part of parts) {
  71. value = value?.[part]
  72. }
  73. return JSON.stringify(value)
  74. }
  75. return JSON.stringify(props.state[match])
  76. })
  77. const fn = new Function(`return ${evalStr}`)
  78. return fn()
  79. } catch (error) {
  80. console.error('按钮条件评估失败:', error, btn.condition)
  81. return false
  82. }
  83. }
  84. // 获取按钮类型
  85. const getButtonType = (btn: any): string => {
  86. if (btn.type === 'dynamic') {
  87. return shouldShowButton(btn) ? 'red' : 'gray'
  88. }
  89. return btn.type
  90. }
  91. // 处理点击事件
  92. const handleClick = () => {
  93. if (props.config.onClick) {
  94. emit('action', {
  95. type: props.config.onClick,
  96. params: props.config.onClickParams || []
  97. })
  98. }
  99. }
  100. // 处理按钮点击
  101. const handleButtonClick = (btn: any) => {
  102. if (btn.action && btn.action !== 'disabled') {
  103. emit('action', {
  104. type: btn.action,
  105. params: btn.params || []
  106. })
  107. }
  108. }
  109. </script>
  110. <style scoped lang="scss">
  111. .active-box {
  112. position: relative;
  113. display: flex;
  114. background-color: #fff;
  115. border-radius: 16rpx;
  116. padding: 30rpx;
  117. margin-bottom: 20rpx;
  118. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
  119. .btn-tag-star {
  120. position: absolute;
  121. top: 0;
  122. left: 0;
  123. z-index: 1;
  124. }
  125. .img {
  126. width: 200rpx;
  127. height: 200rpx;
  128. margin-right: 30rpx;
  129. border-radius: 12rpx;
  130. overflow: hidden;
  131. position: relative;
  132. image {
  133. width: 100%;
  134. height: 100%;
  135. object-fit: cover;
  136. }
  137. .imgTitle {
  138. position: absolute;
  139. bottom: 0;
  140. left: 0;
  141. right: 0;
  142. background: rgba(0, 0, 0, 0.5);
  143. color: #fff;
  144. font-size: 24rpx;
  145. padding: 8rpx;
  146. text-align: center;
  147. }
  148. }
  149. .content {
  150. flex: 1;
  151. display: flex;
  152. flex-direction: column;
  153. .title {
  154. display: flex;
  155. align-items: center;
  156. justify-content: space-between;
  157. margin-bottom: 10rpx;
  158. .name {
  159. font-size: 32rpx;
  160. font-weight: bold;
  161. color: #333;
  162. flex: 1;
  163. overflow: hidden;
  164. text-overflow: ellipsis;
  165. white-space: nowrap;
  166. }
  167. .time {
  168. font-size: 24rpx;
  169. color: #999;
  170. margin-left: 16rpx;
  171. white-space: nowrap;
  172. }
  173. }
  174. .des {
  175. font-size: 28rpx;
  176. color: #666;
  177. margin-bottom: 20rpx;
  178. display: -webkit-box;
  179. -webkit-line-clamp: 2;
  180. -webkit-box-orient: vertical;
  181. overflow: hidden;
  182. line-height: 1.4;
  183. }
  184. .bottom {
  185. display: flex;
  186. flex-wrap: wrap;
  187. gap: 16rpx;
  188. .btn {
  189. padding: 10rpx 24rpx;
  190. border-radius: 40rpx;
  191. font-size: 24rpx;
  192. font-weight: 500;
  193. transition: all 0.3s;
  194. display: inline-flex;
  195. align-items: center;
  196. justify-content: center;
  197. white-space: nowrap;
  198. &.red {
  199. background-color: #ff4d4f;
  200. color: #fff;
  201. border: 1rpx solid #ff4d4f;
  202. &:active {
  203. opacity: 0.8;
  204. }
  205. }
  206. &.gray {
  207. background-color: #f5f5f5;
  208. color: #999;
  209. border: 1rpx solid #e5e5e5;
  210. }
  211. &.check {
  212. background-color: #e6f7ff;
  213. color: #1890ff;
  214. border: 1rpx solid #91d5ff;
  215. &:active {
  216. opacity: 0.8;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223. </style>