order.vue 15 KB

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