detailDialog.vue 15 KB

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