mallPage.vue 18 KB

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