index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <template>
  2. <view class="container p-bottom" v-if="order.goodsList.length">
  3. <!-- 快递配送:配送地址 -->
  4. <view v-if="curDelivery == DeliveryTypeEnum.EXPRESS.value" @click="onSelectAddress" class="flow-delivery">
  5. <view class="flow-delivery__detail dis-flex flex-y-center">
  6. <view class="detail-location dis-flex">
  7. <text class="iconfont icon-dingwei"></text>
  8. </view>
  9. <view class="detail-content flex-box">
  10. <block v-if="order.address">
  11. <view class="detail-content__title dis-flex">
  12. <text class="f-30">{{ order.address.name }}</text>
  13. <text class="detail-content__title-phone f-28">{{ order.address.phone }}</text>
  14. </view>
  15. <view class="address detail-content__describe">
  16. <text class="region" v-for="(region, idx) in order.address.region" :key="idx">{{ region }}</text>
  17. <text class="detail">{{ order.address.detail }}</text>
  18. </view>
  19. </block>
  20. <block v-else>
  21. <view class="detail-content__describe dis-flex">
  22. <text class="col-6">请选择配送地址</text>
  23. </view>
  24. </block>
  25. </view>
  26. <view class="detail-arrow dis-flex">
  27. <text class="iconfont icon-arrow-right"></text>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 商品列表 -->
  32. <view class="m-top20">
  33. <view class="checkout_list" v-for="(item, index) in order.goodsList" :key="index">
  34. <view class="flow-shopList dis-flex" data-index="index" @click="onTargetGoods(item.goods_id)">
  35. <!-- 商品图片 -->
  36. <view class="flow-list-left">
  37. <image mode="scaleToFill" :src="item.goods_image"></image>
  38. </view>
  39. <view class="flow-list-right flex-box">
  40. <!-- 商品名称 -->
  41. <text class="goods-name twoline-hide">{{ item.goods_name }}</text>
  42. <!-- 商品规格 -->
  43. <view class="goods-props clearfix">
  44. <view class="goods-props-item" v-for="(props, idx) in item.skuInfo.goods_props" :key="idx">
  45. <text class="group-name">{{ props.group.name }}: </text>
  46. <text>{{ props.value.name }};</text>
  47. </view>
  48. </view>
  49. <!-- 商品数量和单价 -->
  50. <view class="flow-list-cont dis-flex flex-x-between flex-y-center">
  51. <text class="small">×{{ item.total_num }}</text>
  52. <text class="flow-cont">¥{{ item.goods_price }}</text>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. <view class="flow-num-box b-f">
  58. <text>共{{ order.orderTotalNum }}件商品,合计:</text>
  59. <text class="flow-money col-m">¥{{ order.orderTotalPrice }}</text>
  60. </view>
  61. </view>
  62. <!-- 商品金额 -->
  63. <view class="flow-all-money b-f m-top20">
  64. <view class="flow-all-list dis-flex">
  65. <text class="flex-five">订单总金额:</text>
  66. <view class="flex-five t-r">
  67. <text class="col-m">¥{{ order.orderTotalPrice }}</text>
  68. </view>
  69. </view>
  70. <!-- 优惠券 -->
  71. <view class="flow-all-list dis-flex">
  72. <text class="flex-five">优惠券:</text>
  73. <view class="flex-five t-r">
  74. <view v-if="order.couponList.length > 0" @click="handleShowPopup()">
  75. <text class="col-m" v-if="order.couponId > 0">-¥{{ order.couponMoney }}</text>
  76. <text class="col-m" v-else>有{{ order.couponList.length }}张优惠券</text>
  77. <text class="right-arrow iconfont icon-arrow-right"></text>
  78. </view>
  79. <text v-else class="">无优惠券可用</text>
  80. </view>
  81. </view>
  82. <!-- 积分抵扣 -->
  83. <view v-if="order.isAllowPoints" class="points flow-all-list dis-flex flex-y-center">
  84. <view class="block-left flex-five" @click="handleShowPoints()">
  85. <text class="title">可用{{ order.setting.points_name }}抵扣:</text>
  86. <text class="iconfont icon-help"></text>
  87. </view>
  88. <view class="flex-five dis-flex flex-x-end flex-y-center">
  89. <text class="points-money col-m">-¥{{ order.pointsMoney }}</text>
  90. <u-switch v-model="isUsePoints" size="48" active-color="#07c160" @change="getOrderData()"></u-switch>
  91. </view>
  92. </view>
  93. <!-- 配送费用 -->
  94. <view v-if="curDelivery == DeliveryTypeEnum.EXPRESS.value" class="dis-flex flow-all-list">
  95. <text class="flex-five">配送费用:</text>
  96. <view class="flex-five t-r">
  97. <view v-if="order.address">
  98. <text class="col-m" v-if="order.isIntraRegion">+¥{{ order.expressPrice }}</text>
  99. <text v-else>不在配送范围</text>
  100. </view>
  101. <view v-else>
  102. <text class="col-7">请先选择配送地址</text>
  103. </view>
  104. </view>
  105. </view>
  106. </view>
  107. <!-- 支付方式 -->
  108. <view class="pay-method flow-all-money b-f m-top20">
  109. <view class="flow-all-list dis-flex">
  110. <text class="flex-five">支付方式</text>
  111. </view>
  112. <!-- 微信支付 -->
  113. <!-- #ifdef MP-WEIXIN -->
  114. <view class="pay-item dis-flex flex-x-between" @click="handleSelectPayType(PayTypeEnum.WECHAT.value)">
  115. <view class="item-left dis-flex flex-y-center">
  116. <view class="item-left_icon wechat">
  117. <text class="iconfont icon-wxpay"></text>
  118. </view>
  119. <view class="item-left_text">
  120. <text>{{ PayTypeEnum.WECHAT.name }}</text>
  121. </view>
  122. </view>
  123. <view class="item-right col-m" v-if="curPayType == PayTypeEnum.WECHAT.value">
  124. <text class="iconfont icon-check"></text>
  125. </view>
  126. </view>
  127. <!-- #endif -->
  128. <!-- 余额支付 -->
  129. <view class="pay-item dis-flex flex-x-between" @click="handleSelectPayType(PayTypeEnum.BALANCE.value)">
  130. <view class="item-left dis-flex flex-y-center">
  131. <view class="item-left_icon balance">
  132. <text class="iconfont icon-qiandai"></text>
  133. </view>
  134. <view class="item-left_text">
  135. <text>{{ PayTypeEnum.BALANCE.name }}</text>
  136. </view>
  137. </view>
  138. <view class="item-right col-m" v-if="curPayType == PayTypeEnum.BALANCE.value">
  139. <text class="iconfont icon-check"></text>
  140. </view>
  141. </view>
  142. </view>
  143. <!-- 买家留言 -->
  144. <view class="flow-all-money b-f m-top20">
  145. <view class="ipt-wrapper dis-flex flow-all-list">
  146. <input v-model="remark" placeholder="选填:买家留言(50字以内)"></input>
  147. </view>
  148. </view>
  149. <!-- 提交订单 -->
  150. <view class="flow-fixed-footer b-f m-top10">
  151. <view class="dis-flex chackout-box">
  152. <view class="chackout-left pl-12">实付款:
  153. <text class="col-m">¥{{ order.orderPayPrice }}</text>
  154. </view>
  155. <view class="chackout-right" @click="onSubmitOrder()">
  156. <view class="flow-btn f-32" :class="{ disabled }">提交订单</view>
  157. </view>
  158. </view>
  159. </view>
  160. <!-- 积分说明弹窗 -->
  161. <u-modal v-model="showPoints" :title="`${setting.points_name}说明`">
  162. <scroll-view class="points-content" :scroll-y="true">
  163. <text>{{ order.setting.points_describe }}</text>
  164. </scroll-view>
  165. </u-modal>
  166. <!-- 优惠券弹出框 -->
  167. <u-popup v-model="showPopup" mode="bottom">
  168. <view class="popup__coupon">
  169. <view class="coupon__title f-30">选择优惠券</view>
  170. <!-- 优惠券列表 -->
  171. <view class="coupon-list">
  172. <scroll-view :scroll-y="true" style="height: 565rpx;">
  173. <view class="coupon-item" v-for="(item, index) in order.couponList" :key="index">
  174. <view class="item-wrapper"
  175. :class="[item.is_apply ? 'color-' + CouponColors[index % CouponColors.length] : 'color-gray']"
  176. @click="handleSelectCoupon(index)">
  177. <view class="coupon-type">{{ CouponTypeEnum[item.coupon_type].name }}</view>
  178. <view class="tip dis-flex flex-dir-column flex-x-center">
  179. <view v-if="item.coupon_type == CouponTypeEnum.FULL_DISCOUNT.value">
  180. <text class="f-30">¥</text>
  181. <text class="money">{{ item.reduce_price }}</text>
  182. </view>
  183. <text class="money"
  184. v-if="item.coupon_type == CouponTypeEnum.DISCOUNT.value">{{ item.discount }}折</text>
  185. <text class="pay-line">满{{ item.min_price }}元可用</text>
  186. </view>
  187. <view class="split-line"></view>
  188. <view class="content dis-flex flex-dir-column flex-x-between">
  189. <view class="title">{{ item.name }}</view>
  190. <view class="bottom dis-flex flex-y-center">
  191. <view class="time flex-box">
  192. <block v-if="item.start_time === item.end_time">{{ item.start_time }}</block>
  193. <block v-else>{{ item.start_time }}~{{ item.end_time }}</block>
  194. </view>
  195. </view>
  196. </view>
  197. </view>
  198. </view>
  199. </scroll-view>
  200. </view>
  201. <!-- 不使用优惠券 -->
  202. <view class="coupon__do_not dis-flex flex-y-center flex-x-center">
  203. <view class="control dis-flex flex-y-center flex-x-center" @click="handleNotUseCoupon()">
  204. <text class="f-26">不使用优惠券</text>
  205. </view>
  206. </view>
  207. </view>
  208. </u-popup>
  209. </view>
  210. </template>
  211. <script>
  212. import * as Verify from '@/utils/verify'
  213. import * as CheckoutApi from '@/api/checkout'
  214. import { CouponTypeEnum } from '@/common/enum/coupon'
  215. import DeliveryTypeEnum from '@/common/enum/order/DeliveryType'
  216. import PayTypeEnum from '@/common/enum/order/PayType'
  217. import { wxPayment } from '@/core/app'
  218. const CouponColors = ['red', 'blue', 'violet', 'yellow']
  219. export default {
  220. data() {
  221. return {
  222. // 枚举类
  223. DeliveryTypeEnum,
  224. PayTypeEnum,
  225. CouponTypeEnum,
  226. // 当前页面参数
  227. options: {},
  228. // 优惠券颜色组
  229. CouponColors,
  230. // 系统设置
  231. setting: {},
  232. // 当前选中的配送方式
  233. curDelivery: null,
  234. // 当前选中的支付方式
  235. curPayType: PayTypeEnum.BALANCE.value,
  236. // 选择的优惠券
  237. selectCouponId: 0,
  238. // 是否使用积分抵扣
  239. isUsePoints: false,
  240. // 买家留言
  241. remark: '',
  242. // 禁用submit按钮
  243. disabled: false,
  244. // 是否显示积分说明
  245. showPoints: false,
  246. // 是否显示优惠券弹窗
  247. showPopup: false,
  248. // 按钮禁用
  249. disabled: false,
  250. // 订单信息 (从后端api中获取)
  251. order: {
  252. // 商品列表
  253. goodsList: [],
  254. // 优惠券列表
  255. couponList: [],
  256. // 是否存在收货地址
  257. existAddress: false,
  258. // 默认收货地址
  259. address: null,
  260. // 是否存在收货地址
  261. existAddress: false,
  262. // 当前用户收货城市是否存在配送规则中
  263. isIntraRegion: true,
  264. // 是否存在错误
  265. hasError: false,
  266. // 错误信息
  267. errorMsg: '',
  268. }
  269. }
  270. },
  271. /**
  272. * 生命周期函数--监听页面加载
  273. */
  274. onLoad(options) {
  275. this.options = options
  276. },
  277. /**
  278. * 生命周期函数--监听页面显示
  279. */
  280. onShow() {
  281. // 获取当前订单信息
  282. this.getOrderData()
  283. },
  284. methods: {
  285. // 获取订单数据
  286. getOrderData() {
  287. const app = this
  288. // 请求的参数
  289. const params = {
  290. delivery: app.curDelivery || 0,
  291. couponId: app.selectCouponId || 0,
  292. isUsePoints: app.isUsePoints ? 1 : 0,
  293. }
  294. // 请求api
  295. CheckoutApi.order(app.options.mode, app.getRequestParam())
  296. .then(result => {
  297. app.initData(result.data)
  298. })
  299. .catch(err => err)
  300. },
  301. // 初始化数据
  302. initData({ order }) {
  303. const app = this
  304. app.order = order
  305. // 显示错误信息
  306. if (order.hasError) {
  307. app.$toast(order.errorMsg)
  308. }
  309. // 当前选择的配送方式
  310. app.curDelivery = order.delivery
  311. // 当前选择支付方式 (如果是微信小程序默认使用微信支付)
  312. if (app.$platform === 'MP-WEIXIN') {
  313. app.curPayType = PayTypeEnum.WECHAT.value
  314. }
  315. },
  316. // 获取api请求的参数
  317. getRequestParam() {
  318. const app = this
  319. const { options } = app
  320. // 结算模式的固定参数
  321. const modeParam = {}
  322. // 结算模式: 立即购买
  323. if (options.mode === 'buyNow') {
  324. modeParam.goodsId = options.goodsId
  325. modeParam.goodsNum = options.goodsNum
  326. modeParam.goodsSkuId = options.goodsSkuId
  327. }
  328. // 结算模式: 购物车
  329. if (options.mode === 'cart') {
  330. modeParam.cartIds = options.cartIds
  331. }
  332. // 订单结算参数(用户选择)
  333. const orderParam = {
  334. delivery: app.curDelivery || 0,
  335. couponId: app.selectCouponId || 0,
  336. isUsePoints: app.isUsePoints ? 1 : 0,
  337. }
  338. return { ...orderParam, ...modeParam }
  339. },
  340. // 显示积分说明
  341. handleShowPoints() {
  342. this.showPoints = true
  343. },
  344. // 显示优惠券弹窗
  345. handleShowPopup() {
  346. this.showPopup = true
  347. },
  348. // 选择优惠券
  349. handleSelectCoupon(index) {
  350. const app = this
  351. const { couponList } = app.order
  352. // 当前选择的优惠券
  353. const couponItem = couponList[index]
  354. // 判断是否在适用范围
  355. if (!couponItem.is_apply) {
  356. app.$toast(couponItem.not_apply_info)
  357. return
  358. }
  359. // 记录选中的优惠券id
  360. app.selectCouponId = couponItem.user_coupon_id
  361. // 重新获取订单信息
  362. app.getOrderData()
  363. // 隐藏优惠券弹层
  364. app.showPopup = false
  365. },
  366. // 不使用优惠券
  367. handleNotUseCoupon() {
  368. const app = this
  369. app.selectCouponId = 0
  370. // 重新获取订单信息
  371. app.getOrderData()
  372. // 隐藏优惠券弹层
  373. app.showPopup = false
  374. },
  375. // 选择支付方式
  376. handleSelectPayType(value) {
  377. this.curPayType = value
  378. },
  379. // 快递配送:选择收货地址
  380. onSelectAddress() {
  381. this.$navTo('pages/address/index', { from: 'checkout' })
  382. },
  383. // 跳转到商品详情页
  384. onTargetGoods(goodsId) {
  385. this.$navTo('pages/goods/detail', { goodsId })
  386. },
  387. // 订单提交
  388. onSubmitOrder() {
  389. const app = this
  390. if (app.disabled) {
  391. return false
  392. }
  393. // 表单验证
  394. if (!app.onVerifyFrom()) {
  395. return false
  396. }
  397. // 按钮禁用
  398. app.disabled = true
  399. // 请求api
  400. CheckoutApi.submit(app.options.mode, app.getFormData())
  401. .then(result => app.onSubmitCallback(result))
  402. .catch(err => {
  403. if (err.result) {
  404. const errData = err.result.data
  405. if (errData.is_created) {
  406. app.navToMyOrder()
  407. return false
  408. }
  409. }
  410. app.disabled = false
  411. })
  412. },
  413. // 订单提交成功后回调
  414. onSubmitCallback(result) {
  415. const app = this
  416. // 发起微信支付
  417. if (result.data.payType == PayTypeEnum.WECHAT.value) {
  418. wxPayment(result.data.payment)
  419. .then(() => app.$success('支付成功'))
  420. .catch(err => app.$error('订单未支付'))
  421. .finally(() => {
  422. app.disabled = false
  423. app.navToMyOrder()
  424. })
  425. }
  426. // 余额支付
  427. if (result.data.payType == PayTypeEnum.BALANCE.value) {
  428. app.$success('支付成功')
  429. app.disabled = false
  430. app.navToMyOrder()
  431. }
  432. },
  433. // 跳转到我的订单(等待1秒)
  434. navToMyOrder() {
  435. setTimeout(() => {
  436. this.$navTo('pages/order/index')
  437. }, 1000)
  438. },
  439. // 表单提交的数据
  440. getFormData() {
  441. const app = this
  442. const { options } = app
  443. // 表单数据
  444. const form = {
  445. delivery: app.curDelivery,
  446. payType: app.curPayType,
  447. couponId: app.selectCouponId || 0,
  448. isUsePoints: app.isUsePoints ? 1 : 0,
  449. remark: app.remark || '',
  450. }
  451. // 创建订单-立即购买
  452. if (options.mode === 'buyNow') {
  453. form.goodsId = options.goodsId
  454. form.goodsNum = options.goodsNum
  455. form.goodsSkuId = options.goodsSkuId
  456. }
  457. // 创建订单-购物车结算
  458. if (options.mode === 'cart') {
  459. form.cartIds = options.cartIds || null
  460. }
  461. return form
  462. },
  463. // 表单验证
  464. onVerifyFrom() {
  465. const app = this
  466. if (app.hasError) {
  467. app.$toast(app.errorMsg)
  468. return false
  469. }
  470. return true
  471. },
  472. }
  473. }
  474. </script>
  475. <style lang="scss" scoped>
  476. @import "./style.scss";
  477. </style>