mallPage.vue 23 KB

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