index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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-xiangyoujiantou"></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 twolist-hidden">{{ 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-xiangyoujiantou"></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-weixinzhifu"></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-duihao"></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-duihao"></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" :class="[item.is_apply ? 'color-' + CouponColors[index % CouponColors.length] : 'color-gray']"
  175. @click="handleSelectCoupon(index)">
  176. <view class="coupon-type">{{ CouponTypeEnum[item.coupon_type].name }}</view>
  177. <view class="tip dis-flex flex-dir-column flex-x-center">
  178. <view v-if="item.coupon_type == CouponTypeEnum.FULL_DISCOUNT.value">
  179. <text class="f-30">¥</text>
  180. <text class="money">{{ item.reduce_price }}</text>
  181. </view>
  182. <text class="money" v-if="item.coupon_type == CouponTypeEnum.DISCOUNT.value">{{ item.discount }}折</text>
  183. <text class="pay-line">满{{ item.min_price }}元可用</text>
  184. </view>
  185. <view class="split-line"></view>
  186. <view class="content dis-flex flex-dir-column flex-x-between">
  187. <view class="title">{{ item.name }}</view>
  188. <view class="bottom dis-flex flex-y-center">
  189. <view class="time flex-box">
  190. <text>有效期:{{ item.start_time }}~{{ item.end_time }}</text>
  191. </view>
  192. </view>
  193. </view>
  194. </view>
  195. </view>
  196. </scroll-view>
  197. </view>
  198. <!-- 不使用优惠券 -->
  199. <view class="coupon__do_not dis-flex flex-y-center flex-x-center">
  200. <view class="control dis-flex flex-y-center flex-x-center" @click="handleNotUseCoupon()">
  201. <text class="f-26">不使用优惠券</text>
  202. </view>
  203. </view>
  204. </view>
  205. </u-popup>
  206. </view>
  207. </template>
  208. <script>
  209. import * as Verify from '@/utils/verify'
  210. import * as CheckoutApi from '@/api/checkout'
  211. import { CouponTypeEnum } from '@/common/enum/coupon'
  212. import DeliveryTypeEnum from '@/common/enum/order/DeliveryType'
  213. import PayTypeEnum from '@/common/enum/order/PayType'
  214. import { wxPayment } from '@/utils/app'
  215. const CouponColors = ['red', 'blue', 'violet', 'yellow']
  216. export default {
  217. data() {
  218. return {
  219. // 枚举类
  220. DeliveryTypeEnum,
  221. PayTypeEnum,
  222. CouponTypeEnum,
  223. // 当前页面参数
  224. options: {},
  225. // 优惠券颜色组
  226. CouponColors,
  227. // 系统设置
  228. setting: {},
  229. // 当前选中的配送方式
  230. curDelivery: null,
  231. // 当前选中的支付方式
  232. curPayType: PayTypeEnum.BALANCE.value,
  233. // 选择的优惠券
  234. selectCouponId: 0,
  235. // 是否使用积分抵扣
  236. isUsePoints: false,
  237. // 买家留言
  238. remark: '',
  239. // 禁用submit按钮
  240. disabled: false,
  241. // 是否显示积分说明
  242. showPoints: false,
  243. // 是否显示优惠券弹窗
  244. showPopup: false,
  245. // 按钮禁用
  246. disabled: false,
  247. // 订单信息 (从后端api中获取)
  248. order: {
  249. // 商品列表
  250. goodsList: [],
  251. // 优惠券列表
  252. couponList: [],
  253. // 是否存在收货地址
  254. existAddress: false,
  255. // 默认收货地址
  256. address: null,
  257. // 是否存在收货地址
  258. existAddress: false,
  259. // 当前用户收货城市是否存在配送规则中
  260. isIntraRegion: true,
  261. // 是否存在错误
  262. hasError: false,
  263. // 错误信息
  264. errorMsg: '',
  265. }
  266. }
  267. },
  268. /**
  269. * 生命周期函数--监听页面加载
  270. */
  271. onLoad(options) {
  272. this.options = options
  273. },
  274. /**
  275. * 生命周期函数--监听页面显示
  276. */
  277. onShow() {
  278. // 获取当前订单信息
  279. this.getOrderData()
  280. },
  281. methods: {
  282. // 获取订单数据
  283. getOrderData() {
  284. const app = this
  285. // 请求的参数
  286. const params = {
  287. delivery: app.curDelivery || 0,
  288. couponId: app.selectCouponId || 0,
  289. isUsePoints: app.isUsePoints ? 1 : 0,
  290. }
  291. // 请求api
  292. CheckoutApi.order(app.options.mode, app.getRequestParam())
  293. .then(result => {
  294. app.initData(result.data)
  295. })
  296. .catch(err => err)
  297. },
  298. // 初始化数据
  299. initData({ order }) {
  300. const app = this
  301. app.order = order
  302. // 显示错误信息
  303. if (order.hasError) {
  304. app.$toast(order.errorMsg)
  305. }
  306. // 当前选择的配送方式
  307. app.curDelivery = order.delivery
  308. // 当前选择支付方式 (如果是微信小程序默认使用微信支付)
  309. if (app.$platform === 'MP-WEIXIN') {
  310. app.curPayType = PayTypeEnum.WECHAT.value
  311. }
  312. },
  313. // 获取api请求的参数
  314. getRequestParam() {
  315. const app = this
  316. const { options } = app
  317. // 结算模式的固定参数
  318. const modeParam = {}
  319. // 结算模式: 立即购买
  320. if (options.mode === 'buyNow') {
  321. modeParam.goodsId = options.goodsId
  322. modeParam.goodsNum = options.goodsNum
  323. modeParam.goodsSkuId = options.goodsSkuId
  324. }
  325. // 结算模式: 购物车
  326. if (options.mode === 'cart') {
  327. modeParam.cartIds = options.cartIds
  328. }
  329. // 订单结算参数(用户选择)
  330. const orderParam = {
  331. delivery: app.curDelivery || 0,
  332. couponId: app.selectCouponId || 0,
  333. isUsePoints: app.isUsePoints ? 1 : 0,
  334. }
  335. return { ...orderParam, ...modeParam }
  336. },
  337. // 显示积分说明
  338. handleShowPoints() {
  339. this.showPoints = true
  340. },
  341. // 显示优惠券弹窗
  342. handleShowPopup() {
  343. this.showPopup = true
  344. },
  345. // 选择优惠券
  346. handleSelectCoupon(index) {
  347. const app = this
  348. const { couponList } = app.order
  349. // 当前选择的优惠券
  350. const couponItem = couponList[index]
  351. // 判断是否在适用范围
  352. if (!couponItem.is_apply) {
  353. app.$toast(couponItem.not_apply_info)
  354. return
  355. }
  356. // 记录选中的优惠券id
  357. app.selectCouponId = couponItem.user_coupon_id
  358. // 重新获取订单信息
  359. app.getOrderData()
  360. // 隐藏优惠券弹层
  361. app.showPopup = false
  362. },
  363. // 不使用优惠券
  364. handleNotUseCoupon() {
  365. const app = this
  366. app.selectCouponId = 0
  367. // 重新获取订单信息
  368. app.getOrderData()
  369. // 隐藏优惠券弹层
  370. app.showPopup = false
  371. },
  372. // 选择支付方式
  373. handleSelectPayType(value) {
  374. this.curPayType = value
  375. },
  376. // 快递配送:选择收货地址
  377. onSelectAddress() {
  378. this.$navTo('pages/address/index', { from: 'checkout' })
  379. },
  380. // 跳转到商品详情页
  381. onTargetGoods(goodsId) {
  382. this.$navTo('pages/goods/detail', { goodsId })
  383. },
  384. // 订单提交
  385. onSubmitOrder() {
  386. const app = this
  387. if (app.disabled) {
  388. return false
  389. }
  390. // 表单验证
  391. if (!app.onVerifyFrom()) {
  392. return false
  393. }
  394. // 按钮禁用
  395. app.disabled = true
  396. // 请求api
  397. CheckoutApi.submit(app.options.mode, app.getFormData())
  398. .then(result => app.onSubmitCallback(result))
  399. .catch(err => {
  400. if (err.result) {
  401. const errData = err.result.data
  402. if (errData.is_created) {
  403. app.navToMyOrder()
  404. return false
  405. }
  406. }
  407. app.disabled = false
  408. })
  409. },
  410. // 订单提交成功后回调
  411. onSubmitCallback(result) {
  412. const app = this
  413. // 发起微信支付
  414. if (result.data.payType == PayTypeEnum.WECHAT.value) {
  415. wxPayment(result.data.payment)
  416. .then(() => app.$success('支付成功'))
  417. .catch(err => app.$error('订单未支付'))
  418. .finally(() => {
  419. app.disabled = false
  420. app.navToMyOrder()
  421. })
  422. }
  423. // 余额支付
  424. if (result.data.payType == PayTypeEnum.BALANCE.value) {
  425. app.$success('支付成功')
  426. app.disabled = false
  427. app.navToMyOrder()
  428. }
  429. },
  430. // 跳转到我的订单(等待1秒)
  431. navToMyOrder() {
  432. setTimeout(() => {
  433. this.$navTo('pages/order/index')
  434. }, 1000)
  435. },
  436. // 表单提交的数据
  437. getFormData() {
  438. const app = this
  439. const { options } = app
  440. // 表单数据
  441. const form = {
  442. delivery: app.curDelivery,
  443. payType: app.curPayType,
  444. couponId: app.selectCouponId || 0,
  445. isUsePoints: app.isUsePoints ? 1 : 0,
  446. remark: app.remark || '',
  447. }
  448. // 创建订单-立即购买
  449. if (options.mode === 'buyNow') {
  450. form.goodsId = options.goodsId
  451. form.goodsNum = options.goodsNum
  452. form.goodsSkuId = options.goodsSkuId
  453. }
  454. // 创建订单-购物车结算
  455. if (options.mode === 'cart') {
  456. form.cartIds = options.cartIds || null
  457. }
  458. return form
  459. },
  460. // 表单验证
  461. onVerifyFrom() {
  462. const app = this
  463. if (app.hasError) {
  464. app.$toast(app.errorMsg)
  465. return false
  466. }
  467. return true
  468. },
  469. }
  470. }
  471. </script>
  472. <style lang="scss" scoped>
  473. @import "./style.scss";
  474. </style>