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