金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

735 lines
21 KiB

  1. <template>
  2. <view id="store-order">
  3. <view class="border-box">
  4. <view class="order-item">
  5. <view class="title mx-1px-bottom">
  6. 收货信息
  7. </view>
  8. <view class="address info" @tap="selectAddress">
  9. <view v-if="form.address && form.address.mobile">
  10. <view class="user">
  11. <i class="iconfont icon-yonghu1"></i>
  12. <view class="name-box">
  13. <text class="name">{{form.address.accept_name}}</text>
  14. <text class="telephone">{{form.address.mobile}}</text>
  15. </view>
  16. </view>
  17. <view class="address">
  18. <i class="iconfont icon-weizhi"></i>
  19. <text>{{form.address.address_name}} {{form.address.address}}</text>
  20. </view>
  21. </view>
  22. <view class="user" v-else>
  23. <i class="iconfont icon-juxing"></i>
  24. <view class="name-box">
  25. <text class="add">添加收货地址</text>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. <view class="order-item">
  31. <view class="title mx-1px-bottom">
  32. 商品清单
  33. </view>
  34. <view class="goods info mx-1px-bottom" v-for="(item, index) in block.order.items" :key="index" v-if="block.order.items.length == 1">
  35. <view class="goods-item">
  36. <view class="item-left">
  37. <image :src="item.item_meta.image"></image>
  38. </view>
  39. <view class="item-right">
  40. <view class="goods-name item">
  41. <view class="name">
  42. {{item.item_name}}
  43. </view>
  44. <view>
  45. {{item.quantity}}
  46. </view>
  47. </view>
  48. <view class="goods-money item">
  49. <view>
  50. {{item.item_meta.specs_text}}
  51. </view>
  52. <view>
  53. {{item.redeem_point}} 积分
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. <view class="info mx-1px-bottom all-goods" v-if="block.order.items.length > 1" @tap="jumpGoods">
  60. <view class="good-box">
  61. <view class="good-img" v-for="(item, index) in block.order.items" :key="index" v-if="index < 4">
  62. <image :src="item.item_meta.image" alt></image>
  63. </view>
  64. </view>
  65. <div class="num">
  66. {{block.order.count}}
  67. </div>
  68. </view>
  69. <view class="total">
  70. {{block.order.count}}件商品合计 {{block.order.redeem_point}} 积分
  71. </view>
  72. </view>
  73. <view class="order-item">
  74. <view class="remarks pointFlex">
  75. <view>
  76. 可用积分
  77. </view>
  78. <view>
  79. {{extra.point}}
  80. </view>
  81. </view>
  82. </view>
  83. <view class="order-item">
  84. <view class="remarks ">
  85. <view>
  86. <input type="text" @input="inputNote" placeholder="买家留言"></input>
  87. </view>
  88. </view>
  89. </view>
  90. </view>
  91. <view class="sbumit-order mx-1px-top">
  92. <view class="back">
  93. 支付积分<text>{{block.order.redeem_point}}</text>
  94. </view>
  95. <button class="sbumit" @tap="submitOrder" :style="'background: ' + config.mainColor" :loading="loading">
  96. 确定兑换
  97. </button>
  98. </view>
  99. </view>
  100. </template>
  101. <script>
  102. var app = getApp();
  103. //import { connect, bindActionCreators, store, actions, sandBox, cookieStorage, config } from '../../../lib/myapp.js';
  104. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  105. // const page = connect.Page(store(), state => {
  106. // return {
  107. // detailData: state.goods_detail
  108. // };
  109. // }, dispatch => {
  110. // return {
  111. // getGoodsDetail: bindActionCreators(actions.getGoodsDetail, dispatch, 'GOODS_DETAIL') // queryCommodityStore: bindActionCreators(actions.queryCommodityStore, dispatch ,'COMMODITYSTORE','COMMODITYSPECS')
  112. //
  113. // };
  114. // });
  115. export default {
  116. data() {
  117. return {
  118. loading: false,
  119. show: false,
  120. block: {
  121. order: {
  122. items: []
  123. },
  124. orderPoint: {
  125. pointAmount: 0,
  126. pointCanUse: 0
  127. },
  128. otherCoupon: {}
  129. },
  130. extra: {
  131. point: {}
  132. },
  133. form: {
  134. order_no: '',
  135. address: {},
  136. coupon: {},
  137. invoice: {},
  138. discount: {},
  139. point: 0,
  140. note: '',
  141. formStates: {},
  142. isDisabled: false //是否禁用优惠折扣按钮
  143. },
  144. useList: [[{
  145. name: '不使用',
  146. value: 0
  147. }, {
  148. name: '使用',
  149. value: 1
  150. }]],
  151. formStates: {
  152. discountsCheckIndex: -1,
  153. noDiscountsCheckList: -1,
  154. pointStatus: false
  155. },
  156. temporary: {
  157. coupons: [],
  158. //可选择的优惠券
  159. coupon: {} //当前选择的优惠券
  160. },
  161. paymentMoneys: {
  162. discounts: {},
  163. total: 0
  164. },
  165. check: null,
  166. is_login: '',
  167. type: '',
  168. config: ''
  169. };
  170. },
  171. onShow() {
  172. this.initData(); // let app =getApp();
  173. // app.isBirthday().then(()=>{
  174. // if(this.$cookieStorage.get("birthday_gift")){
  175. // var giftData=this.$cookieStorage.get("birthday_gift").data;
  176. // new app.ToastPannel().__page.showText(giftData);
  177. // }
  178. // });
  179. },
  180. onLoad(e) {
  181. // 第三方平台配置颜色
  182. var gbConfig = this.$cookieStorage.get('globalConfig') || '';
  183. this.setData({
  184. config: gbConfig
  185. });
  186. if (e.type) {
  187. this.setData({
  188. type: e.type
  189. });
  190. }
  191. this.queryUserPoint('default');
  192. },
  193. components: {},
  194. methods: {
  195. addInvoice() {
  196. wx.navigateTo({
  197. url: `/pages/store/invoice/invoice?order_no=${this.block.order.order_no}&url=${getCurrentPages()[getCurrentPages().length - 1].route}`
  198. });
  199. },
  200. selectAddress() {
  201. wx.navigateTo({
  202. url: `/pages/address/list/list?order_no=${this.block.order.order_no}&url=${getCurrentPages()[getCurrentPages().length - 1].route}`
  203. });
  204. },
  205. jumpGoods() {
  206. wx.navigateTo({
  207. url: '/pages/store/orderGoods/orderGoods?no=' + this.block.order.order_no
  208. });
  209. },
  210. inputNote(e) {
  211. this.setData({
  212. 'form.note': e.detail.value
  213. });
  214. },
  215. change(e) {
  216. this.setData({
  217. check: e.detail.value
  218. });
  219. var coupons = this.block.coupons;
  220. var item = coupons[e.detail.value];
  221. var data = this.$cookieStorage.get('order_form');
  222. let exclusive = item.discount.exclusive;
  223. if (!data) return;
  224. coupons.forEach((v, key) => v.checked = key == e.detail.value);
  225. if (Array.isArray(item.adjustments)) {
  226. item.adjustments.sort((a, b) => {
  227. return Math.abs(a.amount) < Math.abs(b.amount);
  228. });
  229. } // 排他
  230. if (exclusive) {
  231. data.formStates.discountsCheckIndex = -1;
  232. data.isDisabled = true;
  233. } else {
  234. data.isDisabled = false;
  235. }
  236. data.otherCoupon = item;
  237. data.coupons = coupons; // Cache.set(cache_keys.order_form, data);
  238. this.setData({
  239. block: Object.assign({}, this.block, data)
  240. });
  241. this.$cookieStorage.set('order_form', data);
  242. this.paymentMoney();
  243. },
  244. sure() {
  245. var block = this.block.otherCoupon;
  246. this.setData({
  247. [`block.coupon`]: block,
  248. [`form.coupon`]: block,
  249. show: false
  250. });
  251. console.log(this.form.coupon);
  252. this.paymentMoney();
  253. },
  254. select() {
  255. if (this.block.coupons.length) {
  256. this.setData({
  257. show: true
  258. });
  259. } else {
  260. wx.showModal({
  261. title: '',
  262. content: '暂无可使用的优惠券',
  263. showCancel: false
  264. });
  265. }
  266. },
  267. cancel() {
  268. var coupons = this.block.coupons;
  269. coupons.forEach(v => v.checked = false);
  270. this.setData({
  271. [`block.coupons`]: coupons,
  272. [`form.coupon`]: this.block.otherCoupon,
  273. [`block.otherCoupon`]: {},
  274. check: null,
  275. show: false
  276. });
  277. },
  278. initData() {
  279. var block = this.$cookieStorage.get('point_order');
  280. var form = this.$cookieStorage.get('order_form');
  281. var is_login = this.$cookieStorage.get('user_token');
  282. this.setData({
  283. is_login: is_login,
  284. loading: false
  285. });
  286. if (block) {
  287. if (!form || form.order_no !== block.order.order_no) {
  288. form = Object.assign({}, this.form);
  289. form.order_no = block.order.order_no;
  290. if (block.address) {
  291. form.address = block.address;
  292. } // Cache.set(cache_keys.order_form, form);
  293. this.$cookieStorage.set('order_form', form);
  294. } else {
  295. if (form.formStates) {
  296. this.setData({
  297. [`formStates.discountsCheckIndex`]: form.formStates.discountsCheckIndex,
  298. [`formStates.pointStatus`]: form.formStates.pointStatus
  299. });
  300. }
  301. this.setData({
  302. [`form.isDisabled`]: form.isDisabled,
  303. [`temporary.coupon`]: form.coupon
  304. });
  305. }
  306. this.setData({
  307. [`temporary.coupons`]: block.coupons
  308. });
  309. this.queryOrderExtra();
  310. this.setData({
  311. block: Object.assign({}, this.block, block),
  312. form: Object.assign({}, this.form, form)
  313. });
  314. console.log(this.form);
  315. this.paymentMoney(); // t.next({block, form});
  316. } else {// this.addHistory();
  317. // t.to.router.forward({name: 'user-order-online', params: {status: 1}});
  318. }
  319. },
  320. queryOrderExtra() {
  321. var oauth = this.is_login;
  322. this.$http.get({
  323. api: `api/shopping/order/extraInfo`,
  324. header: {
  325. Authorization: oauth
  326. }
  327. }).then(res => {
  328. res = res.data;
  329. var data = res.data;
  330. if (res.status) {
  331. var extra = {
  332. point: data.userPoint,
  333. limit: data.pointLimit,
  334. factor: data.pointToMoney
  335. };
  336. this.setData({
  337. extra: extra
  338. }); // dispatch(UserOrderExtra, extra);
  339. }
  340. });
  341. },
  342. changeDiscounts(e) {
  343. this.setData({
  344. [`formStates.discountsCheckIndex`]: e.detail.value
  345. });
  346. this.paymentMoney();
  347. },
  348. submitOrder() {
  349. if (this.loading) return;
  350. this.setData({
  351. loading: true
  352. });
  353. var data = {
  354. order_no: this.form.order_no,
  355. // 订单编号
  356. note: this.form.note // 用户留言
  357. };
  358. console.log('this.form',this.form)
  359. if (this.form.address && this.form.address.id) {
  360. data.address_id = this.form.address.id;
  361. } else {
  362. this.setData({
  363. loading: false
  364. });
  365. wx.showModal({
  366. content:"请填写地址",
  367. showCancel:false
  368. })
  369. return;
  370. }
  371. if (this.form.coupon && this.form.coupon.id) {
  372. data.coupon_id = this.form.coupon.id;
  373. }
  374. if (this.form.invoice && this.form.invoice.id) {
  375. data.invoice_id = this.form.invoice.id;
  376. }
  377. if (this.form.discount && this.form.discount.id) {
  378. data.discount_id = this.form.discount.id;
  379. }
  380. if (this.form.point) {
  381. data.point = this.form.point;
  382. }
  383. this.confirmOrder(data);
  384. },
  385. confirmOrder(data) {
  386. var that = this;
  387. var oauth = this.is_login;
  388. this.$http.post({
  389. api: `api/shopping/order/confirm/point`,
  390. data: data,
  391. header: {
  392. Authorization: oauth
  393. }
  394. }).then(res => {
  395. res = res.data;
  396. console.log(res);
  397. if (res.status) {
  398. this.$cookieStorage.clear('point_order'); // this.$emit('confirm', true, res.data);
  399. that.confirm(true, res.data);
  400. } else {
  401. that.confirm(false, res.message);
  402. }
  403. }).catch(rej => {
  404. wx.showToast({
  405. title: '提交订单失败,请重试!'
  406. });
  407. });
  408. },
  409. queryUserPoint(type) {
  410. var token = this.$cookieStorage.get('user_token');
  411. this.$http.get({
  412. api: 'api/users/point',
  413. header: {
  414. Authorization: token
  415. },
  416. data: {
  417. type: type
  418. }
  419. }).then(res => {
  420. if (res.statusCode == 200) {
  421. res = res.data;
  422. if (res.status) {
  423. this.point = res.data
  424. }
  425. }
  426. });
  427. },
  428. confirm(success, data) {
  429. if (success) {
  430. this.setData({
  431. loading: false
  432. });
  433. wx.redirectTo({
  434. url: `/pages/pointStore/success/success?order_no=${data.order.order_no}`
  435. });
  436. } else {
  437. this.setData({
  438. loading: false
  439. });
  440. wx.showModal({
  441. content: data || '提交订单失败',
  442. showCancel: false
  443. });
  444. }
  445. },
  446. paymentMoney() {
  447. var dis = {
  448. order: 0,
  449. point: 0,
  450. coupon: 0
  451. };
  452. var total = this.block.order.total;
  453. var fixedTotal = this.block.order.total;
  454. var block = this.$cookieStorage.get('point_order'); // var pointToMoney = block.orderPoint.pointToMoney;
  455. // 订单折扣
  456. if (this.block.discounts && Array.isArray(this.block.discounts)) {
  457. let discounts = this.block.discounts;
  458. let check = this.formStates.discountsCheckIndex;
  459. console.warn(check);
  460. if (check == -1) {
  461. // 当选择不使用优惠的情况
  462. dis.order = 0;
  463. this.setData({
  464. [`form.discount`]: {},
  465. [`form.formStates.discountsCheckIndex`]: check
  466. });
  467. if (this.temporary.coupons.length) {
  468. this.setData({
  469. [`block.coupons`]: this.temporary.coupons
  470. });
  471. } // 操作积分
  472. // this.block.orderPoint.pointCanUse = Math.min(total * this.block.orderPoint.pointLimit / this.block.orderPoint.pointToMoney, this.block.orderPoint.userPoint);
  473. // this.block.orderPoint.pointAmount = Math.max(-(total * this.block.orderPoint.pointLimit), -(this.block.orderPoint.userPoint * this.block.orderPoint.pointToMoney));
  474. //
  475. // this.form.coupon = this.temporary.coupon; // 将选择的优惠券还原
  476. this.setData({
  477. [`block.orderPoint.pointCanUse`]: Math.min(total * this.block.orderPoint.pointLimit / this.block.orderPoint.pointToMoney, this.block.orderPoint.userPoint),
  478. [`block.orderPoint.pointAmount`]: Math.max(-(total * this.block.orderPoint.pointLimit), -(this.block.orderPoint.userPoint * this.block.orderPoint.pointToMoney)),
  479. [`form.coupon`]: this.temporary.coupon
  480. });
  481. this.$cookieStorage.set('order_form', this.form);
  482. } else {
  483. // 当使用了优惠的情况
  484. let discount = -discounts[check].adjustmentTotal;
  485. console.log(discount);
  486. let exclusive = discounts[check].exclusive; //是否排他(优惠券);
  487. if (discount <= total) {
  488. if (exclusive) {
  489. this.setData({
  490. [`block.coupons`]: [],
  491. [`form.coupon`]: {}
  492. });
  493. } else {
  494. this.setData({
  495. [`block.coupons`]: this.temporary.coupons,
  496. [`form.coupon`]: this.temporary.coupon
  497. });
  498. }
  499. dis.order = discounts[check].adjustmentTotal;
  500. this.setData({
  501. [`form.discount`]: discounts[check]
  502. });
  503. total -= discount; // 操作积分
  504. this.setData({
  505. [`block.orderPoint.pointCanUse`]: Math.min(total * this.block.orderPoint.pointLimit / this.block.orderPoint.pointToMoney, this.block.orderPoint.userPoint),
  506. [`block.orderPoint.pointAmount`]: Math.max(-(total * this.block.orderPoint.pointLimit), -(this.block.orderPoint.userPoint * this.block.orderPoint.pointToMoney)),
  507. [`form.formStates.discountsCheckIndex`]: check
  508. }); // Cache.set(cache_keys.order_form, this.form);
  509. this.$cookieStorage.set('order_form', this.form);
  510. } else {
  511. // this.$Alert('超过最大优惠折扣', () => {
  512. // check = -1;
  513. // });
  514. wx.showModal({
  515. content: '超过最大折扣',
  516. showCancel: false,
  517. success: function (res) {
  518. if (res.confirm) check = -1;
  519. }
  520. });
  521. this.setData({
  522. [`form.discount`]: {}
  523. });
  524. this.$cookieStorage.set('order_form', this.form);
  525. }
  526. }
  527. } // 优惠券折扣
  528. if (this.block.coupon && this.block.coupon.adjustments && Array.isArray(this.block.coupon.adjustments)) {
  529. let adjustments = this.block.coupon.adjustments;
  530. let discount = -adjustments[0].amount;
  531. if (discount <= total) {
  532. dis.coupon = adjustments[0].amount;
  533. total -= discount; // 操作积分
  534. this.setData({
  535. [`block.orderPoint.pointCanUse`]: total * this.block.orderPoint.pointLimit / this.block.orderPoint.pointToMoney,
  536. [`block.orderPoint.pointAmount`]: -(total * this.block.orderPoint.pointLimit)
  537. });
  538. } else {
  539. wx.showModal({
  540. content: '超过最大折扣',
  541. showCancel: false
  542. });
  543. this.setData({
  544. [`form.coupon`]: {},
  545. [`temporary.coupon`]: {},
  546. [`form.isDisabled`]: false
  547. });
  548. this.$cookieStorage.set('order_form', this.form);
  549. }
  550. }
  551. console.log(this.form, this.block); // 积分折扣
  552. if (this.form.point) {
  553. let factor = this.extra.factor;
  554. let discount = this.form.point * factor;
  555. if (discount <= total) {
  556. dis.point = -discount;
  557. total -= discount;
  558. if (total < 0) {
  559. total = 0;
  560. if (dis.point != 0) {
  561. dis.total = -(fixedTotal + dis.point);
  562. }
  563. } else {
  564. // 除积分外的优惠
  565. dis.total = dis.order + dis.coupon;
  566. }
  567. this.$cookieStorage.set('order_form', this.form);
  568. } else {
  569. this.setData({
  570. [`form.point`]: 0
  571. });
  572. this.$cookieStorage.set('order_form', this.form);
  573. }
  574. } // 除积分外的优惠
  575. dis.total = dis.order + dis.coupon;
  576. if (this.form.point > this.block.orderPoint.pointCanUse) {
  577. this.setData({
  578. [`form.point`]: this.block.orderPoint.pointCanUse
  579. });
  580. dis.point = this.block.orderPoint.pointCanUse * 10;
  581. }
  582. this.setData({
  583. [`paymentMoneys.discounts`]: dis,
  584. [`paymentMoneys.total`]: total
  585. });
  586. },
  587. usePoint() {
  588. this.setData({
  589. [`formStates.pointStatus`]: true,
  590. [`form.point`]: this.block.orderPoint.pointCanUse
  591. });
  592. this.paymentMoney();
  593. },
  594. modifyPoint(e) {
  595. var min = 0;
  596. var max = this.extra.point;
  597. var val = e.detail.value;
  598. var use = Math.floor(this.block.order.total * this.extra.limit / this.extra.factor);
  599. var sun = Math.floor((this.block.order.total + this.paymentMoneys.discounts.total) / this.extra.factor);
  600. if (!isFinite(use)) use = max;
  601. if (!isFinite(sun)) sun = max;
  602. max = Math.min(max, use, sun);
  603. val = parseInt(val);
  604. if (isNaN(val)) {
  605. val = '';
  606. } else if (val < min) {
  607. val = min;
  608. } else if (val > max) {
  609. val = max;
  610. }
  611. e.detail.value = val;
  612. this.setData({
  613. [`form.point`]: val
  614. });
  615. this.paymentMoney();
  616. },
  617. saveForm(e) {
  618. this.$cookieStorage.set('order_form', this.form);
  619. },
  620. back() {
  621. wx.navigateBack();
  622. },
  623. setData: function (obj) {
  624. let that = this;
  625. let keys = [];
  626. let val, data;
  627. Object.keys(obj).forEach(function (key) {
  628. keys = key.split('.');
  629. val = obj[key];
  630. data = that.$data;
  631. keys.forEach(function (key2, index) {
  632. if (index + 1 == keys.length) {
  633. that.$set(data, key2, val);
  634. } else {
  635. if (!data[key2]) {
  636. that.$set(data, key2, {});
  637. }
  638. }
  639. data = data[key2];
  640. });
  641. });
  642. }
  643. },
  644. computed: {},
  645. watch: {}
  646. };
  647. </script>
  648. <style rel="stylesheet/less" lang="less">
  649. @import "order";
  650. </style>