pay.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <template>
  2. <view class="container">
  3. <uv-navbar title="确认订单" placeholder autoBack></uv-navbar>
  4. <view class="u-text-center u-plr30">
  5. <view class="mt82">
  6. <view class="u-ED3D07 u-font44 u-bold">
  7. <text class="u-ml10">{{ total_amount }}</text>
  8. </view>
  9. <view class="u-999 u-font22">付款金额</view>
  10. </view>
  11. <view class="u-bg-fff pay-select">
  12. <radio-group @change="e=>payWay(e.detail.value)">
  13. <!-- <label class="u-flex-center-sb" @click="payWay(0)">
  14. <view class="u-flex-center">
  15. <text class="iconfont3 pay">&#x100dc;</text>
  16. <view class="u-ml20 u-font28 u-181818">
  17. 余额支付<text class="u-font24 u-999"
  18. >(可用余额 ¥ {{ balance || 0 }})</text
  19. >
  20. </view>
  21. </view>
  22. <view>
  23. <radio value="0" :checked="payStatus === 0" />
  24. </view>
  25. </label> -->
  26. <label class="u-flex-center-sb u-mt30" @click.stop="payWay(1)">
  27. <view class="u-flex-center">
  28. <!-- <image class="img60" src="/static/weChat2.png"></image> -->
  29. <text class="iconfont pay weixinPay">&#xe663;</text>
  30. <view class="u-ml20 u-font28 u-181818">微信支付</view>
  31. </view>
  32. <view>
  33. <radio value="1" :checked="payStatus == 1" />
  34. </view>
  35. </label>
  36. <label
  37. class="u-flex-center-sb u-mt30"
  38. @click.stop="payWay(2)"
  39. v-if="env != 0"
  40. >
  41. <view class="u-flex-center">
  42. <text class="iconfont pay zhifuPay">&#xe663;</text>
  43. <view class="u-ml20 u-font28 u-181818">支付宝</view>
  44. </view>
  45. <view>
  46. <radio value="2" :checked="payStatus == 2" />
  47. </view>
  48. </label>
  49. </radio-group>
  50. </view>
  51. <view class="pay-btn">
  52. <button
  53. class="u-flex-center u-btn-two"
  54. @click="$throttle(submitPay, 500)"
  55. >
  56. <view class="u-flex-center">
  57. <view>去付款</view>
  58. <uni-countdown
  59. :backgroundColor="'none'"
  60. :color="'#ffffff'"
  61. :splitorColor="'#ffffff'"
  62. :show-day="time1[0] > 0"
  63. :day="time1[0]"
  64. :hour="time1[1]"
  65. :minute="time1[2]"
  66. :second="time1[3]"
  67. >
  68. </uni-countdown>
  69. </view>
  70. </button>
  71. <view class="u-flex-center u-mt20 u-999 u-font22">
  72. <view> 请在 </view>
  73. {{ payMinutes }}
  74. <view>分钟内完成付款,否则订单失效。</view>
  75. </view>
  76. </view>
  77. </view>
  78. </view>
  79. </template>
  80. <script setup>
  81. import { ref, reactive, onMounted, getCurrentInstance } from "vue";
  82. import { onLoad, onShow } from "@dcloudio/uni-app";
  83. import {
  84. shoporderInfo_Api,
  85. shopOrderGoPay_Api,
  86. shopOrderIsPay_Api,
  87. } from "@/api/order";
  88. import { totalBalance_Api } from "@/api/userInfo.js";
  89. import { shoporderWxMinPay_Api } from "@/api/order.js";
  90. import $mUtil from "@/util/index";
  91. // 响应式数据
  92. const total_amount = ref(null);
  93. const orderNo = ref(null);
  94. const orderId = ref(null);
  95. const loading = ref(false);
  96. const expired_pay_time = ref(null);
  97. const payStatus = ref(1);
  98. const balance = ref(null);
  99. const isGroup = ref(false);
  100. const isService = ref(false);
  101. const time1 = reactive([0, 0, 0, 0]);
  102. const payMinutes = ref(0); // 支付剩余分钟数
  103. const pagesType = ref(null); // 页面类型, 1:通知上个页面支付成功
  104. const eventChannel = ref(null);
  105. onMounted(() => {
  106. const instance = getCurrentInstance().proxy;
  107. eventChannel.value = instance.getOpenerEventChannel();
  108. });
  109. const env = ref(2);
  110. let weixinInfo = null;
  111. const orderInfo = ref({});
  112. const getOrderInfo = () => {
  113. shoporderInfo_Api(orderId.value).then((res) => {
  114. if (res && res.code == 200) {
  115. orderInfo.value = res.data || {};
  116. if (orderInfo.value.paymentMethod != null) {
  117. payStatus.value = orderInfo.value.paymentMethod;
  118. }
  119. }
  120. });
  121. };
  122. // 页面加载
  123. onLoad((options) => {
  124. // console.log(options, "options");
  125. // 微信小程序
  126. // #ifdef MP-WEIXIN
  127. env.value = 0;
  128. payStatus.value = 0;
  129. // #endif
  130. if (options.orderNo) {
  131. orderNo.value = options.orderNo;
  132. }
  133. if (options.orderId) {
  134. orderId.value = options.orderId;
  135. getOrderInfo();
  136. }
  137. if (options.total_amount) {
  138. total_amount.value = options.total_amount;
  139. }
  140. if (options.pagesType) pagesType.value = options.pagesType;
  141. if (options.expired_pay_time) {
  142. expired_pay_time.value = options.expired_pay_time / 1000;
  143. const timeArray = $mUtil
  144. .countDown(expired_pay_time.value)
  145. .split(":")
  146. .map((val) => Number(val));
  147. console.log(timeArray, "timeArray");
  148. time1[0] = timeArray[0];
  149. time1[1] = timeArray[1];
  150. time1[2] = timeArray[2];
  151. time1[3] = timeArray[3];
  152. // console.log("支付时间:" + options.expired_pay_time);
  153. let beginTime = new Date();
  154. let time = expired_pay_time.value - beginTime;
  155. let leave1 = time % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  156. //计算相差分钟数
  157. let leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  158. let minutes = Math.ceil(leave2 / (60 * 1000));
  159. // payMinutes.value = minutes
  160. // console.log("分钟值:" + minutes);
  161. }
  162. if (options.type && options.type == "group") {
  163. isGroup.value = true;
  164. }
  165. if (options.type && options.type == "service") {
  166. isService.value = true;
  167. }
  168. // accountInfo();
  169. // if (options.orderId) {
  170. // returnList(options.orderId);
  171. // }
  172. // console.log("分钟" + time1[2]);
  173. if (time1[3] == "0") {
  174. payMinutes.value = time1[2];
  175. } else {
  176. payMinutes.value = time1[2] + 1;
  177. }
  178. // console.log("秒值:" + time1[3]);
  179. // #ifdef APP-PLUS
  180. plus.share.getServices(
  181. (service) => {
  182. let weixin = service.find((i) => i.id === "weixin");
  183. if (weixin) {
  184. // console.log("weixin==>", weixin);
  185. weixinInfo = weixin;
  186. } else {
  187. // 没有获取到微信分享服务
  188. console.log(weixin);
  189. }
  190. },
  191. (err) => {
  192. // 获取分享服务列表失败
  193. console.log(err);
  194. }
  195. );
  196. // #endif
  197. });
  198. const showPaying = ref(false); // 是否支付中
  199. // 订单支付状态查询
  200. const shopOrderIsPay = () => {
  201. shopOrderIsPay_Api({ orderId: orderId.value })
  202. .then((res) => {
  203. uni.hideLoading();
  204. if (res && res.code == 200) {
  205. let title = "支付成功";
  206. if (!res.data) {
  207. title = "支付失败";
  208. }
  209. uni.showToast({ title, icon: "none" });
  210. }
  211. goJump();
  212. })
  213. .catch((err) => {
  214. goJump();
  215. });
  216. };
  217. // 页面显示
  218. onShow(() => {
  219. if (showPaying.value) {
  220. uni.showLoading({ title: "加载中...", mask: true });
  221. setTimeout(() => {
  222. shopOrderIsPay();
  223. }, 500);
  224. }
  225. });
  226. // // 获取账户信息
  227. // const accountInfo = () => {
  228. // totalBalance_Api().then((res) => {
  229. // if (res && res.code == 200) {
  230. // balance.value = res.data || 0;
  231. // }
  232. // });
  233. // };
  234. // 选择支付方式
  235. const payWay = (e) => {
  236. // console.log(e, 111);
  237. payStatus.value = e;
  238. };
  239. // 跳转页面
  240. const goJump = () => {
  241. if (pagesType.value == 1 || pagesType.value == 2) {
  242. setTimeout(() => {
  243. eventChannel.value.emit("callback");
  244. uni.navigateBack();
  245. }, 500);
  246. } else {
  247. setTimeout(() => {
  248. uni.redirectTo({
  249. url: `/pages/order/mallOrder/orderList?orderType=0`,
  250. });
  251. }, 500);
  252. }
  253. };
  254. // 确认支付
  255. const confirm = () => {
  256. if (env.value == 0) {
  257. uni.navigateToMiniProgram({
  258. appId: "wxcea8ce8b537d83a5",
  259. path: `pages/order/detail?orderId=${orderId.value}&type=0`,
  260. envVersion: "release", // develop(开发版),trial(体验版),release(正式版)
  261. success: (res) => {
  262. showPaying.value = true;
  263. },
  264. fail: (res) => {
  265. console.log("取消支付:" + res);
  266. },
  267. });
  268. } else {
  269. showPaying.value = true;
  270. let isAndroid = true;
  271. // #ifdef APP-PLUS
  272. if (plus.os.name == "Android") {
  273. console.log("当前环境是安卓");
  274. isAndroid = true;
  275. } else {
  276. console.log("当前环境不是安卓");
  277. isAndroid = false;
  278. }
  279. // 微信支付
  280. if (payStatus.value == 0) {
  281. // console.log("微信支付===================");
  282. // #ifdef APP-PLUS
  283. weixinInfo.launchMiniProgram(
  284. {
  285. id: "gh_e5657eb120b6",
  286. path: `/pages/order/detail?orderId=${orderId.value}`,
  287. type: "0", // 可取值: 0-正式版; 1-测试版; 2-体验版。 默认值为0。
  288. // webUrl
  289. },
  290. (res) => {
  291. console.log("res", res);
  292. },
  293. (err) => {
  294. console.log("err", err);
  295. }
  296. );
  297. // #endif
  298. } else {
  299. // console.log("支付宝支付===================");
  300. // 支付宝支付
  301. //参数需要
  302. const query = encodeURIComponent("?orderId=" + orderId.value);
  303. const url = `${
  304. isAndroid ? "alipays" : "alipay"
  305. }://platformapi/startapp?appId=2021006116626014&page=pages/order/detail${query}`;
  306. plus.runtime.openURL(url, (e) => {
  307. console.error("未安装支付宝或无法打开");
  308. showPaying.value = false;
  309. uni.$uv.toast("未安装支付宝或无法打开");
  310. });
  311. }
  312. // #endif
  313. }
  314. };
  315. // 提交支付
  316. // const submitPay = () => {
  317. // if (!orderId.value) {
  318. // uni.$uv.toast("未获取到订单号");
  319. // return;
  320. // }
  321. // if (payStatus.value != orderInfo.value.paymentMethod) {
  322. // uni.showLoading({ title: "支付中...", mask:true });
  323. // shopOrderGoPay_Api({ orderId: orderId.value, paymentMethod: payStatus.value }).then(res => {
  324. // uni.hideLoading();
  325. // if (res && res.code == 200) {
  326. // confirm();
  327. // }
  328. // })
  329. // } else {
  330. // confirm();
  331. // }
  332. // };
  333. // 提交支付
  334. const submitPay = () => {
  335. //微信支付
  336. if (
  337. payStatus.value == 1 ||
  338. payStatus.value == 2 ||
  339. payStatus.value == 10 ||
  340. payStatus.value == 12 ||
  341. payStatus.value == 14
  342. ) {
  343. let data = {
  344. orderNo: orderNo.value,
  345. };
  346. loading.value = true;
  347. shoporderWxMinPay_Api(data)
  348. .then((res) => {
  349. if (res && res.data.code == 200) {
  350. loading.value = false;
  351. let target = res.data.data;
  352. console.log("target==>>", target);
  353. if (payStatus.value == 1) {
  354. uni.requestPayment({
  355. provider: "wxpay",
  356. timeStamp: target.timeStamp,
  357. nonceStr: target.nonceStr,
  358. package: target.package,
  359. signType: target.signType,
  360. paySign: target.paySign,
  361. success: (res) => {
  362. uni.$uv.toast("支付成功");
  363. uni.removeStorageSync("orderreminder");
  364. goJump();
  365. },
  366. fail: function (err) {
  367. console.log("err==>", err);
  368. loading.value = false;
  369. console.log("fail:" + JSON.stringify(err));
  370. },
  371. });
  372. } else if (payStatus.value == 2) {
  373. uni.requestPayment({
  374. provider: "alipay",
  375. orderInfo: target.prePayOrderInfo,
  376. success: (res) => {
  377. uni.$uv.toast("支付成功");
  378. uni.removeStorageSync("orderreminder");
  379. goJump();
  380. },
  381. fail: function (err) {
  382. loading.value = false;
  383. console.log("fail:" + JSON.stringify(err));
  384. },
  385. });
  386. }
  387. } else {
  388. loading.value = false;
  389. uni.$uv.toast("交易失败");
  390. }
  391. })
  392. .catch((err) => {
  393. loading.value = false;
  394. // getApp().globalData.$mUtil.toast('交易失败');
  395. });
  396. } else if (payStatus.value == 0) {
  397. //余额支付
  398. loading.value = false;
  399. uni.showModal({
  400. title: "余额支付",
  401. content: "确定使用余额支付吗?",
  402. success: (res) => {
  403. if (res.confirm) {
  404. let data = {
  405. orderNo: orderNo.value,
  406. };
  407. shoporderBalance_Api(data).then((res) => {
  408. if (res && res.code == 200) {
  409. uni.removeStorageSync("orderreminder");
  410. uni.$uv.toast("支付成功");
  411. goJump();
  412. }
  413. });
  414. } else if (res.cancel) {
  415. }
  416. },
  417. });
  418. }
  419. };
  420. </script>
  421. <style lang="scss">
  422. page {
  423. background-color: #f5f5f5;
  424. }
  425. .mt82 {
  426. margin-top: 82rpx;
  427. }
  428. radio {
  429. transform: scale(0.7);
  430. }
  431. .pay-select {
  432. margin-top: 66rpx;
  433. border-radius: 18rpx;
  434. padding: 48rpx 38rpx;
  435. .pay {
  436. font-size: 60rpx;
  437. // color: #ffa937;
  438. }
  439. .weixinPay {
  440. color: #09bb07;
  441. }
  442. .zhifuPay {
  443. color: #06b4fd;
  444. }
  445. .banks {
  446. justify-content: center;
  447. }
  448. .bank {
  449. display: flex;
  450. // flex-direction: column;
  451. justify-content: space-between;
  452. align-items: center;
  453. .box {
  454. // display: flex;
  455. // justify-content: space-between;
  456. // align-items: center;
  457. &:first-child {
  458. margin-bottom: 10rpx;
  459. }
  460. .left {
  461. display: flex;
  462. align-items: center;
  463. margin-bottom: 10rpx;
  464. }
  465. image {
  466. width: 80rpx;
  467. height: auto;
  468. margin-right: 10rpx;
  469. }
  470. .bank_name {
  471. font-size: 28rpx;
  472. margin-right: 20rpx;
  473. // font-weight: bold;
  474. }
  475. .name {
  476. text-align: left;
  477. }
  478. }
  479. }
  480. .bank-box {
  481. flex: 1;
  482. width: 0;
  483. overflow: hidden;
  484. text-overflow: ellipsis;
  485. white-space: nowrap;
  486. }
  487. .bank-icon {
  488. color: #2f77f1;
  489. font-size: 60rpx;
  490. }
  491. }
  492. .img60 {
  493. width: 50rpx;
  494. height: 50rpx;
  495. margin-left: 6rpx;
  496. }
  497. .pay-btn {
  498. margin-top: 80rpx;
  499. display: flex;
  500. flex-direction: column;
  501. align-items: center;
  502. align-content: center;
  503. button {
  504. background-color: #fa6138;
  505. color: #ffffff;
  506. justify-content: center;
  507. }
  508. }
  509. </style>