小说网站前端代码仓库
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.

377 lines
11 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. <template>
  2. <el-dialog v-model="dialogVisible" width="800px" :show-close="true" :close-on-click-modal="false"
  3. class="interactive-reward-dialog">
  4. <div class="interactive-title">互动打赏</div>
  5. <div class="interactive-items-container">
  6. <div class="interactive-items">
  7. <div v-for="(item, index) in rewardItems" :key="index" class="reward-item" :class="{ active: item.active }"
  8. @click="toggleItemSelection(index)">
  9. <div class="item-header">
  10. <img :src="item.image" :alt="item.title" class="item-icon" />
  11. <div class="check-mark" v-if="item.active">
  12. <el-icon>
  13. <Check />
  14. </el-icon>
  15. </div>
  16. </div>
  17. <div class="item-body">
  18. <div class="item-name">{{ item.title }} <span v-if="item.hot" class="hot-tag">HOT</span></div>
  19. <div class="item-price">{{ item.integerPrice }}豆豆</div>
  20. <div class="item-counter">
  21. <el-button class="counter-btn minus" @click.stop="decreaseCount(index)"
  22. :disabled="item.count <= 0">-</el-button>
  23. <span class="count-value">{{ item.count }}</span>
  24. <el-button class="counter-btn plus" @click.stop="increaseCount(index)">+</el-button>
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. <div class="reward-footer">
  31. <el-button type="primary" class="submit-btn" @click="submitReward">打赏</el-button>
  32. </div>
  33. </el-dialog>
  34. </template>
  35. <script setup>
  36. import { ref, reactive, computed, onMounted } from 'vue';
  37. import { Check } from '@element-plus/icons-vue';
  38. import { ElMessage } from 'element-plus';
  39. import { orderApi } from '@/api/user';
  40. import { useMainStore } from '@/store';
  41. const store = useMainStore();
  42. const props = defineProps({
  43. visible: {
  44. type: Boolean,
  45. default: false
  46. },
  47. bookId: {
  48. type: [String, Number],
  49. required: true
  50. }
  51. });
  52. const emit = defineEmits(['update:visible', 'reward-success']);
  53. const dialogVisible = computed({
  54. get: () => props.visible,
  55. set: (val) => emit('update:visible', val)
  56. });
  57. const loading = ref(false);
  58. // 礼物列表
  59. const rewardItems = ref([]);
  60. // 获取礼物列表
  61. const getGiftList = async () => {
  62. try {
  63. loading.value = true;
  64. const res = await orderApi.getInteractionGiftList({
  65. pageNo: 1,
  66. pageSize: 99999
  67. });
  68. if (res.result && res.result.records) {
  69. // 转换后端数据格式为前端所需格式
  70. rewardItems.value = res.result.records.map(item => ({
  71. id: item.id,
  72. title: item.title,
  73. image: item.image,
  74. integerPrice: item.integerPrice,
  75. count: 0,
  76. active: false,
  77. hot: item.hot || false
  78. }))
  79. }
  80. } catch (error) {
  81. console.error('获取礼物列表失败:', error);
  82. ElMessage.error('获取礼物列表失败');
  83. } finally {
  84. loading.value = false;
  85. }
  86. };
  87. // 获取默认图标
  88. const getDefaultIcon = (id) => {
  89. return 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png';
  90. };
  91. // 组件挂载时获取礼物列表
  92. onMounted(() => {
  93. getGiftList();
  94. });
  95. // 切换选中状态(支持多选)
  96. const toggleItemSelection = (index) => {
  97. rewardItems.value[index].active = !rewardItems.value[index].active;
  98. // 如果取消选中,数量清零
  99. if (!rewardItems.value[index].active) {
  100. rewardItems.value[index].count = 0;
  101. }
  102. // 如果选中且数量为0,设为1
  103. else if (rewardItems.value[index].count === 0) {
  104. rewardItems.value[index].count = 1;
  105. }
  106. };
  107. // 增加数量
  108. const increaseCount = (index) => {
  109. rewardItems.value[index].count++;
  110. // 如果增加数量,自动选中该项
  111. if (rewardItems.value[index].count > 0 && !rewardItems.value[index].active) {
  112. rewardItems.value[index].active = true;
  113. }
  114. };
  115. // 减少数量
  116. const decreaseCount = (index) => {
  117. if (rewardItems.value[index].count > 0) {
  118. rewardItems.value[index].count--;
  119. // 如果数量变为0,取消选中
  120. if (rewardItems.value[index].count === 0) {
  121. rewardItems.value[index].active = false;
  122. }
  123. }
  124. };
  125. // 提交打赏
  126. const submitReward = async () => {
  127. const selectedItems = rewardItems.value.filter(item => item.active && item.count > 0);
  128. if (selectedItems.length === 0) {
  129. ElMessage.warning('请选择至少一项打赏内容');
  130. return;
  131. }
  132. if(store.user.integral < selectedItems.reduce((sum, item) => sum + item.integerPrice * item.count, 0)){
  133. ElMessage.warning('您的豆豆不足,请先充值');
  134. router.push('/user/recharge');
  135. return;
  136. }
  137. try {
  138. loading.value = true;
  139. // 逐个发送打赏请求
  140. for (const item of selectedItems) {
  141. await orderApi.giveGift({
  142. bookId: props.bookId,
  143. giftId: item.id,
  144. num: item.count
  145. });
  146. }
  147. ElMessage.success('打赏成功!感谢您的支持!');
  148. emit('reward-success', {
  149. bookId: props.bookId,
  150. items: selectedItems.map(item => ({
  151. id: item.id,
  152. count: item.count
  153. }))
  154. });
  155. // 更新用户积分信息
  156. await store.fetchLatestUserInfo();
  157. // 重置并关闭弹窗
  158. rewardItems.value.forEach(item => {
  159. item.count = 0;
  160. item.active = false;
  161. });
  162. dialogVisible.value = false;
  163. } catch (error) {
  164. console.error('打赏失败:', error);
  165. ElMessage.error('打赏失败,请稍后重试');
  166. } finally {
  167. loading.value = false;
  168. }
  169. };
  170. </script>
  171. <style lang="scss" scoped>
  172. .interactive-reward-dialog {
  173. :deep(.el-dialog) {
  174. background: linear-gradient(to bottom, #F1E4FE, #F3F9FF) !important;
  175. }
  176. :deep(.el-dialog__header) {
  177. text-align: center;
  178. margin-bottom: 10px;
  179. .el-dialog__title {
  180. font-size: 18px;
  181. font-weight: bold;
  182. }
  183. }
  184. .interactive-title{
  185. font-size: 22px;
  186. font-weight: bold;
  187. margin-bottom: 25px;
  188. text-align: center;
  189. }
  190. .interactive-items-container {
  191. max-height: 600px;
  192. overflow-y: auto;
  193. padding: 5px;
  194. margin-bottom: 20px;
  195. &::-webkit-scrollbar {
  196. width: 6px;
  197. }
  198. &::-webkit-scrollbar-track {
  199. background: #f1f1f1;
  200. border-radius: 3px;
  201. }
  202. &::-webkit-scrollbar-thumb {
  203. background: #0A2463;
  204. border-radius: 3px;
  205. }
  206. }
  207. .interactive-items {
  208. display: grid;
  209. grid-template-columns: repeat(3, 1fr);
  210. gap: 15px;
  211. .reward-item {
  212. aspect-ratio: 1 / 1;
  213. border: 1.5px solid #ebebeb;
  214. border-radius: 12px;
  215. padding: 15px;
  216. cursor: pointer;
  217. transition: all 0.3s;
  218. display: flex;
  219. flex-direction: column;
  220. position: relative;
  221. background-color: white;
  222. &.active {
  223. border-color: #0A2463;
  224. background-color: rgba(10, 36, 99, 0.05);
  225. box-shadow: 0 2px 8px rgba(10, 36, 99, 0.1);
  226. }
  227. .item-header {
  228. flex: 1;
  229. display: flex;
  230. justify-content: center;
  231. align-items: center;
  232. position: relative;
  233. margin-bottom: 12px;
  234. .item-icon {
  235. width: 60px;
  236. height: 60px;
  237. object-fit: contain;
  238. }
  239. .check-mark {
  240. position: absolute;
  241. top: -8px;
  242. right: -8px;
  243. width: 24px;
  244. height: 24px;
  245. background-color: #0A2463;
  246. border-radius: 50%;
  247. display: flex;
  248. align-items: center;
  249. justify-content: center;
  250. color: white;
  251. font-size: 14px;
  252. }
  253. }
  254. .item-body {
  255. display: flex;
  256. flex-direction: column;
  257. align-items: center;
  258. margin-top: 10px;
  259. .item-name {
  260. font-size: 16px;
  261. font-weight: 500;
  262. margin-bottom: 6px;
  263. display: flex;
  264. align-items: center;
  265. .hot-tag {
  266. background-color: #FF7C6A;
  267. color: white;
  268. font-size: 12px;
  269. padding: 1px 5px;
  270. border-radius: 4px;
  271. margin-left: 6px;
  272. }
  273. }
  274. .item-price {
  275. font-size: 14px;
  276. color: #999;
  277. margin-bottom: 12px;
  278. }
  279. .item-counter {
  280. display: flex;
  281. align-items: center;
  282. margin-top: 5px;
  283. .counter-btn {
  284. width: 28px;
  285. height: 28px;
  286. padding: 0;
  287. display: flex;
  288. align-items: center;
  289. justify-content: center;
  290. border: 1px solid #ddd;
  291. font-size: 16px;
  292. &.minus {
  293. border-radius: 4px 0 0 4px;
  294. }
  295. &.plus {
  296. border-radius: 0 4px 4px 0;
  297. }
  298. }
  299. .count-value {
  300. width: 40px;
  301. height: 28px;
  302. line-height: 28px;
  303. text-align: center;
  304. border-top: 1px solid #ddd;
  305. border-bottom: 1px solid #ddd;
  306. font-size: 16px;
  307. }
  308. }
  309. }
  310. }
  311. }
  312. .reward-footer {
  313. text-align: center;
  314. margin-top: 15px;
  315. .submit-btn {
  316. width: 80%;
  317. height: 40px;
  318. background-color: #0A2463;
  319. border: none;
  320. border-radius: 20px;
  321. font-size: 16px;
  322. }
  323. }
  324. }
  325. </style>