confirm.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // pages/order/confirm.js
  2. import Toast from '@vant/weapp/toast/toast';
  3. import {
  4. confirmOrder,
  5. getJhPayInfo,
  6. getCustomerGradeInfo
  7. } from "../../api/home"
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. images: [
  14. 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2684369054,623698254&fm=26&gp=0.jpg',
  15. 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2684369054,623698254&fm=26&gp=0.jpg',
  16. 'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2684369054,623698254&fm=26&gp=0.jpg',
  17. ]
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. let app = getApp();
  24. let orderAmt = getApp().orderAmount;
  25. let oilPrice = getApp().oilPrice;
  26. // console.log("oilGunName:"+oilGunName);
  27. let oilLitre = null; //加油升数
  28. let totalMoney = null;
  29. let youHuiAmount = null;
  30. let discountPrice = null; //优惠价格
  31. let gasoilDiscountAmount = null; //优惠金额-满减,立减,直降
  32. let discountAmount = null; //优惠起点金额
  33. var mobileValue = wx.getStorageSync('mobilePhone')
  34. if (mobileValue) {
  35. if (getApp().gunType === '1') {
  36. getCustomerGradeInfo({
  37. "stationId": app.globalData.stationId,
  38. "userType": "2",
  39. "oilName":app.oilGunName,
  40. "openId": app.globalData.openId,
  41. "mobilePhone": mobileValue
  42. }).then(res => { // handle success
  43. if (res.retCode == 0) { //获取优惠信息成功
  44. if (res.data.length > 0) {
  45. if ((res.data[0].discountWay === '直降')) { //优惠方式为等级直降
  46. discountPrice = res.data[0].gasoilDiscountLitre; //直降方案,按会员等级、按油品单价直降
  47. totalMoney = (orderAmt / oilPrice) * (oilPrice - discountPrice);
  48. // console.log("totalMonry1:"+totalMoney);
  49. totalMoney = totalMoney.toFixed(2);
  50. // console.log("totalMonry2:"+totalMoney);
  51. youHuiAmount = (orderAmt / oilPrice) * discountPrice;
  52. youHuiAmount = youHuiAmount.toFixed(2);
  53. } else if (res.data[0].discountWay === '1') { //优惠方式为满减
  54. gasoilDiscountAmount = Number(res.data[0].gasoilDiscountAmt); //满减方案,价格不变,总额满足条件优惠满减
  55. discountAmount = Number(res.data[0].discountAmt);
  56. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  57. if (orderAmt >= discountAmount) {
  58. let discountTimes = Math.floor(orderAmt / discountAmount);
  59. totalMoney = Number(orderAmt - (gasoilDiscountAmount * discountTimes));
  60. totalMoney = totalMoney.toFixed(2);
  61. youHuiAmount = Number(gasoilDiscountAmount * discountTimes);
  62. youHuiAmount = youHuiAmount.toFixed(2);
  63. } else {
  64. totalMoney = orderAmt;
  65. youHuiAmount = 0;
  66. }
  67. } else if (res.data[0].discountWay === '2') { //优惠方式为立减
  68. gasoilDiscountAmount = res.data[0].gasoilDiscountAmt; //立减方案,价格不变,总额满足条件优惠立减
  69. discountAmount = res.data[0].discountAmt;
  70. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  71. if (orderAmt >= discountAmount) {
  72. totalMoney = Number(orderAmt - gasoilDiscountAmount);
  73. totalMoney = totalMoney.toFixed(2);
  74. youHuiAmount = Number(gasoilDiscountAmount);
  75. youHuiAmount = youHuiAmount.toFixed(2);
  76. } else {
  77. totalMoney = orderAmt;
  78. youHuiAmount = 0;
  79. }
  80. } else if ((res.data[0].discountWay === '3')) { //优惠方式为独立直降
  81. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  82. if (res.data[0].discountTerm === '1') { //按总金额直降
  83. if (orderAmt >= res.data[0].discountAmt) {
  84. discountPrice = Number(res.data[0].gasoilDiscountAmt); //独立直降方案,按油品升数或者总价直降
  85. totalMoney = (orderAmt / oilPrice) * (oilPrice - discountPrice);
  86. // console.log("totalMonry1:"+totalMoney);
  87. totalMoney = totalMoney.toFixed(2);
  88. // console.log("totalMonry2:"+totalMoney);
  89. youHuiAmount = (orderAmt / oilPrice) * discountPrice;
  90. youHuiAmount = youHuiAmount.toFixed(2);
  91. } else {
  92. totalMoney = orderAmt;
  93. youHuiAmount = 0;
  94. }
  95. } else if (res.data[0].discountTerm === '2') { //按升数直降
  96. discountPrice = Number(res.data[0].gasoilDiscountAmt);
  97. //取条件升数
  98. let dislQty = Number(res.data[0].discountAmt);
  99. //计算升数
  100. let lQty = orderAmt / oilPrice;
  101. lQty = lQty.toFixed(2);
  102. if (lQty >= dislQty) {
  103. totalMoney = (orderAmt / oilPrice) * (oilPrice - discountPrice);
  104. // console.log("totalMonry1:"+totalMoney);
  105. totalMoney = totalMoney.toFixed(2);
  106. // console.log("totalMonry2:"+totalMoney);
  107. youHuiAmount = (orderAmt / oilPrice) * discountPrice;
  108. youHuiAmount = youHuiAmount.toFixed(2);
  109. } else {
  110. totalMoney = orderAmt;
  111. youHuiAmount = 0;
  112. }
  113. } else {
  114. totalMoney = orderAmt;
  115. youHuiAmount = 0;
  116. }
  117. } else { //没有以上三种优惠方式
  118. totalMoney = orderAmt;
  119. youHuiAmount = 0;
  120. }
  121. } else { //没有查询到优惠信息,新用户没有优惠
  122. totalMoney = orderAmt;
  123. youHuiAmount = 0;
  124. }
  125. oilLitre = Number(orderAmt / oilPrice);
  126. oilLitre = oilLitre.toFixed(2);
  127. this.setData({
  128. stationName: app.globalData.stationName,
  129. imageURL: app.globalData.stationPic,
  130. gunNo: app.oilGunNum,
  131. oilName: app.oilGunName,
  132. oilPrice: app.oilPrice,
  133. orderMoney: app.orderAmount,
  134. discountPrice: discountPrice,
  135. oilLitre: oilLitre,
  136. manJian: 0,
  137. liJian: 0,
  138. youHuiAmount: youHuiAmount,
  139. youHui: 0,
  140. vipMoney: 0,
  141. apayMoney: totalMoney
  142. })
  143. } else {
  144. Toast.fail(res.message);
  145. }
  146. }).catch(error => { // handle error
  147. Toast.fail(error);
  148. })
  149. } else {
  150. totalMoney = orderAmt;
  151. youHuiAmount = 0;
  152. oilLitre = 0;
  153. this.setData({
  154. stationName: app.globalData.stationName,
  155. imageURL: app.globalData.stationPics,
  156. gunNo: getApp().oilGunNum,
  157. oilName: getApp().oilGunName,
  158. oilPrice: getApp().oilPrice,
  159. orderMoney: getApp().orderAmount,
  160. discountPrice: discountPrice,
  161. oilLitre: oilLitre,
  162. manJian: 0,
  163. liJian: 0,
  164. youHuiAmount: youHuiAmount,
  165. youHui: 0,
  166. vipMoney: 0,
  167. apayMoney: totalMoney
  168. })
  169. }
  170. } else {
  171. wx.showToast({
  172. title: '手机号未授权',
  173. })
  174. }
  175. },
  176. /**
  177. * 生命周期函数--监听页面初次渲染完成
  178. */
  179. onReady: function () {
  180. },
  181. /**
  182. * 生命周期函数--监听页面显示
  183. */
  184. onShow: function () {
  185. },
  186. /**
  187. * 立即支付方法
  188. */
  189. pay: function () {
  190. //支付前,先进行订单确认,确认无误后,获取到订单id,作为入参,调用支付平台的下单接口
  191. var app = getApp()
  192. //请求后台创建订单接口,获取订单号
  193. confirmOrder({
  194. "orderNo": app.orderNo,
  195. "openId": app.globalData.openId,
  196. "userType": 2
  197. }).then(res => { // handle success
  198. // console.log("data:" + res.data);
  199. if (res.retCode == 0 && res.data.length == 1) {
  200. // console.log("orderNo:" + res.data[0].orderNo)
  201. //确认订单存在,获取到订单id,请求聚合支付下单接口
  202. var orderId = res.data[0].orderId;
  203. // console.log("orderId="+orderId);
  204. getJhPayInfo({
  205. "orderId": orderId,
  206. "openId": app.globalData.openId,
  207. "stationId": app.globalData.stationId,
  208. "subject": app.globalData.stationName + "_" + app.oilGunName + "油品支付订单",
  209. "userType": "2"
  210. }).then(res => { // handle success
  211. //判断封装接口请求成功
  212. if (res.retCode === 0) {
  213. //判断随行付支付网关是否请求成功
  214. if (res.data.code == '0000') {
  215. let timeStamp = res.data.respData.payTimeStamp;
  216. let nonceStr = res.data.respData.paynonceStr;
  217. let payPackage = res.data.respData.payPackage;
  218. let signType = res.data.signType;
  219. let paySign = res.data.respData.paySign;
  220. wx.requestPayment({
  221. 'timeStamp': timeStamp,
  222. 'nonceStr': nonceStr,
  223. 'package': payPackage,
  224. 'signType': signType,
  225. 'paySign': paySign,
  226. 'success': function (res) {
  227. wx.navigateTo({
  228. url: '/pages/order/success',
  229. })
  230. },
  231. 'fail': function (res) {
  232. },
  233. 'complete': function (res) {
  234. }
  235. })
  236. } else {
  237. Toast.fail(res.data.msg);
  238. }
  239. } else { //请求失败,提示失败信息
  240. Toast.fail(res.message);
  241. }
  242. }).catch(error => { // handle error
  243. // console.log(error);
  244. Toast.fail(error);
  245. })
  246. } else {
  247. Toast.fail(res.message);
  248. }
  249. }).catch(error => { // handle error
  250. // console.log(error);
  251. Toast.fail(error);
  252. })
  253. }
  254. })