mallPage.vue 23 KB

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