mallPage.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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" 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)">课程包明细
  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. genggaiVip()
  285. } else {
  286. iapChannel.finishTransaction(data.transactionIdentifier)
  287. uni.hideLoading();
  288. toast("苹果内购失败")
  289. console.log('orderPayApple失败');
  290. return false
  291. }
  292. })
  293. }
  294. function applePay() {
  295. console.log('123123');
  296. if (!productId.value) {
  297. uni.showToast({
  298. title: '苹果内购ID缺失,请选择其它支付方式或联系客服',
  299. icon: "none"
  300. });
  301. return false;
  302. }
  303. uni.showLoading({
  304. title: '正在支付中...'
  305. });
  306. try {
  307. plus.payment.getChannels(function(channels) { //判读项目支付通道开通情况
  308. for (var i in channels) {
  309. iapChannel = channels[i];
  310. // 获取 id 为 'appleiap' 的 channel
  311. console.info("支付通道", iapChannel)
  312. if (iapChannel.id === 'appleiap') { //开通了app应用内支付,在manifest.josn中设置,开通后需打自定议基座
  313. console.info("苹果支付通道", iapChannel)
  314. // ids 数组中的项为 App Store Connect 配置的内购买项目产品ID(productId)
  315. var ids = [productId.value];
  316. // iap 为应用内支付对象
  317. iapChannel.requestOrder(ids, function(e) {
  318. // 获取订单信息成功回调方法
  319. console.log('requestOrder success: ' + JSON.stringify(e));
  320. uni.requestPayment({
  321. provider: 'appleiap',
  322. orderInfo: {
  323. productid: productId.value, //产品id,来自于苹果
  324. quantity: quantity.value, //产品数量
  325. manualFinishTransaction: true
  326. },
  327. success: (e) => {
  328. uni.hideLoading();
  329. // toast("苹果内购成功")
  330. console.info("苹果内购成功", e)
  331. applePaySuccess(e)
  332. //e.payment.orderNo = that.orderNo
  333. //支付成功回调,前端调用后台接口
  334. },
  335. fail: (e) => {
  336. uni.hideLoading();
  337. toast("苹果内购失败")
  338. console.info("苹果内购失败", e)
  339. },
  340. })
  341. },
  342. function(e) {
  343. // 获取订单信息失败回调方法
  344. console.log('requestOrder failed: ' + JSON.stringify(e));
  345. });
  346. } else {
  347. console.log('不支持苹果支付')
  348. }
  349. }
  350. },
  351. function(e) {
  352. console.log("获取iap支付通道失败:" + e.message);
  353. });
  354. } catch (e) {
  355. uni.showModal({
  356. title: "init",
  357. content: e.message,
  358. showCancel: false
  359. });
  360. } finally {
  361. uni.hideLoading();
  362. }
  363. }
  364. function wxPay() {
  365. orderPayWx({
  366. id: orderId.value
  367. }).then(res2 => {
  368. uni.hideLoading();
  369. console.log('res2', res2);
  370. uni.requestPayment({
  371. "provider": "wxpay",
  372. "orderInfo": {
  373. "appid": res2.data.appid, // 应用ID(AppID)
  374. "partnerid": res2.data.partnerId, // 商户号(PartnerID)
  375. "prepayid": res2.data.prepayId, // 预支付交易会话ID
  376. "package": res2.data.packageVal, // 固定值
  377. "noncestr": res2.data.nonceStr, // 随机字符串
  378. "timestamp": res2.data.timestamp, // 时间戳(单位:秒)
  379. "sign": res2.data.sign // 签名,这里用的 MD5 签名
  380. }, //此处为服务器返回的订单信息字符串
  381. success: function(res) {
  382. //var rawdata = JSON.parse(res.rawdata);
  383. // console.log('res',res);
  384. // console.log('支付成功');
  385. // console.log('rawdata', rawdata);
  386. uni.showLoading({
  387. title: '开通中,请稍后...'
  388. });
  389. orderCheck({
  390. id: orderId.value
  391. }).then(res3 => {
  392. console.log('res3', res3);
  393. if (res3.code == 0) {
  394. genggaiVip(res3.data)
  395. } else {
  396. setTimeout(() => {
  397. orderCheck({
  398. id: orderId.value
  399. }).then(res4 => {
  400. if (res4.code == 0) {
  401. genggaiVip(res4.data)
  402. } else {
  403. toast(
  404. "开通失败,请联系管理员!"
  405. )
  406. uni
  407. .hideLoading();
  408. return false
  409. }
  410. }).catch(() => {
  411. uni.hideLoading();
  412. toast("check接口报错")
  413. return false
  414. })
  415. }, 5000)
  416. }
  417. }).catch(() => {
  418. uni.hideLoading();
  419. toast("check接口报错")
  420. return false
  421. })
  422. },
  423. fail: function(err) {
  424. uni.hideLoading();
  425. // toast('支付失败:' + JSON.stringify(err));
  426. console.log('支付失败:' + JSON.stringify(err));
  427. }
  428. });
  429. }).catch((error) => {
  430. uni.hideLoading();
  431. console.log(error);
  432. })
  433. }
  434. function aliApy() {
  435. orderPayAli({
  436. id: orderId.value
  437. }).then(res2 => {
  438. console.log('res2', res2);
  439. uni.hideLoading();
  440. uni.requestPayment({
  441. "provider": "alipay",
  442. "orderInfo": res2.data.text, //此处为服务器返回的订单信息字符串
  443. success: function(res) {
  444. // var rawdata = JSON.parse(res.rawdata);
  445. // console.log('支付成功');
  446. // console.log('rawdata', rawdata);
  447. uni.showLoading({
  448. title: '开通中,请稍后...'
  449. });
  450. orderCheck({
  451. id: orderId.value
  452. }).then(res3 => {
  453. if (res3.code == 0) {
  454. genggaiVip(res3.data)
  455. } else {
  456. setTimeout(() => {
  457. orderCheck({
  458. id: orderId.value
  459. }).then(res4 => {
  460. if (res4.code ==
  461. 0) {
  462. genggaiVip(res4.data)
  463. } else {
  464. toast(
  465. "开通失败,请联系管理员!"
  466. )
  467. uni
  468. .hideLoading();
  469. return false
  470. }
  471. }).catch(() => {
  472. uni.hideLoading();
  473. toast("check接口报错")
  474. return false
  475. })
  476. }, 5000)
  477. }
  478. }).catch(() => {
  479. uni.hideLoading();
  480. toast("check接口报错")
  481. return false
  482. })
  483. },
  484. fail: function(err) {
  485. console.log('支付失败:' + JSON.stringify(err));
  486. uni.hideLoading();
  487. }
  488. });
  489. })
  490. }
  491. const creatOrder = debounce((data) => {
  492. if (productData.selectedIds.length === 0) {
  493. toast("请选择至少一个商品");
  494. return;
  495. }
  496. // 获取选中商品的cardId列表
  497. const selectedProducts = productData.list.filter(
  498. item => productData.selectedIds.includes(item.id)
  499. );
  500. const cardIds = selectedProducts.map(item => item.id);
  501. console.log('cardIds', cardIds);
  502. uni.showLoading({
  503. title: '',
  504. mask: true
  505. });
  506. if (payType.value == 'weixin') {
  507. console.log('创建订单11');
  508. let req = {
  509. cardIds: cardIds
  510. }
  511. orderAdd(req).then(res => {
  512. console.log('res', res);
  513. console.log(' res.data.id', res.data.id);
  514. orderId.value = res.data.id
  515. wxPay()
  516. }).catch((err) => {
  517. uni.hideLoading();
  518. toast("订单创建失败")
  519. return false
  520. })
  521. } else {
  522. let req = {
  523. cardIds: cardIds
  524. }
  525. orderAdd(req).then(res => {
  526. console.log('res', res);
  527. orderId.value = res.data.id
  528. aliApy()
  529. }).catch((err) => {
  530. uni.hideLoading();
  531. toast("订单创建失败")
  532. return false
  533. })
  534. }
  535. }, 500)
  536. function onChangeTab(e) {
  537. console.log('e', e);
  538. productData.current = e.currentIndex
  539. productData.selectedIds = [] // 清空选中
  540. getMore()
  541. }
  542. function getMore() {
  543. const opt = {
  544. subjectId: productData.current + 1
  545. }
  546. getMallist(opt).then(res => {
  547. if (res.code == 0) {
  548. productData.list = res.data
  549. // 初始化选中状态(当有外部cardId时)
  550. if (productData.cardId) {
  551. const exist = res.data.some(item => item.id == productData.cardId)
  552. if (exist) {
  553. productData.selectedIds = [Number(productData.cardId)]
  554. }
  555. }
  556. nextTick(() => {
  557. scrollToIdFun(productData.cardId)
  558. })
  559. }
  560. }).catch(err => {
  561. toast("获取产品数据失败")
  562. return false
  563. })
  564. }
  565. function scrollToIdFun(targetId) {
  566. const index = productData.list.findIndex(item => item.id == targetId)
  567. console.log('index', index);
  568. if (index > -1) {
  569. // 安卓设备需要双保险
  570. scrollIntoId.value = `item-${targetId}`
  571. setTimeout(() => {
  572. scrollIntoId.value = `item-${targetId}`
  573. }, 300)
  574. }
  575. }
  576. function addProduct(item) {
  577. console.log('item', item);
  578. const temp = [...productData.selectedIds]
  579. const index = temp.indexOf(item.id)
  580. index > -1 ? temp.splice(index, 1) : temp.push(item.id)
  581. productData.selectedIds = temp
  582. console.log('productData.selectedIds', productData.selectedIds);
  583. }
  584. function isItemSelected(id) {
  585. return productData.cardId ?
  586. id == productData.cardId // 有外部cardId时严格匹配
  587. :
  588. productData.selectedIds.includes(id) // 无cardId时检查选中列表
  589. }
  590. function goBack() {
  591. if (productData.from == 'daoPage') {
  592. uni.redirectTo({
  593. url: '/pages/study/index'
  594. })
  595. } else {
  596. uni.redirectTo({
  597. url: '/pages/my/index'
  598. })
  599. }
  600. }
  601. onLoad((options) => {
  602. if (options.from == 'daoPage') {
  603. productData.current = Number(options.subjectId) - 1
  604. productData.cardId = options.cardId
  605. productData.from = options.from
  606. } else {
  607. productData.current = Number(cacheManager.get('auth').subjectId) - 1
  608. }
  609. //isIOSorAndroid()
  610. getMore();
  611. })
  612. </script>
  613. <style>
  614. </style>