detailDialog.vue 15 KB

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