order.vue 16 KB

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