order.vue 16 KB

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