confirm.js 10 KB

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