mallPage.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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" @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. <tip-big-dialog ref="youkeDialogRef" @confirm-btn="ykConfirm" :imgShow="true"></tip-big-dialog>
  105. </view>
  106. </template>
  107. <script setup>
  108. import {
  109. reactive,
  110. computed,
  111. nextTick,
  112. ref
  113. } from "vue";
  114. import {
  115. getMallist,
  116. getMallistCom
  117. } from "@/api/productMall.js";
  118. import {
  119. orderAdd,
  120. orderPayAli,
  121. orderPayWx,
  122. orderPayApple,
  123. orderCheck
  124. } from "@/api/order.js"
  125. import {
  126. onLoad
  127. } from "@dcloudio/uni-app";
  128. import {
  129. toast,
  130. debounce,
  131. getUserIdentity
  132. } from "@/utils/common";
  133. import cacheManager from '@/utils/cacheManager.js';
  134. import CustomTabBar from '@/components/custom-tabbar/custom-tabbar.vue';
  135. import detailDialog from './detailDialog.vue'
  136. import tipBigDialog from '@/components/dialog/tipBigDialog.vue';
  137. import productDialog from './productDialog.vue'
  138. const showPayWay = ref(true)
  139. const showDetail = ref(false)
  140. const mxjtClass = ref('mxjt-sq-icon')
  141. const mallDetailPopup = ref(null);
  142. const mallProductPopup = ref(null);
  143. const youkeDialogRef = ref(null);
  144. let scrollIntoId = ref(null)
  145. const productData = reactive({
  146. items: ['数学', '英语'],
  147. current: 1,
  148. from: '',
  149. cardId: '',
  150. list: [],
  151. selectedIds: [] // 新增选中状态存储
  152. })
  153. let currentPlatform = ref('android')
  154. const payType = ref('weixin');
  155. let orderId = ref('');
  156. let productId = ref(null)
  157. let iapChannel = ref(null)
  158. let quantity = ref(1)
  159. let channel = ref('')
  160. let appleFlag = ref('')
  161. const subjectId = ref(null); //游客使用
  162. const levelId = ref(null); //游客使用
  163. const typeId = ref(null); //游客使用
  164. const tipFlag = ref(null); //游客使用
  165. const totalPrice = computed(() => {
  166. return productData.list
  167. .filter(item => productData.selectedIds.includes(item.id))
  168. .reduce((sum, item) => sum + parseFloat(item.xianjia || 0), 0)
  169. .toFixed(2)
  170. })
  171. const getSelectedProducts = computed(() => {
  172. return productData.list.filter(item => productData.selectedIds.includes(item.id));
  173. });
  174. function switchPayWay() {
  175. payType.value = payType.value == 'weixin' ? 'zhifubao' : 'weixin'
  176. }
  177. function productBtn(item) {
  178. mallProductPopup.value.detailShow(item);
  179. }
  180. function ykConfirm() {
  181. uni.redirectTo({
  182. url: '/pages/login/index'
  183. });
  184. }
  185. function detailBtn() {
  186. // // 获取选中商品的cardId列表
  187. // const selectedProducts = productData.list.filter(
  188. // item => productData.selectedIds.includes(item.id)
  189. // );
  190. // const cardIds = selectedProducts.map(item => item.id);
  191. // console.log('cardIds', cardIds);
  192. if (productData.selectedIds.length === 0) {
  193. toast("请选择至少一个商品查看明细");
  194. return;
  195. }
  196. showDetail.value = !showDetail.value;
  197. if (showDetail.value) {
  198. const selected = productData.list.filter(item =>
  199. productData.selectedIds.includes(item.id)
  200. );
  201. mxjtClass.value = 'mxjt-zk-icon';
  202. mallDetailPopup.value.detailShow(selected);
  203. } else {
  204. mxjtClass.value = 'mxjt-sq-icon';
  205. mallDetailPopup.value.detailCloseBtn();
  206. }
  207. }
  208. const isIOSorAndroid = () => {
  209. const systemInfo = uni.getSystemInfoSync();
  210. console.log('systemInfo', systemInfo);
  211. if (systemInfo.platform == 'ios') {
  212. return currentPlatform.value = 'ios'
  213. } else {
  214. return currentPlatform.value = 'android'
  215. }
  216. }
  217. function genggaiVip(data) {
  218. uni.hideLoading();
  219. const localList = cacheManager.get('auth').levelIdList || []
  220. const mergeList = [...new Set([...localList, ...data.levelIdList])]
  221. cacheManager.updateObject('auth', {
  222. levelIdList: mergeList
  223. })
  224. toast("支付成功")
  225. uni.redirectTo({
  226. url: '/pages/pay/order?status=2'
  227. })
  228. // toast("chenggong!!!! 之后跳转我的订单页面")
  229. // if (formPage.value == 'my') {
  230. // uni.redirectTo({
  231. // url: '/pages/my/index'
  232. // })
  233. // } else {
  234. // uni.redirectTo({
  235. // url: '/pages/study/index'
  236. // })
  237. // }
  238. }
  239. const creatOrderIos = debounce((data => {
  240. if (productData.selectedIds.length == 0) {
  241. toast("请选择至少一个商品");
  242. return;
  243. }
  244. const selectedProducts = productData.list.filter(
  245. item => productData.selectedIds.includes(item.id)
  246. );
  247. const cardIds = selectedProducts.map(item => item.id);
  248. console.log('cardIds', cardIds);
  249. uni.showLoading({
  250. title: '',
  251. mask: true
  252. });
  253. if (appleFlag.value.toString() == 'true') {
  254. productId.value = 'llisoftEzhuangyuanceshi'
  255. } else {
  256. const productMap = {
  257. 4: 'ezySxJstxL1', //计算特训 L1
  258. 6: 'ezySxTjmtL1', //图解母题 L1
  259. 7: 'ezySxJstxL2', //计算特训 L2
  260. 8: 'ezySxJstxL3', //计算特训 L3
  261. 9: 'ezySxJstxL4', //计算特训 L4
  262. 10: 'ezySxJstxL5', //计算特训 L5
  263. 11: 'ezySxJstxL6', //计算特训 L6
  264. 12: 'ezySxTjmtL2', //图解母题 L2
  265. 13: 'ezySxTjmtL3', //图解母题 L3
  266. 14: 'ezySxTjmtL4', //图解母题 L4
  267. 15: 'ezySxTjmtL5', //图解母题 L5
  268. 16: 'ezySxTjmtL6', //图解母题 L6
  269. 22: 'ezySxTjmtL1L6', //图解母题 L1-6
  270. 23: 'ezyYyZrpdL1', //自然拼读 L1
  271. 24: 'ezyYyZrpdL2', //自然拼读 L2
  272. 25: 'ezyYyZrpdL3', //自然拼读 L3
  273. 26: 'ezyYyZrpdL4', //自然拼读 L4
  274. 27: 'ezyYyZrpdL5', //自然拼读 L5
  275. 28: 'ezyYyZrpdL1L5', //自然拼读 L1-l5
  276. 29: ' ezySxJstxL1L6' //计算特训 L1-L6
  277. };
  278. const firstId = cardIds.length > 0 ? cardIds[0] : null;
  279. if (firstId) {
  280. productId.value = productMap[firstId]
  281. console.log('productId.value', productId.value);
  282. } else {
  283. toast("无对应产品Id,请练习管理员")
  284. uni.hideLoading();
  285. return false
  286. }
  287. }
  288. let req = {
  289. cardIds: cardIds
  290. }
  291. orderAdd(req).then(res => {
  292. console.log('res', res);
  293. if (res.code == 0) {
  294. uni.hideLoading();
  295. orderId.value = res.data.id
  296. // 测试ios 1元
  297. applePay()
  298. } else {
  299. uni.hideLoading();
  300. return false
  301. console.log('请求失败');
  302. }
  303. }).catch((e) => {
  304. uni.hideLoading();
  305. toast("订单创建失败")
  306. return false
  307. })
  308. }), 500)
  309. function applePaySuccess(data) {
  310. uni.showLoading({
  311. title: '开通中',
  312. mask: true
  313. });
  314. let req = {
  315. "id": orderId.value,
  316. "paynum": data.transactionIdentifier,
  317. "receipt": data.transactionReceipt
  318. }
  319. console.log('reqreq', req);
  320. orderPayApple(req).then(res => {
  321. if (res.code == 0 && res.data) {
  322. iapChannel.finishTransaction(data.transactionIdentifier)
  323. console.log('resiapChanneliapChanneliapChannel', res);
  324. orderCheck({
  325. id: orderId.value
  326. }).then(res3 => {
  327. console.log('res3', res3);
  328. if (res3.code == 0 && res3.data.success) {
  329. genggaiVip(res3.data)
  330. } else {
  331. setTimeout(() => {
  332. orderCheck({
  333. id: orderId.value
  334. }).then(res4 => {
  335. if (res4.code == 0 && res4.data.success) {
  336. genggaiVip(res4.data)
  337. } else {
  338. toast(
  339. "开通失败,请联系管理员!"
  340. )
  341. uni
  342. .hideLoading();
  343. return false
  344. }
  345. }).catch(() => {
  346. uni.hideLoading();
  347. toast("check接口报错")
  348. return false
  349. })
  350. }, 5000)
  351. }
  352. }).catch(() => {
  353. uni.hideLoading();
  354. toast("check接口报错")
  355. return false
  356. })
  357. } else {
  358. iapChannel.finishTransaction(data.transactionIdentifier)
  359. uni.hideLoading();
  360. toast("苹果内购失败")
  361. console.log('orderPayApple失败');
  362. return false
  363. }
  364. })
  365. }
  366. function applePay() {
  367. console.log('123123');
  368. if (!productId.value) {
  369. uni.showToast({
  370. title: '苹果内购ID缺失,请选择其它支付方式或联系客服',
  371. icon: "none"
  372. });
  373. return false;
  374. }
  375. uni.showLoading({
  376. title: '正在支付中...'
  377. });
  378. try {
  379. plus.payment.getChannels(function(channels) { //判读项目支付通道开通情况
  380. for (var i in channels) {
  381. iapChannel = channels[i];
  382. // 获取 id 为 'appleiap' 的 channel
  383. console.info("支付通道", iapChannel)
  384. if (iapChannel.id === 'appleiap') { //开通了app应用内支付,在manifest.josn中设置,开通后需打自定议基座
  385. console.info("苹果支付通道", iapChannel)
  386. // ids 数组中的项为 App Store Connect 配置的内购买项目产品ID(productId)
  387. var ids = [productId.value];
  388. // iap 为应用内支付对象
  389. iapChannel.requestOrder(ids, function(e) {
  390. // 获取订单信息成功回调方法
  391. console.log('requestOrder success: ' + JSON.stringify(e));
  392. uni.requestPayment({
  393. provider: 'appleiap',
  394. orderInfo: {
  395. productid: productId.value, //产品id,来自于苹果
  396. quantity: quantity.value, //产品数量
  397. manualFinishTransaction: true
  398. },
  399. success: (e) => {
  400. uni.hideLoading();
  401. // toast("苹果内购成功")
  402. console.info("苹果内购成功", e)
  403. applePaySuccess(e)
  404. //e.payment.orderNo = that.orderNo
  405. //支付成功回调,前端调用后台接口
  406. },
  407. fail: (e) => {
  408. uni.hideLoading();
  409. toast("苹果内购失败")
  410. console.info("苹果内购失败", e)
  411. },
  412. })
  413. },
  414. function(e) {
  415. // 获取订单信息失败回调方法
  416. console.log('requestOrder failed: ' + JSON.stringify(e));
  417. });
  418. } else {
  419. console.log('不支持苹果支付')
  420. }
  421. }
  422. },
  423. function(e) {
  424. console.log("获取iap支付通道失败:" + e.message);
  425. });
  426. } catch (e) {
  427. uni.showModal({
  428. title: "init",
  429. content: e.message,
  430. showCancel: false
  431. });
  432. } finally {
  433. uni.hideLoading();
  434. }
  435. }
  436. function wxPay() {
  437. orderPayWx({
  438. id: orderId.value
  439. }).then(res2 => {
  440. uni.hideLoading();
  441. console.log('res2', res2);
  442. if (res2.code != 0) {
  443. return false
  444. }
  445. uni.requestPayment({
  446. "provider": "wxpay",
  447. "orderInfo": {
  448. "appid": res2.data.appid, // 应用ID(AppID)
  449. "partnerid": res2.data.partnerId, // 商户号(PartnerID)
  450. "prepayid": res2.data.prepayId, // 预支付交易会话ID
  451. "package": res2.data.packageVal, // 固定值
  452. "noncestr": res2.data.nonceStr, // 随机字符串
  453. "timestamp": res2.data.timestamp, // 时间戳(单位:秒)
  454. "sign": res2.data.sign // 签名,这里用的 MD5 签名
  455. }, //此处为服务器返回的订单信息字符串
  456. success: function(res) {
  457. //var rawdata = JSON.parse(res.rawdata);
  458. // console.log('res',res);
  459. // console.log('支付成功');
  460. // console.log('rawdata', rawdata);
  461. uni.showLoading({
  462. title: '开通中,请稍后...'
  463. });
  464. orderCheck({
  465. id: orderId.value
  466. }).then(res3 => {
  467. console.log('res3', res3);
  468. if (res3.code == 0 && res3.data.success) {
  469. genggaiVip(res3.data)
  470. } else {
  471. setTimeout(() => {
  472. orderCheck({
  473. id: orderId.value
  474. }).then(res4 => {
  475. if (res4.code == 0 && res4.data.success) {
  476. genggaiVip(res4.data)
  477. } else {
  478. toast(
  479. "开通失败,请联系管理员!"
  480. )
  481. uni
  482. .hideLoading();
  483. return false
  484. }
  485. }).catch(() => {
  486. uni.hideLoading();
  487. toast("check接口报错")
  488. return false
  489. })
  490. }, 5000)
  491. }
  492. }).catch(() => {
  493. uni.hideLoading();
  494. toast("check接口报错")
  495. return false
  496. })
  497. },
  498. fail: function(err) {
  499. uni.hideLoading();
  500. // toast('支付失败:' + JSON.stringify(err));
  501. console.log('支付失败:' + JSON.stringify(err));
  502. }
  503. });
  504. }).catch((error) => {
  505. uni.hideLoading();
  506. console.log(error);
  507. })
  508. }
  509. function aliApy() {
  510. orderPayAli({
  511. id: orderId.value
  512. }).then(res2 => {
  513. console.log('res2', res2);
  514. if (res2.code != 0) {
  515. return false
  516. }
  517. uni.hideLoading();
  518. uni.requestPayment({
  519. "provider": "alipay",
  520. "orderInfo": res2.data.text, //此处为服务器返回的订单信息字符串
  521. success: function(res) {
  522. // var rawdata = JSON.parse(res.rawdata);
  523. // console.log('支付成功');
  524. // console.log('rawdata', rawdata);
  525. uni.showLoading({
  526. title: '开通中,请稍后...'
  527. });
  528. orderCheck({
  529. id: orderId.value
  530. }).then(res3 => {
  531. if (res3.code == 0 && res3.data.success) {
  532. genggaiVip(res3.data)
  533. } else {
  534. setTimeout(() => {
  535. orderCheck({
  536. id: orderId.value
  537. }).then(res4 => {
  538. if (res4.code ==
  539. 0 && res4.data.success) {
  540. genggaiVip(res4.data)
  541. } else {
  542. toast(
  543. "开通失败,请联系管理员!"
  544. )
  545. uni
  546. .hideLoading();
  547. return false
  548. }
  549. }).catch(() => {
  550. uni.hideLoading();
  551. toast("check接口报错")
  552. return false
  553. })
  554. }, 5000)
  555. }
  556. }).catch(() => {
  557. uni.hideLoading();
  558. toast("check接口报错")
  559. return false
  560. })
  561. },
  562. fail: function(err) {
  563. console.log('支付失败:' + JSON.stringify(err));
  564. uni.hideLoading();
  565. }
  566. });
  567. })
  568. }
  569. const creatOrder = debounce((data) => {
  570. if (productData.selectedIds.length === 0) {
  571. toast("请选择至少一个商品");
  572. return;
  573. }
  574. // 获取选中商品的cardId列表
  575. const selectedProducts = productData.list.filter(
  576. item => productData.selectedIds.includes(item.id)
  577. );
  578. const cardIds = selectedProducts.map(item => item.id);
  579. console.log('cardIds', cardIds);
  580. uni.showLoading({
  581. title: '',
  582. mask: true
  583. });
  584. if (payType.value == 'weixin') {
  585. console.log('创建订单11');
  586. let req = {
  587. cardIds: cardIds
  588. }
  589. orderAdd(req).then(res => {
  590. console.log('res', res);
  591. console.log(' res.data.id', res.data.id);
  592. orderId.value = res.data.id
  593. wxPay()
  594. }).catch((err) => {
  595. uni.hideLoading();
  596. toast("订单创建失败")
  597. return false
  598. })
  599. } else {
  600. let req = {
  601. cardIds: cardIds
  602. }
  603. orderAdd(req).then(res => {
  604. console.log('res', res);
  605. orderId.value = res.data.id
  606. aliApy()
  607. }).catch((err) => {
  608. uni.hideLoading();
  609. toast("订单创建失败")
  610. return false
  611. })
  612. }
  613. }, 500)
  614. function onChangeTab(e) {
  615. console.log('e', e);
  616. productData.current = e.currentIndex
  617. productData.selectedIds = [] // 清空选中
  618. if (!cacheManager.get('auth')) {
  619. getMoreYk()
  620. } else {
  621. getMore()
  622. }
  623. }
  624. function getMore() {
  625. const opt = {
  626. subjectId: productData.current + 1
  627. }
  628. getMallist(opt).then(res => {
  629. if (res.code == 0) {
  630. productData.list = res.data
  631. // 初始化选中状态(当有外部cardId时)
  632. if (productData.cardId) {
  633. const exist = res.data.some(item => item.id == productData.cardId)
  634. if (exist) {
  635. productData.selectedIds = [Number(productData.cardId)]
  636. }
  637. }
  638. nextTick(() => {
  639. scrollToIdFun(productData.cardId)
  640. })
  641. }
  642. }).catch(err => {
  643. toast("获取产品数据失败")
  644. return false
  645. })
  646. }
  647. function getMoreYk() {
  648. const opt = {
  649. subjectId: productData.current + 1
  650. }
  651. getMallistCom(opt).then(res => {
  652. if (res.code == 0) {
  653. productData.list = res.data
  654. }
  655. }).catch(err => {
  656. toast("获取产品数据失败")
  657. return false
  658. })
  659. }
  660. function scrollToIdFun(targetId) {
  661. const index = productData.list.findIndex(item => item.id == targetId)
  662. console.log('index', index);
  663. if (index > -1) {
  664. // 安卓设备需要双保险
  665. scrollIntoId.value = `item-${targetId}`
  666. setTimeout(() => {
  667. scrollIntoId.value = `item-${targetId}`
  668. }, 300)
  669. }
  670. }
  671. function addProduct(item) {
  672. if (!cacheManager.get('auth')) {
  673. youkeDialogRef.value.handleShow();
  674. return false;
  675. }
  676. if (item.yigou == 1) {
  677. return false;
  678. }
  679. const currentSelected = productData.selectedIds;
  680. const isAlreadySelected = currentSelected.includes(item.id);
  681. if (currentPlatform.value == 'ios') {
  682. if (isAlreadySelected) {
  683. productData.selectedIds = currentSelected.filter(id => id !== item.id);
  684. } else {
  685. if (currentSelected.length >= 1) {
  686. toast("只允许单个购买!");
  687. return false;
  688. }
  689. productData.selectedIds = [item.id];
  690. }
  691. } else {
  692. const temp = [...currentSelected];
  693. const index = temp.indexOf(item.id);
  694. index > -1 ? temp.splice(index, 1) : temp.push(item.id);
  695. productData.selectedIds = temp;
  696. }
  697. }
  698. // function addProduct(item) {
  699. // if (!cacheManager.get('auth')) {
  700. // youkeDialogRef.value.handleShow();
  701. // return false
  702. // }
  703. // if (item.yigou == 1) {
  704. // return false
  705. // }
  706. // const temp = [...productData.selectedIds]
  707. // const index = temp.indexOf(item.id)
  708. // index > -1 ? temp.splice(index, 1) : temp.push(item.id)
  709. // productData.selectedIds = temp
  710. // console.log('productData.selectedIds', productData.selectedIds.length);
  711. // }
  712. function isItemSelected(id) {
  713. return productData.cardId ?
  714. id == productData.cardId // 有外部cardId时严格匹配
  715. :
  716. productData.selectedIds.includes(id) // 无cardId时检查选中列表
  717. }
  718. function goBack() {
  719. if (!cacheManager.get('auth')) {
  720. // 游客
  721. uni.redirectTo({
  722. url: `/pages/my/index?levelId=${levelId.value}&typeId=${typeId.value}&subjectId=${subjectId.value}&tipFlag=${tipFlag.value}`
  723. })
  724. } else {
  725. if (productData.from == 'daoPage') {
  726. uni.redirectTo({
  727. url: '/pages/study/index'
  728. })
  729. } else {
  730. uni.redirectTo({
  731. url: '/pages/my/index'
  732. })
  733. }
  734. }
  735. }
  736. onLoad((options) => {
  737. isIOSorAndroid()
  738. if (!cacheManager.get('auth')) {
  739. // 游客
  740. levelId.value = options.levelId
  741. typeId.value = options.typeId
  742. subjectId.value = options.subjectId
  743. tipFlag.value = options.tipFlag
  744. productData.current = Number(options.subjectId) - 1
  745. getMoreYk()
  746. } else {
  747. appleFlag.value = cacheManager.get("auth").appleFlag
  748. if (options.from == 'daoPage') {
  749. productData.current = Number(options.subjectId) - 1
  750. productData.cardId = options.cardId
  751. productData.from = options.from
  752. } else if (options.from == 'myPage') {
  753. productData.current = Number(options.subjectId) - 1
  754. productData.cardId = options.cardId
  755. productData.from = options.from
  756. } else {
  757. productData.current = Number(cacheManager.get('auth').subjectId) - 1
  758. }
  759. getMore();
  760. }
  761. })
  762. </script>
  763. <style>
  764. </style>