confirmorder.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // pages/confirmorder/confirmorder.js
  2. const app = getApp();
  3. const api = require('../../api/api');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. navbarData: {
  10. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  11. title: '确认订单', //导航栏 中间的标题
  12. capsuleMode: 'navBack', //显示模式(navBack:返回上一页;navHome:返回首页)
  13. },
  14. tabType: 'wuliu', //tab类型
  15. showBottomBar: true, //显示底部栏
  16. showReceiptPlace: false, //显示选择收货地址
  17. showSelfMention: false, //显示自取点
  18. showPayTypePicker: false, //显示支付方式选择器
  19. payTypes: [], //支付方式列表
  20. payType: [0], //支付方式
  21. showEMTPopup: false, //显示EMT支付弹出层
  22. showOrderSuccessful: false, //显示下单成功弹出层
  23. btnDisabled: false, //确认支付按钮禁用
  24. /* 表单 */
  25. id: 0, //拼团id
  26. gang_name: '', //拼团名称
  27. head_phone: '', //团长手机号
  28. all_scribe_price: 0, //划线价格总价
  29. all_price: 0, //总价
  30. shopcart: [], //购物车
  31. remarks: '', //备注
  32. logistics_mode: 0, //物流方式(0没有物流,1取货点自提,2送货或者自提)
  33. expected_arrival_time: '', //预计到货时间
  34. head_email: '', //团长邮箱
  35. order_number: '', //订单号
  36. ziquAddress: [], //自取点列表
  37. ziquIndex: 0,
  38. peisongAddress: [], //配送列表
  39. peisongIndex: 0,
  40. order_id: 0, //订单ID
  41. wxpayservice: '', //微信支付手续费
  42. },
  43. /**
  44. * 生命周期函数--监听页面加载
  45. */
  46. onLoad(options) {
  47. // console.log(options);
  48. let that = this;
  49. that.setData({
  50. id: Number(options.id),
  51. all_scribe_price: Number(options.all_scribe_price),
  52. all_price: Number(options.all_price),
  53. gang_name: options.gang_name,
  54. logistics_mode: Number(options.logistics_mode),
  55. head_phone: options.head_phone,
  56. expected_arrival_time: options.expected_arrival_time,
  57. shopcart: wx.getStorageSync('shopcart')
  58. })
  59. },
  60. /**
  61. * 生命周期函数--监听页面初次渲染完成
  62. */
  63. onReady() {
  64. },
  65. /**
  66. * 生命周期函数--监听页面显示
  67. */
  68. onShow() {
  69. let that = this;
  70. // 获取自取点列表
  71. if (that.data.logistics_mode == 1 || that.data.logistics_mode == 2) {
  72. wx.showLoading({
  73. title: '加载中',
  74. mask: true
  75. })
  76. wx.request({
  77. url: api.Gang_point,
  78. header: {
  79. 'Authorization': wx.getStorageSync('token')
  80. },
  81. data: {
  82. id: that.data.id
  83. },
  84. method: 'POST',
  85. success(res) {
  86. wx.hideLoading()
  87. console.log(res);
  88. if (res.data.code === 1) {
  89. that.setData({
  90. ziquAddress: res.data.data
  91. })
  92. } else {
  93. wx.showToast({
  94. title: res.data.msg,
  95. mask: true,
  96. icon: 'none'
  97. })
  98. }
  99. },
  100. fail(err) {
  101. wx.hideLoading()
  102. wx.showToast({
  103. title: '发起网络请求失败',
  104. icon: 'none',
  105. mask: true
  106. })
  107. },
  108. complete() {
  109. // wx.hideLoading()
  110. }
  111. })
  112. }
  113. // 获取收货地址列表
  114. if (that.data.logistics_mode == 2) {
  115. wx.showLoading({
  116. title: '加载中',
  117. mask: true
  118. })
  119. wx.request({
  120. url: api.Address_list,
  121. header: {
  122. 'Authorization': wx.getStorageSync('token')
  123. },
  124. method: 'POST',
  125. success(res) {
  126. wx.hideLoading()
  127. console.log(res);
  128. if (res.data.code === 1) {
  129. that.setData({
  130. peisongAddress: res.data.data
  131. })
  132. } else {
  133. wx.showToast({
  134. title: res.data.msg,
  135. mask: true,
  136. icon: 'none'
  137. })
  138. }
  139. },
  140. fail(err) {
  141. wx.hideLoading()
  142. wx.showToast({
  143. title: '发起网络请求失败',
  144. icon: 'none',
  145. mask: true
  146. })
  147. },
  148. complete() {
  149. // wx.hideLoading()
  150. }
  151. })
  152. }
  153. },
  154. /**
  155. * 生命周期函数--监听页面隐藏
  156. */
  157. onHide() {
  158. },
  159. /**
  160. * 生命周期函数--监听页面卸载
  161. */
  162. onUnload() {
  163. },
  164. /**
  165. * 页面相关事件处理函数--监听用户下拉动作
  166. */
  167. onPullDownRefresh() {
  168. },
  169. /**
  170. * 页面上拉触底事件的处理函数
  171. */
  172. onReachBottom() {
  173. },
  174. /**
  175. * 用户点击右上角分享
  176. */
  177. onShareAppMessage() {
  178. },
  179. // 切换tab
  180. switchTab(e) {
  181. console.log(e);
  182. this.setData({
  183. tabType: e.currentTarget.dataset.type
  184. })
  185. },
  186. // 打开收货地址弹出层
  187. openReceiptPlace() {
  188. this.setData({
  189. showReceiptPlace: true,
  190. showBottomBar: false
  191. })
  192. },
  193. // 关闭收货地址弹出层
  194. closeReceiptPlace() {
  195. this.setData({
  196. showReceiptPlace: false,
  197. showBottomBar: true
  198. })
  199. },
  200. // 打开自取点弹出层
  201. openSelfMention() {
  202. this.setData({
  203. showSelfMention: true,
  204. showBottomBar: false
  205. })
  206. },
  207. // 关闭自取点弹出层
  208. closeSelfMention() {
  209. this.setData({
  210. showSelfMention: false,
  211. showBottomBar: true
  212. })
  213. },
  214. // 下单
  215. openPayTypePicker() {
  216. let that = this;
  217. // 第一次下单
  218. if (that.data.order_id == 0) {
  219. let logistics_mode = 0; //收货方式
  220. if (that.data.logistics_mode == 1) { //只有自取
  221. logistics_mode = 1;
  222. }
  223. if (that.data.logistics_mode == 2) {
  224. if (that.data.tabType == 'wuliu') {
  225. logistics_mode = 0;
  226. } else if (that.data.tabType == 'ziqu') {
  227. logistics_mode = 1;
  228. }
  229. }
  230. // 商品列表处理
  231. let arr = that.data.shopcart;
  232. for (let i = 0; i < arr.length; i++) {
  233. if (arr[i].type == 'guige') {
  234. let guige_index = arr[i].guige_index; //当前规格索引
  235. let specifications = [];
  236. specifications.push(arr[i].specifications[guige_index]); //当前规格
  237. arr[i].commodity_number = arr[i].count; //商品数量
  238. arr[i].commodity_price = arr[i].specifications[guige_index].price; //商品价格
  239. arr[i].specifications = specifications; //商品规格
  240. delete arr[i].price;
  241. delete arr[i].scribe_price;
  242. delete arr[i].sold;
  243. delete arr[i].stock;
  244. delete arr[i].purchase_restrictions_second;
  245. delete arr[i].step_price;
  246. delete arr[i].count;
  247. delete arr[i].type;
  248. delete arr[i].guige_index;
  249. } else if (arr[i].type == 'step') {
  250. let step_index = arr[i].step_index; //当前阶梯价索引
  251. let step_price = [];
  252. step_price.push(arr[i].step_price[step_index]); //当前阶梯价
  253. arr[i].commodity_number = arr[i].count; //商品数量
  254. arr[i].commodity_price = arr[i].step_price[step_index].price; //商品价格
  255. arr[i].step_price = step_price; //商品阶梯价
  256. delete arr[i].price;
  257. delete arr[i].scribe_price;
  258. delete arr[i].sold;
  259. delete arr[i].stock;
  260. delete arr[i].purchase_restrictions_second;
  261. delete arr[i].count;
  262. delete arr[i].type;
  263. delete arr[i].step_index;
  264. delete arr[i].specifications;
  265. }
  266. }
  267. wx.showLoading({
  268. title: '加载中',
  269. mask: true
  270. })
  271. let discount = that.data.all_scribe_price - that.data.all_price;
  272. if (discount < 0) {
  273. discount = -discount;
  274. }
  275. if (logistics_mode == 0) {
  276. wx.request({
  277. url: api.Order_add,
  278. header: {
  279. 'Authorization': wx.getStorageSync('token')
  280. },
  281. data: {
  282. id: that.data.id,
  283. logistics_mode: logistics_mode,
  284. original_price: that.data.all_scribe_price,
  285. discount: discount,
  286. payment_amount: that.data.all_price,
  287. remarks: that.data.remarks,
  288. commodity_list: arr,
  289. consignee: that.data.peisongAddress[that.data.peisongIndex].receiving_name,
  290. phone: that.data.peisongAddress[that.data.peisongIndex].receiving_phone,
  291. receiving_address: that.data.peisongAddress[that.data.peisongIndex].receiving_address
  292. },
  293. method: 'POST',
  294. success(res) {
  295. wx.hideLoading()
  296. console.log(res);
  297. if (res.data.code === 1) {
  298. let order_id = res.data.data;
  299. that.setData({
  300. order_id
  301. })
  302. that.getOrder_payment_method(order_id);
  303. } else {
  304. wx.showToast({
  305. title: res.data.msg,
  306. mask: true,
  307. icon: 'none'
  308. })
  309. }
  310. },
  311. fail(err) {
  312. wx.hideLoading()
  313. wx.showToast({
  314. title: '发起网络请求失败',
  315. icon: 'none',
  316. mask: true
  317. })
  318. },
  319. complete() {
  320. // wx.hideLoading()
  321. }
  322. })
  323. } else if (logistics_mode == 1) {
  324. wx.request({
  325. url: api.Order_add,
  326. header: {
  327. 'Authorization': wx.getStorageSync('token')
  328. },
  329. data: {
  330. id: that.data.id,
  331. logistics_mode: logistics_mode,
  332. original_price: that.data.all_scribe_price,
  333. discount: that.data.all_scribe_price - that.data.all_price,
  334. payment_amount: that.data.all_price,
  335. remarks: that.data.remarks,
  336. commodity_list: arr,
  337. self_access_point: that.data.ziquAddress[that.data.ziquIndex].address,
  338. point_remarks: that.data.ziquAddress[that.data.ziquIndex].remarks,
  339. reserved_phone: that.data.ziquAddress[that.data.ziquIndex].phone
  340. },
  341. method: 'POST',
  342. success(res) {
  343. wx.hideLoading()
  344. console.log(res);
  345. if (res.data.code === 1) {
  346. let order_id = res.data.data;
  347. that.setData({
  348. order_id
  349. })
  350. that.getOrder_payment_method(order_id);
  351. } else {
  352. wx.showToast({
  353. title: res.data.msg,
  354. mask: true,
  355. icon: 'none'
  356. })
  357. }
  358. },
  359. fail(err) {
  360. wx.hideLoading()
  361. wx.showToast({
  362. title: '发起网络请求失败',
  363. icon: 'none',
  364. mask: true
  365. })
  366. },
  367. complete() {
  368. // wx.hideLoading()
  369. }
  370. })
  371. }
  372. } else {
  373. // 重复点击下单
  374. let order_id = Number(that.data.order_id);
  375. that.getOrder_payment_method(order_id);
  376. }
  377. },
  378. // 获取用户支付方式
  379. getOrder_payment_method(order_id) {
  380. let that = this;
  381. wx.showLoading({
  382. title: '加载中',
  383. mask: true
  384. })
  385. wx.request({
  386. url: api.Order_payment_method,
  387. header: {
  388. 'Authorization': wx.getStorageSync('token')
  389. },
  390. data: {
  391. id: Number(order_id)
  392. },
  393. method: 'POST',
  394. success(res) {
  395. wx.hideLoading()
  396. console.log(res);
  397. if (res.data.code === 1) {
  398. that.setData({
  399. payTypes: res.data.data,
  400. showPayTypePicker: true,
  401. showBottomBar: false
  402. })
  403. } else {
  404. wx.showToast({
  405. title: res.data.msg,
  406. mask: true,
  407. icon: 'none'
  408. })
  409. }
  410. },
  411. fail(err) {
  412. wx.hideLoading()
  413. wx.showToast({
  414. title: '发起网络请求失败',
  415. icon: 'none',
  416. mask: true
  417. })
  418. },
  419. complete() {
  420. // wx.hideLoading()
  421. }
  422. })
  423. },
  424. // 关闭支付方式弹出层
  425. closePayTypePicker() {
  426. this.setData({
  427. showPayTypePicker: false,
  428. showBottomBar: true
  429. })
  430. },
  431. // 支付方式改变
  432. payTypeChanged(e) {
  433. let that = this;
  434. that.setData({
  435. payType: e.detail.value
  436. })
  437. // if(that.data.payTypes[that.data.payType[0]].payment_name == '微信支付') {
  438. // let all_price = that.data.all_price;
  439. // all_price = all_price * (1 + that.data.wxpayservice);
  440. // all_price = all_price.toFixed(2);
  441. // that.setData({
  442. // all_price
  443. // })
  444. // }
  445. },
  446. // 打开EMT支付弹出层
  447. openEMTPopup() {
  448. this.setData({
  449. showPayTypePicker: false,
  450. showEMTPopup: true,
  451. showBottomBar: false
  452. })
  453. },
  454. // 关闭EMT支付弹出层
  455. closeEMTPopup() {
  456. this.setData({
  457. showEMTPopup: false,
  458. showBottomBar: true
  459. })
  460. },
  461. // 确认支付
  462. confirmPay() {
  463. let that = this;
  464. let payType = that.data.payTypes[that.data.payType[0]];
  465. let id = that.data.order_id; //订单ID
  466. let payment_method = payType.payment_method; //支付方式(0微信支付,1EMT支付,2线下支付)
  467. let payment_price = that.data.all_price; //支付金额
  468. // 如果支付方式为微信支付-获取微信支付手续费
  469. if (payType.payment_name == '微信支付') {
  470. wx.showLoading({
  471. title: '加载中',
  472. mask: true
  473. })
  474. wx.request({
  475. url: api.Service,
  476. header: {
  477. 'Authorization': wx.getStorageSync('token')
  478. },
  479. method: 'POST',
  480. success(res) {
  481. wx.hideLoading();
  482. // console.log(res);
  483. that.setData({
  484. wxpayservice: Number(res.data.data)
  485. })
  486. payment_price = payment_price * (1 + that.data.wxpayservice);
  487. payment_price = payment_price.toFixed(2);
  488. that.zhenDePay(id, payment_method, payment_price);
  489. },
  490. fail(err) {
  491. wx.hideLoading()
  492. }
  493. })
  494. }else{
  495. that.zhenDePay(id, payment_method, payment_price);
  496. }
  497. },
  498. // 真的要支付吗?在这里?就现在?
  499. zhenDePay(id, payment_method, payment_price) {
  500. let that = this;
  501. wx.showLoading({
  502. title: '加载中',
  503. mask: true
  504. })
  505. wx.request({
  506. url: api.Order_payment,
  507. header: {
  508. 'Authorization': wx.getStorageSync('token')
  509. },
  510. data: {
  511. id: Number(id),
  512. payment_method: Number(payment_method),
  513. payment_price: payment_price
  514. },
  515. method: 'POST',
  516. success(res) {
  517. wx.hideLoading()
  518. console.log(res);
  519. if (res.data.code === 1) {
  520. if (res.data.msg == 'EMT支付') {
  521. that.setData({
  522. head_email: res.data.data.email,
  523. order_number: res.data.data.order_number
  524. })
  525. that.openEMTPopup()
  526. }
  527. if (res.data.msg == '线下支付') {
  528. that.setData({
  529. showPayTypePicker: false,
  530. showBottomBar: true
  531. })
  532. wx.navigateTo({
  533. url: '/pages/orderdetail/orderdetail?id=' + id,
  534. })
  535. }
  536. if (res.data.msg == '微信支付') {
  537. wx.requestPayment({
  538. appId: res.data.data.payParams.appId,
  539. nonceStr: res.data.data.payParams.nonceStr,
  540. package: res.data.data.payParams.package,
  541. paySign: res.data.data.payParams.paySign,
  542. timeStamp: res.data.data.payParams.timeStamp,
  543. signType: res.data.data.payParams.signType,
  544. success(res) {
  545. wx.hideLoading()
  546. console.log(res);
  547. wx.showToast({
  548. title: '支付成功',
  549. mask: true,
  550. success() {
  551. setTimeout(() => {
  552. that.setData({
  553. showPayTypePicker: false,
  554. showBottomBar: true
  555. })
  556. wx.navigateTo({
  557. url: '/pages/orderdetail/orderdetail?id=' + id,
  558. })
  559. }, 1500)
  560. }
  561. })
  562. },
  563. fail(res) {
  564. wx.hideLoading()
  565. console.log(res);
  566. if (res.errMsg.search('cancel')) {
  567. wx.showToast({
  568. title: '用户取消支付',
  569. icon: 'error',
  570. mask: true
  571. })
  572. } else if (res.errMsg.search('offline')) {
  573. wx.showToast({
  574. title: '网络断开连接',
  575. icon: 'error',
  576. mask: true
  577. })
  578. }
  579. }
  580. })
  581. }
  582. } else {
  583. wx.showToast({
  584. title: res.data.msg,
  585. mask: true,
  586. icon: 'none'
  587. })
  588. }
  589. },
  590. fail(err) {
  591. wx.hideLoading()
  592. wx.showToast({
  593. title: '发起网络请求失败',
  594. icon: 'none',
  595. mask: true
  596. })
  597. },
  598. complete() {
  599. // wx.hideLoading()
  600. }
  601. })
  602. },
  603. // 打开下单成功弹窗
  604. openOrderSuccessfulPopup() {
  605. this.setData({
  606. showOrderSuccessful: true
  607. })
  608. },
  609. // 关闭下单成功弹窗
  610. closeOrderSuccessfulPopup() {
  611. this.setData({
  612. showOrderSuccessful: false
  613. })
  614. },
  615. // 输入备注
  616. inputRemarks(e) {
  617. this.setData({
  618. remarks: e.detail.value
  619. })
  620. },
  621. // 选择配送地址
  622. choosePeisongAddress(e) {
  623. this.setData({
  624. peisongIndex: e.currentTarget.dataset.index,
  625. showReceiptPlace: false,
  626. showBottomBar: true
  627. })
  628. },
  629. // 选择自取地址
  630. chooseZiquAddress(e) {
  631. this.setData({
  632. ziquIndex: e.currentTarget.dataset.index,
  633. showSelfMention: false,
  634. showBottomBar: true
  635. })
  636. },
  637. // 联系团长
  638. callHead() {
  639. let that = this;
  640. wx.makePhoneCall({
  641. phoneNumber: that.data.head_phone,
  642. })
  643. },
  644. // 复制团长邮箱
  645. copyHeadEmail() {
  646. wx.setClipboardData({
  647. data: this.data.head_email,
  648. })
  649. },
  650. // 复制订单号
  651. copyOrderNumber() {
  652. wx.setClipboardData({
  653. data: this.data.order_number,
  654. })
  655. },
  656. // EMT已支付
  657. EMTpaid() {
  658. let id = this.data.order_id; //订单ID
  659. wx.navigateTo({
  660. url: '/pages/orderdetail/orderdetail?id=' + id,
  661. })
  662. },
  663. // 新增收货地址
  664. addAddress() {
  665. wx.navigateTo({
  666. url: '/pages/addaddress/addaddress?type=add',
  667. })
  668. },
  669. // 编辑收货地点
  670. editAddress(e) {
  671. let index = e.currentTarget.dataset.index
  672. // console.log(e.currentTarget.dataset.index);
  673. let address = this.data.peisongAddress[index];
  674. wx.setStorageSync('address', address);
  675. wx.navigateTo({
  676. url: '/pages/addaddress/addaddress?type=edit',
  677. })
  678. },
  679. // 获取微信支付手续费
  680. getWXPayServiceCharge() {
  681. let that = this;
  682. wx.showLoading({
  683. title: '加载中',
  684. mask: true
  685. })
  686. wx.request({
  687. url: api.Service,
  688. header: {
  689. 'Authorization': wx.getStorageSync('token')
  690. },
  691. method: 'POST',
  692. success(res) {
  693. wx.hideLoading()
  694. that.setData({
  695. wxpayservice: res.data.data
  696. })
  697. },
  698. fail(err) {
  699. wx.hideLoading()
  700. }
  701. })
  702. },
  703. })