order.vue 17 KB

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