order.vue 14 KB

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