mallPage.vue 17 KB

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