order-submit.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. // order-submit.js
  2. var api = require('../../api.js');
  3. var app = getApp();
  4. var longitude = "";
  5. var latitude = "";
  6. var util = require('../../utils/utils.js');
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. total_price: 0,
  13. address: null,
  14. express_price: 0.00,
  15. content: '',
  16. offline: 0,
  17. express_price_1: 0.00,
  18. name: "",
  19. mobile: "",
  20. integral_radio: 1,
  21. new_total_price: 0,
  22. show_card: false,
  23. payment: -1,
  24. show_payment: false
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad: function (options) {
  30. app.pageOnLoad(this);
  31. var page = this;
  32. var time = util.formatData(new Date());
  33. console.log(options);
  34. page.setData({
  35. options: options,
  36. store: wx.getStorageSync("store"),
  37. time: time
  38. });
  39. },
  40. bindkeyinput: function (e) {
  41. this.setData({
  42. content: e.detail.value
  43. });
  44. },
  45. KeyName: function (e) {
  46. this.setData({
  47. name: e.detail.value
  48. });
  49. },
  50. KeyMobile: function (e) {
  51. this.setData({
  52. mobile: e.detail.value
  53. });
  54. },
  55. getOffline: function (e) {
  56. var page = this;
  57. var express = this.data.express_price;
  58. var express_1 = this.data.express_price_1;
  59. var offline = e.target.dataset.index;
  60. if (offline == 1) {
  61. this.setData({
  62. offline: 1,
  63. express_price: 0,
  64. express_price_1: express
  65. });
  66. } else {
  67. this.setData({
  68. offline: 0,
  69. express_price: express_1
  70. });
  71. }
  72. page.getPrice();
  73. },
  74. dingwei: function () {
  75. var page = this;
  76. wx.chooseLocation({
  77. success: function (e) {
  78. longitude = e.longitude;
  79. latitude = e.latitude;
  80. page.setData({
  81. location: e.address,
  82. });
  83. },
  84. fail: function (res) {
  85. app.getauth({
  86. content: "需要获取您的地理位置授权,请到小程序设置中打开授权",
  87. success: function (e) {
  88. if (e) {
  89. if (e.authSetting["scope.userLocation"]) {
  90. page.dingwei();
  91. } else {
  92. wx.showToast({
  93. title: '您取消了授权',
  94. image: '/images/icon-warning.png'
  95. })
  96. }
  97. }
  98. }
  99. });
  100. }
  101. })
  102. },
  103. orderSubmit: function (e) {
  104. var page = this;
  105. var offline = page.data.offline;
  106. var data = {};
  107. if (offline == 0) {
  108. if (!page.data.address || !page.data.address.id) {
  109. wx.showToast({
  110. title: "请选择收货地址",
  111. image: "/images/icon-warning.png",
  112. });
  113. return;
  114. }
  115. data.address_id = page.data.address.id;
  116. } else {
  117. data.address_name = page.data.name;
  118. data.address_mobile = page.data.mobile;
  119. if (page.data.shop.id) {
  120. data.shop_id = page.data.shop.id;
  121. } else {
  122. wx.showModal({
  123. title: '警告',
  124. content: '请选择门店',
  125. showCancel: false
  126. });
  127. return;
  128. }
  129. if (!data.address_name || data.address_name == undefined) {
  130. page.showToast({
  131. title: "请填写收货人",
  132. image: "/images/icon-warning.png",
  133. });
  134. return;
  135. }
  136. if (!data.address_mobile || data.address_mobile == undefined) {
  137. page.showToast({
  138. title: "请填写联系方式",
  139. image: "/images/icon-warning.png",
  140. });
  141. return;
  142. } else {
  143. var check_mobile = /^1\d{10}$/;
  144. if (!check_mobile.test(data.address_mobile)) {
  145. wx.showModal({
  146. title: '提示',
  147. content: '手机号格式不正确',
  148. showCancel: false
  149. });
  150. return;
  151. }
  152. }
  153. }
  154. data.offline = offline;
  155. var form = page.data.form;
  156. if (form.is_form == 1) {
  157. var form_list = form.list;
  158. for (var i in form_list) {
  159. if (form_list[i].type == 'date') {
  160. form_list[i].default = form_list[i].default ? form_list[i].default : page.data.time;
  161. }
  162. if (form_list[i].type == 'time') {
  163. form_list[i].default = form_list[i].default ? form_list[i].default : '00:00';
  164. }
  165. if (form_list[i].required == 1) {
  166. if (form_list[i].type == 'radio' || form_list[i].type == 'checkboxc') {
  167. var is_true = false;
  168. for (var j in form_list[i].default_list) {
  169. if (form_list[i].default_list[j].is_selected == 1) {
  170. is_true = true;
  171. }
  172. }
  173. if (!is_true) {
  174. wx.showModal({
  175. title: '提示',
  176. content: '请填写' + form.name + ',加‘*’为必填项',
  177. showCancel: false
  178. })
  179. return false;
  180. }
  181. } else {
  182. if (!form_list[i].default || form_list[i].default == undefined) {
  183. wx.showModal({
  184. title: '提示',
  185. content: '请填写' + form.name + ',加‘*’为必填项',
  186. showCancel: false
  187. })
  188. return false;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. data.form = JSON.stringify(form);
  195. if (page.data.cart_id_list) {
  196. data.cart_id_list = JSON.stringify(page.data.cart_id_list);
  197. }
  198. if (page.data.goods_info) {
  199. data.goods_info = JSON.stringify(page.data.goods_info);
  200. }
  201. if (page.data.picker_coupon) {
  202. data.user_coupon_id = page.data.picker_coupon.user_coupon_id;
  203. }
  204. if (page.data.content) {
  205. data.content = page.data.content
  206. }
  207. if (page.data.cart_list) {
  208. data.cart_list = JSON.stringify(page.data.cart_list);
  209. }
  210. wx.showLoading({
  211. title: "正在提交",
  212. mask: true,
  213. });
  214. page.data.integral_radio == 1 ? data.use_integral = 1 : data.use_integral = 2;
  215. data.payment = page.data.payment;
  216. //提交订单
  217. app.request({
  218. url: api.order.submit,
  219. method: "post",
  220. data: data,
  221. success: function (res) {
  222. if (res.code == 0) {
  223. setTimeout(function () {
  224. wx.hideLoading();
  225. }, 1000);
  226. setTimeout(function () {
  227. page.setData({
  228. options: {},
  229. });
  230. }, 1);
  231. var order_id = res.data.order_id;
  232. //获取支付数据
  233. if (data.payment == 0) {
  234. app.request({
  235. url: api.order.pay_data,
  236. data: {
  237. order_id: order_id,
  238. pay_type: 'WECHAT_PAY',
  239. },
  240. success: function (res) {
  241. if (res.code == 0) {
  242. //发起支付
  243. wx.requestPayment({
  244. timeStamp: res.data.timeStamp,
  245. nonceStr: res.data.nonceStr,
  246. package: res.data.package,
  247. signType: res.data.signType,
  248. paySign: res.data.paySign,
  249. success: function (e) {
  250. },
  251. fail: function (e) {
  252. },
  253. complete: function (e) {
  254. if (e.errMsg == "requestPayment:fail" || e.errMsg == "requestPayment:fail cancel") {//支付失败转到待支付订单列表
  255. wx.showModal({
  256. title: "提示",
  257. content: "订单尚未支付",
  258. showCancel: false,
  259. confirmText: "确认",
  260. success: function (res) {
  261. if (res.confirm) {
  262. wx.redirectTo({
  263. url: "/pages/order/order?status=0",
  264. });
  265. }
  266. }
  267. });
  268. return;
  269. }
  270. if (e.errMsg == "requestPayment:ok") {
  271. if (page.data.goods_card_list.length > 0) {
  272. page.setData({
  273. show_card: true
  274. });
  275. } else {
  276. wx.redirectTo({
  277. url: '/pages/pay-success/index'
  278. })
  279. }
  280. return;
  281. }
  282. wx.redirectTo({
  283. url: '/pages/pay-success/index'
  284. })
  285. },
  286. });
  287. var quick_list = wx.getStorageSync('quick_list')
  288. if (quick_list) {
  289. var length = quick_list.length;
  290. for (var i = 0; i < length; i++) {
  291. var goods = quick_list[i]['goods'];
  292. var length2 = goods.length;
  293. for (var a = 0; a < length2; a++) {
  294. goods[a]['num'] = 0;
  295. }
  296. }
  297. wx.setStorageSync("quick_lists", quick_list)
  298. var carGoods = wx.getStorageSync('carGoods')
  299. var length = carGoods.length;
  300. for (var i = 0; i < length; i++) {
  301. carGoods[i]['num'] = 0;
  302. carGoods[i]['goods_price'] = 0;
  303. page.setData({
  304. 'carGoods': carGoods
  305. });
  306. }
  307. wx.setStorageSync("carGoods", carGoods)
  308. var total = wx.getStorageSync('total')
  309. if (total) {
  310. total.total_num = 0;
  311. total.total_price = 0;
  312. wx.setStorageSync("total", total)
  313. }
  314. var check_num = wx.getStorageSync('check_num')
  315. check_num = 0;
  316. wx.setStorageSync("check_num", check_num)
  317. var quick_hot_goods_lists = wx.getStorageSync('quick_hot_goods_lists')
  318. var length = quick_hot_goods_lists.length;
  319. for (var i = 0; i < length; i++) {
  320. quick_hot_goods_lists[i]['num'] = 0;
  321. page.setData({
  322. 'quick_hot_goods_lists': quick_hot_goods_lists
  323. });
  324. }
  325. wx.setStorageSync("quick_hot_goods_lists", quick_hot_goods_lists)
  326. }
  327. return;
  328. }
  329. if (res.code == 1) {
  330. page.showToast({
  331. title: res.msg,
  332. image: "/images/icon-warning.png",
  333. });
  334. return;
  335. }
  336. }
  337. });
  338. }
  339. if (data.payment == 2) {
  340. app.request({
  341. url: api.order.pay_data,
  342. data: {
  343. order_id: order_id,
  344. pay_type: 'HUODAO_PAY',
  345. form_id: e.detail.formId
  346. },
  347. success: function (res) {
  348. if (res.code == 0) {
  349. wx.redirectTo({
  350. url: "/pages/order/order?status=-1",
  351. });
  352. } else {
  353. page.showToast({
  354. title: res.msg,
  355. image: "/images/icon-warning.png",
  356. });
  357. return;
  358. }
  359. }
  360. });
  361. }
  362. if (data.payment == 3) {
  363. app.request({
  364. url: api.order.pay_data,
  365. data: {
  366. order_id: order_id,
  367. pay_type: 'BALANCE_PAY',
  368. form_id: e.detail.formId
  369. },
  370. success: function (res) {
  371. if (res.code == 0) {
  372. wx.redirectTo({
  373. url: "/pages/order/order?status=-1",
  374. });
  375. } else {
  376. page.showToast({
  377. title: res.msg,
  378. image: "/images/icon-warning.png",
  379. });
  380. setTimeout(function () {
  381. wx.redirectTo({
  382. url: "/pages/order/order?status=-1",
  383. });
  384. }, 1000)
  385. return;
  386. }
  387. }
  388. });
  389. }
  390. }
  391. if (res.code == 1) {
  392. wx.hideLoading();
  393. page.showToast({
  394. title: res.msg,
  395. image: "/images/icon-warning.png",
  396. });
  397. return;
  398. }
  399. }
  400. });
  401. },
  402. /**
  403. * 生命周期函数--监听页面初次渲染完成
  404. */
  405. onReady: function () {
  406. },
  407. /**
  408. * 生命周期函数--监听页面显示
  409. */
  410. onShow: function () {
  411. var page = this;
  412. var address = wx.getStorageSync("picker_address");
  413. if (address) {
  414. page.setData({
  415. address: address,
  416. name: address.name,
  417. mobile: address.mobile
  418. });
  419. wx.removeStorageSync("picker_address");
  420. }
  421. page.getOrderData(page.data.options);
  422. },
  423. getOrderData: function (options) {
  424. var page = this;
  425. var data = {};
  426. var address_id = "";
  427. if (page.data.address && page.data.address.id)
  428. address_id = page.data.address.id;
  429. data.address_id = address_id;
  430. data.longitude = longitude;
  431. data.latitude = latitude;
  432. wx.showLoading({
  433. title: "正在加载",
  434. mask: true,
  435. });
  436. if (options.cart_list) {
  437. var cart_list = JSON.parse(options.cart_list);
  438. data.cart_list = options.cart_list;
  439. }
  440. if (options.cart_id_list) {
  441. var cart_id_list = JSON.parse(options.cart_id_list);
  442. data.cart_id_list = cart_id_list;
  443. }
  444. if (options.goods_info) {
  445. data.goods_info = options.goods_info;
  446. }
  447. app.request({
  448. url: api.order.submit_preview,
  449. data: data,
  450. success: function (res) {
  451. wx.hideLoading();
  452. if (res.code == 0) {
  453. var shop_list = res.data.shop_list;
  454. var shop = {};
  455. if (shop_list && shop_list.length == 1) {
  456. shop = shop_list[0];
  457. }
  458. if (res.data.is_shop) {
  459. shop = res.data.is_shop;
  460. }
  461. page.setData({
  462. total_price: res.data.total_price,
  463. goods_list: res.data.list,
  464. address: res.data.address,
  465. express_price: parseFloat(res.data.express_price),
  466. coupon_list: res.data.coupon_list,
  467. shop_list: shop_list,
  468. shop: shop,
  469. name: res.data.address ? res.data.address.name : '',
  470. mobile: res.data.address ? res.data.address.mobile : '',
  471. send_type: res.data.send_type,
  472. level: res.data.level,
  473. new_total_price: res.data.total_price,
  474. integral: res.data.integral,
  475. goods_card_list: res.data.goods_card_list,
  476. form: res.data.form,
  477. is_payment: res.data.is_payment,
  478. pay_type_list: res.data.pay_type_list,
  479. payment: res.data.pay_type_list[0].payment,
  480. });
  481. if (res.data.goods_info){
  482. page.setData({
  483. goods_info: res.data.goods_info,
  484. });
  485. }
  486. if (res.data.cart_id_list) {
  487. page.setData({
  488. cart_id_list: res.data.cart_id_list,
  489. });
  490. }
  491. if (res.data.cart_list) {
  492. page.setData({
  493. cart_list: res.data.cart_list,
  494. });
  495. }
  496. if (res.data.send_type == 1) {//仅快递
  497. page.setData({
  498. offline: 0,
  499. });
  500. }
  501. if (res.data.send_type == 2) {//仅自提
  502. page.setData({
  503. offline: 1,
  504. });
  505. }
  506. page.getPrice();
  507. }
  508. if (res.code == 1) {
  509. wx.showModal({
  510. title: "提示",
  511. content: res.msg,
  512. showCancel: false,
  513. confirmText: "返回",
  514. success: function (res) {
  515. if (res.confirm) {
  516. wx.navigateBack({
  517. delta: 1,
  518. });
  519. }
  520. }
  521. });
  522. }
  523. }
  524. });
  525. },
  526. copyText: function (e) {
  527. var text = e.currentTarget.dataset.text;
  528. if (!text)
  529. return;
  530. wx.setClipboardData({
  531. data: text,
  532. success: function () {
  533. page.showToast({
  534. title: "已复制内容",
  535. });
  536. },
  537. fail: function () {
  538. page.showToast({
  539. title: "复制失败",
  540. image: "/images/icon-warning.png",
  541. });
  542. },
  543. });
  544. },
  545. showCouponPicker: function () {
  546. var page = this;
  547. if (page.data.coupon_list && page.data.coupon_list.length > 0) {
  548. page.setData({
  549. show_coupon_picker: true,
  550. });
  551. }
  552. },
  553. pickCoupon: function (e) {
  554. var page = this;
  555. var index = e.currentTarget.dataset.index;
  556. if (index == '-1' || index == -1) {
  557. page.setData({
  558. picker_coupon: false,
  559. show_coupon_picker: false,
  560. });
  561. } else {
  562. // var new_total_price = page.data.total_price - page.data.coupon_list[index].sub_price - page.data.integral.forehead;
  563. // if (page.data.level) {
  564. // new_total_price = new_total_price * page.data.level.discount / 10;
  565. // }
  566. page.setData({
  567. picker_coupon: page.data.coupon_list[index],
  568. show_coupon_picker: false,
  569. // new_total_price: parseFloat(new_total_price.toFixed(2)),
  570. });
  571. }
  572. page.getPrice();
  573. },
  574. numSub: function (num1, num2, length) {
  575. return 100;
  576. },
  577. showShop: function (e) {
  578. var page = this;
  579. page.dingwei();
  580. if (page.data.shop_list && page.data.shop_list.length >= 1) {
  581. page.setData({
  582. show_shop: true,
  583. });
  584. }
  585. },
  586. pickShop: function (e) {
  587. var page = this;
  588. var index = e.currentTarget.dataset.index;
  589. if (index == '-1' || index == -1) {
  590. page.setData({
  591. shop: false,
  592. show_shop: false,
  593. });
  594. } else {
  595. page.setData({
  596. shop: page.data.shop_list[index],
  597. show_shop: false,
  598. });
  599. }
  600. page.getPrice();
  601. },
  602. // integralRadio:function(e){
  603. // var page = this;
  604. // var index = e.currentTarget.dataset.index;
  605. // if (index == null || index =='radio'){
  606. // page.setData({
  607. // integral_radio: 'radio-active',
  608. // });
  609. // } else {
  610. // page.setData({
  611. // integral_radio: 'radio',
  612. // });
  613. // }
  614. // },
  615. integralSwitchChange: function (e) {
  616. var page = this;
  617. if (e.detail.value != false) {
  618. page.setData({
  619. integral_radio: 1,
  620. });
  621. } else {
  622. page.setData({
  623. integral_radio: 2,
  624. });
  625. }
  626. page.getPrice();
  627. },
  628. integration: function (e) {
  629. var page = this;
  630. var integration = page.data.integral.integration;
  631. wx.showModal({
  632. title: '积分使用规则',
  633. content: integration,
  634. showCancel: false,
  635. confirmText: '我知道了',
  636. confirmColor: '#ff4544',
  637. success: function (res) {
  638. if (res.confirm) {
  639. console.log('用户点击确定')
  640. }
  641. }
  642. });
  643. },
  644. /**
  645. * 计算总价
  646. */
  647. getPrice: function () {
  648. var page = this;
  649. var total_price = page.data.total_price;
  650. var new_total_price = total_price;
  651. var express_price = page.data.express_price;
  652. var picker_coupon = page.data.picker_coupon;
  653. var integral = page.data.integral;
  654. var integral_radio = page.data.integral_radio;
  655. var level = page.data.level;
  656. var offline = page.data.offline;
  657. if (picker_coupon) {
  658. new_total_price = new_total_price - picker_coupon.sub_price;
  659. }
  660. if (integral && integral_radio == 1) {
  661. new_total_price = new_total_price - parseFloat(integral.forehead);
  662. }
  663. if (level) {
  664. new_total_price = new_total_price * level.discount / 10;
  665. }
  666. if (new_total_price <= 0.01) {
  667. new_total_price = 0.01;
  668. }
  669. if (offline == 0) {
  670. new_total_price = new_total_price + express_price;
  671. }
  672. page.setData({
  673. new_total_price: parseFloat(new_total_price.toFixed(2))
  674. });
  675. },
  676. cardDel: function () {
  677. var page = this;
  678. page.setData({
  679. show_card: false
  680. });
  681. wx.redirectTo({
  682. url: '/pages/order/order?status=1',
  683. })
  684. },
  685. cardTo: function () {
  686. var page = this;
  687. page.setData({
  688. show_card: false
  689. });
  690. wx.redirectTo({
  691. url: '/pages/card/card'
  692. })
  693. },
  694. formInput: function (e) {
  695. var page = this;
  696. var index = e.currentTarget.dataset.index;
  697. var form = page.data.form;
  698. var form_list = form.list;
  699. form_list[index].default = e.detail.value;
  700. form.list = form_list;
  701. page.setData({
  702. form: form
  703. });
  704. },
  705. selectForm: function (e) {
  706. var page = this;
  707. var index = e.currentTarget.dataset.index;
  708. var k = e.currentTarget.dataset.k;
  709. var form = page.data.form;
  710. var form_list = form.list;
  711. if (form_list[index].type == 'radio') {
  712. var default_list = form_list[index].default_list;
  713. for (var i in default_list) {
  714. if (i == k) {
  715. default_list[k].is_selected = 1;
  716. } else {
  717. default_list[i].is_selected = 0;
  718. }
  719. }
  720. form_list[index].default_list = default_list;
  721. }
  722. if (form_list[index].type == 'checkbox') {
  723. var default_list = form_list[index].default_list;
  724. if (default_list[k].is_selected == 1) {
  725. default_list[k].is_selected = 0;
  726. } else {
  727. default_list[k].is_selected = 1;
  728. }
  729. form_list[index].default_list = default_list;
  730. }
  731. form.list = form_list;
  732. page.setData({
  733. form: form
  734. });
  735. },
  736. showPayment: function () {
  737. this.setData({
  738. show_payment: true
  739. });
  740. },
  741. payPicker: function (e) {
  742. var index = e.currentTarget.dataset.index;
  743. this.setData({
  744. payment: index
  745. });
  746. },
  747. payClose: function () {
  748. this.setData({
  749. show_payment: false
  750. });
  751. }
  752. });