order.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <scroll-view scroll-y="true" class="scroll-view">
  6. <view class="list-scroll-view">
  7. <view class="one_1" @tap="toChoose">
  8. <text class="localicon iconfont icon-dingweixiao"></text>
  9. <view class="other" v-if="address._id">
  10. <view class="name">
  11. <text>{{address.name}},</text>{{address.phone}}
  12. </view>
  13. <view class="other_1">
  14. <text>{{address.province}}</text><text>{{address.city}}</text>
  15. <text>{{address.area}}</text><text>{{address.address}}</text>
  16. </view>
  17. </view>
  18. <view class="address" v-else><text>请选择一个收货地址</text></view>
  19. <text class="iconfont icon-jiantouyou"></text>
  20. </view>
  21. <view class="one_2">
  22. <view class="list" v-for="(item,index) in orderList" :key="index">
  23. <view class="list_1">
  24. <view class="l">
  25. <text class="iconfont icon-shangdian"></text>
  26. <text>{{item.shop_name}}</text>
  27. </view>
  28. </view>
  29. <view class="list_2" v-for="(tag,index) in item.goods" :key="index">
  30. <view class="l">
  31. <image class="image" :src="tag.file&&tag.file.length>0?tag.file[0].url:''"
  32. mode=""></image>
  33. </view>
  34. <view class="c">
  35. <view class="name">
  36. {{tag.goods_name}}
  37. </view>
  38. <view class="Spec">
  39. 规格:{{tag.goodsSpec_name}}
  40. </view>
  41. </view>
  42. <view class="r">
  43. <view class="price" v-if="type=='0'">
  44. ¥{{tag.money}}
  45. </view>
  46. <view v-else class="price">
  47. ¥{{tag.group_sell_money}}
  48. </view>
  49. <view class="num">
  50. ×{{tag.num}}
  51. </view>
  52. </view>
  53. </view>
  54. <view class="list_3">
  55. <view class="other">
  56. <view class="other_1">配送方式</view>
  57. <view class="other_2"><text class="iconfont icon-duihao"></text>快递配送</view>
  58. </view>
  59. <view class="other">
  60. <view class="other_1">运费</view>
  61. <view class="other_2" v-if="!item.freight_total==0">¥{{item.freight_total}}
  62. </view>
  63. <view class="other_2" v-else>包邮</view>
  64. </view>
  65. <view class="other">
  66. <view class="other_1">订单备注</view>
  67. <view class="other_3">
  68. <input type="text" v-model="item.remarks" placeholder="选填,可填写您与卖家达成一致的要求" />
  69. </view>
  70. </view>
  71. </view>
  72. </view>
  73. <view class="other" v-if="type=='0'">
  74. <view class="other_1">优惠劵</view>
  75. <view class="other_2" @click="toCoupon" v-if="couponList.length">
  76. {{coupon_name||'请选择优惠劵'}}
  77. </view>
  78. <view class="other_2" v-else>暂无优惠劵使用</view>
  79. </view>
  80. </view>
  81. </view>
  82. </scroll-view>
  83. </view>
  84. <view class="two">
  85. <view class="two_1">
  86. <view>实付金额:</view>
  87. <view>¥{{goods_total}}</view>
  88. </view>
  89. <view class="two_2">
  90. <text @click="toSubmit">提交订单</text>
  91. </view>
  92. </view>
  93. </view>
  94. <uni-popup ref="popup" background-color="#fff" type="bottom">
  95. <view class="popup" v-if="popup.type=='1'">
  96. <scroll-view scroll-y="true" class="scroll-view">
  97. <discount :Style="Style" :couponList="couponList" @toDiscount="toDiscount"></discount>
  98. </scroll-view>
  99. </view>
  100. </uni-popup>
  101. </mobile-frame>
  102. </template>
  103. <script>
  104. import discount from '@/components/discount/index.vue';
  105. export default {
  106. components: {
  107. discount
  108. },
  109. data() {
  110. return {
  111. popup: {
  112. type: '1'
  113. },
  114. Style: {
  115. btn: true
  116. },
  117. user: {},
  118. key: '',
  119. address: {},
  120. orderList: [],
  121. shop: [],
  122. goods_total: 0,
  123. // 金额明细
  124. total_detail: {},
  125. // 收货地址
  126. addressList: [],
  127. // 优惠劵
  128. couponList: [],
  129. coupon: [],
  130. // 优惠劵名称
  131. coupon_name: '',
  132. // 是否开团
  133. type: '0',
  134. // 团id
  135. group_id: '',
  136. };
  137. },
  138. onLoad: function(e) {
  139. const that = this;
  140. uni.$on("id", async (id) => {
  141. let arr = await that.$api(`/address/${id}`, `GET`)
  142. if (arr.errcode == '0') {
  143. that.$set(that, `address`, arr.data)
  144. }
  145. })
  146. that.$set(that, `key`, e.key || '');
  147. that.$set(that, `group_id`, e.group_id || '');
  148. that.watchLogin()
  149. },
  150. methods: {
  151. // 使用优惠劵
  152. toDiscount(e) {
  153. const that = this;
  154. that.$set(that, 'coupon_name', e.name)
  155. that.coupon.push(e._id)
  156. that.$refs.popup.close();
  157. this.computedTotal();
  158. },
  159. // 选择收货地址
  160. toChoose() {
  161. const that = this;
  162. if (that.address?._id) {
  163. uni.navigateTo({
  164. url: `/pagesMy/address/index?type=${"shopping"}&id=${that.address._id}`
  165. })
  166. } else {
  167. uni.navigateTo({
  168. url: `/pagesMy/address/index?type=${"shopping"}`
  169. })
  170. }
  171. },
  172. // 选择优惠劵
  173. toCoupon() {
  174. const that = this;
  175. that.$set(that.popup, 'type', '1')
  176. that.$set(that, 'coupon', [])
  177. that.$refs.popup.open();
  178. },
  179. // 提交订单
  180. async toSubmit() {
  181. const that = this;
  182. if (that.address) {
  183. let data = {
  184. address: that.address,
  185. goods: that.orderList,
  186. total_detail: that.total_detail,
  187. coupon: that.coupon,
  188. type: that.type,
  189. }
  190. if (that.group_id) {
  191. data.group = that.group_id
  192. }
  193. const arr = await that.$api(`/order`, 'POST', data)
  194. if (arr.errcode == '0') {
  195. uni.getStorage({
  196. key: 'system',
  197. success: async function(res) {
  198. // 微信小程序支付
  199. if (res.data.uniPlatform == "mp-weixin") {
  200. uni.showLoading({
  201. title: '加载中'
  202. })
  203. const res = await that.$api('/pay/toPayOrder', 'POST', {
  204. order_id: arr.data,
  205. type: '0'
  206. })
  207. uni.requestPayment({
  208. "provider": "wxpay",
  209. ...res.data,
  210. async success(res) {
  211. const group = await that.$api('/group/getGroup',
  212. 'GET', {
  213. order_id: arr.data,
  214. })
  215. if (group.errcode == '0') {
  216. if (group.data) {
  217. uni.hideLoading();
  218. uni.reLaunch({
  219. url: `/pagesHome/group/share?id=${group.data}`
  220. })
  221. } else {
  222. uni.hideLoading();
  223. uni.reLaunch({
  224. url: `/pagesMy/order/index?status=${'1'}`
  225. })
  226. }
  227. } else {
  228. uni.showToast({
  229. title: group.errmsg,
  230. icon: 'none'
  231. })
  232. }
  233. },
  234. fail(e) {
  235. uni.showToast({
  236. title: `支付失败`,
  237. icon: 'none'
  238. })
  239. uni.hideLoading();
  240. uni.reLaunch({
  241. url: `/pagesMy/order/index?status=${'0'}`
  242. })
  243. }
  244. })
  245. } else if (res.data.uniPlatform == "app") {
  246. // app支付
  247. uni.requestPayment({
  248. provider: 'alipay',
  249. orderInfo: 'orderInfo', //微信、支付宝订单数据 【注意微信的订单信息,键值应该全部是小写,不能采用驼峰命名】
  250. success: function(res) {
  251. console.log('success:' + JSON.stringify(res));
  252. },
  253. fail: function(err) {
  254. console.log('fail:' + JSON.stringify(err));
  255. }
  256. });
  257. } else {
  258. uni.showToast({
  259. title: `平台不支持支付`,
  260. icon: 'none'
  261. })
  262. }
  263. },
  264. fail: function(err) {}
  265. })
  266. } else {
  267. uni.showToast({
  268. title: arr.errmsg,
  269. icon: 'none'
  270. })
  271. }
  272. } else {
  273. uni.showToast({
  274. title: `没有收货地址`,
  275. icon: 'none'
  276. })
  277. }
  278. },
  279. // 监听用户是否登录
  280. watchLogin() {
  281. const that = this;
  282. uni.getStorage({
  283. key: 'token',
  284. success: function(res) {
  285. let user = that.$jwt(res.data);
  286. that.$set(that, `user`, user);
  287. that.search()
  288. },
  289. fail: function(err) {
  290. uni.reLaunch({
  291. url: `/pages/login/index`
  292. })
  293. }
  294. })
  295. },
  296. // 查询列表
  297. async search() {
  298. const that = this;
  299. let user = that.user;
  300. const res = await that.$api(`/address`, 'GET', {
  301. customer: user._id
  302. })
  303. if (res.errcode == '0') {
  304. that.$set(that, `addressList`, res.data.reverse());
  305. }
  306. const arr = await that.$api(`/order/toMakeOrder`, 'POST', {
  307. key: that.key
  308. })
  309. if (arr.errcode == '0') {
  310. that.$set(that, `address`, arr.data.address);
  311. that.$set(that, `orderList`, arr.data.goodsData);
  312. that.$set(that, `total_detail`, arr.data.orderTotal);
  313. that.$set(that, `type`, arr.data.type);
  314. this.computedTotal();
  315. that.$set(that, `couponList`, arr.data.couponList);
  316. }
  317. },
  318. async computedTotal() {
  319. const total_detail = this.total_detail;
  320. let total = this.$plus(total_detail.freight_total, total_detail.goods_total)
  321. if (this.coupon.length > 0) {
  322. let discount = 0;
  323. for (const coupon of this.coupon) {
  324. const r = this.couponList.find(f => f._id === coupon)
  325. if (!r) continue;
  326. const {
  327. discount_config,
  328. discount_type
  329. } = r;
  330. if (discount_type === 'min') {
  331. const min = discount_config.min;
  332. discount = this.$plus(discount, min)
  333. } else if (discount_type === 'discount') {
  334. const min = discount_config.min || 0;
  335. let discount_money = 0;
  336. const max = discount_config.max || 0;
  337. // TODO 现在是平台发放的优惠券,所以可以直接用总价*折扣,如果是店铺的折扣券,则需要将金额分开,对应店铺计算折扣
  338. // const dm = min / 10 * total;
  339. const dm = this.$multiply(this.divide(min, 10), total)
  340. if (max !== 0) {
  341. if (max > dm) discount_money = dm;
  342. else discount_money = max
  343. }
  344. discount += discount_money
  345. }
  346. }
  347. total = this.$minus(total, discount)
  348. }
  349. if (total >= 0) {
  350. this.$set(this, `goods_total`, total);
  351. } else {
  352. uni.showToast({
  353. title: `实付金额不能为负数 不能使用该优惠劵`,
  354. icon: 'none'
  355. })
  356. }
  357. }
  358. }
  359. }
  360. </script>
  361. <style lang="scss">
  362. .main {
  363. display: flex;
  364. flex-direction: column;
  365. width: 100vw;
  366. height: 100vh;
  367. .one {
  368. position: relative;
  369. flex-grow: 1;
  370. background-color: var(--f1Color);
  371. .one_1 {
  372. display: flex;
  373. justify-content: space-between;
  374. align-items: center;
  375. width: 100vw;
  376. padding: 2vw;
  377. border-bottom: 1vw dashed var(--fcColor);
  378. .address {
  379. flex-grow: 1;
  380. margin: 0 0 0 2vw;
  381. font-size: var(--font14Size);
  382. color: var(--f85Color);
  383. }
  384. .iconfont {
  385. font-size: var(--font20Szie);
  386. }
  387. .other {
  388. width: 82vw;
  389. padding: 0 2vw;
  390. .name {
  391. font-size: var(--font16Size);
  392. }
  393. .other_1 {
  394. font-size: var(--font14Size);
  395. color: var(--f85Color);
  396. text {
  397. margin: 0 1vw 0 0;
  398. }
  399. }
  400. }
  401. }
  402. .one_2 {
  403. margin: 2vw 0 0 0;
  404. .list {
  405. width: 100vw;
  406. margin: 2vw 0;
  407. .list_1 {
  408. padding: 2vw;
  409. background-color: var(--mainColor);
  410. border-bottom: 0.1vw solid var(--fcColor);
  411. text {
  412. padding: 0 0 0 1vw;
  413. }
  414. }
  415. .list_2 {
  416. padding: 2vw;
  417. display: flex;
  418. background-color: var(--mainColor);
  419. .l {
  420. width: 20vw;
  421. .image {
  422. width: 100%;
  423. height: 20vw;
  424. border-radius: 5px;
  425. }
  426. }
  427. .c {
  428. width: 60vw;
  429. padding: 0 2vw;
  430. .Spec {
  431. font-size: var(--font12Size);
  432. color: var(--f85Color);
  433. }
  434. }
  435. .r {
  436. width: 15vw;
  437. text-align: right;
  438. }
  439. }
  440. .list_3 {
  441. width: 96vw;
  442. padding: 2vw;
  443. background-color: var(--mainColor);
  444. .other {
  445. display: flex;
  446. justify-content: space-between;
  447. border-bottom: 0.1vw solid var(--fcColor);
  448. margin: 0 0 2vw 0;
  449. padding: 2vw 0;
  450. .other_1 {
  451. font-size: var(--font16Size);
  452. color: var(--f85Color);
  453. }
  454. .other_3 {
  455. flex-grow: 1;
  456. margin: 0 0 0 2vw;
  457. font-size: var(--font12Size);
  458. }
  459. text {
  460. padding: 0 1vw 0 0;
  461. font-size: var(--font20Szie);
  462. }
  463. }
  464. }
  465. }
  466. .other {
  467. display: flex;
  468. justify-content: space-between;
  469. background-color: var(--mainColor);
  470. border-bottom: 0.1vw solid var(--fcColor);
  471. margin: 0 0 2vw 0;
  472. width: 96vw;
  473. padding: 2vw;
  474. .other_1 {
  475. font-size: var(--font16Size);
  476. color: var(--f85Color);
  477. }
  478. .other_3 {
  479. flex-grow: 1;
  480. margin: 0 0 0 2vw;
  481. font-size: var(--font12Size);
  482. }
  483. text {
  484. padding: 0 1vw 0 0;
  485. font-size: var(--font20Szie);
  486. }
  487. }
  488. }
  489. }
  490. .two {
  491. display: flex;
  492. justify-content: space-between;
  493. border-top: 0.1vw solid var(--fcColor);
  494. .two_1 {
  495. display: flex;
  496. align-items: center;
  497. padding: 0 2vw;
  498. font-size: var(--font16Size);
  499. view:last-child {
  500. margin: 0 0 0 2vw;
  501. color: var(--ff0Color);
  502. font-weight: bold;
  503. }
  504. }
  505. .two_2 {
  506. padding: 3vw 4vw;
  507. color: var(--mainColor);
  508. font-size: var(--font16Size);
  509. background-color: var(--ff0Color);
  510. }
  511. }
  512. }
  513. .scroll-view {
  514. position: absolute;
  515. top: 0;
  516. left: 0;
  517. right: 0;
  518. bottom: 0;
  519. .list-scroll-view {
  520. display: flex;
  521. flex-direction: row;
  522. flex-wrap: wrap;
  523. }
  524. }
  525. .popup {
  526. display: flex;
  527. flex-direction: column;
  528. height: 100vw;
  529. overflow-x: hidden;
  530. background-color: var(--f5Color);
  531. padding: 2vw;
  532. .list {
  533. .one {
  534. display: flex;
  535. justify-content: space-between;
  536. margin: 2vw 0 0 0;
  537. padding: 2vw;
  538. background-color: var(--mainColor);
  539. .name {
  540. font-size: var(--font16Size);
  541. }
  542. .address {
  543. font-size: var(--font14Size);
  544. }
  545. .iconfont {
  546. line-height: 15vw;
  547. font-size: var(--font20Szie);
  548. }
  549. }
  550. .two {
  551. display: flex;
  552. justify-content: space-between;
  553. border-top: 0.1vw solid var(--fcColor);
  554. padding: 2vw;
  555. background-color: var(--mainColor);
  556. font-size: var(--font14Size);
  557. .text {
  558. margin: 0 2vw 0 0;
  559. border: 0.1vw solid var(--fFB1Color);
  560. padding: 1vw;
  561. font-size: var(--font12Size);
  562. border-radius: 2vw;
  563. color: var(--fFB1Color);
  564. }
  565. }
  566. }
  567. }
  568. </style>