order.vue 16 KB

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