detailDialog.vue 14 KB

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