detail.vue 11 KB

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