index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. // index.js
  2. let myPro = require("../../utils/wxRequest.js");
  3. let util = require("../../utils/util.js");
  4. Page({
  5. data: {
  6. couponBg: "/statics/img/quan_bg_tu.png",
  7. checkedCard: true, // 默认选择余额付
  8. result: [],
  9. checkedWechat: false,
  10. showCashPop: false, // 会员卡钱不足时
  11. couponIdArr: [], // 优惠券选中 数组
  12. couponId:0,
  13. showCoupons: false,
  14. cardList: [], // 充值券
  15. userMoney: 0, // 用户余额
  16. totalPrice: 0, // 订单总金额
  17. fare_type: 0, // 配送方式 0外卖 1自提
  18. order_id: null, // 订单id(未支付订单)
  19. loading: false, // 全局loading
  20. is_member: getApp().globalData.is_member, // 会员 0否 1是
  21. addLock: false // 测试锁
  22. },
  23. onLoad: function (options) {
  24. // Do some initialize when page load.
  25. let that = this;
  26. // 配送方式
  27. if(options.fare_type){
  28. that.setData({
  29. fare_type: options.fare_type
  30. })
  31. };
  32. // 地址
  33. if(options.address_id){
  34. that.setData({
  35. address_id: options.address_id
  36. })
  37. };
  38. if(options.order_id){
  39. that.setData({
  40. order_id: options.order_id
  41. });
  42. that.getOrderDetail();
  43. };
  44. that.setData({
  45. couponBg: "data:image/png;base64," + wx.getFileSystemManager().readFileSync(that.data.couponBg, "base64")
  46. });
  47. },
  48. onShow: function () {
  49. // Do something when page show.
  50. let that = this;
  51. // 店铺
  52. if(wx.getStorageSync('store')){
  53. that.setData({
  54. store: JSON.parse(wx.getStorageSync('store'))
  55. });
  56. };
  57. // 获取用户余额
  58. that.getUserCash();
  59. // 获取充值卡
  60. that.getRechargeCard();
  61. // 获取券(只支持初次订单,不支持二次支付订单)
  62. if(!that.data.order_id && !that.data.addLock){
  63. that.getCreatelist();
  64. };
  65. // console.log('show=====');
  66. },
  67. onReady: function () {
  68. // Do something when page ready.
  69. },
  70. onHide: function () {
  71. // Do something when page hide.
  72. },
  73. onUnload: function () {
  74. // Do something when page close.
  75. },
  76. onPullDownRefresh: function () {
  77. // Do something when pull down.
  78. },
  79. onReachBottom: function () {
  80. // Do something when page reach bottom.
  81. },
  82. onPageScroll: function () {
  83. // Do something when page scroll
  84. },
  85. onResize: function () {
  86. // Do something when page resize
  87. },
  88. // 获取可用优惠券(同时总了总价等)
  89. getCreatelist(){
  90. let that = this;
  91. let params = {
  92. fare_type: that.data.fare_type
  93. };
  94. if(that.data.fare_type == 0){
  95. params.address_id = that.data.address_id
  96. };
  97. if(wx.getStorageSync('user_mark')){
  98. that.setData({
  99. user_mark: wx.getStorageSync('user_mark')
  100. });
  101. params.user_mark = that.data.user_mark;
  102. };
  103. if(that.data.couponId){
  104. params.user_coupon_id = that.data.couponId;
  105. };
  106. // return;
  107. myPro.wxRequest("user/v3/order/createlist","GET",params).then(res=>{
  108. let couponList = res.result.userCouponList;
  109. that.setData({
  110. totalPrice: res.result.orderInfo.pay_price,
  111. couponList: couponList,
  112. couponNums: couponList.length // 几张券
  113. });
  114. }).catch(err=>{
  115. console.log('报错信息',err);
  116. wx.showToast({
  117. title: err,
  118. icon: "none"
  119. })
  120. })
  121. },
  122. // 获取用户的余额
  123. getUserCash(){
  124. let that = this;
  125. myPro.wxRequest("user/v2/userinfo","GET",{}).then(res=>{
  126. that.setData({
  127. userMoney: parseFloat(res.result.money)
  128. });
  129. }).catch(err=>{
  130. console.log('报错信息',err);
  131. wx.showToast({
  132. title: err,
  133. icon: 'none'
  134. });
  135. });
  136. },
  137. // 选卡付
  138. changeCard(){
  139. let that = this;
  140. that.setData({
  141. checkedCard: !that.data.checkedCard
  142. });
  143. // 余额和微信方式只能选一种
  144. if(that.data.checkedCard){
  145. that.setData({
  146. checkedWechat: false
  147. })
  148. };
  149. },
  150. // 选微信付
  151. changeWechat(){
  152. let that = this;
  153. that.setData({
  154. checkedWechat: !that.data.checkedWechat
  155. });
  156. // 余额和微信方式只能选一种
  157. if(that.data.checkedWechat){
  158. that.setData({
  159. checkedCard: false
  160. })
  161. };
  162. },
  163. // 创建订单前的相关判断
  164. confirmCreateOrder(){
  165. let that = this;
  166. // wx.redirectTo({
  167. // url: '/pages/pay-success/index?order_id=176',
  168. // complete: function(){
  169. // // 清掉备注
  170. // // wx.removeStorageSync("user_mark");
  171. // }
  172. // });
  173. // return;
  174. // that.setData({
  175. // loading: true
  176. // });
  177. // 是否勾选支付方式
  178. if(!that.data.checkedCard && !that.data.checkedWechat ){
  179. wx.showToast({
  180. title: "请选择支付方式",
  181. icon: "none"
  182. });
  183. // that.setData({
  184. // loading: false
  185. // });
  186. return;
  187. };
  188. // 判断支付方式
  189. let pay_type = that.data.checkedWechat ? 1 : 2; //1微信 2充值卡
  190. that.setData({
  191. pay_type: pay_type
  192. });
  193. // 余额不足以支付时
  194. if( that.data.pay_type == 2 && that.data.userMoney < that.data.totalPrice){
  195. that.setData({
  196. showCashPop: true
  197. });
  198. // that.setData({
  199. // loading: false
  200. // });
  201. return;
  202. };
  203. // 余额付
  204. if(that.data.pay_type == 2){
  205. if(that.data.order_id){
  206. that.orderPay(2);
  207. }else{
  208. that.createOrder(2);
  209. }
  210. };
  211. // 微信付
  212. if(that.data.pay_type == 1){
  213. if(that.data.order_id){
  214. that.orderPay(1);
  215. }else{
  216. that.createOrder(1);
  217. }
  218. };
  219. },
  220. // 创建订单
  221. createOrder(pay_type){
  222. // pay_type 有三种类型 1微信 2余额 3混合
  223. let that = this;
  224. let data = {
  225. pay_type: pay_type,
  226. fare_type: that.data.fare_type
  227. };
  228. // 收货地址
  229. if(that.data.fare_type == 0 && that.data.address_id){
  230. data.address_id = that.data.address_id
  231. };
  232. // 券
  233. if(that.data.couponId){
  234. data.user_coupon_id = that.data.couponId
  235. };
  236. // 备注
  237. if(that.data.user_mark){
  238. data.user_mark = that.data.user_mark
  239. };
  240. // 创建订单
  241. myPro.wxRequest("user/v3/order/create","POST",data).then(res=>{
  242. let order = res.result.order;
  243. if(res.result.config){
  244. that.goWxPay(order,res.result.config)
  245. }else{
  246. // 不需要调微信支付,直接创建成功
  247. that.onCreateOrderAfter(order,1)
  248. }
  249. }).catch(err=>{
  250. console.log('报错信息',err);
  251. wx.showToast({
  252. title: err,
  253. icon: "none"
  254. });
  255. })
  256. },
  257. // 余额不足,加微信支付
  258. onAddWechat(){
  259. let that = this;
  260. // 混合付
  261. if(that.data.order_id){
  262. that.orderPay(3);
  263. }else{
  264. that.createOrder(3);
  265. };
  266. that.setData({
  267. showCashPop: false
  268. });
  269. },
  270. // 余额不足,充值
  271. onRecharge(){
  272. let that = this;
  273. that.setData({
  274. showCashPop: false
  275. });
  276. },
  277. // 调起微信
  278. goWxPay(order,config) {
  279. let that = this;
  280. const configArr = JSON.parse(config);
  281. wx.requestPayment({
  282. timeStamp: configArr.timeStamp,
  283. nonceStr: configArr.nonceStr,
  284. package: configArr.package,
  285. signType: configArr.signType,
  286. paySign: configArr.paySign,
  287. success(res) {
  288. // console.log(res);
  289. // 执行支付成功后续操作
  290. that.onCreateOrderAfter(order,1);
  291. },
  292. fail(res) {
  293. // console.error(res);
  294. if ( res.errMsg == 'requestPayment:fail cancel' ) {
  295. that.setData({
  296. addLock: true // 为了避免付款后,又进了onshow,影响了调优惠券的接口
  297. });
  298. wx.showToast({
  299. title: "您取消了支付,请到我的订单中继续付款",
  300. icon: "none"
  301. });
  302. } else {
  303. wx.showToast({
  304. title: "支付失败,请到我的订单中继续付款",
  305. icon: "none"
  306. });
  307. }
  308. // 订单创建成功,但取消了支付 -1
  309. that.onCreateOrderAfter(order,-1);
  310. }
  311. });
  312. },
  313. // 支付后的处理
  314. onCreateOrderAfter(order,type){
  315. let that = this;
  316. // 清掉下单时的备注
  317. wx.removeStorageSync("user_mark");
  318. // type 1成功 -1失败
  319. if(type == 1){
  320. that.setData({
  321. addLock: true // 为了避免付款后,又进了onshow,影响了调优惠券的接口
  322. });
  323. wx.redirectTo({
  324. url: '/pages/pay-success/index?order_id='+ order.id
  325. });
  326. }else{
  327. // 支付失败后 跳转到订单详情(此时订单已创建成功)
  328. setTimeout(function(){
  329. wx.redirectTo({
  330. url: '/pages/user/order/detail/index?order_id='+order.id
  331. });
  332. },1000)
  333. }
  334. },
  335. // 打开优惠券弹层
  336. onShowCoupons(){
  337. let that = this;
  338. that.setData({
  339. showCoupons: true
  340. });
  341. },
  342. // 获取当前点击的优惠券
  343. onClickCoupons(event){
  344. let that = this;
  345. let item = event.currentTarget.dataset.item;
  346. if(that.data.couponId == item.id){
  347. that.setData({
  348. couponIdArr: [],
  349. couponId:0,
  350. checkedCoupon: null
  351. });
  352. // 更新金额
  353. that.getCreatelist();
  354. }
  355. else{
  356. that.setData({
  357. couponIdArr: [''+item.id],
  358. couponId:item.id
  359. });
  360. if(that.data.couponId == item.id){
  361. that.setData({
  362. checkedCoupon: item,
  363. showCoupons: false,
  364. });
  365. // 更新金额
  366. that.getCreatelist();
  367. }else{
  368. that.setData({
  369. checkedCoupon: null,
  370. showCoupons: false
  371. });
  372. }
  373. }
  374. // console.log('选中的优惠券',that.data.checkedCoupon)
  375. },
  376. // 关闭优惠券弹层
  377. onCloseCoupons(){
  378. let that = this;
  379. that.setData({
  380. showCoupons: false
  381. });
  382. },
  383. // 获取订单详情
  384. getOrderDetail(){
  385. let that = this;
  386. let params = {
  387. order_id: that.data.order_id
  388. };
  389. myPro.wxRequest("user/v2/order/detail","GET",params).then(res=>{
  390. that.setData({
  391. totalPrice: res.result.pay_price
  392. });
  393. }).catch(err=>{
  394. console.log('报错信息',err);
  395. wx.showToast({
  396. title: err,
  397. icon: "none"
  398. });
  399. })
  400. },
  401. // 未支付订单,进行支付
  402. orderPay(pay_type){
  403. let that = this;
  404. let params = {
  405. order_id: that.data.order_id,
  406. pay_type: pay_type
  407. };
  408. myPro.wxRequest("user/v2/order/topay","POST",params).then(res=>{
  409. let order = res.result.order;
  410. // res.result.type 1余额支付成功 2走微信
  411. if(res.result.config){
  412. that.goWxPay(order,res.result.config);
  413. }else{
  414. that.onCreateOrderAfter(order,1);
  415. }
  416. }).catch(err=>{
  417. console.log('报错信息',err);
  418. wx.showToast({
  419. title: err,
  420. icon: "none"
  421. })
  422. });
  423. },
  424. // 获取充值卡
  425. getRechargeCard(){
  426. let that = this;
  427. let params = {
  428. type: 1, // 1返回3条数据 2返回全部数据
  429. card_type: 1 // 1普通 2年卡(会员卡)
  430. };
  431. myPro.wxRequest("user/v2/rechargecardlist","GET",params).then(res=>{
  432. let list = res.result;
  433. for(let i in list){
  434. list[i].checked = false;
  435. // 卡+券的总金额 (都按代金券走,折扣券不考虑)
  436. if(list[i].coupon_rule){
  437. list[i].priceAddCoupons = ((list[i].coupon_rule.coupon.price * list[i].coupon_rule.num) + parseFloat(list[i].price)).toFixed(2)
  438. }else{
  439. list[i].priceAddCoupons = list[i].price
  440. }
  441. };
  442. that.setData({
  443. cardList: list
  444. });
  445. }).catch(err=>{
  446. console.log('报错信息',err);
  447. wx.showToast({
  448. title: err,
  449. icon: "none"
  450. })
  451. })
  452. },
  453. // 选择充值卡
  454. clickCard(event){
  455. let that = this;
  456. let id = event.currentTarget.dataset.id;
  457. let list = that.data.cardList;
  458. // 测试(模拟流程,能跑了再删)
  459. // that.setData({
  460. // is_member: 1
  461. // })
  462. // that.getUserCash();
  463. // return;
  464. for(let i in list){
  465. if(list[i].id == id){
  466. list[i].checked = !list[i].checked;
  467. // 选中状态,则去购卡
  468. if(list[i].checked){
  469. that.nextFun(list[i].id);
  470. }
  471. }else{
  472. list[i].checked = false
  473. }
  474. };
  475. that.setData({
  476. cardList: list
  477. });
  478. },
  479. // 充值
  480. nextFun(id){
  481. let that = this;
  482. let params = {
  483. recharge_card_id: id,
  484. store_id: that.data.store ? that.data.store.id : 1 // 门店id
  485. }
  486. myPro.wxRequest("user/recharge","POST",params).then(res=>{
  487. let config = res.result.config;
  488. that.wxPayCard(JSON.parse(config));
  489. }).catch(err=>{
  490. that.setData({
  491. loading: false
  492. });
  493. console.log('报错信息',err);
  494. wx.showToast({
  495. title: err,
  496. icon: "none"
  497. })
  498. })
  499. },
  500. // 卡充值微信付款
  501. wxPayCard: function(result) {
  502. let that = this;
  503. wx.requestPayment({
  504. timeStamp: result.timeStamp,
  505. nonceStr: result.nonceStr,
  506. package: result.package,
  507. signType: result.signType,
  508. paySign: result.paySign,
  509. success(res) {
  510. wx.showToast({
  511. title: "充值成功",
  512. icon: "none"
  513. });
  514. // 拉取用户余额
  515. that.getUserCash();
  516. // 获取券(非商品待支付订单)
  517. if(!that.data.order_id){
  518. that.getCreatelist();
  519. };
  520. },
  521. fail(res) {
  522. wx.showToast({
  523. title: "充值失败",
  524. icon: "none"
  525. });
  526. console.log("报错信息",res)
  527. }
  528. });
  529. },
  530. });