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