mallPage.vue 18 KB

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