order.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. // 分享人id
  137. inviter: '',
  138. };
  139. },
  140. onLoad: function(e) {
  141. const that = this;
  142. uni.$on("id", async (id) => {
  143. let arr = await that.$api(`/address/${id}`, `GET`)
  144. if (arr.errcode == '0') {
  145. that.$set(that, `address`, arr.data)
  146. }
  147. })
  148. that.$set(that, `key`, e.key || '');
  149. that.$set(that, `group_id`, e.group_id || '');
  150. that.watchLogin()
  151. },
  152. methods: {
  153. // 使用优惠劵
  154. toDiscount(e) {
  155. const that = this;
  156. that.$set(that, 'coupon_name', e.name)
  157. that.coupon.push(e._id)
  158. that.$refs.popup.close();
  159. this.computedTotal();
  160. },
  161. // 选择收货地址
  162. toChoose() {
  163. const that = this;
  164. if (that.address?._id) {
  165. uni.navigateTo({
  166. url: `/pagesMy/address/index?type=${"shopping"}&id=${that.address._id}`
  167. })
  168. } else {
  169. uni.navigateTo({
  170. url: `/pagesMy/address/index?type=${"shopping"}`
  171. })
  172. }
  173. },
  174. // 选择优惠劵
  175. toCoupon() {
  176. const that = this;
  177. that.$set(that.popup, 'type', '1')
  178. that.$set(that, 'coupon', [])
  179. that.$refs.popup.open();
  180. },
  181. // 提交订单
  182. async toSubmit() {
  183. const that = this;
  184. if (that.address) {
  185. let data = {
  186. address: that.address,
  187. goods: that.orderList,
  188. total_detail: that.total_detail,
  189. coupon: that.coupon,
  190. type: that.type,
  191. inviter: that.inviter
  192. }
  193. if (that.group_id) {
  194. data.group = that.group_id
  195. }
  196. const arr = await that.$api(`/order`, 'POST', data)
  197. if (arr.errcode == '0') {
  198. uni.getStorage({
  199. key: 'system',
  200. success: async function(res) {
  201. // 微信小程序支付
  202. if (res.data.uniPlatform == "mp-weixin") {
  203. uni.showLoading({
  204. title: '加载中'
  205. })
  206. const res = await that.$api('/pay/toPayOrder', 'POST', {
  207. order_id: arr.data,
  208. type: '0'
  209. })
  210. uni.requestPayment({
  211. "provider": "wxpay",
  212. ...res.data,
  213. async success(res) {
  214. const group = await that.$api('/group/getGroup',
  215. 'GET', {
  216. order_id: arr.data,
  217. })
  218. if (group.errcode == '0') {
  219. if (group.data) {
  220. uni.hideLoading();
  221. uni.reLaunch({
  222. url: `/pagesHome/group/share?id=${group.data}`
  223. })
  224. } else {
  225. uni.hideLoading();
  226. uni.reLaunch({
  227. url: `/pagesMy/order/index?status=${'1'}`
  228. })
  229. }
  230. } else {
  231. uni.showToast({
  232. title: group.errmsg,
  233. icon: 'none'
  234. })
  235. }
  236. },
  237. fail(e) {
  238. uni.showToast({
  239. title: `支付失败`,
  240. icon: 'none'
  241. })
  242. uni.hideLoading();
  243. uni.reLaunch({
  244. url: `/pagesMy/order/index?status=${'0'}`
  245. })
  246. }
  247. })
  248. } else if (res.data.uniPlatform == "app") {
  249. // app支付
  250. uni.requestPayment({
  251. provider: 'alipay',
  252. orderInfo: 'orderInfo', //微信、支付宝订单数据 【注意微信的订单信息,键值应该全部是小写,不能采用驼峰命名】
  253. success: function(res) {
  254. console.log('success:' + JSON.stringify(res));
  255. },
  256. fail: function(err) {
  257. console.log('fail:' + JSON.stringify(err));
  258. }
  259. });
  260. } else {
  261. uni.showToast({
  262. title: `平台不支持支付`,
  263. icon: 'none'
  264. })
  265. }
  266. },
  267. fail: function(err) {}
  268. })
  269. } else {
  270. uni.showToast({
  271. title: arr.errmsg,
  272. icon: 'none'
  273. })
  274. }
  275. } else {
  276. uni.showToast({
  277. title: `没有收货地址`,
  278. icon: 'none'
  279. })
  280. }
  281. },
  282. // 监听用户是否登录
  283. watchLogin() {
  284. const that = this;
  285. uni.getStorage({
  286. key: 'token',
  287. success: function(res) {
  288. let user = that.$jwt(res.data);
  289. that.$set(that, `user`, user);
  290. that.search()
  291. },
  292. fail: function(err) {
  293. uni.reLaunch({
  294. url: `/pages/login/index`
  295. })
  296. }
  297. })
  298. },
  299. // 查询列表
  300. async search() {
  301. const that = this;
  302. let user = that.user;
  303. const res = await that.$api(`/address`, 'GET', {
  304. customer: user._id
  305. })
  306. if (res.errcode == '0') {
  307. that.$set(that, `addressList`, res.data.reverse());
  308. }
  309. const arr = await that.$api(`/order/toMakeOrder`, 'POST', {
  310. key: that.key
  311. })
  312. if (arr.errcode == '0') {
  313. // 分享人id
  314. that.$set(that, `inviter`, arr.data.inviter);
  315. // 地址
  316. that.$set(that, `address`, arr.data.address);
  317. // 订单
  318. that.$set(that, `orderList`, arr.data.goodsData);
  319. // 总价
  320. that.$set(that, `total_detail`, arr.data.orderTotal);
  321. that.$set(that, `type`, arr.data.type);
  322. this.computedTotal();
  323. // 优惠劵
  324. that.$set(that, `couponList`, arr.data.couponList);
  325. }
  326. },
  327. async computedTotal() {
  328. const total_detail = this.total_detail;
  329. let total = this.$plus(total_detail.freight_total, total_detail.goods_total)
  330. if (this.coupon.length > 0) {
  331. let discount = 0;
  332. for (const coupon of this.coupon) {
  333. const r = this.couponList.find(f => f._id === coupon)
  334. if (!r) continue;
  335. const {
  336. discount_config,
  337. discount_type
  338. } = r;
  339. if (discount_type === 'min') {
  340. const min = discount_config.min;
  341. discount = this.$plus(discount, min)
  342. } else if (discount_type === 'discount') {
  343. const min = discount_config.min || 0;
  344. let discount_money = 0;
  345. const max = discount_config.max || 0;
  346. // TODO 现在是平台发放的优惠券,所以可以直接用总价*折扣,如果是店铺的折扣券,则需要将金额分开,对应店铺计算折扣
  347. // const dm = min / 10 * total;
  348. const dm = this.$multiply(this.divide(min, 10), total)
  349. if (max !== 0) {
  350. if (max > dm) discount_money = dm;
  351. else discount_money = max
  352. }
  353. discount += discount_money
  354. }
  355. }
  356. total = this.$minus(total, discount)
  357. }
  358. if (total >= 0) {
  359. this.$set(this, `goods_total`, total);
  360. } else {
  361. uni.showToast({
  362. title: `实付金额不能为负数 不能使用该优惠劵`,
  363. icon: 'none'
  364. })
  365. }
  366. }
  367. }
  368. }
  369. </script>
  370. <style lang="scss">
  371. .main {
  372. display: flex;
  373. flex-direction: column;
  374. width: 100vw;
  375. height: 100vh;
  376. .one {
  377. position: relative;
  378. flex-grow: 1;
  379. background-color: var(--f1Color);
  380. .one_1 {
  381. display: flex;
  382. justify-content: space-between;
  383. align-items: center;
  384. width: 100vw;
  385. padding: 2vw;
  386. border-bottom: 1vw dashed var(--fcColor);
  387. .address {
  388. flex-grow: 1;
  389. margin: 0 0 0 2vw;
  390. font-size: var(--font14Size);
  391. color: var(--f85Color);
  392. }
  393. .iconfont {
  394. font-size: var(--font20Szie);
  395. }
  396. .other {
  397. width: 82vw;
  398. padding: 0 2vw;
  399. .name {
  400. font-size: var(--font16Size);
  401. }
  402. .other_1 {
  403. font-size: var(--font14Size);
  404. color: var(--f85Color);
  405. text {
  406. margin: 0 1vw 0 0;
  407. }
  408. }
  409. }
  410. }
  411. .one_2 {
  412. margin: 2vw 0 0 0;
  413. .list {
  414. width: 100vw;
  415. margin: 2vw 0;
  416. .list_1 {
  417. padding: 2vw;
  418. background-color: var(--mainColor);
  419. border-bottom: 0.1vw solid var(--fcColor);
  420. text {
  421. padding: 0 0 0 1vw;
  422. }
  423. }
  424. .list_2 {
  425. padding: 2vw;
  426. display: flex;
  427. background-color: var(--mainColor);
  428. .l {
  429. width: 20vw;
  430. .image {
  431. width: 100%;
  432. height: 20vw;
  433. border-radius: 5px;
  434. }
  435. }
  436. .c {
  437. width: 60vw;
  438. padding: 0 2vw;
  439. .Spec {
  440. font-size: var(--font12Size);
  441. color: var(--f85Color);
  442. }
  443. }
  444. .r {
  445. width: 15vw;
  446. text-align: right;
  447. }
  448. }
  449. .list_3 {
  450. width: 96vw;
  451. padding: 2vw;
  452. background-color: var(--mainColor);
  453. .other {
  454. display: flex;
  455. justify-content: space-between;
  456. border-bottom: 0.1vw solid var(--fcColor);
  457. margin: 0 0 2vw 0;
  458. padding: 2vw 0;
  459. .other_1 {
  460. font-size: var(--font16Size);
  461. color: var(--f85Color);
  462. }
  463. .other_3 {
  464. flex-grow: 1;
  465. margin: 0 0 0 2vw;
  466. font-size: var(--font12Size);
  467. }
  468. text {
  469. padding: 0 1vw 0 0;
  470. font-size: var(--font20Szie);
  471. }
  472. }
  473. }
  474. }
  475. .other {
  476. display: flex;
  477. justify-content: space-between;
  478. background-color: var(--mainColor);
  479. border-bottom: 0.1vw solid var(--fcColor);
  480. margin: 0 0 2vw 0;
  481. width: 96vw;
  482. padding: 2vw;
  483. .other_1 {
  484. font-size: var(--font16Size);
  485. color: var(--f85Color);
  486. }
  487. .other_3 {
  488. flex-grow: 1;
  489. margin: 0 0 0 2vw;
  490. font-size: var(--font12Size);
  491. }
  492. text {
  493. padding: 0 1vw 0 0;
  494. font-size: var(--font20Szie);
  495. }
  496. }
  497. }
  498. }
  499. .two {
  500. display: flex;
  501. justify-content: space-between;
  502. border-top: 0.1vw solid var(--fcColor);
  503. .two_1 {
  504. display: flex;
  505. align-items: center;
  506. padding: 0 2vw;
  507. font-size: var(--font16Size);
  508. view:last-child {
  509. margin: 0 0 0 2vw;
  510. color: var(--ff0Color);
  511. font-weight: bold;
  512. }
  513. }
  514. .two_2 {
  515. padding: 3vw 4vw;
  516. color: var(--mainColor);
  517. font-size: var(--font16Size);
  518. background-color: var(--ff0Color);
  519. }
  520. }
  521. }
  522. .scroll-view {
  523. position: absolute;
  524. top: 0;
  525. left: 0;
  526. right: 0;
  527. bottom: 0;
  528. .list-scroll-view {
  529. display: flex;
  530. flex-direction: row;
  531. flex-wrap: wrap;
  532. }
  533. }
  534. .popup {
  535. display: flex;
  536. flex-direction: column;
  537. height: 100vw;
  538. overflow-x: hidden;
  539. background-color: var(--f5Color);
  540. padding: 2vw;
  541. .list {
  542. .one {
  543. display: flex;
  544. justify-content: space-between;
  545. margin: 2vw 0 0 0;
  546. padding: 2vw;
  547. background-color: var(--mainColor);
  548. .name {
  549. font-size: var(--font16Size);
  550. }
  551. .address {
  552. font-size: var(--font14Size);
  553. }
  554. .iconfont {
  555. line-height: 15vw;
  556. font-size: var(--font20Szie);
  557. }
  558. }
  559. .two {
  560. display: flex;
  561. justify-content: space-between;
  562. border-top: 0.1vw solid var(--fcColor);
  563. padding: 2vw;
  564. background-color: var(--mainColor);
  565. font-size: var(--font14Size);
  566. .text {
  567. margin: 0 2vw 0 0;
  568. border: 0.1vw solid var(--fFB1Color);
  569. padding: 1vw;
  570. font-size: var(--font12Size);
  571. border-radius: 2vw;
  572. color: var(--fFB1Color);
  573. }
  574. }
  575. }
  576. }
  577. </style>