mallPage.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. <template>
  2. <view class="ezy-mall-page">
  3. <view class="icon-title-navBar-box">
  4. <view @click="goBack" class="nav-bar-icon"></view>
  5. <text class="nav-bar-title">鹅状元进阶营</text>
  6. </view>
  7. <view class="ezy-tab-border">
  8. <uni-segmented-control :current="productData.current" :values="productData.items" active-color="#3A7FE9"
  9. @clickItem="onChangeTab" class="ezy-tab-box" />
  10. <view>
  11. <view v-if="productData.current === 0">
  12. <scroll-view scroll-y="true" :scroll-into-view="scrollIntoId" class="order-scroll-view">
  13. <!--数学-->
  14. <view :id="'item-' + item.id" v-for="item in productData.list" :key="item.id"
  15. class="mall-list-item">
  16. <view class="mall-content-box" :class="item.yigou ==1?'mall-yigou-box':''">
  17. <img :src='item.cover' class="mall-image" />
  18. <view class="content-body-box">
  19. <view class="content-name">
  20. <view class="name-text">{{item.name}}</view>
  21. </view>
  22. <view class="content-text">{{item.intro}}</view>
  23. <view class="content-row">
  24. <view class="content-yuanjia">原价:{{item.yuanjia}}</view>
  25. <view class="shop-car-box" @click="addProduct(item)">
  26. <icon v-if="productData.selectedIds.includes(item.id)" class="car-change">
  27. </icon>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. <view class="mall-bottom-row">
  33. <view v-if="item.type ==1" class="kcb-btn" @click="productBtn(item.levelList)">课程包明细
  34. <icon>
  35. </icon>
  36. </view>
  37. <view class="hdj-text">活动价:{{item.xianjia}}</view>
  38. </view>
  39. </view>
  40. </scroll-view>
  41. </view>
  42. <view v-if="productData.current === 1">
  43. <scroll-view scroll-y="true" :scroll-into-view="scrollIntoId" class="order-scroll-view">
  44. <!--数学-->
  45. <view :id="'item-' + item.id" v-for="item in productData.list" :key="item.id"
  46. class="mall-list-item">
  47. <view class="mall-content-box" :class="item.yigou ==1?'mall-yigou-box':''">
  48. <img :src='item.cover' class="mall-image" />
  49. <view class="content-body-box">
  50. <view class="content-name">
  51. <view class="name-text">{{item.name}}</view>
  52. </view>
  53. <view class="content-text">{{item.intro}}</view>
  54. <view class="content-row">
  55. <view class="content-yuanjia">原价:{{item.yuanjia}}</view>
  56. <view class="shop-car-box" v-if="item.yigou !=1" @click="addProduct(item)">
  57. <icon v-if="productData.selectedIds.includes(item.id)" class="car-change">
  58. </icon>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <view class="mall-bottom-row">
  64. <view v-if="item.type ==1" class="kcb-btn" @click="productBtn(item.levelList)">课程包明细
  65. <icon>
  66. </icon>
  67. </view>
  68. <view class="hdj-text">活动价:{{item.xianjia}}</view>
  69. </view>
  70. </view>
  71. </scroll-view>
  72. </view>
  73. <!-- <view v-if="productData.current === 2">
  74. <view>语文</view>
  75. </view> -->
  76. </view>
  77. </view>
  78. <view class="footer-mall-pay-box">
  79. <view class="mall-left-box" @click="detailBtn">
  80. <!-- 需要换成从接口中取得 wgy -->
  81. <view class="price-icon-box">
  82. <text class="red-price fh-text">¥</text>
  83. <text class="red-price">{{totalPrice}}</text>明细
  84. <icon :class="mxjtClass"></icon>
  85. </view>
  86. <view>购买即同意虚拟产品不支持退订</view>
  87. </view>
  88. <!-- 微信 -->
  89. <view class="pay-status-box" v-if="payType =='weixin'&&currentPlatform =='android'" @click="switchPayWay">
  90. <icon class="wx-icon"></icon>微信
  91. </view>
  92. <view class="pay-status-box" v-if="payType =='zhifubao'&&currentPlatform =='android'" @click="switchPayWay">
  93. <icon class="zfb-icon"></icon>支付宝
  94. </view>
  95. <view class="pay-status-box apple-status-box" v-if="currentPlatform =='ios'">
  96. <icon class="apple-icon"></icon>apple
  97. </view>
  98. <view v-if="currentPlatform =='android'" class="pay-btn" @touchstart="creatOrder">立即支付
  99. </view>
  100. <view v-if="currentPlatform =='ios'" class="pay-btn" @touchstart="creatOrderIos">立即支付</view>
  101. </view>
  102. <detail-dialog ref="mallDetailPopup" :selected-list="getSelectedProducts"></detail-dialog>
  103. <product-dialog ref="mallProductPopup"></product-dialog>
  104. </view>
  105. </template>
  106. <script setup>
  107. import {
  108. reactive,
  109. computed,
  110. nextTick,
  111. ref
  112. } from "vue";
  113. import {
  114. getMallist
  115. } from "@/api/productMall.js";
  116. import {
  117. orderAdd,
  118. orderPayAli,
  119. orderPayWx,
  120. orderPayApple,
  121. orderCheck
  122. } from "@/api/order.js"
  123. import {
  124. onLoad
  125. } from "@dcloudio/uni-app";
  126. import {
  127. toast,
  128. debounce,
  129. getUserIdentity
  130. } from "@/utils/common";
  131. import cacheManager from '@/utils/cacheManager.js';
  132. import CustomTabBar from '@/components/custom-tabbar/custom-tabbar.vue';
  133. import detailDialog from './detailDialog.vue'
  134. import productDialog from './productDialog.vue'
  135. const showPayWay = ref(true)
  136. const showDetail = ref(false)
  137. const mxjtClass = ref('mxjt-sq-icon')
  138. const mallDetailPopup = ref(null);
  139. const mallProductPopup = ref(null);
  140. let scrollIntoId = ref(null)
  141. const productData = reactive({
  142. items: ['数学', '英语'],
  143. current: 1,
  144. from: '',
  145. cardId: '',
  146. list: [],
  147. selectedIds: [] // 新增选中状态存储
  148. })
  149. let currentPlatform = ref('android')
  150. const payType = ref('weixin');
  151. let orderId = ref('');
  152. let productId = ref(null)
  153. let iapChannel = ref(null)
  154. let quantity = ref(1)
  155. let channel = ref('')
  156. let appleFlag = ref('')
  157. const totalPrice = computed(() => {
  158. return productData.list
  159. .filter(item => productData.selectedIds.includes(item.id))
  160. .reduce((sum, item) => sum + parseFloat(item.xianjia || 0), 0)
  161. .toFixed(2)
  162. })
  163. const getSelectedProducts = computed(() => {
  164. return productData.list.filter(item => productData.selectedIds.includes(item.id));
  165. });
  166. function switchPayWay() {
  167. payType.value = payType.value == 'weixin' ? 'zhifubao' : 'weixin'
  168. }
  169. function productBtn(item) {
  170. mallProductPopup.value.detailShow(item);
  171. }
  172. function detailBtn() {
  173. // // 获取选中商品的cardId列表
  174. // const selectedProducts = productData.list.filter(
  175. // item => productData.selectedIds.includes(item.id)
  176. // );
  177. // const cardIds = selectedProducts.map(item => item.id);
  178. // console.log('cardIds', cardIds);
  179. if (productData.selectedIds.length === 0) {
  180. toast("请选择至少一个商品查看明细");
  181. return;
  182. }
  183. showDetail.value = !showDetail.value;
  184. if (showDetail.value) {
  185. const selected = productData.list.filter(item =>
  186. productData.selectedIds.includes(item.id)
  187. );
  188. mxjtClass.value = 'mxjt-zk-icon';
  189. mallDetailPopup.value.detailShow(selected);
  190. } else {
  191. mxjtClass.value = 'mxjt-sq-icon';
  192. mallDetailPopup.value.detailCloseBtn();
  193. }
  194. }
  195. const isIOSorAndroid = () => {
  196. const systemInfo = uni.getSystemInfoSync();
  197. console.log('systemInfo', systemInfo);
  198. if (systemInfo.platform == 'ios') {
  199. return currentPlatform.value = 'ios'
  200. } else {
  201. return currentPlatform.value = 'android'
  202. }
  203. }
  204. function genggaiVip(data) {
  205. uni.hideLoading();
  206. const localList = cacheManager.get('auth').levelIdList || []
  207. const mergeList = [...new Set([...localList, ...data.levelIdList])]
  208. cacheManager.updateObject('auth', {
  209. levelIdList: mergeList
  210. })
  211. uni.redirectTo({
  212. url: '/pages/pay/order?status=2'
  213. })
  214. // toast("chenggong!!!! 之后跳转我的订单页面")
  215. // if (formPage.value == 'my') {
  216. // uni.redirectTo({
  217. // url: '/pages/my/index'
  218. // })
  219. // } else {
  220. // uni.redirectTo({
  221. // url: '/pages/study/index'
  222. // })
  223. // }
  224. }
  225. const creatOrderIos = debounce((data => {
  226. if (productData.selectedIds.length === 0) {
  227. toast("请选择至少一个商品");
  228. return;
  229. }
  230. const selectedProducts = productData.list.filter(
  231. item => productData.selectedIds.includes(item.id)
  232. );
  233. const cardIds = selectedProducts.map(item => item.id);
  234. console.log('cardIds', cardIds);
  235. uni.showLoading({
  236. title: '',
  237. mask: true
  238. });
  239. if (appleFlag.value.toString() == 'true') {
  240. productId.value = 'llisoftEzhuangyuanceshi'
  241. } else {
  242. if (cardId.value == 1) {
  243. productId.value = 'llisoftEzhuangyuan'
  244. } else {
  245. productId.value = 'llisoftEzhuangyuanYingyu'
  246. }
  247. }
  248. let req = {
  249. cardIds: cardIds
  250. }
  251. orderAdd(req).then(res => {
  252. console.log('res', res);
  253. if (res.code == 0) {
  254. uni.hideLoading();
  255. orderId.value = res.data.id
  256. // 测试ios 1元
  257. applePay()
  258. } else {
  259. uni.hideLoading();
  260. return false
  261. console.log('请求失败');
  262. }
  263. }).catch((e) => {
  264. uni.hideLoading();
  265. toast("订单创建失败")
  266. return false
  267. })
  268. }), 500)
  269. function applePaySuccess(data) {
  270. uni.showLoading({
  271. title: '开通中',
  272. mask: true
  273. });
  274. let req = {
  275. "id": orderId.value,
  276. "paynum": data.transactionIdentifier,
  277. "receipt": data.transactionReceipt
  278. }
  279. console.log('reqreq', req);
  280. orderPayApple(req).then(res => {
  281. if (res.code == 0 && res.data) {
  282. iapChannel.finishTransaction(data.transactionIdentifier)
  283. console.log('resiapChanneliapChanneliapChannel', res);
  284. orderCheck({
  285. id: orderId.value
  286. }).then(res3 => {
  287. console.log('res3', res3);
  288. if (res3.code == 0) {
  289. genggaiVip(res3.data)
  290. } else {
  291. setTimeout(() => {
  292. orderCheck({
  293. id: orderId.value
  294. }).then(res4 => {
  295. if (res4.code == 0) {
  296. genggaiVip(res4.data)
  297. } else {
  298. toast(
  299. "开通失败,请联系管理员!"
  300. )
  301. uni
  302. .hideLoading();
  303. return false
  304. }
  305. }).catch(() => {
  306. uni.hideLoading();
  307. toast("check接口报错")
  308. return false
  309. })
  310. }, 5000)
  311. }
  312. }).catch(() => {
  313. uni.hideLoading();
  314. toast("check接口报错")
  315. return false
  316. })
  317. } else {
  318. iapChannel.finishTransaction(data.transactionIdentifier)
  319. uni.hideLoading();
  320. toast("苹果内购失败")
  321. console.log('orderPayApple失败');
  322. return false
  323. }
  324. })
  325. }
  326. function applePay() {
  327. console.log('123123');
  328. if (!productId.value) {
  329. uni.showToast({
  330. title: '苹果内购ID缺失,请选择其它支付方式或联系客服',
  331. icon: "none"
  332. });
  333. return false;
  334. }
  335. uni.showLoading({
  336. title: '正在支付中...'
  337. });
  338. try {
  339. plus.payment.getChannels(function(channels) { //判读项目支付通道开通情况
  340. for (var i in channels) {
  341. iapChannel = channels[i];
  342. // 获取 id 为 'appleiap' 的 channel
  343. console.info("支付通道", iapChannel)
  344. if (iapChannel.id === 'appleiap') { //开通了app应用内支付,在manifest.josn中设置,开通后需打自定议基座
  345. console.info("苹果支付通道", iapChannel)
  346. // ids 数组中的项为 App Store Connect 配置的内购买项目产品ID(productId)
  347. var ids = [productId.value];
  348. // iap 为应用内支付对象
  349. iapChannel.requestOrder(ids, function(e) {
  350. // 获取订单信息成功回调方法
  351. console.log('requestOrder success: ' + JSON.stringify(e));
  352. uni.requestPayment({
  353. provider: 'appleiap',
  354. orderInfo: {
  355. productid: productId.value, //产品id,来自于苹果
  356. quantity: quantity.value, //产品数量
  357. manualFinishTransaction: true
  358. },
  359. success: (e) => {
  360. uni.hideLoading();
  361. // toast("苹果内购成功")
  362. console.info("苹果内购成功", e)
  363. applePaySuccess(e)
  364. //e.payment.orderNo = that.orderNo
  365. //支付成功回调,前端调用后台接口
  366. },
  367. fail: (e) => {
  368. uni.hideLoading();
  369. toast("苹果内购失败")
  370. console.info("苹果内购失败", e)
  371. },
  372. })
  373. },
  374. function(e) {
  375. // 获取订单信息失败回调方法
  376. console.log('requestOrder failed: ' + JSON.stringify(e));
  377. });
  378. } else {
  379. console.log('不支持苹果支付')
  380. }
  381. }
  382. },
  383. function(e) {
  384. console.log("获取iap支付通道失败:" + e.message);
  385. });
  386. } catch (e) {
  387. uni.showModal({
  388. title: "init",
  389. content: e.message,
  390. showCancel: false
  391. });
  392. } finally {
  393. uni.hideLoading();
  394. }
  395. }
  396. function wxPay() {
  397. orderPayWx({
  398. id: orderId.value
  399. }).then(res2 => {
  400. uni.hideLoading();
  401. console.log('res2', res2);
  402. if(res2.code !=0){
  403. return false
  404. }
  405. uni.requestPayment({
  406. "provider": "wxpay",
  407. "orderInfo": {
  408. "appid": res2.data.appid, // 应用ID(AppID)
  409. "partnerid": res2.data.partnerId, // 商户号(PartnerID)
  410. "prepayid": res2.data.prepayId, // 预支付交易会话ID
  411. "package": res2.data.packageVal, // 固定值
  412. "noncestr": res2.data.nonceStr, // 随机字符串
  413. "timestamp": res2.data.timestamp, // 时间戳(单位:秒)
  414. "sign": res2.data.sign // 签名,这里用的 MD5 签名
  415. }, //此处为服务器返回的订单信息字符串
  416. success: function(res) {
  417. //var rawdata = JSON.parse(res.rawdata);
  418. // console.log('res',res);
  419. // console.log('支付成功');
  420. // console.log('rawdata', rawdata);
  421. uni.showLoading({
  422. title: '开通中,请稍后...'
  423. });
  424. orderCheck({
  425. id: orderId.value
  426. }).then(res3 => {
  427. console.log('res3', res3);
  428. if (res3.code == 0) {
  429. genggaiVip(res3.data)
  430. } else {
  431. setTimeout(() => {
  432. orderCheck({
  433. id: orderId.value
  434. }).then(res4 => {
  435. if (res4.code == 0) {
  436. genggaiVip(res4.data)
  437. } else {
  438. toast(
  439. "开通失败,请联系管理员!"
  440. )
  441. uni
  442. .hideLoading();
  443. return false
  444. }
  445. }).catch(() => {
  446. uni.hideLoading();
  447. toast("check接口报错")
  448. return false
  449. })
  450. }, 5000)
  451. }
  452. }).catch(() => {
  453. uni.hideLoading();
  454. toast("check接口报错")
  455. return false
  456. })
  457. },
  458. fail: function(err) {
  459. uni.hideLoading();
  460. // toast('支付失败:' + JSON.stringify(err));
  461. console.log('支付失败:' + JSON.stringify(err));
  462. }
  463. });
  464. }).catch((error) => {
  465. uni.hideLoading();
  466. console.log(error);
  467. })
  468. }
  469. function aliApy() {
  470. orderPayAli({
  471. id: orderId.value
  472. }).then(res2 => {
  473. console.log('res2', res2);
  474. if(res2.code !=0){
  475. return false
  476. }
  477. uni.hideLoading();
  478. uni.requestPayment({
  479. "provider": "alipay",
  480. "orderInfo": res2.data.text, //此处为服务器返回的订单信息字符串
  481. success: function(res) {
  482. // var rawdata = JSON.parse(res.rawdata);
  483. // console.log('支付成功');
  484. // console.log('rawdata', rawdata);
  485. uni.showLoading({
  486. title: '开通中,请稍后...'
  487. });
  488. orderCheck({
  489. id: orderId.value
  490. }).then(res3 => {
  491. if (res3.code == 0) {
  492. genggaiVip(res3.data)
  493. } else {
  494. setTimeout(() => {
  495. orderCheck({
  496. id: orderId.value
  497. }).then(res4 => {
  498. if (res4.code ==
  499. 0) {
  500. genggaiVip(res4.data)
  501. } else {
  502. toast(
  503. "开通失败,请联系管理员!"
  504. )
  505. uni
  506. .hideLoading();
  507. return false
  508. }
  509. }).catch(() => {
  510. uni.hideLoading();
  511. toast("check接口报错")
  512. return false
  513. })
  514. }, 5000)
  515. }
  516. }).catch(() => {
  517. uni.hideLoading();
  518. toast("check接口报错")
  519. return false
  520. })
  521. },
  522. fail: function(err) {
  523. console.log('支付失败:' + JSON.stringify(err));
  524. uni.hideLoading();
  525. }
  526. });
  527. })
  528. }
  529. const creatOrder = debounce((data) => {
  530. if (productData.selectedIds.length === 0) {
  531. toast("请选择至少一个商品");
  532. return;
  533. }
  534. // 获取选中商品的cardId列表
  535. const selectedProducts = productData.list.filter(
  536. item => productData.selectedIds.includes(item.id)
  537. );
  538. const cardIds = selectedProducts.map(item => item.id);
  539. console.log('cardIds', cardIds);
  540. uni.showLoading({
  541. title: '',
  542. mask: true
  543. });
  544. if (payType.value == 'weixin') {
  545. console.log('创建订单11');
  546. let req = {
  547. cardIds: cardIds
  548. }
  549. orderAdd(req).then(res => {
  550. console.log('res', res);
  551. console.log(' res.data.id', res.data.id);
  552. orderId.value = res.data.id
  553. wxPay()
  554. }).catch((err) => {
  555. uni.hideLoading();
  556. toast("订单创建失败")
  557. return false
  558. })
  559. } else {
  560. let req = {
  561. cardIds: cardIds
  562. }
  563. orderAdd(req).then(res => {
  564. console.log('res', res);
  565. orderId.value = res.data.id
  566. aliApy()
  567. }).catch((err) => {
  568. uni.hideLoading();
  569. toast("订单创建失败")
  570. return false
  571. })
  572. }
  573. }, 500)
  574. function onChangeTab(e) {
  575. console.log('e', e);
  576. productData.current = e.currentIndex
  577. productData.selectedIds = [] // 清空选中
  578. getMore()
  579. }
  580. function getMore() {
  581. const opt = {
  582. subjectId: productData.current + 1
  583. }
  584. getMallist(opt).then(res => {
  585. if (res.code == 0) {
  586. productData.list = res.data
  587. // 初始化选中状态(当有外部cardId时)
  588. if (productData.cardId) {
  589. const exist = res.data.some(item => item.id == productData.cardId)
  590. if (exist) {
  591. productData.selectedIds = [Number(productData.cardId)]
  592. }
  593. }
  594. nextTick(() => {
  595. scrollToIdFun(productData.cardId)
  596. })
  597. }
  598. }).catch(err => {
  599. toast("获取产品数据失败")
  600. return false
  601. })
  602. }
  603. function scrollToIdFun(targetId) {
  604. const index = productData.list.findIndex(item => item.id == targetId)
  605. console.log('index', index);
  606. if (index > -1) {
  607. // 安卓设备需要双保险
  608. scrollIntoId.value = `item-${targetId}`
  609. setTimeout(() => {
  610. scrollIntoId.value = `item-${targetId}`
  611. }, 300)
  612. }
  613. }
  614. function addProduct(item) {
  615. console.log('item', item);
  616. const temp = [...productData.selectedIds]
  617. const index = temp.indexOf(item.id)
  618. index > -1 ? temp.splice(index, 1) : temp.push(item.id)
  619. productData.selectedIds = temp
  620. console.log('productData.selectedIds', productData.selectedIds);
  621. }
  622. function isItemSelected(id) {
  623. return productData.cardId ?
  624. id == productData.cardId // 有外部cardId时严格匹配
  625. :
  626. productData.selectedIds.includes(id) // 无cardId时检查选中列表
  627. }
  628. function goBack() {
  629. if (productData.from == 'daoPage') {
  630. uni.redirectTo({
  631. url: '/pages/study/index'
  632. })
  633. } else {
  634. uni.redirectTo({
  635. url: '/pages/my/index'
  636. })
  637. }
  638. }
  639. onLoad((options) => {
  640. if (options.from == 'daoPage') {
  641. productData.current = Number(options.subjectId) - 1
  642. productData.cardId = options.cardId
  643. productData.from = options.from
  644. } else {
  645. productData.current = Number(cacheManager.get('auth').subjectId) - 1
  646. }
  647. //isIOSorAndroid()
  648. getMore();
  649. })
  650. </script>
  651. <style>
  652. </style>