index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. var app = getApp();
  2. const util = require('../../utils/util.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. textareaStatus:true,
  9. parameter: {
  10. 'navbar': '1',
  11. 'return': '1',
  12. 'title': '提交订单'
  13. },
  14. //支付方式
  15. cartArr: [
  16. { "name": "微信支付", "icon": "icon-weixin2", value: 'weixin', title: '微信快捷支付' },
  17. { "name": "余额支付", "icon": "icon-icon-test", value: 'yue',title:'可用余额:'},
  18. ],
  19. payType:'weixin',//支付方式
  20. openType:1,//优惠券打开方式 1=使用
  21. active:0,//支付方式切换
  22. coupon: { coupon: false, list: [], statusTile:'立即使用'},//优惠券组件
  23. address: {address: false},//地址组件
  24. addressInfo:{},//地址信息
  25. pinkId:0,//拼团id
  26. addressId:0,//地址id
  27. couponId:0,//优惠券id
  28. cartId:'',//购物车id
  29. userInfo:{},//用户信息
  30. mark:'',//备注信息
  31. couponTitle:'请选择',//优惠券
  32. coupon_price:0,//优惠券抵扣金额
  33. useIntegral:false,//是否使用积分
  34. integral_price:0,//积分抵扣金额
  35. ChangePrice:0,//使用积分抵扣变动后的金额
  36. formIds:[],//收集formid
  37. status:0,
  38. is_address:false,
  39. isClose:false,
  40. toPay:false,//修复进入支付时页面隐藏从新刷新页面
  41. },
  42. /**
  43. * 授权回调事件
  44. *
  45. */
  46. onLoadFun:function(){
  47. this.getaddressInfo();
  48. this.getConfirm();
  49. //调用子页面方法授权后执行获取地址列表
  50. this.selectComponent('#address-window').getAddressList();
  51. },
  52. /**
  53. * 生命周期函数--监听页面显示
  54. */
  55. onShow: function () {
  56. this.setData({ textareaStatus: true });
  57. if (app.globalData.isLog && this.data.isClose && this.data.toPay==false) {
  58. this.getaddressInfo();
  59. this.selectComponent('#address-window').getAddressList();
  60. }
  61. },
  62. /**
  63. * 生命周期函数--监听页面隐藏
  64. */
  65. onHide: function () {
  66. this.setData({ isClose: true });
  67. },
  68. ChangCouponsClone:function(){
  69. this.setData({'coupon.coupon':false});
  70. },
  71. changeTextareaStatus:function(){
  72. for (var i = 0, len = this.data.coupon.list.length; i < len;i++){
  73. this.data.coupon.list[i].use_title='';
  74. this.data.coupon.list[i].is_use = 0;
  75. }
  76. this.setData({ textareaStatus: true, status: 0, "coupon.list": this.data.coupon.list});
  77. },
  78. /**
  79. * 处理点击优惠券后的事件
  80. *
  81. */
  82. ChangCoupons:function(e){
  83. var index = e.detail, list = this.data.coupon.list, couponTitle = '请选择', couponId = 0, coupon_price = 0, totalPrice = 0,
  84. change_coupon_price=0;
  85. for (var i = 0, len = list.length; i < len; i++) {
  86. if(i != index){
  87. list[i].use_title = '';
  88. list[i].is_use = 0;
  89. }
  90. //获取当前优惠券抵扣金额
  91. if (list[i].id == this.data.couponId) change_coupon_price = list[i].coupon_price;
  92. }
  93. if (this.data.totalPrice <= 0 && this.data.status != 1) return app.Tips({title:'支付金额为0无法使用优惠卷!'});
  94. if (this.data.status==1 || this.data.is_address) {
  95. this.setData({ totalPrice: this.data.priceGroup.totalPrice });
  96. }else{
  97. //使用优惠券抵扣前先把之前的抵扣金额加回去
  98. this.setData({ totalPrice: util.$h.Add(this.data.totalPrice, change_coupon_price) });
  99. }
  100. if (list[index].is_use) {
  101. //不使用优惠券
  102. list[index].use_title = '';
  103. list[index].is_use = 0;
  104. totalPrice = this.data.totalPrice;
  105. //用户取消使用优惠卷但是使用了积分抵扣
  106. if (totalPrice > 0 && this.data.useIntegral && !this.data.is_Integral)
  107. {
  108. totalPrice = this.changeCouponPrice(totalPrice, this.data.userInfo.integral);
  109. this.setData({is_Integral:true});
  110. }
  111. this.data.status = 0;
  112. } else {
  113. //使用优惠券
  114. list[index].use_title = '不使用';
  115. list[index].is_use = 1;
  116. couponTitle = list[index].coupon_title;
  117. couponId = list[index].id;
  118. coupon_price = list[index].coupon_price;
  119. //使用积分抵扣,使用优惠券金额大于当前支付金额
  120. if (this.data.totalPrice < coupon_price && this.data.useIntegral){
  121. //超出金额
  122. var changePrice = util.$h.Sub(coupon_price, this.data.totalPrice);
  123. //超出回退积分
  124. var changeIntegral = util.$h.Div(changePrice, this.data.integralRatio);
  125. //回退积分和积分抵扣金额
  126. this.setData({
  127. integral: util.$h.Add(this.data.integral, changeIntegral),
  128. integral_price: util.$h.Sub(this.data.integral_price, changePrice)
  129. });
  130. totalPrice=0;
  131. this.data.status=0;
  132. } else if (this.data.totalPrice < coupon_price && !this.data.useIntegral){
  133. //使用优惠券金额大于当前支付金额
  134. totalPrice=0;
  135. this.data.status=1;
  136. } else if (this.data.totalPrice > coupon_price && this.data.useIntegral){
  137. //支付金额大于优惠券金额并且使用了积分
  138. totalPrice = util.$h.Sub(this.data.totalPrice, list[index].coupon_price);
  139. //当前优惠券大于0的时候再去减去可兑换的金额
  140. if (this.data.integral > 0) totalPrice = this.changeCouponPrice(totalPrice, this.data.integral);
  141. this.data.status = 0;
  142. } else if (this.data.totalPrice > coupon_price && !this.data.useIntegral){
  143. //支付金额大于优惠券金额没有使用积分
  144. totalPrice = util.$h.Sub(this.data.totalPrice, list[index].coupon_price);
  145. this.data.status = 0;
  146. }
  147. }
  148. this.setData({
  149. couponTitle: couponTitle,
  150. couponId: couponId,
  151. 'coupon.coupon': false,
  152. "coupon.list":list,
  153. coupon_price: coupon_price,
  154. totalPrice: totalPrice,
  155. status: this.data.status,
  156. });
  157. },
  158. /**
  159. * 处理点击优惠券后支付金额和积分变动
  160. * @param string | float totalPrice 当前支付金额
  161. * @return float totalPrice 当前支付金额
  162. *
  163. */
  164. changeCouponPrice: function (totalPrice, integral){
  165. var changePrice = util.$h.Mul(this.data.integralRatio,integral);
  166. this.data.integral_price=0;
  167. if (changePrice > totalPrice) {
  168. //超出金额
  169. var minParice = util.$h.Sub(changePrice, totalPrice);
  170. //超出积分
  171. var changeIntegral = util.$h.Div(minParice, this.data.integralRatio);
  172. //抵扣金额需要原本的订单金额
  173. this.setData({ integral: changeIntegral, integral_price: this.data.totalPrice });
  174. //超出金额当前支付金额为0
  175. totalPrice=0;
  176. } else {
  177. this.setData({ integral: 0, integral_price: util.$h.Add(this.data.integral_price, changePrice) });
  178. totalPrice = util.$h.Sub(totalPrice, changePrice);
  179. }
  180. return totalPrice;
  181. },
  182. /**
  183. * 使用积分抵扣
  184. */
  185. ChangeIntegral:function(){
  186. var integral=parseFloat(this.data.integral);
  187. if (this.data.userInfo.integral <= 0) return app.Tips({ title: '您当前积分为较低不能使用抵扣' }, function () {
  188. this.setData({ useIntegral:false });
  189. }.bind(this));
  190. if (this.data.totalPrice <= 0 && !this.data.useIntegral) return app.Tips({title:'当前支付金额不能在使用积分抵扣啦~'},function(){
  191. this.setData({ useIntegral: false });
  192. }.bind(this));
  193. this.setData({useIntegral:!this.data.useIntegral});
  194. //使用积分抵扣时
  195. if (this.data.useIntegral){
  196. var changePrice = util.$h.Mul(this.data.integralRatio, integral);
  197. if (changePrice > this.data.totalPrice){
  198. //超出金额
  199. var minParice = util.$h.Sub(changePrice, this.data.totalPrice);
  200. //超出积分
  201. var changeIntegral = util.$h.Div(minParice, this.data.integralRatio);
  202. //超出当前金额支付金额为0,积分抵扣金额为当前支付金额,积分剩余等于超出积分
  203. this.setData({ integral: changeIntegral, integral_price:this.data.totalPrice,totalPrice:0});
  204. }else{
  205. this.setData({ integral: 0, integral_price: changePrice, totalPrice: util.$h.Sub(this.data.totalPrice, changePrice)});
  206. }
  207. }else{
  208. var integral_price = this.data.integral_price;
  209. //不使用积分返回原始数据
  210. this.setData({ integral_price: 0, integral: this.data.userInfo.integral, totalPrice: util.$h.Add(this.data.totalPrice, integral_price.toString())});
  211. }
  212. },
  213. /**
  214. * 选择地址后改变事件
  215. * @param object e
  216. */
  217. OnChangeAddress:function(e){
  218. this.setData({ textareaStatus:true,addressId: e.detail,'address.address':false});
  219. this.getaddressInfo();
  220. },
  221. /**
  222. * 生命周期函数--监听页面加载
  223. */
  224. onLoad: function (options) {
  225. if (!options.cartId) return app.Tips({ title:'请选择要购买的商品'},{tab:3,url:1});
  226. this.setData({
  227. couponId: options.couponId || 0,
  228. pinkId: options.pinkId ? parseInt(options.pinkId) : 0,
  229. addressId: options.addressId || 0,
  230. cartId: options.cartId,
  231. is_address: options.is_address ? true : false,
  232. });
  233. },
  234. bindHideKeyboard: function (e) {
  235. this.setData({mark: e.detail.value});
  236. },
  237. /**
  238. * 获取当前订单详细信息
  239. *
  240. */
  241. getConfirm:function(){
  242. var that=this;
  243. app.basePost(app.U({ c: 'auth_api', a: 'confirm_order' }), { cartId: this.data.cartId},function(res){
  244. that.setData({
  245. userInfo: res.data.userInfo,
  246. integral: res.data.userInfo.integral,
  247. cartInfo: res.data.cartInfo,
  248. integralRatio: res.data.integralRatio,
  249. offlinePostage: res.data.offlinePostage,
  250. orderKey: res.data.orderKey,
  251. priceGroup: res.data.priceGroup,
  252. totalPrice: app.help().Add(parseFloat(res.data.priceGroup.totalPrice), parseFloat(res.data.priceGroup.storePostage)),
  253. cartId: res.data.cartId,
  254. seckillId: parseInt(res.data.seckill_id),
  255. usableCoupon: res.data.usableCoupon
  256. });
  257. that.data.cartArr[1].title ='可用余额:'+ res.data.userInfo.now_money;
  258. that.setData({ cartArr: that.data.cartArr, ChangePrice:that.data.totalPrice});
  259. that.getBargainId();
  260. that.getCouponList();
  261. },function(res){
  262. return app.Tips({title:res.msg},{tab:3,url:1});
  263. });
  264. },
  265. /*
  266. * 提取砍价和拼团id
  267. */
  268. getBargainId: function () {
  269. var that = this;
  270. var cartINfo = that.data.cartInfo;
  271. var BargainId = 0;
  272. var combinationId = 0;
  273. cartINfo.forEach(function (value, index, cartINfo) {
  274. BargainId = cartINfo[index].bargain_id,
  275. combinationId = cartINfo[index].combination_id
  276. })
  277. that.setData({ BargainId: parseInt(BargainId), combinationId: parseInt(combinationId)});
  278. },
  279. /**
  280. * 获取当前金额可用优惠券
  281. *
  282. */
  283. getCouponList:function(){
  284. var that=this;
  285. app.baseGet(app.U({ c: "coupons_api", a: 'get_use_coupon_order', q: { totalPrice:this.data.totalPrice }}),function(res){
  286. that.setData({ 'coupon.list': res.data, openType:1});
  287. });
  288. },
  289. /*
  290. * 获取默认收货地址或者获取某条地址信息
  291. */
  292. getaddressInfo:function(){
  293. var that=this;
  294. var url = that.data.addressId ?
  295. app.U({ c: 'user_api', a: 'get_user_address', q: { addressId: that.data.addressId } }) :
  296. app.U({ c: 'user_api', a:'user_default_address'});
  297. app.baseGet(url,function(res){
  298. res.data.is_default = parseInt(res.data.is_default);
  299. that.setData({ addressInfo: res.data || {}, addressId: res.data.id || 0, 'address.addressId': res.data.id || 0});
  300. });
  301. },
  302. payItem:function(e){
  303. var that = this;
  304. var active = e.currentTarget.dataset.index;
  305. that.setData({
  306. active: active,
  307. animated: true,
  308. payType: that.data.cartArr[active].value,
  309. })
  310. setTimeout(function () {
  311. that.car();
  312. }, 500);
  313. },
  314. coupon: function () {
  315. this.setData({
  316. 'coupon.coupon': true
  317. })
  318. },
  319. car: function () {
  320. var that = this;
  321. that.setData({
  322. animated: false
  323. });
  324. },
  325. address:function(){
  326. this.setData({
  327. textareaStatus:false,
  328. 'address.address': true,
  329. pagesUrl: '/pages/user_address_list/index?cartId=' + this.data.cartId + '&pinkId=' + this.data.pinkId + '&couponId=' + this.data.couponId
  330. });
  331. },
  332. SubOrder:function(e){
  333. var formId = e.detail.formId, that = this, data={};
  334. if (!this.data.payType) return app.Tips({title:'请选择支付方式'});
  335. if (!this.data.addressId) return app.Tips({ title:'请选择收货地址'});
  336. data={
  337. addressId: that.data.addressId,
  338. formId: formId,
  339. couponId: that.data.couponId,
  340. payType: that.data.payType,
  341. useIntegral: that.data.useIntegral,
  342. bargainId: that.data.BargainId,
  343. combinationId: that.data.combinationId,
  344. pinkId: that.data.pinkId,
  345. seckill_id: that.data.seckillId,
  346. mark: that.data.mark
  347. };
  348. if (data.payType == 'yue' && parseFloat(that.data.userInfo.now_money) < parseFloat(that.data.totalPrice)) return app.Tips({title:'余额不足!'});
  349. wx.showLoading({ title: '订单支付中'});
  350. app.basePost(app.U({ c: 'auth_api', a:'create_order',q:{key:this.data.orderKey}}),data,function(res){
  351. var status = res.data.status, orderId = res.data.result.orderId, jsConfig = res.data.result.jsConfig,
  352. goPages = '/pages/order_pay_status/index?order_id=' + orderId+'&msg='+res.msg;
  353. switch (status){
  354. case 'ORDER_EXIST': case 'EXTEND_ORDER': case 'PAY_ERROR':
  355. wx.hideLoading();
  356. return app.Tips({ title: res.msg}, { tab: 5, url: goPages });
  357. break;
  358. case 'SUCCESS':
  359. wx.hideLoading();
  360. if (that.data.BargainId || that.data.combinationId || that.data.pinkId || that.data.seckillId) return app.Tips({ title: res.msg, icon: 'success' }, { tab: 4, url: goPages });
  361. return app.Tips({ title: res.msg,icon: 'success' }, { tab: 5, url: goPages });
  362. break;
  363. case 'WECHAT_PAY':
  364. that.setData({toPay:true});
  365. wx.requestPayment({
  366. timeStamp: jsConfig.timestamp,
  367. nonceStr: jsConfig.nonceStr,
  368. package: jsConfig.package,
  369. signType: jsConfig.signType,
  370. paySign: jsConfig.paySign,
  371. success: function (res) {
  372. wx.hideLoading();
  373. if (that.data.BargainId || that.data.combinationId || that.data.pinkId || that.data.seckillId) return app.Tips({ title: '支付成功', icon: 'success' }, { tab: 4, url: goPages });
  374. return app.Tips({ title: '支付成功', icon:'success' }, { tab: 5, url: goPages });
  375. },
  376. fail:function(e){
  377. wx.hideLoading();
  378. return app.Tips({ title: '取消支付' }, { tab: 5, url: goPages +'&status=2'});
  379. },
  380. complete:function(e){
  381. wx.hideLoading();
  382. //关闭当前页面跳转至订单状态
  383. if (res.errMsg == 'requestPayment:cancel') return app.Tips({ title: '取消支付' }, { tab: 5, url: goPages + '&status=2'});
  384. },
  385. })
  386. break;
  387. case 'PAY_DEFICIENCY':
  388. wx.hideLoading();
  389. //余额不足
  390. return app.Tips({ title: res.msg}, { tab: 5, url: goPages+'&status=1' });
  391. break;
  392. }
  393. });
  394. }
  395. })