detailDialog.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <template>
  2. <uni-popup ref="detailPopup" :animation="false" :is-mask-click="false"
  3. mask-background-color="rgba(51, 137, 217, 0.65);">
  4. <view class="mall-detail-dialog">
  5. <view class="detail-content-box">
  6. <icon class="yfmx-title"></icon>
  7. <icon class="dialog-close-btn" @click="detailCloseBtn"></icon>
  8. <view class="detail-body-box">
  9. <!-- 使用 checkbox-group 管理多选 -->
  10. <checkbox-group @change="handleCheckboxChange">
  11. <view class="detail-item-box" v-for="item in localList" :key="item.id">
  12. <checkbox :value="item.id.toString()" :checked="selectedIds.includes(item.id)"
  13. class="detail-checkbox" color="transparent" />
  14. <img :src="item.cover" class="mall-image" />
  15. <view class="content-body-box">
  16. <view class="content-name">
  17. <view class="name-text">{{item.name}}</view>
  18. </view>
  19. <view class="content-text">{{item.intro}}</view>
  20. <view class="content-row">
  21. <view class="content-yuanjia">¥{{item.xianjia}}</view>
  22. </view>
  23. </view>
  24. </view>
  25. </checkbox-group>
  26. </view>
  27. <!-- 全选/取消全选 -->
  28. <view class="select-all-box" @click="toggleSelectAll">
  29. <checkbox :checked="isAllSelected" class="detail-checkbox" color="transparent" />
  30. <text>全选</text>
  31. </view>
  32. </view>
  33. <!-- 子组件自己的底部结算栏 -->
  34. <view class="footer-mall-pay-box">
  35. <view class="mall-left-box">
  36. <view class="price-icon-box">
  37. <text class="red-price fh-text">¥</text>
  38. <text class="red-price">{{totalPrice}}</text>
  39. </view>
  40. <view>购买即同意虚拟产品不支持退订</view>
  41. </view>
  42. <view class="pay-status-box" v-if="payType =='weixin'&&currentPlatform =='android'"
  43. @click="switchPayWay">
  44. <icon class="wx-icon"></icon>微信
  45. </view>
  46. <view class="pay-status-box" v-if="payType =='zhifubao'&&currentPlatform =='android'"
  47. @click="switchPayWay">
  48. <icon class="zfb-icon"></icon>支付宝
  49. </view>
  50. <view class="pay-status-box apple-status-box" v-if="currentPlatform =='ios'">
  51. <icon class="apple-icon"></icon>apple
  52. </view>
  53. <view v-if="currentPlatform =='android'" class="pay-btn" @touchstart="creatOrder">立即支付</view>
  54. <view v-if="currentPlatform =='ios'" class="pay-btn" @touchstart="creatOrderIos">立即支付</view>
  55. </view>
  56. </view>
  57. </uni-popup>
  58. </template>
  59. <script setup>
  60. import {
  61. ref,
  62. computed,
  63. onMounted,
  64. watch
  65. } from 'vue';
  66. import {
  67. debounce,
  68. toast
  69. } from "@/utils/common";
  70. import cacheManager from "@/utils/cacheManager.js";
  71. import {
  72. orderAdd,
  73. orderPayAli,
  74. orderPayWx,
  75. orderPayApple,
  76. orderCheck
  77. } from "@/api/order.js"
  78. const props = defineProps({
  79. selectedList: { // 父组件传入的初始选中商品列表
  80. type: Array,
  81. default: () => []
  82. }
  83. });
  84. const payType = ref('weixin');
  85. const mxjtClass = ref('mxjt-sq-icon');
  86. // 本地维护的商品列表(独立于父组件)
  87. const localList = ref([]);
  88. const selectedIds = ref([]); // 存储选中项的id
  89. let orderId = ref('');
  90. let productId = ref(null)
  91. let iapChannel = ref(null)
  92. let quantity = ref(1)
  93. let channel = ref('')
  94. let appleFlag = ref('')
  95. // 本地选中状态管理
  96. const localSelectedMap = ref({});
  97. let currentPlatform = ref('android')
  98. // 初始化本地数据
  99. // 初始化数据
  100. // watch(() => props.selectedList, (newVal) => {
  101. // localList.value = [...newVal];
  102. // selectedIds.value = newVal.map(item => item.id); // 初始全部选中
  103. // }, {
  104. // immediate: true
  105. // });
  106. onMounted(() => {
  107. isIOSorAndroid()
  108. })
  109. const isIOSorAndroid = () => {
  110. const systemInfo = uni.getSystemInfoSync();
  111. console.log('systemInfo', systemInfo);
  112. if (systemInfo.platform == 'ios') {
  113. return currentPlatform.value = 'ios'
  114. } else {
  115. return currentPlatform.value = 'android'
  116. }
  117. }
  118. function genggaiVip(data) {
  119. uni.hideLoading();
  120. const localList = cacheManager.get('auth').levelIdList || []
  121. const mergeList = [...new Set([...localList, ...data.levelIdList])]
  122. cacheManager.updateObject('auth', {
  123. levelIdList: mergeList
  124. })
  125. toast("支付成功")
  126. uni.redirectTo({
  127. url: '/pages/pay/order?status=2'
  128. })
  129. // if (formPage.value == 'my') {
  130. // uni.redirectTo({
  131. // url: '/pages/my/index'
  132. // })
  133. // } else {
  134. // uni.redirectTo({
  135. // url: '/pages/study/index'
  136. // })
  137. // }
  138. }
  139. const creatOrderIos = debounce((data => {
  140. const selectedItems = localList.value
  141. .filter(item => selectedIds.value.includes(item.id));
  142. console.log('selectedItems', selectedItems);
  143. const cardIds = selectedItems.map(item => item.id);
  144. if (cardIds.length == 0) {
  145. uni.showToast({
  146. title: '请选择至少一个商品',
  147. icon: 'none'
  148. });
  149. return;
  150. }
  151. uni.showLoading({
  152. title: '',
  153. mask: true
  154. });
  155. const productMap = {
  156. 4: 'ezySxJstxL1', //计算特训 L1
  157. 6: 'ezySxTjmtL1',//图解母题 L1
  158. 7: 'ezySxJstxL2',//计算特训 L2
  159. 8: 'ezySxJstxL3',//计算特训 L3
  160. 9: 'ezySxJstxL4',//计算特训 L4
  161. 10: 'ezySxJstxL5',//计算特训 L5
  162. 11: 'ezySxJstxL6',//计算特训 L6
  163. 12: 'ezySxTjmtL2',//图解母题 L2
  164. 13: 'ezySxTjmtL3',//图解母题 L3
  165. 14: 'ezySxTjmtL4',//图解母题 L4
  166. 15: 'ezySxTjmtL5',//图解母题 L5
  167. 16: 'ezySxTjmtL6',//图解母题 L6
  168. 22: 'ezySxTjmtL1L6',//图解母题 L1-6
  169. 23: 'ezyYyZrpdL1',//自然拼读 L1
  170. 24: 'ezyYyZrpdL2',//自然拼读 L2
  171. 25: 'ezyYyZrpdL3',//自然拼读 L3
  172. 26: 'ezyYyZrpdL4',//自然拼读 L4
  173. 27: 'ezyYyZrpdL5',//自然拼读 L5
  174. 28: 'ezyYyZrpdL1L5',//自然拼读 L1-l5
  175. 29: ' ezySxJstxL1L6'//计算特训 L1-L6
  176. };
  177. const firstId = cardIds.length > 0 ? cardIds[0] : null;
  178. if (firstId) {
  179. productId.value = productMap[firstId]
  180. console.log('productId.value',productId.value);
  181. } else {
  182. toast("无对应产品Id,请练习管理员")
  183. uni.hideLoading();
  184. return false
  185. }
  186. let req = {
  187. cardIds: cardIds
  188. }
  189. orderAdd(req).then(res => {
  190. console.log('res', res);
  191. if (res.code == 0) {
  192. setTimeout(() => {
  193. uni.hideLoading();
  194. }, 1000)
  195. orderId.value = res.data.id
  196. // 测试ios 1元
  197. applePay()
  198. } else {
  199. uni.hideLoading();
  200. return false
  201. console.log('请求失败');
  202. }
  203. }).catch((e) => {
  204. uni.hideLoading();
  205. toast("订单创建失败")
  206. return false
  207. })
  208. }), 500)
  209. function applePaySuccess(data) {
  210. uni.showLoading({
  211. title: '开通中',
  212. mask: true
  213. });
  214. let req = {
  215. "id": orderId.value,
  216. "paynum": data.transactionIdentifier,
  217. "receipt": data.transactionReceipt
  218. }
  219. console.log('reqreq', req);
  220. orderPayApple(req).then(res => {
  221. if (res.code == 0 && res.data) {
  222. iapChannel.finishTransaction(data.transactionIdentifier)
  223. console.log('resiapChanneliapChanneliapChannel', res);
  224. orderCheck({
  225. id: orderId.value
  226. }).then(res3 => {
  227. console.log('res3', res3);
  228. if (res3.code == 0&&res3.data.success) {
  229. genggaiVip(res3.data)
  230. } else {
  231. setTimeout(() => {
  232. orderCheck({
  233. id: orderId.value
  234. }).then(res4 => {
  235. if (res4.code == 0&&res4.data.success) {
  236. genggaiVip(res4.data)
  237. } else {
  238. toast(
  239. "开通失败,请联系管理员!"
  240. )
  241. uni
  242. .hideLoading();
  243. return false
  244. }
  245. }).catch(() => {
  246. uni.hideLoading();
  247. toast("check接口报错")
  248. return false
  249. })
  250. }, 5000)
  251. }
  252. }).catch(() => {
  253. uni.hideLoading();
  254. toast("check接口报错")
  255. return false
  256. })
  257. } else {
  258. iapChannel.finishTransaction(data.transactionIdentifier)
  259. uni.hideLoading();
  260. toast("苹果内购失败")
  261. console.log('orderPayApple失败');
  262. return false
  263. }
  264. })
  265. }
  266. function applePay() {
  267. console.log('123123');
  268. if (!productId.value) {
  269. uni.showToast({
  270. title: '苹果内购ID缺失,请选择其它支付方式或联系客服',
  271. icon: "none"
  272. });
  273. return false;
  274. }
  275. uni.showLoading({
  276. title: '正在支付中...'
  277. });
  278. try {
  279. plus.payment.getChannels(function(channels) { //判读项目支付通道开通情况
  280. for (var i in channels) {
  281. iapChannel = channels[i];
  282. // 获取 id 为 'appleiap' 的 channel
  283. console.info("支付通道", iapChannel)
  284. if (iapChannel.id === 'appleiap') { //开通了app应用内支付,在manifest.josn中设置,开通后需打自定议基座
  285. console.info("苹果支付通道", iapChannel)
  286. // ids 数组中的项为 App Store Connect 配置的内购买项目产品ID(productId)
  287. var ids = [productId.value];
  288. // iap 为应用内支付对象
  289. iapChannel.requestOrder(ids, function(e) {
  290. // 获取订单信息成功回调方法
  291. console.log('requestOrder success: ' + JSON.stringify(e));
  292. uni.requestPayment({
  293. provider: 'appleiap',
  294. orderInfo: {
  295. productid: productId.value, //产品id,来自于苹果
  296. quantity: quantity.value, //产品数量
  297. manualFinishTransaction: true
  298. },
  299. success: (e) => {
  300. uni.hideLoading();
  301. // toast("苹果内购成功")
  302. console.info("苹果内购成功", e)
  303. applePaySuccess(e)
  304. //e.payment.orderNo = that.orderNo
  305. //支付成功回调,前端调用后台接口
  306. },
  307. fail: (e) => {
  308. uni.hideLoading();
  309. toast("苹果内购失败")
  310. console.info("苹果内购失败", e)
  311. },
  312. })
  313. },
  314. function(e) {
  315. // 获取订单信息失败回调方法
  316. console.log('requestOrder failed: ' + JSON.stringify(e));
  317. });
  318. } else {
  319. console.log('不支持苹果支付')
  320. }
  321. }
  322. },
  323. function(e) {
  324. console.log("获取iap支付通道失败:" + e.message);
  325. });
  326. } catch (e) {
  327. uni.showModal({
  328. title: "init",
  329. content: e.message,
  330. showCancel: false
  331. });
  332. } finally {
  333. uni.hideLoading();
  334. }
  335. }
  336. function wxPay() {
  337. orderPayWx({
  338. id: orderId.value
  339. }).then(res2 => {
  340. uni.hideLoading();
  341. console.log('res2', res2);
  342. if (res2.code != 0) {
  343. return false
  344. }
  345. uni.requestPayment({
  346. "provider": "wxpay",
  347. "orderInfo": {
  348. "appid": res2.data.appid, // 应用ID(AppID)
  349. "partnerid": res2.data.partnerId, // 商户号(PartnerID)
  350. "prepayid": res2.data.prepayId, // 预支付交易会话ID
  351. "package": res2.data.packageVal, // 固定值
  352. "noncestr": res2.data.nonceStr, // 随机字符串
  353. "timestamp": res2.data.timestamp, // 时间戳(单位:秒)
  354. "sign": res2.data.sign // 签名,这里用的 MD5 签名
  355. }, //此处为服务器返回的订单信息字符串
  356. success: function(res) {
  357. //var rawdata = JSON.parse(res.rawdata);
  358. // console.log('res',res);
  359. // console.log('支付成功');
  360. // console.log('rawdata', rawdata);
  361. uni.showLoading({
  362. title: '开通中,请稍后...'
  363. });
  364. orderCheck({
  365. id: orderId.value
  366. }).then(res3 => {
  367. console.log('res3', res3);
  368. if (res3.code == 0&&res3.data.success) {
  369. genggaiVip(res3.data)
  370. } else {
  371. setTimeout(() => {
  372. orderCheck({
  373. id: orderId.value
  374. }).then(res4 => {
  375. if (res4.code == 0&&res4.data.success) {
  376. genggaiVip(res4.data)
  377. } else {
  378. toast(
  379. "开通失败,请联系管理员!"
  380. )
  381. uni
  382. .hideLoading();
  383. return false
  384. }
  385. }).catch(() => {
  386. uni.hideLoading();
  387. toast("check接口报错")
  388. return false
  389. })
  390. }, 5000)
  391. }
  392. }).catch(() => {
  393. uni.hideLoading();
  394. toast("check接口报错")
  395. return false
  396. })
  397. },
  398. fail: function(err) {
  399. uni.hideLoading();
  400. // toast('支付失败:' + JSON.stringify(err));
  401. console.log('支付失败:' + JSON.stringify(err));
  402. }
  403. });
  404. }).catch((error) => {
  405. uni.hideLoading();
  406. console.log(error);
  407. })
  408. }
  409. function aliApy() {
  410. orderPayAli({
  411. id: orderId.value
  412. }).then(res2 => {
  413. console.log('res2', res2);
  414. uni.hideLoading();
  415. if (res2.code != 0) {
  416. return false
  417. }
  418. uni.requestPayment({
  419. "provider": "alipay",
  420. "orderInfo": res2.data.text, //此处为服务器返回的订单信息字符串
  421. success: function(res) {
  422. // var rawdata = JSON.parse(res.rawdata);
  423. // console.log('支付成功');
  424. // console.log('rawdata', rawdata);
  425. uni.showLoading({
  426. title: '开通中,请稍后...'
  427. });
  428. orderCheck({
  429. id: orderId.value
  430. }).then(res3 => {
  431. if (res3.code == 0&&res3.data.success) {
  432. genggaiVip(res3.data)
  433. } else {
  434. setTimeout(() => {
  435. orderCheck({
  436. id: orderId.value
  437. }).then(res4 => {
  438. if (res4.code ==
  439. 0&&res4.data.success) {
  440. genggaiVip(res4.data)
  441. } else {
  442. toast(
  443. "开通失败,请联系管理员!"
  444. )
  445. uni
  446. .hideLoading();
  447. return false
  448. }
  449. }).catch(() => {
  450. uni.hideLoading();
  451. toast("check接口报错")
  452. return false
  453. })
  454. }, 5000)
  455. }
  456. }).catch(() => {
  457. uni.hideLoading();
  458. toast("check接口报错")
  459. return false
  460. })
  461. },
  462. fail: function(err) {
  463. console.log('支付失败:' + JSON.stringify(err));
  464. uni.hideLoading();
  465. }
  466. });
  467. })
  468. }
  469. const creatOrder = debounce((data) => {
  470. const selectedItems = localList.value
  471. .filter(item => selectedIds.value.includes(item.id));
  472. console.log('selectedItems', selectedItems);
  473. const cardIds = selectedItems.map(item => item.id);
  474. if (cardIds.length === 0) {
  475. uni.showToast({
  476. title: '请选择至少一个商品',
  477. icon: 'none'
  478. });
  479. return;
  480. }
  481. console.log('123123123123');
  482. uni.showLoading({
  483. title: '',
  484. mask: true
  485. });
  486. if (payType.value == 'weixin') {
  487. console.log('创建订单11');
  488. let req = {
  489. cardIds: cardIds
  490. }
  491. orderAdd(req).then(res => {
  492. console.log('res', res);
  493. console.log(' res.data.id', res.data.id);
  494. orderId.value = res.data.id
  495. wxPay()
  496. }).catch((err) => {
  497. uni.hideLoading();
  498. toast("订单创建失败")
  499. return false
  500. })
  501. } else {
  502. let req = {
  503. cardIds: cardIds
  504. }
  505. orderAdd(req).then(res => {
  506. console.log('res', res);
  507. orderId.value = res.data.id
  508. aliApy()
  509. }).catch((err) => {
  510. uni.hideLoading();
  511. toast("订单创建失败")
  512. return false
  513. })
  514. }
  515. }, 500)
  516. // 是否全选
  517. const isAllSelected = computed(() => {
  518. return localList.value.length > 0 &&
  519. selectedIds.value.length === localList.value.length;
  520. });
  521. // 处理checkbox变化
  522. function handleCheckboxChange(e) {
  523. selectedIds.value = e.detail.value.map(id => parseInt(id));
  524. }
  525. // 全选/取消全选
  526. function toggleSelectAll() {
  527. if (isAllSelected.value) {
  528. selectedIds.value = []; // 取消全选
  529. } else {
  530. selectedIds.value = localList.value.map(item => item.id); // 全选
  531. }
  532. }
  533. // 计算总价
  534. const totalPrice = computed(() => {
  535. return localList.value
  536. .filter(item => selectedIds.value.includes(item.id))
  537. .reduce((sum, item) => sum + parseFloat(item.xianjia || 0), 0)
  538. .toFixed(2);
  539. });
  540. // 支付方式切换
  541. function switchPayWay() {
  542. payType.value = payType.value == 'weixin' ? 'zhifubao' : 'weixin'
  543. }
  544. // 支付处理
  545. const detailPopup = ref(null);
  546. function detailShow(newVal) {
  547. console.log('getSelectedProducts', newVal);
  548. localList.value = [...newVal];
  549. selectedIds.value = newVal.map(item => item.id); // 初始全部选中
  550. detailPopup.value.open();
  551. }
  552. function detailCloseBtn() {
  553. detailPopup.value.close();
  554. }
  555. defineExpose({
  556. detailShow,
  557. detailCloseBtn
  558. });
  559. </script>
  560. <style>
  561. </style>