detailDialog.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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'" class="open-svip-btn" @touchstart="creatOrder">立即支付
  54. </view>
  55. <view v-if="currentPlatform =='ios'" style="margin-top: 20rpx;" class="open-svip-btn"
  56. @touchstart="creatOrderIos">立即支付</view>
  57. </view>
  58. </view>
  59. </uni-popup>
  60. </template>
  61. <script setup>
  62. import {
  63. ref,
  64. computed,
  65. onMounted,
  66. watch
  67. } from 'vue';
  68. import {
  69. debounce,
  70. toast
  71. } from "@/utils/common";
  72. import cacheManager from "@/utils/cacheManager.js";
  73. import {
  74. orderAdd,
  75. orderPayAli,
  76. orderPayWx,
  77. orderPayApple,
  78. orderCheck
  79. } from "@/api/order.js"
  80. const props = defineProps({
  81. selectedList: { // 父组件传入的初始选中商品列表
  82. type: Array,
  83. default: () => []
  84. }
  85. });
  86. const payType = ref('weixin');
  87. const mxjtClass = ref('mxjt-sq-icon');
  88. // 本地维护的商品列表(独立于父组件)
  89. const localList = ref([]);
  90. const selectedIds = ref([]); // 存储选中项的id
  91. let orderId = ref('');
  92. let productId = ref(null)
  93. let iapChannel = ref(null)
  94. let quantity = ref(1)
  95. let channel = ref('')
  96. let appleFlag = ref('')
  97. // 本地选中状态管理
  98. const localSelectedMap = ref({});
  99. let currentPlatform = ref('android')
  100. // 初始化本地数据
  101. // 初始化数据
  102. // watch(() => props.selectedList, (newVal) => {
  103. // localList.value = [...newVal];
  104. // selectedIds.value = newVal.map(item => item.id); // 初始全部选中
  105. // }, {
  106. // immediate: true
  107. // });
  108. onMounted(() => {
  109. isIOSorAndroid()
  110. })
  111. const isIOSorAndroid = () => {
  112. const systemInfo = uni.getSystemInfoSync();
  113. console.log('systemInfo', systemInfo);
  114. if (systemInfo.platform == 'ios') {
  115. return currentPlatform.value = 'ios'
  116. } else {
  117. return currentPlatform.value = 'android'
  118. }
  119. }
  120. function genggaiVip(data) {
  121. uni.hideLoading();
  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. const selectedItems = localList.value
  140. .filter(item => selectedIds.value.includes(item.id));
  141. console.log('selectedItems', selectedItems);
  142. const cardIds = selectedItems.map(item => item.id);
  143. if (cardIds.length === 0) {
  144. uni.showToast({
  145. title: '请选择至少一个商品',
  146. icon: 'none'
  147. });
  148. return;
  149. }
  150. uni.showLoading({
  151. title: '',
  152. mask: true
  153. });
  154. if (appleFlag.value.toString() == 'true') {
  155. productId.value = 'llisoftEzhuangyuanceshi'
  156. } else {
  157. if (cardId.value == 1) {
  158. productId.value = 'llisoftEzhuangyuan'
  159. } else {
  160. productId.value = 'llisoftEzhuangyuanYingyu'
  161. }
  162. }
  163. let req = {
  164. cardIds: cardIds
  165. }
  166. orderAdd(req).then(res => {
  167. console.log('res', res);
  168. if (res.code == 0) {
  169. uni.hideLoading();
  170. orderId.value = res.data.id
  171. // 测试ios 1元
  172. applePay()
  173. } else {
  174. uni.hideLoading();
  175. return false
  176. console.log('请求失败');
  177. }
  178. }).catch((e) => {
  179. uni.hideLoading();
  180. toast("订单创建失败")
  181. return false
  182. })
  183. }), 500)
  184. function applePaySuccess(data) {
  185. uni.showLoading({
  186. title: '会员开通中',
  187. mask: true
  188. });
  189. let req = {
  190. "id": orderId.value,
  191. "paynum": data.transactionIdentifier,
  192. "receipt": data.transactionReceipt
  193. }
  194. console.log('reqreq', req);
  195. orderPayApple(req).then(res => {
  196. if (res.code == 0 && res.data) {
  197. iapChannel.finishTransaction(data.transactionIdentifier)
  198. console.log('resiapChanneliapChanneliapChannel', res);
  199. genggaiVip()
  200. } else {
  201. iapChannel.finishTransaction(data.transactionIdentifier)
  202. uni.hideLoading();
  203. toast("苹果内购失败")
  204. console.log('orderPayApple失败');
  205. return false
  206. }
  207. })
  208. }
  209. function applePay() {
  210. console.log('123123');
  211. if (!productId.value) {
  212. uni.showToast({
  213. title: '苹果内购ID缺失,请选择其它支付方式或联系客服',
  214. icon: "none"
  215. });
  216. return false;
  217. }
  218. uni.showLoading({
  219. title: '正在支付中...'
  220. });
  221. try {
  222. plus.payment.getChannels(function(channels) { //判读项目支付通道开通情况
  223. for (var i in channels) {
  224. iapChannel = channels[i];
  225. // 获取 id 为 'appleiap' 的 channel
  226. console.info("支付通道", iapChannel)
  227. if (iapChannel.id === 'appleiap') { //开通了app应用内支付,在manifest.josn中设置,开通后需打自定议基座
  228. console.info("苹果支付通道", iapChannel)
  229. // ids 数组中的项为 App Store Connect 配置的内购买项目产品ID(productId)
  230. var ids = [productId.value];
  231. // iap 为应用内支付对象
  232. iapChannel.requestOrder(ids, function(e) {
  233. // 获取订单信息成功回调方法
  234. console.log('requestOrder success: ' + JSON.stringify(e));
  235. uni.requestPayment({
  236. provider: 'appleiap',
  237. orderInfo: {
  238. productid: productId.value, //产品id,来自于苹果
  239. quantity: quantity.value, //产品数量
  240. manualFinishTransaction: true
  241. },
  242. success: (e) => {
  243. uni.hideLoading();
  244. // toast("苹果内购成功")
  245. console.info("苹果内购成功", e)
  246. applePaySuccess(e)
  247. //e.payment.orderNo = that.orderNo
  248. //支付成功回调,前端调用后台接口
  249. },
  250. fail: (e) => {
  251. uni.hideLoading();
  252. toast("苹果内购失败")
  253. console.info("苹果内购失败", e)
  254. },
  255. })
  256. },
  257. function(e) {
  258. // 获取订单信息失败回调方法
  259. console.log('requestOrder failed: ' + JSON.stringify(e));
  260. });
  261. } else {
  262. console.log('不支持苹果支付')
  263. }
  264. }
  265. },
  266. function(e) {
  267. console.log("获取iap支付通道失败:" + e.message);
  268. });
  269. } catch (e) {
  270. uni.showModal({
  271. title: "init",
  272. content: e.message,
  273. showCancel: false
  274. });
  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. console.log(error);
  347. })
  348. }
  349. function aliApy() {
  350. orderPayAli({
  351. id: orderId.value
  352. }).then(res2 => {
  353. console.log('res2', res2);
  354. uni.hideLoading();
  355. uni.requestPayment({
  356. "provider": "alipay",
  357. "orderInfo": res2.data.text, //此处为服务器返回的订单信息字符串
  358. success: function(res) {
  359. // var rawdata = JSON.parse(res.rawdata);
  360. // console.log('支付成功');
  361. // console.log('rawdata', rawdata);
  362. uni.showLoading({
  363. title: '会员开通中,请稍后...'
  364. });
  365. orderCheck({
  366. id: orderId.value
  367. }).then(res3 => {
  368. if (res3.code == 0) {
  369. genggaiVip(res3.data)
  370. } else {
  371. setTimeout(() => {
  372. orderCheck({
  373. id: orderId.value
  374. }).then(res4 => {
  375. if (res4.code ==
  376. 0) {
  377. genggaiVip(res4.data)
  378. } else {
  379. toast(
  380. "开通失败,请联系管理员!"
  381. )
  382. uni
  383. .hideLoading();
  384. return false
  385. }
  386. }).catch(() => {
  387. uni.hideLoading();
  388. toast("check接口报错")
  389. return false
  390. })
  391. }, 5000)
  392. }
  393. }).catch(() => {
  394. uni.hideLoading();
  395. toast("check接口报错")
  396. return false
  397. })
  398. },
  399. fail: function(err) {
  400. console.log('支付失败:' + JSON.stringify(err));
  401. uni.hideLoading();
  402. }
  403. });
  404. })
  405. }
  406. const creatOrder = debounce((data) => {
  407. const selectedItems = localList.value
  408. .filter(item => selectedIds.value.includes(item.id));
  409. console.log('selectedItems', selectedItems);
  410. const cardIds = selectedItems.map(item => item.id);
  411. if (cardIds.length === 0) {
  412. uni.showToast({
  413. title: '请选择至少一个商品',
  414. icon: 'none'
  415. });
  416. return;
  417. }
  418. console.log('123123123123');
  419. uni.showLoading({
  420. title: '',
  421. mask: true
  422. });
  423. if (payType.value == 'weixin') {
  424. console.log('创建订单11');
  425. let req = {
  426. cardIds: cardIds
  427. }
  428. orderAdd(req).then(res => {
  429. console.log('res', res);
  430. console.log(' res.data.id', res.data.id);
  431. orderId.value = res.data.id
  432. wxPay()
  433. }).catch((err) => {
  434. uni.hideLoading();
  435. toast("订单创建失败")
  436. return false
  437. })
  438. } else {
  439. let req = {
  440. cardIds: cardIds
  441. }
  442. orderAdd(req).then(res => {
  443. console.log('res', res);
  444. orderId.value = res.data.id
  445. aliApy()
  446. }).catch((err) => {
  447. uni.hideLoading();
  448. toast("订单创建失败")
  449. return false
  450. })
  451. }
  452. }, 500)
  453. // 是否全选
  454. const isAllSelected = computed(() => {
  455. return localList.value.length > 0 &&
  456. selectedIds.value.length === localList.value.length;
  457. });
  458. // 处理checkbox变化
  459. function handleCheckboxChange(e) {
  460. selectedIds.value = e.detail.value.map(id => parseInt(id));
  461. }
  462. // 全选/取消全选
  463. function toggleSelectAll() {
  464. if (isAllSelected.value) {
  465. selectedIds.value = []; // 取消全选
  466. } else {
  467. selectedIds.value = localList.value.map(item => item.id); // 全选
  468. }
  469. }
  470. // 计算总价
  471. const totalPrice = computed(() => {
  472. return localList.value
  473. .filter(item => selectedIds.value.includes(item.id))
  474. .reduce((sum, item) => sum + parseFloat(item.xianjia || 0), 0)
  475. .toFixed(2);
  476. });
  477. // 支付方式切换
  478. function switchPayWay() {
  479. payType.value = payType.value == 'weixin' ? 'zhifubao' : 'weixin'
  480. }
  481. // 支付处理
  482. const detailPopup = ref(null);
  483. function detailShow(newVal) {
  484. console.log('getSelectedProducts',newVal);
  485. localList.value = [...newVal];
  486. selectedIds.value = newVal.map(item => item.id); // 初始全部选中
  487. detailPopup.value.open();
  488. }
  489. function detailCloseBtn() {
  490. detailPopup.value.close();
  491. }
  492. defineExpose({
  493. detailShow,
  494. detailCloseBtn
  495. });
  496. </script>
  497. <style>
  498. </style>