mallPage.vue 17 KB

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