detailDialog.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. <!-- 全选/取消全选 -->
  10. <view class="select-all-box" @click="toggleSelectAll">
  11. <checkbox :checked="isAllSelected" class="detail-checkbox" />
  12. <text>全选</text>
  13. </view>
  14. <!-- 使用 checkbox-group 管理多选 -->
  15. <checkbox-group @change="handleCheckboxChange">
  16. <view class="detail-item-box" v-for="item in localList" :key="item.id">
  17. <checkbox :value="item.id.toString()" :checked="selectedIds.includes(item.id)"
  18. class="detail-checkbox" />
  19. <img :src="item.cover" class="mall-image" />
  20. <view class="content-body-box">
  21. <view class="content-name">
  22. <view class="name-text">{{item.name}}</view>
  23. </view>
  24. <view class="content-text">{{item.intro}}</view>
  25. <view class="content-row">
  26. <view class="content-yuanjia">¥{{item.xianjia}}</view>
  27. </view>
  28. </view>
  29. </view>
  30. </checkbox-group>
  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. <icon :class="mxjtClass"></icon>
  40. </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'&&!disabled" class="open-svip-btn" @touchstart="creatOrder">立即支付
  54. </view>
  55. <view v-if="currentPlatform =='android'&&disabled" class="open-svip-btn-hui">立即支付</view>
  56. <view v-if="currentPlatform =='ios'&&!disabled" style="margin-top: 20rpx;" class="open-svip-btn"
  57. @touchstart="creatOrderIos">立即支付</view>
  58. <view v-if="currentPlatform =='ios'&&disabled" style="margin-top: 20rpx;" class="open-svip-btn-hui">立即支付
  59. </view>
  60. </view>
  61. </view>
  62. </uni-popup>
  63. </template>
  64. <script setup>
  65. import {
  66. ref,
  67. computed,
  68. onMounted,
  69. watch
  70. } from 'vue';
  71. import {
  72. debounce,
  73. toast
  74. } from "@/utils/common";
  75. import cacheManager from "@/utils/cacheManager.js";
  76. import {
  77. orderAdd,
  78. orderPayAli,
  79. orderPayWx,
  80. orderPayApple,
  81. orderCheck
  82. } from "@/api/order.js"
  83. const props = defineProps({
  84. selectedList: { // 父组件传入的初始选中商品列表
  85. type: Array,
  86. default: () => []
  87. }
  88. });
  89. const payType = ref('weixin');
  90. const mxjtClass = ref('mxjt-sq-icon');
  91. // 本地维护的商品列表(独立于父组件)
  92. const localList = ref([]);
  93. const selectedIds = ref([]); // 存储选中项的id
  94. let orderId = ref('');
  95. // 本地选中状态管理
  96. const localSelectedMap = ref({});
  97. let currentPlatform = ref('android')
  98. let disabled = ref(false)
  99. // 初始化本地数据
  100. // 初始化数据
  101. watch(() => props.selectedList, (newVal) => {
  102. localList.value = [...newVal];
  103. selectedIds.value = newVal.map(item => item.id); // 初始全部选中
  104. }, {
  105. immediate: true
  106. });
  107. onMounted(() => {
  108. isIOSorAndroid()
  109. })
  110. const isIOSorAndroid = () => {
  111. const systemInfo = uni.getSystemInfoSync();
  112. console.log('systemInfo', systemInfo);
  113. if (systemInfo.platform == 'ios') {
  114. return currentPlatform.value = 'ios'
  115. } else {
  116. return currentPlatform.value = 'android'
  117. }
  118. }
  119. function genggaiVip(data) {
  120. uni.hideLoading();
  121. disabled.value = false
  122. const localList = cacheManager.get('auth').levelIdList || []
  123. const mergeList = [...new Set([...localList, ...data.levelIdList])]
  124. cacheManager.updateObject('auth', {
  125. levelIdList: mergeList
  126. })
  127. toast("chenggong!!!! 之后跳转我的订单页面")
  128. // if (formPage.value == 'my') {
  129. // uni.redirectTo({
  130. // url: '/pages/my/index'
  131. // })
  132. // } else {
  133. // uni.redirectTo({
  134. // url: '/pages/study/index'
  135. // })
  136. // }
  137. }
  138. const creatOrderIos = debounce((data => {
  139. disabled.value = true
  140. uni.showLoading({
  141. title: '',
  142. mask: true
  143. });
  144. if (appleFlag.value.toString() == 'true') {
  145. productId.value = 'llisoftEzhuangyuanceshi'
  146. } else {
  147. if (cardId.value == 1) {
  148. productId.value = 'llisoftEzhuangyuan'
  149. } else {
  150. productId.value = 'llisoftEzhuangyuanYingyu'
  151. }
  152. }
  153. if (orderId.value) {
  154. uni.hideLoading();
  155. applePay()
  156. } else {
  157. let req = {
  158. cardId: cardId.value
  159. }
  160. orderAdd(req).then(res => {
  161. console.log('res', res);
  162. if (res.code == 0) {
  163. uni.hideLoading();
  164. orderId.value = res.data.id
  165. // 测试ios 1元
  166. applePay()
  167. } else {
  168. uni.hideLoading();
  169. //disabled.value = false
  170. return false
  171. console.log('请求失败');
  172. }
  173. }).catch((e) => {
  174. uni.hideLoading();
  175. disabled.value = false
  176. toast("订单创建失败")
  177. return false
  178. })
  179. }
  180. }), 500)
  181. function applePaySuccess(data) {
  182. uni.showLoading({
  183. title: '会员开通中',
  184. mask: true
  185. });
  186. let req = {
  187. "id": orderId.value,
  188. "paynum": data.transactionIdentifier,
  189. "receipt": data.transactionReceipt
  190. }
  191. console.log('reqreq', req);
  192. orderPayApple(req).then(res => {
  193. if (res.code == 0 && res.data) {
  194. iapChannel.finishTransaction(data.transactionIdentifier)
  195. console.log('resiapChanneliapChanneliapChannel', res);
  196. genggaiVip()
  197. } else {
  198. //disabled.value = false
  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. // disabled.value = false
  250. uni.hideLoading();
  251. toast("苹果内购失败")
  252. console.info("苹果内购失败", e)
  253. },
  254. })
  255. },
  256. function(e) {
  257. // 获取订单信息失败回调方法
  258. console.log('requestOrder failed: ' + JSON.stringify(e));
  259. });
  260. } else {
  261. console.log('不支持苹果支付')
  262. }
  263. }
  264. },
  265. function(e) {
  266. console.log("获取iap支付通道失败:" + e.message);
  267. });
  268. } catch (e) {
  269. uni.showModal({
  270. title: "init",
  271. content: e.message,
  272. showCancel: false
  273. });
  274. // disabled.value = false
  275. } finally {
  276. uni.hideLoading();
  277. }
  278. }
  279. function wxPay() {
  280. orderPayWx({
  281. id: orderId.value
  282. }).then(res2 => {
  283. uni.hideLoading();
  284. console.log('res2', res2);
  285. uni.requestPayment({
  286. "provider": "wxpay",
  287. "orderInfo": {
  288. "appid": res2.data.appid, // 应用ID(AppID)
  289. "partnerid": res2.data.partnerId, // 商户号(PartnerID)
  290. "prepayid": res2.data.prepayId, // 预支付交易会话ID
  291. "package": res2.data.packageVal, // 固定值
  292. "noncestr": res2.data.nonceStr, // 随机字符串
  293. "timestamp": res2.data.timestamp, // 时间戳(单位:秒)
  294. "sign": res2.data.sign // 签名,这里用的 MD5 签名
  295. }, //此处为服务器返回的订单信息字符串
  296. success: function(res) {
  297. //var rawdata = JSON.parse(res.rawdata);
  298. // console.log('res',res);
  299. // console.log('支付成功');
  300. // console.log('rawdata', rawdata);
  301. uni.showLoading({
  302. title: '会员开通中,请稍后...'
  303. });
  304. orderCheck({
  305. id: orderId.value
  306. }).then(res3 => {
  307. console.log('res3', res3);
  308. if (res3.code == 0) {
  309. genggaiVip(res3.data)
  310. } else {
  311. setTimeout(() => {
  312. orderCheck({
  313. id: orderId.value
  314. }).then(res4 => {
  315. if (res4.code == 0) {
  316. genggaiVip(res4.data)
  317. } else {
  318. toast(
  319. "开通失败,请联系管理员!"
  320. )
  321. uni
  322. .hideLoading();
  323. return false
  324. }
  325. }).catch(() => {
  326. uni.hideLoading();
  327. toast("check接口报错")
  328. return false
  329. })
  330. }, 5000)
  331. }
  332. }).catch(() => {
  333. uni.hideLoading();
  334. toast("check接口报错")
  335. return false
  336. })
  337. },
  338. fail: function(err) {
  339. uni.hideLoading();
  340. // toast('支付失败:' + JSON.stringify(err));
  341. console.log('支付失败:' + JSON.stringify(err));
  342. }
  343. });
  344. }).catch((error) => {
  345. uni.hideLoading();
  346. // disabled.value = false
  347. console.log(error);
  348. })
  349. }
  350. function aliApy() {
  351. orderPayAli({
  352. id: orderId.value
  353. }).then(res2 => {
  354. console.log('res2', res2);
  355. uni.hideLoading();
  356. uni.requestPayment({
  357. "provider": "alipay",
  358. "orderInfo": res2.data.text, //此处为服务器返回的订单信息字符串
  359. success: function(res) {
  360. // var rawdata = JSON.parse(res.rawdata);
  361. // console.log('支付成功');
  362. // console.log('rawdata', rawdata);
  363. uni.showLoading({
  364. title: '会员开通中,请稍后...'
  365. });
  366. orderCheck({
  367. id: orderId.value
  368. }).then(res3 => {
  369. if (res3.code == 0) {
  370. genggaiVip(res3.data)
  371. } else {
  372. setTimeout(() => {
  373. orderCheck({
  374. id: orderId.value
  375. }).then(res4 => {
  376. if (res4.code ==
  377. 0) {
  378. genggaiVip(res4.data)
  379. } else {
  380. // disabled.value =false
  381. toast(
  382. "开通失败,请联系管理员!"
  383. )
  384. uni
  385. .hideLoading();
  386. return false
  387. }
  388. }).catch(() => {
  389. // disabled.value =false
  390. uni.hideLoading();
  391. toast("check接口报错")
  392. return false
  393. })
  394. }, 5000)
  395. }
  396. }).catch(() => {
  397. // disabled.value = false
  398. uni.hideLoading();
  399. toast("check接口报错")
  400. return false
  401. })
  402. },
  403. fail: function(err) {
  404. // disabled.value = false
  405. // toast('支付失败:' + JSON.stringify(err));
  406. console.log('支付失败:' + JSON.stringify(err));
  407. uni.hideLoading();
  408. }
  409. });
  410. })
  411. }
  412. const creatOrder = debounce((data) => {
  413. const selectedItems = localList.value
  414. .filter(item => selectedIds.value.includes(item.id));
  415. console.log('selectedItems', selectedItems);
  416. const cardIds = selectedItems.map(item => item.id);
  417. if (cardIds.length === 0) {
  418. uni.showToast({
  419. title: '请选择至少一个商品',
  420. icon: 'none'
  421. });
  422. return;
  423. }
  424. console.log('123123123123');
  425. disabled.value = true
  426. uni.showLoading({
  427. title: '',
  428. mask: true
  429. });
  430. if (payType.value == 'weixin') {
  431. console.log('创建订单11');
  432. if (orderId.value) {
  433. wxPay()
  434. } else {
  435. let req = {
  436. cardIds: cardIds
  437. }
  438. orderAdd(req).then(res => {
  439. console.log('res', res);
  440. console.log(' res.data.id', res.data.id);
  441. orderId.value = res.data.id
  442. wxPay()
  443. }).catch((err) => {
  444. uni.hideLoading();
  445. disabled.value = false
  446. toast("订单创建失败")
  447. return false
  448. })
  449. }
  450. } else {
  451. if (orderId.value) {
  452. aliApy()
  453. } else {
  454. let req = {
  455. cardIds: cardIds
  456. }
  457. orderAdd(req).then(res => {
  458. console.log('res', res);
  459. orderId.value = res.data.id
  460. aliApy()
  461. }).catch((err) => {
  462. disabled.value = false
  463. uni.hideLoading();
  464. toast("订单创建失败")
  465. return false
  466. })
  467. }
  468. }
  469. }, 500)
  470. // 是否全选
  471. const isAllSelected = computed(() => {
  472. return localList.value.length > 0 &&
  473. selectedIds.value.length === localList.value.length;
  474. });
  475. // 处理checkbox变化
  476. function handleCheckboxChange(e) {
  477. selectedIds.value = e.detail.value.map(id => parseInt(id));
  478. }
  479. // 全选/取消全选
  480. function toggleSelectAll() {
  481. if (isAllSelected.value) {
  482. selectedIds.value = []; // 取消全选
  483. } else {
  484. selectedIds.value = localList.value.map(item => item.id); // 全选
  485. }
  486. }
  487. // 计算总价
  488. const totalPrice = computed(() => {
  489. return localList.value
  490. .filter(item => selectedIds.value.includes(item.id))
  491. .reduce((sum, item) => sum + parseFloat(item.xianjia || 0), 0)
  492. .toFixed(2);
  493. });
  494. // 支付方式切换
  495. function switchPayWay() {
  496. payType.value = payType.value == 'weixin' ? 'zhifubao' : 'weixin'
  497. }
  498. // 支付处理
  499. const detailPopup = ref(null);
  500. function detailShow() {
  501. detailPopup.value.open();
  502. }
  503. function detailCloseBtn() {
  504. detailPopup.value.close();
  505. }
  506. defineExpose({
  507. detailShow,
  508. detailCloseBtn
  509. });
  510. </script>
  511. <style>
  512. </style>