detailDialog.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. uni.hideLoading();
  193. orderId.value = res.data.id
  194. // 测试ios 1元
  195. applePay()
  196. } else {
  197. uni.hideLoading();
  198. return false
  199. console.log('请求失败');
  200. }
  201. }).catch((e) => {
  202. uni.hideLoading();
  203. toast("订单创建失败")
  204. return false
  205. })
  206. }), 500)
  207. function applePaySuccess(data) {
  208. uni.showLoading({
  209. title: '开通中',
  210. mask: true
  211. });
  212. let req = {
  213. "id": orderId.value,
  214. "paynum": data.transactionIdentifier,
  215. "receipt": data.transactionReceipt
  216. }
  217. console.log('reqreq', req);
  218. orderPayApple(req).then(res => {
  219. if (res.code == 0 && res.data) {
  220. iapChannel.finishTransaction(data.transactionIdentifier)
  221. console.log('resiapChanneliapChanneliapChannel', res);
  222. orderCheck({
  223. id: orderId.value
  224. }).then(res3 => {
  225. console.log('res3', res3);
  226. if (res3.code == 0&&res3.data.success) {
  227. genggaiVip(res3.data)
  228. } else {
  229. setTimeout(() => {
  230. orderCheck({
  231. id: orderId.value
  232. }).then(res4 => {
  233. if (res4.code == 0&&res4.data.success) {
  234. genggaiVip(res4.data)
  235. } else {
  236. toast(
  237. "开通失败,请联系管理员!"
  238. )
  239. uni
  240. .hideLoading();
  241. return false
  242. }
  243. }).catch(() => {
  244. uni.hideLoading();
  245. toast("check接口报错")
  246. return false
  247. })
  248. }, 5000)
  249. }
  250. }).catch(() => {
  251. uni.hideLoading();
  252. toast("check接口报错")
  253. return false
  254. })
  255. } else {
  256. iapChannel.finishTransaction(data.transactionIdentifier)
  257. uni.hideLoading();
  258. toast("苹果内购失败")
  259. console.log('orderPayApple失败');
  260. return false
  261. }
  262. })
  263. }
  264. function applePay() {
  265. console.log('123123');
  266. if (!productId.value) {
  267. uni.showToast({
  268. title: '苹果内购ID缺失,请选择其它支付方式或联系客服',
  269. icon: "none"
  270. });
  271. return false;
  272. }
  273. uni.showLoading({
  274. title: '正在支付中...'
  275. });
  276. try {
  277. plus.payment.getChannels(function(channels) { //判读项目支付通道开通情况
  278. for (var i in channels) {
  279. iapChannel = channels[i];
  280. // 获取 id 为 'appleiap' 的 channel
  281. console.info("支付通道", iapChannel)
  282. if (iapChannel.id === 'appleiap') { //开通了app应用内支付,在manifest.josn中设置,开通后需打自定议基座
  283. console.info("苹果支付通道", iapChannel)
  284. // ids 数组中的项为 App Store Connect 配置的内购买项目产品ID(productId)
  285. var ids = [productId.value];
  286. // iap 为应用内支付对象
  287. iapChannel.requestOrder(ids, function(e) {
  288. // 获取订单信息成功回调方法
  289. console.log('requestOrder success: ' + JSON.stringify(e));
  290. uni.requestPayment({
  291. provider: 'appleiap',
  292. orderInfo: {
  293. productid: productId.value, //产品id,来自于苹果
  294. quantity: quantity.value, //产品数量
  295. manualFinishTransaction: true
  296. },
  297. success: (e) => {
  298. uni.hideLoading();
  299. // toast("苹果内购成功")
  300. console.info("苹果内购成功", e)
  301. applePaySuccess(e)
  302. //e.payment.orderNo = that.orderNo
  303. //支付成功回调,前端调用后台接口
  304. },
  305. fail: (e) => {
  306. uni.hideLoading();
  307. toast("苹果内购失败")
  308. console.info("苹果内购失败", e)
  309. },
  310. })
  311. },
  312. function(e) {
  313. // 获取订单信息失败回调方法
  314. console.log('requestOrder failed: ' + JSON.stringify(e));
  315. });
  316. } else {
  317. console.log('不支持苹果支付')
  318. }
  319. }
  320. },
  321. function(e) {
  322. console.log("获取iap支付通道失败:" + e.message);
  323. });
  324. } catch (e) {
  325. uni.showModal({
  326. title: "init",
  327. content: e.message,
  328. showCancel: false
  329. });
  330. } finally {
  331. uni.hideLoading();
  332. }
  333. }
  334. function wxPay() {
  335. orderPayWx({
  336. id: orderId.value
  337. }).then(res2 => {
  338. uni.hideLoading();
  339. console.log('res2', res2);
  340. if (res2.code != 0) {
  341. return false
  342. }
  343. uni.requestPayment({
  344. "provider": "wxpay",
  345. "orderInfo": {
  346. "appid": res2.data.appid, // 应用ID(AppID)
  347. "partnerid": res2.data.partnerId, // 商户号(PartnerID)
  348. "prepayid": res2.data.prepayId, // 预支付交易会话ID
  349. "package": res2.data.packageVal, // 固定值
  350. "noncestr": res2.data.nonceStr, // 随机字符串
  351. "timestamp": res2.data.timestamp, // 时间戳(单位:秒)
  352. "sign": res2.data.sign // 签名,这里用的 MD5 签名
  353. }, //此处为服务器返回的订单信息字符串
  354. success: function(res) {
  355. //var rawdata = JSON.parse(res.rawdata);
  356. // console.log('res',res);
  357. // console.log('支付成功');
  358. // console.log('rawdata', rawdata);
  359. uni.showLoading({
  360. title: '开通中,请稍后...'
  361. });
  362. orderCheck({
  363. id: orderId.value
  364. }).then(res3 => {
  365. console.log('res3', res3);
  366. if (res3.code == 0&&res3.data.success) {
  367. genggaiVip(res3.data)
  368. } else {
  369. setTimeout(() => {
  370. orderCheck({
  371. id: orderId.value
  372. }).then(res4 => {
  373. if (res4.code == 0&&res4.data.success) {
  374. genggaiVip(res4.data)
  375. } else {
  376. toast(
  377. "开通失败,请联系管理员!"
  378. )
  379. uni
  380. .hideLoading();
  381. return false
  382. }
  383. }).catch(() => {
  384. uni.hideLoading();
  385. toast("check接口报错")
  386. return false
  387. })
  388. }, 5000)
  389. }
  390. }).catch(() => {
  391. uni.hideLoading();
  392. toast("check接口报错")
  393. return false
  394. })
  395. },
  396. fail: function(err) {
  397. uni.hideLoading();
  398. // toast('支付失败:' + JSON.stringify(err));
  399. console.log('支付失败:' + JSON.stringify(err));
  400. }
  401. });
  402. }).catch((error) => {
  403. uni.hideLoading();
  404. console.log(error);
  405. })
  406. }
  407. function aliApy() {
  408. orderPayAli({
  409. id: orderId.value
  410. }).then(res2 => {
  411. console.log('res2', res2);
  412. uni.hideLoading();
  413. if (res2.code != 0) {
  414. return false
  415. }
  416. uni.requestPayment({
  417. "provider": "alipay",
  418. "orderInfo": res2.data.text, //此处为服务器返回的订单信息字符串
  419. success: function(res) {
  420. // var rawdata = JSON.parse(res.rawdata);
  421. // console.log('支付成功');
  422. // console.log('rawdata', rawdata);
  423. uni.showLoading({
  424. title: '开通中,请稍后...'
  425. });
  426. orderCheck({
  427. id: orderId.value
  428. }).then(res3 => {
  429. if (res3.code == 0&&res3.data.success) {
  430. genggaiVip(res3.data)
  431. } else {
  432. setTimeout(() => {
  433. orderCheck({
  434. id: orderId.value
  435. }).then(res4 => {
  436. if (res4.code ==
  437. 0&&res4.data.success) {
  438. genggaiVip(res4.data)
  439. } else {
  440. toast(
  441. "开通失败,请联系管理员!"
  442. )
  443. uni
  444. .hideLoading();
  445. return false
  446. }
  447. }).catch(() => {
  448. uni.hideLoading();
  449. toast("check接口报错")
  450. return false
  451. })
  452. }, 5000)
  453. }
  454. }).catch(() => {
  455. uni.hideLoading();
  456. toast("check接口报错")
  457. return false
  458. })
  459. },
  460. fail: function(err) {
  461. console.log('支付失败:' + JSON.stringify(err));
  462. uni.hideLoading();
  463. }
  464. });
  465. })
  466. }
  467. const creatOrder = debounce((data) => {
  468. const selectedItems = localList.value
  469. .filter(item => selectedIds.value.includes(item.id));
  470. console.log('selectedItems', selectedItems);
  471. const cardIds = selectedItems.map(item => item.id);
  472. if (cardIds.length === 0) {
  473. uni.showToast({
  474. title: '请选择至少一个商品',
  475. icon: 'none'
  476. });
  477. return;
  478. }
  479. console.log('123123123123');
  480. uni.showLoading({
  481. title: '',
  482. mask: true
  483. });
  484. if (payType.value == 'weixin') {
  485. console.log('创建订单11');
  486. let req = {
  487. cardIds: cardIds
  488. }
  489. orderAdd(req).then(res => {
  490. console.log('res', res);
  491. console.log(' res.data.id', res.data.id);
  492. orderId.value = res.data.id
  493. wxPay()
  494. }).catch((err) => {
  495. uni.hideLoading();
  496. toast("订单创建失败")
  497. return false
  498. })
  499. } else {
  500. let req = {
  501. cardIds: cardIds
  502. }
  503. orderAdd(req).then(res => {
  504. console.log('res', res);
  505. orderId.value = res.data.id
  506. aliApy()
  507. }).catch((err) => {
  508. uni.hideLoading();
  509. toast("订单创建失败")
  510. return false
  511. })
  512. }
  513. }, 500)
  514. // 是否全选
  515. const isAllSelected = computed(() => {
  516. return localList.value.length > 0 &&
  517. selectedIds.value.length === localList.value.length;
  518. });
  519. // 处理checkbox变化
  520. function handleCheckboxChange(e) {
  521. selectedIds.value = e.detail.value.map(id => parseInt(id));
  522. }
  523. // 全选/取消全选
  524. function toggleSelectAll() {
  525. if (isAllSelected.value) {
  526. selectedIds.value = []; // 取消全选
  527. } else {
  528. selectedIds.value = localList.value.map(item => item.id); // 全选
  529. }
  530. }
  531. // 计算总价
  532. const totalPrice = computed(() => {
  533. return localList.value
  534. .filter(item => selectedIds.value.includes(item.id))
  535. .reduce((sum, item) => sum + parseFloat(item.xianjia || 0), 0)
  536. .toFixed(2);
  537. });
  538. // 支付方式切换
  539. function switchPayWay() {
  540. payType.value = payType.value == 'weixin' ? 'zhifubao' : 'weixin'
  541. }
  542. // 支付处理
  543. const detailPopup = ref(null);
  544. function detailShow(newVal) {
  545. console.log('getSelectedProducts', newVal);
  546. localList.value = [...newVal];
  547. selectedIds.value = newVal.map(item => item.id); // 初始全部选中
  548. detailPopup.value.open();
  549. }
  550. function detailCloseBtn() {
  551. detailPopup.value.close();
  552. }
  553. defineExpose({
  554. detailShow,
  555. detailCloseBtn
  556. });
  557. </script>
  558. <style>
  559. </style>