order.vue 15 KB

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