detail.vue 11 KB

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