confirmProjectOrder.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. // pages/confirmProjectOrder/confirmProjectOrder.js
  2. import {
  3. get,
  4. post
  5. } from '../../utils/http'
  6. const app = getApp()
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. currentCoupon: -1,
  13. visible: false,
  14. CouPonIndex: [],
  15. needSelect: false,
  16. coupSelectType: '',
  17. productDiscount: '',
  18. isSelectCoup: [], // 选择的优惠券
  19. // 下面是支付的数据
  20. pay_methon: 1,
  21. payNow: false,
  22. switchPay: 'product',
  23. showMoney: 0, // 价值总额
  24. showRuleText: 10086,
  25. dotSelectCard: false,
  26. marks: {
  27. // 优惠券类型,1直接抵扣券,2满减券,3全额减免券,4卡券
  28. 1: '抵扣券',
  29. 2: '满减券',
  30. 3: '全额减免券',
  31. 4: '项目卡券'
  32. },
  33. orderDiscount: 0,
  34. priceSet: 0,
  35. cardList: [], // 我的次卡对应项目列表
  36. selectCodeList: [], //选择的次卡的列表
  37. visibleCard: false,
  38. card_data: [], //选择次卡的项目id
  39. selectCardProjectList: [], //选择次卡的项目列表
  40. selectCoupPrice: 0, //选择优惠券之后的价格
  41. discountList: [], //折扣列表
  42. level: '', //用户充值折扣等级 (0是未拥有等级)
  43. levelList: {
  44. 1: '0.98',
  45. 4: '0.95',
  46. 7: '0.90',
  47. }, //折扣列表
  48. levelPrice: '', //充值等级折扣优惠价格
  49. },
  50. // 获取用户充值折扣等级
  51. getLevel() {
  52. get('/v2/api/user/discount_level', {}, (res) => {
  53. if (res.code == 200) {
  54. if (res.data == 0) {
  55. this.setData({
  56. level: 0
  57. })
  58. return
  59. }
  60. let getValue = (item, val) => {
  61. return item[val]
  62. }
  63. this.setData({
  64. level: Number(getValue(this.data.levelList, res.data))
  65. })
  66. this.setData({
  67. tolevel: (this.data.level * 10).toFixed(1)
  68. })
  69. }
  70. })
  71. },
  72. clickRuleText(e) {
  73. let that = this
  74. let index = e.currentTarget.dataset.index
  75. if (this.data.showRuleText == index) {
  76. that.setData({
  77. showRuleText: 10086
  78. })
  79. } else {
  80. that.setData({
  81. showRuleText: index
  82. })
  83. }
  84. },
  85. /**
  86. * 点击使用按钮、勾选按钮
  87. */
  88. checkSelect: function (e) {
  89. // let index = e.currentTarget.dataset.index
  90. let CouPonIndex = this.data.productDiscount
  91. let selectIndex = e.currentTarget.dataset.index
  92. let project_list = this.data.orderDetails.project_list //这里是项目列表
  93. let project_ids = this.data.productDiscount[selectIndex].project_ids
  94. let _ids = 'productDiscount[' + selectIndex + '].project_ids'
  95. let isReturn = false
  96. //去掉已选择次卡的项目id
  97. this.data.card_data.forEach(res => {
  98. if (project_ids.indexOf(res.id) > -1) {
  99. project_ids.forEach((e, i) => {
  100. if (res.id == e) {
  101. project_ids.splice(i, 1)
  102. }
  103. })
  104. }
  105. })
  106. this.setData({
  107. _ids: project_ids
  108. })
  109. if (this.data.productDiscount[selectIndex].type != 4 && this.data.card_data.length != 0) {
  110. wx.showToast({
  111. title: '该项目已选择次卡',
  112. icon: 'none'
  113. })
  114. return
  115. }
  116. this.data.selectCardProjectList.forEach(res => {
  117. if (this.data.productDiscount[selectIndex].project_ids.indexOf(res.id) > -1) {
  118. wx.showToast({
  119. title: '该项目已选择次卡',
  120. icon: 'none'
  121. })
  122. isReturn = true
  123. }
  124. })
  125. if (isReturn) {
  126. return
  127. }
  128. let curreValue = !CouPonIndex[selectIndex].isSelect
  129. // 判断是否是不相同的项目券
  130. if (this.data.productDiscount[selectIndex].type != 4) {
  131. this.setData({
  132. priceSet: 0,
  133. showMoney: this.data.orderDetails.price
  134. })
  135. for (let i = 0; i < CouPonIndex.length; i++) {
  136. CouPonIndex[i].isSelect = false
  137. }
  138. for (let i = 0; i < project_list.length; i++) {
  139. project_list[i].cardId = ''
  140. }
  141. } else if (this.data.coupSelectType != 4) {
  142. for (let i = 0; i < CouPonIndex.length; i++) {
  143. CouPonIndex[i].isSelect = false
  144. }
  145. for (let i = 0; i < project_list.length; i++) {
  146. project_list[i].cardId = ''
  147. }
  148. } else {
  149. let numberSelect = 0
  150. for (let i = 0; i < CouPonIndex.length; i++) {
  151. if (CouPonIndex[i].isSelect) {
  152. numberSelect = numberSelect + 1
  153. }
  154. }
  155. console.log('numberSelect')
  156. console.log(numberSelect)
  157. if (numberSelect == this.data.orderDetails.project_list.length && curreValue) {
  158. wx.showToast({
  159. title: '最多选择' + this.data.orderDetails.project_list.length + '项目券!',
  160. icon: 'none'
  161. })
  162. return
  163. }
  164. }
  165. CouPonIndex[selectIndex].isSelect = curreValue
  166. if (!curreValue) {
  167. for (let j = 0; j < project_list.length; j++) {
  168. if (project_list[j].cardId && project_list[j].cardId == this.data.productDiscount[selectIndex].id) { // 如果是取消选择,那么也要取消绑定项目的id
  169. project_list[j].cardId = ''
  170. }
  171. }
  172. CouPonIndex[selectIndex].projectids = ''
  173. }
  174. let orderDetails1 = this.data.orderDetails
  175. orderDetails1.project_list = project_list
  176. this.setData({
  177. orderDetails: orderDetails1,
  178. productDiscount: CouPonIndex,
  179. coupSelectType: this.data.productDiscount[selectIndex].type
  180. })
  181. console.log(this.data.CouPonIndex)
  182. this.selectCoup()
  183. },
  184. // 计算总额
  185. calculateMoney() {
  186. let that = this
  187. let initPrice = 0
  188. if (this.data.selectCardProjectList.length > 0) {
  189. this.data.selectCardProjectList.forEach(res => {
  190. initPrice = Number(initPrice) + Number(res.price)
  191. })
  192. }
  193. console.log(initPrice, 'initPrice');
  194. let money = this.data.selectCardProjectList.length > 0 ? Number(this.data.orderDetails.price - initPrice) : this.data.orderDetails.price
  195. console.log(money, 'moneymoney');
  196. let isSelectCoup = this.data.isSelectCoup
  197. let project_list = this.data.orderDetails.project_list //这里是项目列表
  198. // if (isSelectCoup.length == 0) {
  199. // that.setData({
  200. // showMoney: money.toFixed(2)
  201. // })
  202. // return
  203. // }
  204. // 如果是项目
  205. if (isSelectCoup.length > 0) {
  206. if (isSelectCoup[0].type == 4) {
  207. let numberHaveCardId = 0
  208. for (let i = 0; i < project_list.length; i++) {
  209. if (project_list[i].cardId) {
  210. money = money - project_list[i].price
  211. numberHaveCardId = numberHaveCardId + 1
  212. }
  213. }
  214. } else if (isSelectCoup[0].type == 3) {
  215. for (let i = 0; i < project_list.length; i++) {
  216. money = money - project_list[i].price
  217. }
  218. } else {
  219. money = money - isSelectCoup[0].price
  220. }
  221. }
  222. /**
  223. * type:1项目,2设备分类,3耗材
  224. * discount_type:折扣条件,1数量/类型,2金额
  225. */
  226. let notSelectId = []
  227. this.data.isSelectCoup.forEach(item => {
  228. notSelectId.push(item.projectids)
  229. })
  230. //去除选择优惠券或者次卡的项目
  231. let _project_list = project_list.filter(res => {
  232. return notSelectId.toString().indexOf(res.id) && res.isCard == false
  233. })
  234. let _money = money
  235. console.log(money, 'money===', _money);
  236. let devicePrice = 0 //满足折扣设备的项目总金额
  237. let productPrice = 0 //耗材总金额
  238. //满足折扣设备的项目
  239. let deviceList = _project_list.filter(item => {
  240. return item.device_type_id == item.device_id
  241. })
  242. if (deviceList.length > 0) {
  243. deviceList.forEach(item => {
  244. devicePrice = devicePrice + Number(item.price)
  245. })
  246. }
  247. //拥有耗材的项目
  248. let productList = _project_list.filter(item => {
  249. return item.product_name != ""
  250. })
  251. console.log(productList, 'productList');
  252. if (productList.length > 0) {
  253. productList.forEach(item => {
  254. productPrice = productPrice + Number(item.product_price)
  255. })
  256. }
  257. let _priceSet = this.data.priceSet
  258. if (this.data.discountList.length > 0) {
  259. let discountList = this.data.discountList
  260. //计算所有的折扣金额
  261. discountList.forEach(item => {
  262. let priceSet = 0,
  263. initmoney = money,
  264. _initmoney = money
  265. if (item.type == 1 && item.discount_type == 1 && _project_list.length >= item.num) {
  266. initmoney = initmoney * item.discount_rate / 10
  267. priceSet = _initmoney - initmoney
  268. item["priceSet"] = priceSet
  269. } else if (item.type == 1 && item.discount_type == 2 && money > Number(item.amount)) {
  270. initmoney = initmoney * item.discount_rate / 10
  271. priceSet = _initmoney - initmoney
  272. item["priceSet"] = priceSet
  273. } else if (item.type == 2 && item.discount_type == 2 && devicePrice > Number(item.amount)) {
  274. //满足条件的项目折扣
  275. initmoney = (initmoney - devicePrice) + devicePrice * item.discount_rate / 10
  276. priceSet = _initmoney - initmoney
  277. item["priceSet"] = priceSet
  278. } else if (item.type == 3 && item.discount_type == 1 && productList.length >= item.num) {
  279. //满足条件的项目折扣
  280. initmoney = (initmoney - productPrice) + productPrice * item.discount_rate / 10
  281. priceSet = _initmoney - initmoney
  282. item["priceSet"] = priceSet
  283. } else if (item.type == 3 && item.discount_type == 2 && productPrice >= item.amount) {
  284. //满足条件的项目折扣
  285. initmoney = (initmoney - productPrice) + productPrice * item.discount_rate / 10
  286. priceSet = _initmoney - initmoney
  287. item["priceSet"] = priceSet
  288. }
  289. })
  290. console.log(discountList, 'discountList');
  291. //排序,最优折扣放第一位
  292. discountList = discountList.sort(this.compare("priceSet"))
  293. let discountInfo = discountList[0]
  294. if (discountInfo.type == 1 && discountInfo.discount_type == 1 && _project_list.length >= discountInfo.num) {
  295. money = money * discountInfo.discount_rate / 10
  296. _priceSet = _money - money
  297. } else if (discountInfo.type == 1 && discountInfo.discount_type == 2 && money > Number(discountInfo.amount)) {
  298. money = money * discountInfo.discount_rate / 10
  299. _priceSet = _money - money
  300. } else if (discountInfo.type == 2 && discountInfo.discount_type == 2 && devicePrice > Number(discountInfo.amount)) {
  301. //满足条件的项目折扣
  302. money = (money - devicePrice) + devicePrice * discountInfo.discount_rate / 10
  303. _priceSet = _money - money
  304. } else if (discountInfo.type == 3 && discountInfo.discount_type == 1 && productList.length >= discountInfo.num) {
  305. //满足条件的项目折扣
  306. money = (money - productPrice) + productPrice * discountInfo.discount_rate / 10
  307. _priceSet = _money - money
  308. } else if (discountInfo.type == 3 && discountInfo.discount_type == 2 && productPrice >= discountInfo.amount) {
  309. //满足条件的项目折扣
  310. money = (money - productPrice) + productPrice * discountInfo.discount_rate / 10
  311. _priceSet = _money - money
  312. }
  313. }
  314. console.log(_priceSet, '_priceSet');
  315. that.setData({
  316. showMoney: Number(money).toFixed(2) < 0 ? 0.00 : Number(money).toFixed(2),
  317. priceSet: Number(_priceSet).toFixed(2)
  318. })
  319. },
  320. //排序
  321. compare(property) {
  322. return function (obj1, obj2) {
  323. var value1 = obj1[property];
  324. var value2 = obj2[property];
  325. return value2 - value1; // 降序
  326. }
  327. },
  328. // 支付的操作
  329. onClosePay() {
  330. this.setData({
  331. payNow: false
  332. })
  333. },
  334. /**
  335. * 关闭弹框
  336. */
  337. onClose() {
  338. this.setData({
  339. visible: false
  340. })
  341. },
  342. // 关闭次卡弹窗
  343. onCloseCard() {
  344. this.setData({
  345. visibleCard: false
  346. })
  347. },
  348. // 点击x关闭次卡弹窗
  349. onCancelCard() {
  350. this.setData({
  351. visibleCard: false
  352. })
  353. },
  354. showCoup() {
  355. if (this.data.dotSelectCard) {
  356. wx.showToast({
  357. title: '本订单已使用优惠券!',
  358. icon: 'none'
  359. })
  360. return
  361. }
  362. this.setData({
  363. visible: true
  364. })
  365. },
  366. // 打开次卡窗口
  367. showCard() {
  368. this.setData({
  369. visibleCard: true,
  370. selectCoupPrice: this.data.showMoney
  371. })
  372. },
  373. // 选中次卡触发
  374. cardSel(e) {
  375. console.log(this.data.selectCardProjectList, e.currentTarget.dataset, 'e.currentTarget.dataset')
  376. let index = e.currentTarget.dataset.index
  377. let item = e.currentTarget.dataset.item
  378. let selList = this.data.cardList
  379. let isselectCard = false
  380. let isRepeat = false
  381. this.data.cardList.forEach(res => {
  382. if (JSON.stringify(item.project_ids) == JSON.stringify(res.project_ids) && res.isSelectCard && res.id != item.id) {
  383. isRepeat = true
  384. }
  385. })
  386. if (isRepeat) {
  387. wx.showToast({
  388. title: '不可重复选择相同次卡',
  389. icon: 'none'
  390. })
  391. return
  392. }
  393. this.data.orderDetails.project_list.forEach((res, i) => {
  394. if (res.cardId && this.data.cardList[index].project_ids.indexOf(res.id) > -1) {
  395. isselectCard = true
  396. }
  397. })
  398. if (isselectCard) {
  399. wx.showToast({
  400. title: '该项目已选择优惠券',
  401. icon: 'none'
  402. })
  403. return
  404. }
  405. let isProjectList = []
  406. //判断是否重复选择次卡
  407. let isTrueCode = false
  408. this.data.cardList.forEach(res => {
  409. if (res.isSelectCard) {
  410. isProjectList = isProjectList.concat(res.project_ids)
  411. }
  412. })
  413. if (e.currentTarget.dataset.item.isSelectCard) {
  414. e.currentTarget.dataset.item.project_ids.forEach((_item, index) => {
  415. isProjectList.forEach((item, i) => {
  416. if (_item == item) {
  417. isProjectList.splice(i, 1)
  418. }
  419. })
  420. })
  421. }
  422. if (this.includes(isProjectList, e.currentTarget.dataset.item.project_ids)) {
  423. isTrueCode = true
  424. }
  425. if (isTrueCode) {
  426. wx.showToast({
  427. title: '请勿重复选择次卡',
  428. icon: 'none'
  429. })
  430. return
  431. }
  432. console.log(isProjectList, 'isProjectList');
  433. let selValue = !selList[index].isSelectCard
  434. selList[index].isSelectCard = selValue
  435. this.setData({
  436. cardList: selList
  437. })
  438. },
  439. includes(arr1, arr2) {
  440. return arr2.every(val => arr1.includes(val));
  441. },
  442. onConfirmCard() {
  443. let price = this.data.orderDetails.price //订单价格
  444. let card_data = []
  445. let card_data_info = {}
  446. let selectCodeList = []
  447. let selectCardProjectList = []
  448. //选择次卡&价格计算
  449. let projectId = []
  450. this.data.orderDetails.project_list.forEach((res, i) => {
  451. if (res.cardId) {
  452. price = price - res.price
  453. }
  454. this.data.cardList.forEach(item => {
  455. //如果已选择优惠券
  456. console.log(res.cardId, 'cardId.length', item.isSelectCard);
  457. if (item.project_ids.indexOf(res.id) > -1 && projectId.indexOf(res.id) == -1 && item.isSelectCard) {
  458. projectId.push(res.id)
  459. price = price - res.price
  460. price = price < 0 ? 0 : price
  461. selectCardProjectList.push(res)
  462. selectCardProjectList = Array.from(new Set(selectCardProjectList))
  463. selectCodeList.push(item)
  464. card_data_info = {
  465. id: res.id,
  466. card_id: item.id
  467. }
  468. card_data.push(card_data_info)
  469. let _item = 'orderDetails.project_list[' + i + '].isCard'
  470. this.setData({
  471. [_item]: true
  472. })
  473. }
  474. })
  475. })
  476. let priceSet = 0
  477. if (selectCodeList.length == 0 && this.data.discountList.length > 0) {
  478. priceSet = this.data.discountList[0].priceSet.toFixed(2)
  479. price = this.data.orderDetails.price - priceSet
  480. }
  481. this.setData({
  482. card_data: card_data,
  483. showMoney: price,
  484. visibleCard: false,
  485. selectCodeList: selectCodeList,
  486. selectCardProjectList: selectCardProjectList,
  487. priceSet: priceSet
  488. })
  489. this.calculateMoney() //计算总额
  490. },
  491. onCancelCoupon() {
  492. this.setData({
  493. visible: false
  494. })
  495. },
  496. onConfirmCoupon() {
  497. // let currentChangeCoupon = this.data.couponList[this.data.currentCoupon]
  498. this.setData({
  499. visible: false,
  500. // currentChangeCoupon
  501. },
  502. () => {
  503. // this.calculateOrderPayMoney()
  504. }
  505. )
  506. },
  507. selectCoup() {
  508. let that = this
  509. let isSelectCoups = []
  510. for (let i = 0; i < this.data.productDiscount.length; i++) {
  511. if (this.data.productDiscount[i].isSelect) {
  512. let selectCoup = that.data.productDiscount[i]
  513. isSelectCoups.push(selectCoup)
  514. }
  515. }
  516. this.setData({
  517. isSelectCoup: isSelectCoups
  518. })
  519. console.log(isSelectCoups)
  520. that.theMoneyBest()
  521. },
  522. /**
  523. * 获取订单详情
  524. */
  525. getOrderData() {
  526. let that = this
  527. get('v2/api/order/info', {
  528. id: this.data.orderDetailsId
  529. }, (res) => {
  530. res.data.project_list.forEach(item => {
  531. item.isCard = false
  532. })
  533. if (res.data.discount != 0) {
  534. let newsPrice = res.data.price
  535. res.data.price = newsPrice * 1 + res.data.discount_price * 1
  536. this.setData({
  537. dotSelectCard: true,
  538. orderDetails: res.data,
  539. showMoney: newsPrice,
  540. priceSet: res.data.discount_price
  541. }, )
  542. return
  543. }
  544. if (res.data.coupon_list.length > 0) {
  545. res.data.coupon_list[0].type = 0
  546. }
  547. this.setData({
  548. orderDetails: res.data,
  549. showMoney: res.data.price,
  550. isSelectCoup: res.data.coupon_list,
  551. dotSelectCard: res.data.coupon_list.length !== 0 ? true : false,
  552. }, )
  553. that.productDiscount()
  554. if (res.data.type == "one") {
  555. return
  556. }
  557. that.orderDiscount()
  558. })
  559. },
  560. // 这里是立即支付的接口
  561. // v2/api/order/pay
  562. payNow: function () {
  563. // 计算折扣
  564. this.setData({
  565. levelPrice: (this.data.showMoney * this.data.level).toFixed(2)
  566. })
  567. this.setData({
  568. toLevelPrice: (this.data.showMoney - this.data.levelPrice).toFixed(2)
  569. })
  570. this.setData({
  571. payNow: true,
  572. })
  573. },
  574. // 活动折扣类型,非必填,0无折扣(默认),50新人五折活动,100仪器免费使用权益
  575. paybtm() {
  576. let isSelectCoup = this.data.isSelectCoup
  577. let coupType = isSelectCoup.length > 0 ? isSelectCoup[0].type : 5
  578. let coupData = []
  579. // 卡券,项目券数据包,id:订单项目ID,coupon_id:优惠券ID,如:[{"id":1220,coupon_id:199}]
  580. // 支付方式,amount余额支付,weixin微信支付
  581. if (coupType == 4) {
  582. for (let i = 0; i < isSelectCoup.length; i++) {
  583. let notData = {
  584. id: isSelectCoup[i].projectids,
  585. coupon_id: isSelectCoup[i].id,
  586. }
  587. coupData.push(notData)
  588. }
  589. }
  590. let dataTo = {
  591. id: this.data.orderDetails.id,
  592. pay_way: this.data.pay_methon == 1 ? 'weixin' : 'amount',
  593. }
  594. if (coupType == 4) {
  595. dataTo.coupon_data = JSON.stringify(coupData)
  596. } else if (coupType != 5) {
  597. dataTo.coupon_id = isSelectCoup[0].id
  598. }
  599. if (this.data.card_data.length != 0) {
  600. dataTo.card_data = JSON.stringify(this.data.card_data)
  601. }
  602. if (this.data.priceSet != 0 && !this.data.dotSelectCard && this.data.card_data.length == 0) {
  603. dataTo.discount = this.data.orderDiscount
  604. }
  605. let that = this
  606. console.log(dataTo)
  607. post('v2/api/order/pay', dataTo, (res) => {
  608. that.setData({
  609. payNow: false
  610. })
  611. if (res.data.pay_status == 1) {
  612. // wx.showToast({
  613. // title: '支付成功!',
  614. // icon: "none"
  615. // })
  616. that.payOkey()
  617. return
  618. }
  619. if (that.data.pay_methon == 2) {
  620. // wx.showToast({
  621. // title: '支付成功!',
  622. // icon: "none"
  623. // })
  624. that.payOkey()
  625. return
  626. }
  627. wx.requestPayment({
  628. timeStamp: res.data.pay_data.timeStamp,
  629. nonceStr: res.data.pay_data.nonceStr,
  630. package: res.data.pay_data.package,
  631. signType: res.data.pay_data.signType,
  632. paySign: res.data.pay_data.paySign,
  633. success(res) {
  634. if (res.errMsg == 'requestPayment:ok') {
  635. // wx.showToast({
  636. // title: '支付成功!',
  637. // icon: "none"
  638. // })
  639. that.payOkey()
  640. }
  641. },
  642. fail(res) {
  643. this.orderDiscount()
  644. this.getOrderData()
  645. wx.showToast({
  646. title: '支付失败!',
  647. icon: "none"
  648. })
  649. }
  650. })
  651. })
  652. },
  653. payOkey() {
  654. wx.showToast({
  655. title: '支付成功!',
  656. icon: "none"
  657. })
  658. let that = this
  659. setTimeout(function () {
  660. that.toDetails()
  661. }, 1000); //延迟时间 这里是1秒
  662. },
  663. toDetails() {
  664. wx.redirectTo({
  665. url: '/pages/orderDetailsProject/orderDetailsProject?id=' + this.data.orderDetails.id,
  666. })
  667. },
  668. pay_methonw() {
  669. this.setData({
  670. pay_methon: 1
  671. })
  672. },
  673. pay_methonc() {
  674. this.setData({
  675. pay_methon: 2
  676. })
  677. },
  678. //
  679. // 卡券,项目券数据包,id:订单项目ID,coupon_id:优惠券ID,如:[{"id":1220,coupon_id:199}]
  680. theMoneyBest() {
  681. let that = this
  682. let project_list = this.data.orderDetails.project_list //这里是项目列表
  683. let isSelectCoup = this.data.isSelectCoup //这里选择了的卡券列表
  684. let selectProjectList = [] // 适合的卡券列表
  685. // let CouPonIndex = this.data.CouPonIndex // 卡券选择的Id
  686. let productDiscount = this.data.productDiscount // 卡券列表
  687. console.log(productDiscount)
  688. if (isSelectCoup.length == 0) {
  689. this.setData({
  690. priceSet: 0,
  691. showMoney: this.data.orderDetails.price
  692. })
  693. that.onShowPrice()
  694. return
  695. }
  696. // debugger
  697. if (isSelectCoup[0].type == 4) {
  698. for (let i = 0; i < isSelectCoup.length; i++) { // 首先遍历卡券
  699. if (!isSelectCoup[i].projectids) { // 如果卡券没绑定项目
  700. console.log('没绑定过!')
  701. let selectIndexMoney = 0 // 初始值
  702. let selectIndex = 111 // 初始金额
  703. for (let j = 0; j < project_list.length; j++) { // 遍历项目
  704. if (isSelectCoup[i].project_ids.indexOf(project_list[j].id) != -1 && !project_list[j].cardId) { // 如果卡券能够使用这个项目并且这个项目没有被绑定过
  705. console.log('project_list[j].price')
  706. console.log(project_list[j].price)
  707. console.log(selectIndexMoney)
  708. console.log(project_list[j].price > selectIndexMoney)
  709. if (project_list[j].price * 1 > selectIndexMoney * 1) {
  710. selectIndex = j
  711. selectIndexMoney = project_list[j].price
  712. console.log('--' + selectIndexMoney)
  713. }
  714. // selectProjectList.push(project_list[i])
  715. }
  716. }
  717. if (selectIndex != 111) {
  718. isSelectCoup[i].projectids = project_list[selectIndex].id
  719. isSelectCoup[i].projectName = project_list[selectIndex].name
  720. project_list[selectIndex].cardId = isSelectCoup[i].id
  721. } else {
  722. for (let k = 0; k < productDiscount.length; k++) {
  723. if (productDiscount[k].id == isSelectCoup[i].id) {
  724. // isSelectCoup.
  725. productDiscount[k].isSelect = false
  726. // isSelectCoup.splice(isSelectCoup.length-1,1);
  727. wx.showToast({
  728. title: '没有适合的项目!',
  729. icon: "none"
  730. })
  731. }
  732. }
  733. }
  734. if (selectIndex == 111) {
  735. isSelectCoup.splice(isSelectCoup.length - 1, 1);
  736. }
  737. }
  738. }
  739. let orderDetails1 = this.data.orderDetails
  740. orderDetails1.project_list = project_list
  741. this.setData({
  742. // CouPonIndex: CouPonIndex,
  743. productDiscount: productDiscount,
  744. isSelectCoup: isSelectCoup,
  745. orderDetails: orderDetails1,
  746. })
  747. }
  748. this.calculateMoney() //计算总额
  749. },
  750. /**
  751. * 获取订单详情
  752. */
  753. productDiscount() {
  754. let that = this
  755. let ids = ''
  756. // for (let i = 0; i < this.data.orderDetails.project_list.length; i++) {
  757. // ids = ids + ',' + this.data.orderDetails.project_list[i].id
  758. // }
  759. let storeData = getApp().globalData.storeData
  760. let storeId = storeData.id || wx.getStorageSync('store_id')
  761. get('v2/api/order/coupon', {
  762. id: this.data.orderDetails.id,
  763. store_id: storeId,
  764. page: 1,
  765. limit: 200,
  766. }, (res) => {
  767. let showLine = res.data.list
  768. let CouPonIndex = []
  769. for (let i = 0; i < showLine.length; i++) {
  770. if (showLine[i].full_price * 1 < that.data.orderDetails.price * 1 || showLine[i].full_price * 1 == that.data.orderDetails.price * 1) {
  771. showLine[i].isSelect = false
  772. CouPonIndex.push(showLine[i])
  773. }
  774. }
  775. this.setData({
  776. productDiscount: CouPonIndex,
  777. // CouPonIndex: CouPonIndex
  778. }, )
  779. })
  780. },
  781. /**
  782. * 获取当前用户折扣
  783. */
  784. orderDiscount() {
  785. get('/v3/api/order/discount', {
  786. id: this.data.orderDetailsId
  787. }, (res) => {
  788. this.setData({
  789. discountList: res.data.list
  790. }, )
  791. this.onShowPrice()
  792. })
  793. },
  794. // 计算折扣价格
  795. onShowPrice() {
  796. if (this.data.discountList.length == 0) {
  797. return
  798. }
  799. this.calculateMoney()
  800. // let money = Number(this.data.orderDetails.price) * Number(this.data.discountList[0].discount_rate) / 10
  801. // this.setData({
  802. // priceSet: (Number(this.data.orderDetails.price) - money).toFixed(2),
  803. // showMoney: money.toFixed(2)
  804. // })
  805. // orderDiscount
  806. },
  807. /**
  808. * 生命周期函数--监听页面加载
  809. */
  810. onLoad: function (options) {
  811. // 获取用户折扣等级
  812. this.getLevel()
  813. if (options.q) {
  814. let conUrlAll = decodeURIComponent(options.q)
  815. this.setData({
  816. orderDetailsId: conUrlAll.replace('https://test-img.ijolijoli.com/wxmini/pay?id=', '')
  817. })
  818. } else {
  819. this.setData({
  820. orderDetailsId: options.id
  821. })
  822. }
  823. this.getOrderData()
  824. // 获取项目可用的次卡列表
  825. this.getAvaCardList()
  826. let lat1 = app.address.latitude
  827. let lng1 = app.address.longitude
  828. this.setData({
  829. lat1: lat1,
  830. lng1: lng1,
  831. })
  832. },
  833. onCopy: function (e) {
  834. let content = e.currentTarget.dataset.no
  835. var that = this;
  836. wx.setClipboardData({
  837. data: content,
  838. success: function (res) {
  839. wx.showToast({
  840. title: '复制成功!',
  841. icon: "none"
  842. })
  843. }
  844. });
  845. },
  846. /**
  847. * 生命周期函数--监听页面初次渲染完成
  848. */
  849. onReady: function () {
  850. },
  851. /**
  852. * 生命周期函数--监听页面显示
  853. */
  854. onShow: function () {
  855. this.getAmount()
  856. },
  857. /**
  858. * 获取我的储值
  859. * api/user
  860. */
  861. getAmount() {
  862. get('api/user/amount', {}, (res) => {
  863. if (res.data) {
  864. this.setData({
  865. inserllAmount: res.data.amount
  866. })
  867. }
  868. })
  869. },
  870. // 获取项目可用的次卡列表
  871. getAvaCardList() {
  872. console.log(this, 'shit')
  873. get('v2/api/order/card', {
  874. page: 1,
  875. limit: 200,
  876. id: this.data.orderDetailsId
  877. }, (res) => {
  878. console.log(res, 'getAvaCardList')
  879. res.data.list.forEach((item) => {
  880. item.isSelectCard = false
  881. })
  882. this.setData({
  883. cardList: res.data.list,
  884. })
  885. }, (error) => {
  886. console.log("请求失败", error)
  887. })
  888. },
  889. /**
  890. * 生命周期函数--监听页面隐藏
  891. */
  892. onHide: function () {
  893. },
  894. /**
  895. * 生命周期函数--监听页面卸载
  896. */
  897. onUnload: function () {
  898. },
  899. /**
  900. * 页面相关事件处理函数--监听用户下拉动作
  901. */
  902. onPullDownRefresh: function () {
  903. },
  904. /**
  905. * 页面上拉触底事件的处理函数
  906. */
  907. onReachBottom: function () {
  908. },
  909. /**
  910. * 用户点击右上角分享
  911. */
  912. onShareAppMessage: function () {
  913. }
  914. })