detail.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <!-- 优惠券详情 -->
  2. <template>
  3. <s-layout title="优惠券详情">
  4. <view class="bg-white">
  5. <!-- 详情卡片 -->
  6. <view class="detail-wrap ss-p-20">
  7. <view class="detail-box">
  8. <view class="tag-box ss-flex ss-col-center ss-row-center">
  9. <image
  10. class="tag-image"
  11. :src="sheep.$url.static('/static/img/shop/app/coupon_icon.png')"
  12. mode="aspectFit"
  13. />
  14. </view>
  15. <view class="top ss-flex-col ss-col-center">
  16. <view class="title ss-m-t-50 ss-m-b-20 ss-m-x-20">{{ state.coupon.name }}</view>
  17. <view class="subtitle ss-m-b-50">
  18. 满 {{ fen2yuan(state.coupon.usePrice) }} 元,
  19. {{
  20. state.coupon.discountType === 1
  21. ? '减 ' + fen2yuan(state.coupon.discountPrice) + ' 元'
  22. : '打 ' + state.coupon.discountPercent / 10.0 + ' 折'
  23. }}
  24. </view>
  25. <button
  26. class="ss-reset-button ss-m-b-30"
  27. :class="
  28. state.coupon.canTake || state.coupon.status === 1
  29. ? 'use-btn' // 优惠劵模版(可领取)、优惠劵(可使用)
  30. : 'disable-btn'
  31. "
  32. :disabled="!state.coupon.canTake"
  33. @click="getCoupon"
  34. >
  35. <text v-if="state.id > 0">{{ state.coupon.canTake ? '立即领取' : '已领取' }}</text>
  36. <text v-else>
  37. {{
  38. state.coupon.status === 1
  39. ? '可使用'
  40. : state.coupon.status === 2
  41. ? '已使用'
  42. : '已过期'
  43. }}
  44. </text>
  45. </button>
  46. <view class="time ss-m-y-30" v-if="state.coupon.validityType === 2">
  47. 有效期:领取后 {{ state.coupon.fixedEndTerm }} 天内可用
  48. </view>
  49. <view class="time ss-m-y-30" v-else>
  50. 有效期: {{ sheep.$helper.timeFormat(state.coupon.validStartTime, 'yyyy-mm-dd') }} 至
  51. {{ sheep.$helper.timeFormat(state.coupon.validEndTime, 'yyyy-mm-dd') }}
  52. </view>
  53. <view class="coupon-line ss-m-t-14"></view>
  54. </view>
  55. <view class="bottom">
  56. <view class="type ss-flex ss-col-center ss-row-between ss-p-x-30">
  57. <view>优惠券类型</view>
  58. <view>{{ state.coupon.discountType === 1 ? '满减券' : '折扣券' }}</view>
  59. </view>
  60. <uni-collapse>
  61. <uni-collapse-item title="优惠券说明" v-if="state.coupon.description">
  62. <view class="content ss-p-b-20">
  63. <text class="des ss-p-l-30">{{ state.coupon.description }}</text>
  64. </view>
  65. </uni-collapse-item>
  66. </uni-collapse>
  67. </view>
  68. </view>
  69. </view>
  70. <!-- 适用商品 -->
  71. <view
  72. class="all-user ss-flex ss-row-center ss-col-center"
  73. v-if="state.coupon.productScope === 1"
  74. >
  75. 全场通用
  76. </view>
  77. <su-sticky v-else bgColor="#fff">
  78. <view class="goods-title ss-p-20">
  79. {{ state.coupon.productScope === 2 ? '指定商品可用' : '指定分类可用' }}
  80. </view>
  81. <su-tabs
  82. :scrollable="true"
  83. :list="state.tabMaps"
  84. @change="onTabsChange"
  85. :current="state.currentTab"
  86. v-if="state.coupon.productScope === 3"
  87. />
  88. </su-sticky>
  89. <!-- 指定商品 -->
  90. <view v-if="state.coupon.productScope === 2">
  91. <view v-for="(item, index) in state.pagination.list" :key="index">
  92. <s-goods-column
  93. class="ss-m-20"
  94. size="lg"
  95. :data="item"
  96. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  97. :goodsFields="{
  98. title: { show: true },
  99. subtitle: { show: true },
  100. price: { show: true },
  101. original_price: { show: true },
  102. sales: { show: true },
  103. stock: { show: false },
  104. }"
  105. />
  106. </view>
  107. </view>
  108. <!-- 指定分类 -->
  109. <view v-if="state.coupon.productScope === 3">
  110. <view v-for="(item, index) in state.pagination.list" :key="index">
  111. <s-goods-column
  112. class="ss-m-20"
  113. size="lg"
  114. :data="item"
  115. @click="sheep.$router.go('/pages/goods/index', { id: item.id })"
  116. :goodsFields="{
  117. title: { show: true },
  118. subtitle: { show: true },
  119. price: { show: true },
  120. original_price: { show: true },
  121. sales: { show: true },
  122. stock: { show: false },
  123. }"
  124. ></s-goods-column>
  125. </view>
  126. </view>
  127. <uni-load-more
  128. v-if="state.pagination.total > 0 && state.coupon.productScope === 3"
  129. :status="state.loadStatus"
  130. :content-text="{
  131. contentdown: '上拉加载更多',
  132. }"
  133. @tap="loadMore"
  134. />
  135. <s-empty
  136. v-if="state.coupon.productScope === 3 && state.pagination.total === 0"
  137. paddingTop="0"
  138. icon="/static/soldout-empty.png"
  139. text="暂无商品"
  140. />
  141. </view>
  142. </s-layout>
  143. </template>
  144. <script setup>
  145. import sheep from '@/sheep';
  146. import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  147. import { reactive } from 'vue';
  148. import _ from 'lodash-es';
  149. import CouponApi from '@/sheep/api/promotion/coupon';
  150. import { fen2yuan } from '@/sheep/hooks/useGoods';
  151. import SpuApi from '@/sheep/api/product/spu';
  152. import CategoryApi from '@/sheep/api/product/category';
  153. import { resetPagination } from '@/sheep/util';
  154. const state = reactive({
  155. id: 0, // 优惠劵模版编号 templateId
  156. couponId: 0, // 用户优惠劵编号 couponId
  157. coupon: {}, // 优惠劵信息
  158. pagination: {
  159. list: [],
  160. total: 0,
  161. pageNo: 1,
  162. pageSize: 8,
  163. },
  164. categoryId: 0, // 选中的商品分类编号
  165. tabMaps: [], // 指定分类时,每个分类构成一个 tab
  166. currentTab: 0, // 选中的 tabMaps 下标
  167. loadStatus: '',
  168. });
  169. function onTabsChange(e) {
  170. resetPagination(state.pagination);
  171. state.currentTab = e.index;
  172. state.categoryId = e.value;
  173. getGoodsListByCategory();
  174. }
  175. async function getGoodsListByCategory() {
  176. state.loadStatus = 'loading';
  177. const { code, data } = await SpuApi.getSpuPage({
  178. categoryId: state.categoryId,
  179. pageNo: state.pagination.pageNo,
  180. pageSize: state.pagination.pageSize,
  181. });
  182. if (code !== 0) {
  183. return;
  184. }
  185. state.pagination.list = _.concat(state.pagination.list, data.list);
  186. state.pagination.total = data.total;
  187. state.loadStatus = state.pagination.list.length < state.pagination.total ? 'more' : 'noMore';
  188. }
  189. // 获得商品列表,指定商品范围
  190. async function getGoodsListById() {
  191. const { data, code } = await SpuApi.getSpuListByIds(state.coupon.productScopeValues.join(','));
  192. if (code !== 0) {
  193. return;
  194. }
  195. state.pagination.list = data;
  196. }
  197. // 获得分类列表
  198. async function getCategoryList() {
  199. const { data, code } = await CategoryApi.getCategoryListByIds(
  200. state.coupon.productScopeValues.join(','),
  201. );
  202. if (code !== 0) {
  203. return;
  204. }
  205. state.tabMaps = data.map((category) => ({ name: category.name, value: category.id }));
  206. // 加载第一个分类的商品列表
  207. if (state.tabMaps.length > 0) {
  208. state.categoryId = state.tabMaps[0].value;
  209. await getGoodsListByCategory();
  210. }
  211. }
  212. // 领取优惠劵
  213. async function getCoupon() {
  214. const { code } = await CouponApi.takeCoupon(state.id);
  215. if (code !== 0) {
  216. return;
  217. }
  218. uni.showToast({
  219. title: '领取成功',
  220. });
  221. setTimeout(() => {
  222. getCouponContent();
  223. }, 1000);
  224. }
  225. // 加载优惠劵信息
  226. async function getCouponContent() {
  227. const { code, data } =
  228. state.id > 0
  229. ? await CouponApi.getCouponTemplate(state.id)
  230. : await CouponApi.getCoupon(state.couponId);
  231. if (code !== 0) {
  232. return;
  233. }
  234. state.coupon = data;
  235. // 不同指定范围,加载不同数据
  236. if (state.coupon.productScope === 2) {
  237. await getGoodsListById();
  238. } else if (state.coupon.productScope === 3) {
  239. await getCategoryList();
  240. }
  241. }
  242. // 加载更多
  243. function loadMore() {
  244. if (state.loadStatus === 'noMore') {
  245. return;
  246. }
  247. state.pagination.pageNo++;
  248. getGoodsListByCategory();
  249. }
  250. onLoad((options) => {
  251. state.id = options.id;
  252. state.couponId = options.couponId;
  253. getCouponContent(state.id, state.couponId);
  254. });
  255. // 上拉加载更多
  256. onReachBottom(() => {
  257. loadMore();
  258. });
  259. </script>
  260. <style lang="scss" scoped>
  261. .goods-title {
  262. font-size: 34rpx;
  263. font-weight: bold;
  264. color: #333333;
  265. }
  266. .detail-wrap {
  267. background: linear-gradient(
  268. 180deg,
  269. var(--ui-BG-Main),
  270. var(--ui-BG-Main-gradient),
  271. var(--ui-BG-Main),
  272. #fff
  273. );
  274. }
  275. .detail-box {
  276. // background-color: var(--ui-BG);
  277. border-radius: 6rpx;
  278. position: relative;
  279. margin-top: 100rpx;
  280. .tag-box {
  281. width: 140rpx;
  282. height: 140rpx;
  283. background: var(--ui-BG);
  284. border-radius: 50%;
  285. position: absolute;
  286. top: -70rpx;
  287. left: 50%;
  288. z-index: 6;
  289. transform: translateX(-50%);
  290. .tag-image {
  291. width: 104rpx;
  292. height: 104rpx;
  293. border-radius: 50%;
  294. }
  295. }
  296. .top {
  297. background-color: #fff;
  298. border-radius: 20rpx 20rpx 0 0;
  299. -webkit-mask: radial-gradient(circle at 16rpx 100%, #0000 16rpx, red 0) -16rpx;
  300. padding: 110rpx 0 0 0;
  301. position: relative;
  302. z-index: 5;
  303. .title {
  304. font-size: 40rpx;
  305. color: #333;
  306. font-weight: bold;
  307. }
  308. .subtitle {
  309. font-size: 28rpx;
  310. color: #333333;
  311. }
  312. .use-btn {
  313. width: 386rpx;
  314. height: 80rpx;
  315. line-height: 80rpx;
  316. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  317. border-radius: 40rpx;
  318. color: $white;
  319. }
  320. .disable-btn {
  321. width: 386rpx;
  322. height: 80rpx;
  323. line-height: 80rpx;
  324. background: #e5e5e5;
  325. border-radius: 40rpx;
  326. color: $white;
  327. }
  328. .time {
  329. font-size: 26rpx;
  330. font-weight: 400;
  331. color: #999999;
  332. }
  333. .coupon-line {
  334. width: 95%;
  335. border-bottom: 2rpx solid #eeeeee;
  336. }
  337. }
  338. .bottom {
  339. background-color: #fff;
  340. border-radius: 0 0 20rpx 20rpx;
  341. -webkit-mask: radial-gradient(circle at 16rpx 0%, #0000 16rpx, red 0) -16rpx;
  342. padding: 40rpx 30rpx;
  343. .type {
  344. height: 96rpx;
  345. border-bottom: 2rpx solid #eeeeee;
  346. }
  347. }
  348. .des {
  349. font-size: 24rpx;
  350. font-weight: 400;
  351. color: #666666;
  352. }
  353. }
  354. .all-user {
  355. width: 100%;
  356. height: 300rpx;
  357. font-size: 34rpx;
  358. font-weight: bold;
  359. color: #333333;
  360. }
  361. </style>