detail.vue 11 KB

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