ActivityCard.vue 7.0 KB

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