booking.vue 11.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
<template>
	<view class="graybg">
		<view class="rrcard">
			<view class="box">
				<view class="billItem">
					<view>订单编号: <text>{{order.id}}</text></view>
					<view v-if="orderFrom==0">下单时间: <text>{{dayjs().format('YYYY-MM-DD')}}</text></view>
					<view v-if="orderFrom==1">下单时间: <text>{{order.createTime}}</text></view>
					<view>预订人: <text>{{currUser.userName||order.orderName}}</text></view>
					<view>手机号: <text>{{currUser.phonenumber||order.orderTelno}}</text></view>
				</view>
			</view>
		</view>
		<view class="rrcard">
			<view class="t">已选场地信息</view>
			<view class="box">
				<view class="billItem">
					<view v-if="order.projectName">项目: <text>{{order.projectName}}</text></view>
					<view v-else-if="order.sonList">项目: <text>{{order.sonList[0].projectName}}</text></view>
					<view class="billFoot" style="margin:0">
					</view>
					<view v-if="order.sonList" v-for="(info,index) in order.sonList" :key="info.id">
						{{`明细 ${info.siteName}  ${info.orderDate.substring(5,10)}  (${info.orderTimePeriod.replace(/,/,'-')} )  ${info.orderHour}小时`}}
						<text style="width: auto;">{{info.orderAmount}}</text>
					</view>
					<view v-if="!order.sonList">
						{{`明细 ${order.siteName} ${order.orderDate.substring(5,10)}  (${order.orderTimePeriod.replace(/,/,'-')})  ${order.orderHour}小时`}}<text
							style="width: auto;">{{order.orderAmount}}</text>
					</view>
					<view></view>
				</view>

			</view>
		</view>
		<view class="rrcard">
			<view class="t">付款信息</view>
			<view class="box">
				<view class="billItem">
					<view>合计:
						<!-- 原价 -->
						<text class="originPrice" v-if="form.payType =='2'">{{order.unuseBalancePrice}}</text>
						<!-- 优惠价 useBalancePrice-->
						<text v-if="form.payType =='2'" class="price">{{order.useBalancePrice}}</text>
						<text v-if="form.payType =='7'" class="price">{{order.unuseBalancePrice}}</text>
					</view>
					<view>备注:
						<input placeholder="给商家留言" v-model="form.remark" type="text" />
					</view>
				</view>
			</view>
		</view>

		<view class="rrcard">
			<view class="t">支付方式</view>
			<view class="whitebg">
				<radio-group @change="payChange">
					<label class="uni-list-cell uni-list-cell-pd">
						<view class="littleimgBox">
							<image class="littleimg" src="@/static/zf01.png"></image>微信支付
						</view>
						<view>
							<radio checked="checked" value="7" />
						</view>
					</label>
					<label class="uni-list-cell uni-list-cell-pd">
						<view class="littleimgBox">
							<image class="littleimg" src="@/static/zf02.png"></image>会员卡支付(余额:¥{{myBalance}})
						</view>
						<view>
							<radio checked="checked" value="2" />
						</view>
					</label>
				</radio-group>
			</view>
		</view>
		<view class="payBtnBox" v-if="orderFrom==0">
			<view class="cancelBtn" @click="payCancel">取消订单</view>
			<view class="payBtn" @click="pay(form.payType)">立即支付
				<uni-countdown style="display: inline-block;" color="#fff" splitorColor="#fff" @timeup="timeup"
					:show-day="false" :show-hour="false" :minute="15" :second="0"></uni-countdown>
			</view>
		</view>
		<view class="payBtnBox" v-else>
			<view class="payBtn" style="width: 100%;" @click="pay(form.payType)">立即支付
				<uni-countdown style="display: inline-block;" color="#fff" splitorColor="#fff" @timeup="timeup"
					:show-day="false" :show-hour="false" :start="countdown.start" :minute="countdown.minute"
					:second="countdown.second"></uni-countdown>
			</view>
		</view>

		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="取消" confirmText="确定" content="是否确认支付?" @confirm="dialogConfirm" 
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>

		<uni-popup ref="alertPayOk" type="dialog">
			<uni-popup-dialog type="success" confirmText="查看订单详情" content="支付成功" @confirm="goBillDetail"  @close="goBillDetail">
			</uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="alertPayWrong" type="dialog">
			<uni-popup-dialog type="error" confirmText="查看订单详情" content="支付失败" @confirm="goBillDetail" @close="goBillDetail">
			</uni-popup-dialog>
		</uni-popup>

	</view>

</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import * as api from '@/common/api.js';
	import _ from 'lodash'
	import {
		ref,
		getCurrentInstance
	} from 'vue';
	import dayjs from 'dayjs'
	import {
		getCurrUser
	} from '../../common/login';


	const {
		proxy
	} = getCurrentInstance()

	const vTypeImg = ref('/static/v1.png');
	const items = ref(['消费记录', '充值记录']);
	const current = ref(0);
	const isPay = ref(false);
	const lockPay = ref(false);
	const isClick = ref(false);
	const alertDialog = ref(null);
	const alertPayOk = ref(null);
	const alertPayWrong = ref(null);
	const currUser = ref({});
	const orderId = ref('');
	const order = ref({});
	const orderFrom = ref(0);
	const orderInfo = ref([]);
	const myBalance = ref(0);
	const form = ref({
		payType: '2'
	});
	const countdown = ref({
		start: false,
		minute: 0,
		second: 0
	})

	onLoad((option) => {
		uni.showLoading({
			title: '加载中'
		});
		if (option.id) {
			orderFrom.value = 1
			api.getMaItemById(option.id).then(res => {
				uni.hideLoading();
				order.value = res.data
				form.value.orderId = option.id
				orderId.value = option.id
				form.value.orderAmount = res.data.useBalancePrice

				let leftTime = dayjs(res.data.createTime).add(15, 'minute').valueOf() - dayjs().valueOf()
				if (leftTime > 0) {
					countdown.value = {
						start: true,
						minute: Math.floor(leftTime / 1000 / 60),
						second: Math.ceil(leftTime / 1000) % 60
					}
				} else {
					countdown.value = {
						start: true
					}
					timeup()
				}
			})
		} else {
			const eventChannel = proxy.getOpenerEventChannel()
			eventChannel.on('acceptDataFromOpenerPage', (data) => {
				// console.log(data)
				orderInfo.value = data.orderInfo
				orderId.value =  data.order.id
				api.getMaItemById(orderId.value).then(res => {
					uni.hideLoading();
					order.value = res.data
					form.value.orderAmount = res.data.useBalancePrice
				})

				eventChannel.emit('acceptDataFromOpenedPage');

				countdown.value = {
					start: true
				}
			})
		}

		currUser.value = uni.getStorageSync('currUser')

		api.getMyBalance().then((res) => {
			myBalance.value = res.data
		})
	})

	function payChange(evt) {
		form.value.payType = evt.detail.value
	}

	function pay(n) {
		if(lockPay.value == true){
			return
		}else{
			lockPay.value = true
		}
		if (n == '2'&&(parseFloat(myBalance.value) < parseFloat(order.value.useBalancePrice))) {
			uni.showToast({
				title: '余额不足,无法支付',
				icon: 'none',
				duration: 2000
			})
			lockPay.value = false
		} else {
			alertDialog.value.open()
		}
	}

	let timeHandle = null;

	function dialogConfirm() {
		// console.log(isClick.value)
		if(isClick.value == true){
			return
		} else {
			isClick.value = true
			uni.showLoading({
				title: '支付中...'
			});
			goPayForOrder()
		}
	}

	function goPayForOrder() {
		if (form.value.payType != '2') {
			form.value.orderAmount = order.value.unuseBalancePrice
		}
		api.payForOrder(orderId.value, form.value)
			.then((res) => {
				isClick.value = false
				if (res.data.weixinData) {
					// console.log('weipay')
					wePay(res.data.weixinData)
				} else {
					alertPayOk.value.open()
					subscribe()
					lockPay.value = true
				}
			}).catch(() => {
				alertPayWrong.value.open()
			}).finally(() => {
				uni.hideLoading()
				// timeHandle = setTimeout(() => {
				// 	goBillDetail()
				// }, 3000)
			})
	}


	function wePay(configdata) {
		// console.log(configdata)
		// 微信支付
		uni.requestPayment({
			"provider": "wxpay",
			"appId": configdata.appId,
			"nonceStr": configdata.nonceStr,
			"package": configdata.packageValue, // 随机字符串
			"timeStamp": configdata.timeStamp, // 时间戳(单位:秒)
			"signType": configdata.signType, // 签名,这里用的 MD5/RSA 签名
			"paySign": configdata.paySign,
			success(res) {
				alertPayOk.value.open()
				subscribe()
				api.maPayConfirm({
					orderId:orderId.value,
					successFlag:true
				}).then((res)=>{
					lockPay.value = false
				})
			},
			fail(e) {
				uni.showToast({
					title: '支付取消',
					icon: 'none',
					duration: 2000
				})
				api.maPayConfirm({
					orderId:orderId.value,
					successFlag:false
				}).then((res)=>{
					lockPay.value = false
				})

			}
		})
	}

	function payCancel() {
		uni.showModal({
			title: '提示',
			content: '确定取消订单吗',
			success: function(res) {
				if (res.confirm) {
					const eventChannel = proxy.getOpenerEventChannel()
					api.cancelOrder(orderId.value).then((res) => {
						eventChannel.emit('acceptDataFromOpenedPage');
						uni.navigateBack()
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}

	function dialogClose() {
		alertDialog.value.close()
		lockPay.value = false
	}

	function goBillDetail() {
		if (timeHandle) {
			clearTimeout(timeHandle)
		}
		uni.redirectTo({
			url: `/pages/usercenter/billsDetail?id=${orderId.value}`
		})
	}

	function timeup() {
		if (countdown.value.start) {
			// 自动取消订单
			uni.showModal({
				title: '提示',
				content: '订单支付已超时',
				showCancel: false,
				success: function(res) {
					if (res.confirm) {
						api.cancelOrder(orderId.value).then((res) => {
							uni.navigateBack()
						})
					}
				}
			})
		}
	}

	// 订阅
	function subscribe() {
		// 场馆预订成功通知 订场取消提醒 订单退款通知
		uni.requestSubscribeMessage({
			tmplIds: ['p7_a1SGyGvJeOdwx_GKl6UlrWiWpNnejUArbnEPf-k4'],
			success: (res) => {
				console.log('success:', res)
			}
		})
	}

</script>

<style lang="scss" scoped>
	.billItem {
		width: auto;

		.uni-input {
			padding: 15rpx 0;
			color: #000;
		}

		.trInput {
			text-align: right;
			float: right;
		}
	}

	.rrcard .box {
		padding: 0 0 1px;
		margin: 20rpx 0 0;
	}

	.graybg {
		background: #f7f8fa;
		height: 100vh;
		width: 100vw;
		overflow: auto;
	}

	.whitebg {
		background: #fff;
		margin-top: 15rpx;
		border-radius: 20rpx;
		margin-bottom: 180rpx;
	}

	.payBtnBox {
		width: 750rpx;
		line-height: 90rpx;
		position: fixed;
		bottom: 0;
		display: flex;
		height: 120rpx;
	}

	.cancelBtn {
		background: #fff;
		font-size: 28rpx;
		color: #999;
		text-align: center;
		width: 40%;
	}

	.payBtn {
		background: #FF8124;
		text-align: center;
		color: #FFFFFF;
		width: 60%;
		font-size: 36rpx;
	}

	.uni-list-cell::after {
		display: none;
	}

	.littleimgBox {
		display: flex;
		align-items: center;
	}

	.littleimg {
		width: 50rpx;
		height: 50rpx;
		margin-right: 15rpx;
	}

	.billItem .price {
		font-weight: bold;
		font-size: 45rpx;
		color: #ff8124;
	}

	.originPrice {
		line-height: 70rpx;
		margin: 0 0 0 6rpx;
		color: #999;
		text-decoration: line-through;
	}
</style>