文件
yjyf/pages/order/quick.vue
linda 7f62823334 fix(order): 默认预约时间自动避开过去时段
立即下单页与确认订单页根据当前时间生成可用预约时段,避免默认 14:00 已过期导致后端拒绝下单
2026-10-28 18:02:59 +08:00

495 行
21 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="page">
<view class="hero">
<text class="hero-t">找师傅上门</text>
<text class="hero-s">一口价 / 师傅报价 / 自主定价,随您选</text>
</view>
<!-- 服务项目 -->
<view class="card">
<view class="card-head">
<text class="card-title">服务项目</text>
<text class="card-link" @tap="openPicker">{{ svc ? '更换' : '去选择' }} ›</text>
</view>
<view class="svc-selected" v-if="svc" @tap="openPicker">
<view class="svc-thumb"><text class="svc-thumb-t">{{ svc.name.slice(0, 2) }}</text></view>
<view class="svc-info">
<text class="svc-name">{{ svc.name }}</text>
<text class="svc-price">{{ svc.price_text }}</text>
</view>
<text class="svc-arrow">›</text>
</view>
<view class="svc-empty" v-else @tap="openPicker">
<view class="svc-empty-ic">+</view>
<view class="svc-empty-txt">
<text class="svc-empty-t">请选择需要上门服务的项目</text>
<text class="svc-empty-s">家具 / 卫浴 / 灯具 / 家电等</text>
</view>
<view class="svc-empty-btn">选择服务</view>
</view>
</view>
<!-- 计价方式 -->
<view class="card">
<view class="card-title">选择下单方式</view>
<view class="modes">
<view
v-for="m in modes"
:key="m.key"
class="mode"
:class="{ on: priceType === m.key }"
@tap="selectMode(m.key)"
>
<text class="mode-name">{{ m.name }}</text>
<text class="mode-desc">{{ m.desc }}</text>
<view class="mode-check" v-if="priceType === m.key">✓</view>
</view>
</view>
<view class="mode-note">{{ modeNote }}</view>
<view class="fi" v-if="priceType === 'custom'">
<text class="fk">我的出价</text>
<view class="price-input-wrap">
<text class="price-unit">¥</text>
<input class="fv price-input" v-model="userPrice" type="digit" placeholder="请输入您愿意支付的总价" />
</view>
</view>
</view>
<!-- 服务地址 -->
<view class="card">
<view class="card-head">
<text class="card-title">服务地址</text>
<text class="card-link" @tap="goAddress">{{ address ? '更换' : '去添加' }} ›</text>
</view>
<view class="addr" v-if="address" @tap="goAddress">
<view class="addr-main">
<text class="addr-c">{{ address.contact }} {{ address.mobile }}</text>
<text class="addr-d">{{ address.province }}{{ address.city }}{{ address.district }}{{ address.address }}</text>
<text class="addr-x">楼层 {{ address.floor }} · {{ Number(address.has_elevator) ? '有电梯' : '无电梯' }}</text>
</view>
</view>
<view class="addr-empty" v-else @tap="goAddress">
<text class="addr-empty-t">还没有服务地址</text>
<text class="addr-empty-s">点击添加地址,师傅上门前会与您联系</text>
</view>
</view>
<!-- 预约与需求 -->
<view class="card">
<view class="card-title">预约与需求</view>
<picker mode="date" :value="date" :start="today" @change="onDate">
<view class="fi"><text class="fk">预约日期</text><text class="fv2">{{ date }}</text><text class="arrow">›</text></view>
</picker>
<picker mode="time" :value="time" @change="onTime">
<view class="fi"><text class="fk">预约时段</text><text class="fv2">{{ time }}</text><text class="arrow">›</text></view>
</picker>
<view class="fi" v-if="svc && svc.need_model === 1">
<text class="fk">商品型号</text>
<input class="fv" v-model="form.goods_model" placeholder="如:三人位布艺沙发" />
</view>
<view class="fi">
<text class="fk">数量</text>
<view class="stepper">
<view class="st-btn" @tap="stepNum(-1)">-</view>
<text class="st-val">{{ form.goods_num }}</text>
<view class="st-btn" @tap="stepNum(1)">+</view>
</view>
</view>
<view class="fi">
<text class="fk">备注</text>
<input class="fv" v-model="form.remark" placeholder="施工要求 / 门禁 / 宠物等" />
</view>
<view class="fi" v-if="priceType !== 'custom'">
<text class="fk">加急服务</text>
<view class="urgent-switch" :class="{ on: urgent }" @tap="urgent = !urgent">
{{ urgent ? '已加急 +20%' : '普通预约' }}
</view>
</view>
</view>
<!-- 费用 -->
<view class="card" v-if="amount">
<view class="card-title">{{ priceType === 'quote' ? '费用预估' : '费用明细' }}</view>
<view class="li">
<text class="k">{{ priceType === 'custom' ? '您的出价' : '基础价' }}</text>
<text class="v">¥{{ amount.base_amount }}</text>
</view>
<view class="li" v-if="priceType !== 'custom'">
<text class="k">楼层费</text>
<text class="v">¥{{ amount.floor_amount }}</text>
</view>
<view class="li" v-if="priceType !== 'custom' && amount.urgent_amount > 0">
<text class="k">加急费</text>
<text class="v">¥{{ amount.urgent_amount }}</text>
</view>
<view class="li" v-if="amount.discount_amount > 0">
<text class="k">优惠券抵扣</text>
<text class="v">-¥{{ amount.discount_amount }}</text>
</view>
<view class="li total">
<text class="k">{{ priceType === 'quote' ? '参考合计' : '应付合计' }}</text>
<text class="v big">¥{{ amount.pay_amount }}</text>
</view>
<text class="fee-note" v-if="priceType === 'quote'">先支付参考价,选定师傅报价后按最终报价结算</text>
<text class="fee-note" v-if="priceType === 'custom'">自主定价为含楼层等的一口价总价,师傅接单即按此价服务</text>
</view>
<view class="bottom-space"></view>
<view class="bar">
<view class="bar-l">
<text class="bar-k">{{ priceType === 'quote' ? '参考价' : '合计' }}</text>
<text class="bar-p">¥{{ amount ? amount.pay_amount : 0 }}</text>
</view>
<view class="bar-btn" :class="{ disabled: submitting }" @tap="submit">
{{ submitting ? '提交中...' : (priceType === 'quote' ? '发布需求' : '提交并支付') }}
</view>
</view>
<!-- 服务选择弹层 -->
<view class="mask" v-if="showPicker" @tap="showPicker = false">
<view class="sheet" @tap.stop>
<view class="sheet-head">
<text class="sheet-title">选择服务项目</text>
<text class="sheet-close" @tap="showPicker = false">关闭</text>
</view>
<scroll-view class="cat-scroll" scroll-x="true">
<view
v-for="c in cats"
:key="c.id"
class="cat-chip"
:class="{ on: activeCatId === c.id }"
@tap="switchCat(c)"
>{{ c.name }}</view>
</scroll-view>
<scroll-view class="svc-scroll" scroll-y="true">
<view class="svc-item" v-for="s in services" :key="s.id" @tap="chooseService(s)">
<view class="svc-item-thumb"><text class="svc-item-thumb-t">{{ s.name.slice(0, 2) }}</text></view>
<view class="svc-item-main">
<text class="svc-item-name">{{ s.name }}</text>
<text class="svc-item-desc">{{ s.description || '专业师傅上门,验收满意再付款' }}</text>
</view>
<text class="svc-item-price">{{ s.price_text }}</text>
</view>
<view class="sheet-empty" v-if="servicesLoading">加载中...</view>
<view class="sheet-empty" v-else-if="!services.length">该类目暂无服务</view>
<view class="sheet-bottom-space"></view>
</scroll-view>
</view>
</view>
</view>
</template>
<script setup>
import { ref, reactive, watch, computed } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import api from '@/utils/api.js';
import { isLogin, toLogin } from '@/utils/request.js';
const cats = ref([]);
const activeCatId = ref(0);
const services = ref([]);
const servicesLoading = ref(false);
const showPicker = ref(false);
const svc = ref(null);
const serviceId = ref(0);
const address = ref(null);
const priceType = ref('fixed');
const userPrice = ref('');
const urgent = ref(false);
const amount = ref(null);
const submitting = ref(false);
const modes = [
{ key: 'fixed', name: '一口价', desc: '平台明码标价' },
{ key: 'quote', name: '师傅报价', desc: '师傅竞价,您来选' },
{ key: 'custom', name: '自主定价', desc: '您出价,师傅接单' }
];
const modeNote = computed(() => {
if (priceType.value === 'quote') return '发布后进入报价大厅,师傅提交报价,您对比价格与评分后选择';
if (priceType.value === 'custom') return '填写您愿意支付的总价(含楼层等),师傅认可即可接单';
return '按平台标准价结算,支付后师傅先到先得';
});
const now = new Date();
const pad = (n) => (n < 10 ? '0' + n : '' + n);
const today = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
// 默认预约时间取当前时间之后的可用时段,避免默认值落在过去导致下单失败
function defaultAppointment() {
const h = now.getHours();
if (h >= 18) {
const t = new Date(now.getTime() + 86400000);
return { date: `${t.getFullYear()}-${pad(t.getMonth() + 1)}-${pad(t.getDate())}`, time: '09:00' };
}
if (h >= 14) return { date: today, time: pad(h + 1) + ':00' };
return { date: today, time: '14:00' };
}
const defAppointment = defaultAppointment();
const date = ref(defAppointment.date);
const time = ref(defAppointment.time);
const form = reactive({ goods_model: '', goods_num: 1, remark: '' });
let calcTimer = null;
function defaultMode(s) {
if (!s) return 'fixed';
if (s.price_type === 'quote') return 'quote';
if (s.price_type === 'range') return 'custom';
return 'fixed';
}
async function loadCats(preferCatId) {
try {
const res = await api.cats();
cats.value = res.list || [];
if (preferCatId) {
activeCatId.value = Number(preferCatId);
}
if (!activeCatId.value && cats.value.length) {
activeCatId.value = cats.value[0].id;
}
} catch (e) {}
}
async function loadServices() {
if (!activeCatId.value) return;
servicesLoading.value = true;
services.value = [];
try {
const res = await api.services({ cat_id: activeCatId.value });
services.value = res.list || [];
} catch (e) {
} finally {
servicesLoading.value = false;
}
}
async function openPicker() {
showPicker.value = true;
if (!cats.value.length) {
await loadCats();
}
if (!services.value.length) {
await loadServices();
}
}
function switchCat(c) {
if (activeCatId.value === c.id) return;
activeCatId.value = c.id;
loadServices();
}
function chooseService(s) {
svc.value = s;
serviceId.value = Number(s.id);
showPicker.value = false;
priceType.value = defaultMode(s);
userPrice.value = '';
amount.value = null;
calc();
}
function selectMode(key) {
if (priceType.value === key) return;
priceType.value = key;
if (key === 'custom') urgent.value = false;
amount.value = null;
calc();
}
async function loadAddress() {
if (!isLogin()) { toLogin(); return; }
try {
const res = await api.addressList();
const list = res.list || [];
address.value = list.find(a => Number(a.is_default) === 1) || list[0] || null;
} catch (e) {}
if (serviceId.value) calc();
}
function goAddress() {
uni.navigateTo({ url: '/pages/address/list' });
}
async function calc() {
if (!serviceId.value) return;
const custom = priceType.value === 'custom';
if (custom && (!userPrice.value || Number(userPrice.value) <= 0)) {
amount.value = null;
return;
}
const floor = address.value ? address.value.floor : 1;
const elev = address.value ? address.value.has_elevator : 1;
try {
amount.value = await api.calc({
service_id: serviceId.value,
floor: floor,
has_elevator: elev,
urgent: custom ? 0 : (urgent.value ? 1 : 0),
price_type: priceType.value,
user_price: custom ? Number(userPrice.value) : 0
});
} catch (e) {}
}
function scheduleCalc() {
clearTimeout(calcTimer);
calcTimer = setTimeout(calc, 300);
}
watch([urgent, priceType], scheduleCalc);
watch(userPrice, scheduleCalc);
function onDate(e) { date.value = e.detail.value; }
function onTime(e) { time.value = e.detail.value; }
function stepNum(d) { form.goods_num = Math.max(1, form.goods_num + d); }
async function submit() {
if (submitting.value) return;
if (!isLogin()) { toLogin(); return; }
if (!serviceId.value) { uni.showToast({ title: '请选择服务项目', icon: 'none' }); return; }
if (!address.value) { uni.showToast({ title: '请添加服务地址', icon: 'none' }); return; }
if (priceType.value === 'custom' && (!userPrice.value || Number(userPrice.value) <= 0)) {
uni.showToast({ title: '请填写您的出价', icon: 'none' });
return;
}
submitting.value = true;
try {
const appoint = Math.floor(new Date(date.value.replace(/-/g, '/') + ' ' + time.value + ':00').getTime() / 1000);
const res = await api.orderCreate({
service_id: serviceId.value,
address_id: address.value.id,
appoint_time: appoint,
urgent: priceType.value === 'custom' ? 0 : (urgent.value ? 1 : 0),
goods_model: form.goods_model,
goods_num: form.goods_num,
remark: form.remark,
price_type: priceType.value,
user_price: priceType.value === 'custom' ? Number(userPrice.value) : 0
});
await api.orderPay({ order_id: res.order_id, pay_type: 'mock' });
const tip = priceType.value === 'quote' ? '支付成功,等待师傅报价' : '支付成功,等待师傅接单';
uni.showToast({ title: tip });
setTimeout(() => {
uni.redirectTo({ url: '/pages/order/detail?id=' + res.order_id });
}, 700);
} catch (e) {
} finally {
submitting.value = false;
}
}
onLoad(async (options) => {
if (!isLogin()) { toLogin(); return; }
await loadCats(options && options.cat_id);
await loadServices();
if (options && options.service_id) {
try {
const res = await api.serviceDetail(options.service_id);
if (res && res.detail) chooseService(res.detail);
} catch (e) {}
}
});
onShow(() => {
if (isLogin() && !cats.value.length) {
loadCats().then(loadServices);
}
loadAddress();
});
</script>
<style scoped>
.page { min-height: 100vh; background: #F5F6F8; padding-bottom: 180rpx; }
.hero {
padding: 34rpx 30rpx 40rpx;
background: linear-gradient(160deg, #FFB85C 0%, #F98D25 60%, #ED6F0E 100%);
display: flex; flex-direction: column;
}
.hero-t { font-size: 40rpx; font-weight: 700; color: #fff; letter-spacing: 2rpx; }
.hero-s { margin-top: 8rpx; font-size: 23rpx; color: rgba(255,255,255,0.9); }
.card { margin: 22rpx 28rpx 0; padding: 22rpx 24rpx; background: #fff; border-radius: 20rpx; box-shadow: 0 6rpx 20rpx rgba(240,129,31,0.06); }
.card-title { font-size: 28rpx; font-weight: 700; color: #1F1F1F; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14rpx; }
.card-link { font-size: 23rpx; color: #F0710D; }
.svc-selected { display: flex; align-items: center; }
.svc-thumb { width: 92rpx; height: 92rpx; border-radius: 16rpx; flex-shrink: 0; background: linear-gradient(160deg,#FFF7F0,#FCE9D6); display: flex; align-items: center; justify-content: center; }
.svc-thumb-t { font-size: 26rpx; font-weight: 700; color: #B85E2A; }
.svc-info { flex: 1; min-width: 0; margin-left: 18rpx; display: flex; flex-direction: column; }
.svc-name { font-size: 30rpx; font-weight: 700; color: #1F1F1F; }
.svc-price { margin-top: 6rpx; font-size: 24rpx; color: #F0710D; }
.svc-arrow { font-size: 32rpx; color: #C4C4C4; }
.svc-empty { display: flex; align-items: center; padding: 8rpx 0; }
.svc-empty-ic { width: 72rpx; height: 72rpx; line-height: 70rpx; text-align: center; border-radius: 50%; background: #FFF3E6; color: #E36B0C; font-size: 34rpx; flex-shrink: 0; }
.svc-empty-txt { flex: 1; min-width: 0; margin-left: 16rpx; display: flex; flex-direction: column; }
.svc-empty-t { font-size: 27rpx; font-weight: 600; color: #333; }
.svc-empty-s { margin-top: 4rpx; font-size: 21rpx; color: #9A9A9A; }
.svc-empty-btn { flex-shrink: 0; padding: 0 24rpx; line-height: 56rpx; border-radius: 28rpx; background: linear-gradient(135deg,#FFA93F 0%,#F0710D 100%); color: #fff; font-size: 23rpx; font-weight: 600; }
.modes { display: flex; margin-top: 14rpx; }
.mode { position: relative; flex: 1; min-width: 0; margin-right: 14rpx; padding: 20rpx 16rpx; border-radius: 16rpx; background: #F8F8F8; border: 2rpx solid transparent; display: flex; flex-direction: column; }
.mode:last-child { margin-right: 0; }
.mode.on { background: #FFF3E6; border-color: #F0710D; }
.mode-name { font-size: 27rpx; font-weight: 700; color: #444; }
.mode.on .mode-name { color: #E36B0C; }
.mode-desc { margin-top: 6rpx; font-size: 20rpx; color: #9A9A9A; line-height: 28rpx; }
.mode.on .mode-desc { color: #C07A3A; }
.mode-check { position: absolute; right: 10rpx; top: 8rpx; width: 30rpx; height: 30rpx; line-height: 30rpx; text-align: center; border-radius: 50%; background: #F0710D; color: #fff; font-size: 20rpx; }
.mode-note { margin-top: 14rpx; padding: 14rpx 16rpx; border-radius: 12rpx; background: #FAFAFA; font-size: 22rpx; color: #8A8A8A; line-height: 32rpx; }
.addr { display: flex; align-items: center; }
.addr-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.addr-c { font-size: 28rpx; font-weight: 600; color: #222; }
.addr-d { margin-top: 6rpx; font-size: 24rpx; color: #666; line-height: 34rpx; }
.addr-x { margin-top: 6rpx; font-size: 22rpx; color: #9A9A9A; }
.addr-empty { padding: 20rpx 0 8rpx; display: flex; flex-direction: column; }
.addr-empty-t { font-size: 27rpx; font-weight: 600; color: #333; }
.addr-empty-s { margin-top: 6rpx; font-size: 22rpx; color: #9A9A9A; }
.fi { display: flex; align-items: center; min-height: 88rpx; border-bottom: 1rpx solid #F4F4F4; }
.fk { width: 150rpx; font-size: 26rpx; color: #666; }
.fv { flex: 1; height: 88rpx; font-size: 28rpx; color: #222; }
.fv2 { flex: 1; font-size: 28rpx; color: #222; }
.arrow { font-size: 30rpx; color: #C4C4C4; }
.stepper { display: flex; align-items: center; }
.st-btn { width: 52rpx; height: 52rpx; line-height: 50rpx; text-align: center; border-radius: 26rpx; background: #F5F5F5; color: #666; font-size: 26rpx; }
.st-val { min-width: 60rpx; text-align: center; font-size: 28rpx; color: #222; }
.urgent-switch { padding: 0 24rpx; line-height: 56rpx; border-radius: 28rpx; background: #F5F5F5; font-size: 23rpx; color: #666; }
.urgent-switch.on { background: #FFF0E5; color: #E36B0C; font-weight: 600; }
.price-input-wrap { flex: 1; display: flex; align-items: center; }
.price-unit { font-size: 32rpx; font-weight: 700; color: #F0710D; margin-right: 8rpx; }
.price-input { flex: 1; }
.li { display: flex; justify-content: space-between; padding: 14rpx 0; border-bottom: 1rpx solid #F4F4F4; }
.li.total { border-bottom: none; }
.k { font-size: 26rpx; color: #666; }
.v { font-size: 26rpx; color: #333; }
.v.big { font-size: 34rpx; font-weight: 700; color: #F0710D; }
.fee-note { display: block; margin-top: 10rpx; font-size: 21rpx; color: #9A9A9A; line-height: 30rpx; }
.bottom-space { height: 20rpx; }
.bar { position: fixed; left: 0; right: 0; bottom: 0; height: 120rpx; padding: 0 28rpx; padding-bottom: env(safe-area-inset-bottom); display: flex; align-items: center; justify-content: space-between; background: #fff; box-shadow: 0 -6rpx 24rpx rgba(0,0,0,0.07); z-index: 20; }
.bar-l { display: flex; align-items: baseline; }
.bar-k { font-size: 24rpx; color: #666; }
.bar-p { margin-left: 8rpx; font-size: 38rpx; font-weight: 700; color: #F0710D; }
.bar-btn { height: 84rpx; padding: 0 46rpx; display: flex; align-items: center; border-radius: 42rpx; color: #fff; font-size: 30rpx; font-weight: 600; background: linear-gradient(135deg, #FFA93F 0%, #F0710D 100%); box-shadow: 0 8rpx 20rpx rgba(240,113,13,0.3); }
.bar-btn.disabled { opacity: 0.7; }
.mask { position: fixed; left: 0; right: 0; top: 0; bottom: 0; background: rgba(0,0,0,0.45); z-index: 50; display: flex; align-items: flex-end; }
.sheet { width: 100%; height: 72vh; background: #fff; border-radius: 28rpx 28rpx 0 0; display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 26rpx 28rpx 18rpx; }
.sheet-title { font-size: 32rpx; font-weight: 700; color: #1F1F1F; }
.sheet-close { font-size: 24rpx; color: #9A9A9A; }
.cat-scroll { white-space: nowrap; padding: 0 0 12rpx; }
.cat-chip { display: inline-flex; align-items: center; padding: 0 24rpx; line-height: 60rpx; margin-left: 14rpx; border-radius: 30rpx; background: #F5F5F5; font-size: 24rpx; color: #666; }
.cat-chip:first-child { margin-left: 28rpx; }
.cat-chip.on { background: linear-gradient(135deg,#FFA93F 0%,#F0710D 100%); color: #fff; font-weight: 600; }
.svc-scroll { flex: 1; height: 0; padding: 0 28rpx; }
.svc-item { display: flex; align-items: center; padding: 20rpx 0; border-bottom: 1rpx solid #F4F4F4; }
.svc-item-thumb { width: 88rpx; height: 88rpx; border-radius: 16rpx; flex-shrink: 0; background: linear-gradient(160deg,#FFF7F0,#FCE9D6); display: flex; align-items: center; justify-content: center; }
.svc-item-thumb-t { font-size: 25rpx; font-weight: 700; color: #B85E2A; }
.svc-item-main { flex: 1; min-width: 0; margin-left: 16rpx; display: flex; flex-direction: column; }
.svc-item-name { font-size: 28rpx; font-weight: 600; color: #1F1F1F; }
.svc-item-desc { margin-top: 4rpx; font-size: 21rpx; color: #9A9A9A; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.svc-item-price { flex-shrink: 0; margin-left: 12rpx; font-size: 24rpx; font-weight: 600; color: #F0710D; }
.sheet-empty { padding: 70rpx 0; text-align: center; font-size: 24rpx; color: #9A9A9A; }
.sheet-bottom-space { height: 40rpx; }
</style>