order-confirm.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // pages/order-confirm/order-confirm.js
  2. var app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. cartArr: [
  9. { "name": "微信", "icon": "icon-weixinzhifu", value:'weixin'},
  10. { "name": "余额支付", "icon": "icon-yuezhifu", value: 'yue' },
  11. // { "name": "线下付款", "icon": "icon-wallet", value: 'offline' },
  12. // { "name": "到店自提", "icon": "icon-dianpu", value: 'ziti' },
  13. ],
  14. cartInfo : [],
  15. cartId : '',
  16. priceGroup :[],
  17. totalPrice:0,
  18. orderKey:'',
  19. seckillId:0,
  20. BargainId:0,
  21. combinationId:0,
  22. pinkId:0,
  23. offlinePostage : 0,
  24. integralRatio: 1,
  25. usableCoupon : [],
  26. userInfo : [],
  27. url: app.globalData.urlImages,
  28. addressId:0,
  29. couponId:0,
  30. couponPrice:0,
  31. couponInfo:[],
  32. addressInfo:[],
  33. mark:'',
  34. payType:'weixin',
  35. useIntegral:false,
  36. useInegealOrder:0,
  37. statusIntegral: false,
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: function (options) {
  43. app.setBarColor();
  44. app.setUserInfo();
  45. var that = this;
  46. if (options.pinkId){
  47. that.setData({
  48. pinkId: options.pinkId
  49. })
  50. }
  51. if (options.addressId){
  52. that.setData({
  53. addressId: options.addressId
  54. });
  55. }
  56. if (options.couponId) {
  57. that.setData({
  58. couponId: options.couponId
  59. });
  60. }
  61. if (options.id == ''){
  62. wx.showToast({
  63. title: '请选择要购买的商品',
  64. icon: 'none',
  65. duration: 1000,
  66. })
  67. setTimeout(function(){
  68. that.toBuy();
  69. },1100)
  70. }else{
  71. this.getConfirm(options.id);
  72. }
  73. that.getaddressInfo();
  74. that.getCouponRope();
  75. },
  76. //使用积分
  77. checkboxChange: function () {
  78. var that = this;
  79. that.setData({
  80. statusIntegral: !that.data.statusIntegral,//更改是否勾选积分
  81. useIntegral: !that.data.useIntegral //是否使用积分
  82. });
  83. //用户所有计算积分抵扣金额
  84. var integralCountPrice = (Number(that.data.userInfo.integral) * Number(that.data.integralRatio)).toFixed(2);
  85. console.log(that.data.userInfo.integral);
  86. console.log(integralCountPrice);
  87. //如果积分抵扣金额能大于订单总金额
  88. if (Number(integralCountPrice) >= Number(that.data.totalPrice) && that.data.statusIntegral) {
  89. var iCountPrice = Number(that.data.totalPrice);/* priceGroup. */
  90. var useinegeal = (Number(that.data.totalPrice) / Number(that.data.integralRatio)).toFixed(2);
  91. }else{
  92. //实际抵扣的积分金额
  93. if (that.data.useInegealOrder > 0){
  94. var iCountPrice = (Number(that.data.useInegealOrder) * Number(that.data.integralRatio)).toFixed(2);
  95. var useinegeal = that.data.useInegealOrder;
  96. }else{
  97. var iCountPrice = (Number(that.data.userInfo.integral) * Number(that.data.integralRatio)).toFixed(2);
  98. var useinegeal = Number(that.data.userInfo.integral);
  99. }
  100. }
  101. //如果使用积分
  102. console.log(useinegeal);
  103. console.log(that.data.useInegealOrder);
  104. console.log(that.data.totalPrice);
  105. console.log(iCountPrice);
  106. var totalPrice = 0;
  107. if ((Number(that.data.totalPrice) - Number(iCountPrice)).toFixed(2) > 0) totalPrice = (Number(that.data.totalPrice) - Number(iCountPrice)).toFixed(2);
  108. if (that.data.statusIntegral) {
  109. that.setData({
  110. priceIntegral: '-' + useinegeal,
  111. totalPrice: totalPrice,
  112. useInegealOrder: useinegeal
  113. });
  114. console.log(that.data.totalPrice);
  115. } else {
  116. console.log(that.data.totalPrice);
  117. that.setData({
  118. priceIntegral: '',
  119. totalPrice: Number(Number(that.data.totalPrice) + Number(iCountPrice)).toFixed(2)
  120. });
  121. }
  122. },
  123. bindHideKeyboard:function(e){
  124. this.setData({
  125. mark: e.detail.value
  126. })
  127. },
  128. radioChange:function(e){
  129. this.setData({
  130. payType: e.currentTarget.dataset.value
  131. })
  132. },
  133. subOrder:function(e){
  134. var that = this;
  135. var header = {
  136. 'content-type': 'application/json',
  137. };
  138. if (that.data.payType == ''){
  139. wx.showToast({
  140. title: '请选择支付方式',
  141. icon: 'none',
  142. duration: 1000,
  143. })
  144. } else if (!that.data.addressId){
  145. wx.showToast({
  146. title: '请选择收货地址',
  147. icon: 'none',
  148. duration: 1000,
  149. })
  150. } else {
  151. wx.request({
  152. url: app.globalData.url + '/routine/auth_api/create_order?uid=' + app.globalData.uid +'&key='+ that.data.orderKey,
  153. method: 'POST',
  154. header: header,
  155. data: {
  156. addressId: that.data.addressId,
  157. formId: e.detail.formId,
  158. couponId: that.data.couponId,
  159. payType: that.data.payType,
  160. useIntegral: that.data.useIntegral,
  161. bargainId: that.data.BargainId,
  162. combinationId: that.data.combinationId,
  163. pinkId: that.data.pinkId,
  164. seckill_id: that.data.seckillId,
  165. mark: that.data.mark
  166. },
  167. success: function (res) {
  168. var data = res.data.data;
  169. if (res.data.code == 200 && res.data.data.status == 'SUCCESS'){
  170. wx.showToast({
  171. title: res.data.msg,
  172. icon: 'success',
  173. duration: 1000,
  174. })
  175. setTimeout(function () {
  176. wx.navigateTo({
  177. url: '/pages/orders-con/orders-con?order_id=' + data.result.orderId
  178. })
  179. }, 1200)
  180. } else if (res.data.code == 200 && res.data.data.status == 'ORDER_EXIST') {
  181. wx.showToast({
  182. title: res.data.msg,
  183. icon: 'none',
  184. duration: 1000,
  185. })
  186. setTimeout(function () {
  187. wx.navigateTo({
  188. url: '/pages/orders-con/orders-con?order_id=' + data.result.orderId
  189. })
  190. }, 1200)
  191. } else if (res.data.code == 200 && res.data.data.status == 'ORDER_EXIST') {
  192. wx.showToast({
  193. title: res.data.msg,
  194. icon: 'none',
  195. duration: 1000,
  196. })
  197. setTimeout(function () {
  198. wx.navigateTo({
  199. url: '/pages/orders-con/orders-con?order_id=' + data.result.orderId
  200. })
  201. }, 1200)
  202. }else if (res.data.code == 200 && res.data.data.status == 'EXTEND_ORDER'){
  203. wx.showToast({
  204. title: res.data.msg,
  205. icon: 'none',
  206. duration: 1000,
  207. })
  208. setTimeout(function () {
  209. wx.navigateTo({
  210. url: '/pages/orders-con/orders-con?order_id=' + data.result.orderId
  211. })
  212. }, 1200)
  213. } else if (res.data.code == 200 && res.data.data.status == 'WECHAT_PAY'){
  214. var jsConfig = res.data.data.result.jsConfig;
  215. wx.requestPayment({
  216. timeStamp: jsConfig.timestamp,
  217. nonceStr: jsConfig.nonceStr,
  218. package: jsConfig.package,
  219. signType: jsConfig.signType,
  220. paySign: jsConfig.paySign,
  221. success: function(res) {
  222. wx.showToast({
  223. title: '支付成功',
  224. icon: 'success',
  225. duration: 1000,
  226. })
  227. setTimeout(function () {
  228. wx.navigateTo({
  229. url: '/pages/orders-con/orders-con?order_id=' + data.result.orderId
  230. })
  231. }, 1200)
  232. },
  233. fail: function(res) {
  234. wx.showToast({
  235. title: '支付取消',
  236. icon: 'none',
  237. duration: 1000,
  238. })
  239. setTimeout(function () {
  240. wx.navigateTo({
  241. url: '/pages/orders-con/orders-con?order_id='+data.result.orderId
  242. })
  243. }, 1200)
  244. },
  245. complete: function(res) {
  246. if (res.errMsg == 'requestPayment:cancel') {
  247. wx.showToast({
  248. title: '取消支付',
  249. icon: 'none',
  250. duration: 1000,
  251. })
  252. setTimeout(function () {
  253. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  254. url: '/pages/orders-con/orders-con?order_id=' + data.result.orderId
  255. })
  256. }, 1200)
  257. }
  258. },
  259. })
  260. }else{
  261. wx.showToast({
  262. title: res.data.msg,
  263. icon: 'none',
  264. duration: 1000,
  265. })
  266. }
  267. }
  268. })
  269. }
  270. },
  271. //使用优惠券
  272. getCouponRope:function(){
  273. var that = this;
  274. if (that.data.couponId){
  275. wx.request({
  276. url: app.globalData.url + '/routine/auth_api/get_coupon_rope?uid=' + app.globalData.uid,
  277. method: 'GET',
  278. data: {
  279. couponId: that.data.couponId
  280. },
  281. success: function (res) {
  282. if (res.data.code == 200) {
  283. //原价
  284. var totalPrice = that.data.totalPrice;
  285. if (Number(res.data.data.coupon_price) > 0){
  286. totalPrice = Number(that.data.totalPrice) - Number(res.data.data.coupon_price);
  287. }
  288. totalPrice = totalPrice > 0 ? totalPrice : 0;
  289. that.setData({
  290. couponInfo: res.data.data,
  291. totalPrice: totalPrice
  292. })
  293. }else{
  294. that.setData({
  295. couponInfo: [],
  296. couponPrice: ''
  297. })
  298. }
  299. }
  300. })
  301. }
  302. },
  303. getaddressInfo:function(){
  304. var that = this;
  305. if (that.data.addressId){
  306. wx.request({
  307. url: app.globalData.url + '/routine/auth_api/get_user_address?uid=' + app.globalData.uid,
  308. method: 'GET',
  309. data:{
  310. addressId: that.data.addressId
  311. },
  312. success: function (res) {
  313. if (res.data.code == 200) {
  314. that.setData({
  315. addressInfo:res.data.data
  316. })
  317. }
  318. }
  319. })
  320. }else{
  321. wx.request({
  322. url: app.globalData.url + '/routine/auth_api/user_default_address?uid=' + app.globalData.uid+ '&openid=' + app.globalData.openid,
  323. method: 'GET',
  324. success: function (res) {
  325. if (res.data.code == 200) {
  326. that.setData({
  327. addressInfo: res.data.data,
  328. addressId: res.data.data.id
  329. })
  330. }
  331. }
  332. })
  333. }
  334. },
  335. getAddress: function () {
  336. var that = this;
  337. var header = {
  338. 'content-type': 'application/x-www-form-urlencoded'
  339. };
  340. wx.request({
  341. url: app.globalData.url + '/routine/auth_api/user_address_list?uid=' + app.globalData.uid + '&openid=' + app.globalData.openid,
  342. method: 'POST',
  343. header: header,
  344. success: function (res) {
  345. if (res.data.code == 200) {
  346. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  347. url: '/pages/address/address?cartId=' + that.data.cartId + '&pinkId=' + that.data.pinkId + '&couponId=' + that.data.couponId
  348. })
  349. }
  350. }
  351. })
  352. },
  353. getCoupon:function(){
  354. var that = this;
  355. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  356. url: '/pages/coupon-status/coupon-status?cartId=' + that.data.cartId + '&totalPrice=' + that.data.priceGroup.totalPrice + '&pinkId=' + that.data.pinkId + '&addressId=' + that.data.addressId
  357. })
  358. },
  359. toBuy:function(){
  360. wx.switchTab({
  361. url: '/pages/buycar/buycar'
  362. });
  363. },
  364. toAddress: function () {
  365. var that = this;
  366. wx.navigateTo({ //跳转至指定页面并关闭其他打开的所有页面(这个最好用在返回至首页的的时候)
  367. url: '/pages/addaddress/addaddress?cartId=' + that.data.cartId + '&pinkId=' + that.data.pinkId + '&couponId=' + that.data.couponId
  368. })
  369. },
  370. getConfirm: function (cartIdsStr){
  371. var that = this;
  372. var header = {
  373. 'content-type': 'application/x-www-form-urlencoded',
  374. };
  375. wx.request({
  376. url: app.globalData.url + '/routine/auth_api/confirm_order?uid=' + app.globalData.uid,
  377. method: 'POST',
  378. data: {
  379. cartId: cartIdsStr
  380. },
  381. header: header,
  382. success: function (res) {
  383. if(res.data.code == 200){
  384. that.setData({
  385. userInfo: res.data.data.userInfo,
  386. cartInfo: res.data.data.cartInfo,
  387. integralRatio: res.data.data.integralRatio,
  388. offlinePostage: res.data.data.offlinePostage,
  389. orderKey: res.data.data.orderKey,
  390. priceGroup: res.data.data.priceGroup,
  391. totalPrice: res.data.data.priceGroup.totalPrice,
  392. cartId: res.data.data.cartId,
  393. seckillId: res.data.data.seckill_id,
  394. usableCoupon: res.data.data.usableCoupon
  395. })
  396. that.getBargainId();
  397. }
  398. }
  399. })
  400. },
  401. getBargainId:function(){
  402. var that = this;
  403. var cartINfo = that.data.cartInfo;
  404. var BargainId = 0;
  405. var combinationId = 0;
  406. cartINfo.forEach(function (value, index, cartINfo){
  407. BargainId = cartINfo[index].bargain_id,
  408. combinationId = cartINfo[index].combination_id
  409. })
  410. that.setData({
  411. BargainId: BargainId,
  412. combinationId:combinationId
  413. })
  414. // console.log(that.data.BargainId);
  415. // console.log(that.data.seckillId);
  416. // console.log(that.data.combinationId);
  417. },
  418. /**
  419. * 生命周期函数--监听页面初次渲染完成
  420. */
  421. onReady: function () {
  422. },
  423. /**
  424. * 生命周期函数--监听页面显示
  425. */
  426. onShow: function () {
  427. },
  428. /**
  429. * 生命周期函数--监听页面隐藏
  430. */
  431. onHide: function () {
  432. },
  433. /**
  434. * 生命周期函数--监听页面卸载
  435. */
  436. onUnload: function () {
  437. },
  438. /**
  439. * 页面相关事件处理函数--监听用户下拉动作
  440. */
  441. onPullDownRefresh: function () {
  442. },
  443. /**
  444. * 页面上拉触底事件的处理函数
  445. */
  446. onReachBottom: function () {
  447. },
  448. /**
  449. * 用户点击右上角分享
  450. */
  451. onShareAppMessage: function () {
  452. }
  453. })