连接数据库并修改页面

This commit is contained in:
not-like-juvenile
2026-02-02 18:20:22 +08:00
parent 5f856a96c9
commit 8efe6d5e89
22 changed files with 1630 additions and 992 deletions

View File

@@ -200,142 +200,134 @@
<script setup lang="uts">
import { ref, onMounted, computed } from 'vue'
import type { DeliveryDriverType, DeliveryTaskType, ApiResponseType } from '@/types/mall-types'
import supa, { supaReady } from '@/components/supadb/aksupainstance.uts'
import { getCurrentUserId } from '@/utils/store.uts'
import type { DeliveryDriverType, DeliveryTaskType } from '@/types/mall-types'
/* ----------------- 返回按钮 ----------------- */
function backToIndex() {
uni.navigateBack({ url: '/pages/mall/delivery/index' })
}
/* ----------------- 数据 ----------------- */
const driverInfo = ref({
id: '',
real_name: '配送员',
avatar_url: '',
rating: 4.9,
total_orders: 368,
work_status: 1
})
const workStatus = ref(1) // 1 工作中 0 休息中
const currentLocation = ref('朝阳区建国门附近') // 默认位置
/* ----------------- 数据 & 状态 ----------------- */
const driverInfo = ref({ id: '', real_name: '配送员', avatar_url: '', rating: 4.9, total_orders: 0, work_status: 1 })
const workStatus = ref(1)
const currentLocation = ref('定位中...')
const taskCounts = ref({ total: 0, pending: 0, ongoing: 0, completed: 0 })
const todayStats = ref({
deliveries: 12,
earnings: '156.80',
distance: 45.6,
efficiency: 96.5
})
const todayStats = ref({ deliveries: 0, earnings: '0.00', distance: 0, efficiency: 0 })
const currentTask = ref<DeliveryTaskType | null>(null)
const recentTasks = ref<DeliveryTaskType[]>([])
const weeklyEarnings = ref([
{ day: '周一', amount: 120 },
{ day: '周二', amount: 156 },
{ day: '周三', amount: 189 },
{ day: '周四', amount: 145 },
{ day: '周五', amount: 203 },
{ day: '周六', amount: 245 },
{ day: '周日', amount: 198 }
])
const weeklyEarnings = ref([{ day: '周一', amount: 0 }, { day: '周二', amount: 0 }, { day: '周三', amount: 0 }, { day: '周四', amount: 0 }, { day: '周五', amount: 0 }, { day: '周六', amount: 0 }, { day: '周日', amount: 0 }])
const maxEarnings = computed(() => Math.max(...weeklyEarnings.value.map(i => i.amount)))
const isLoading = ref(false)
/* ----------------- 生命周期 ----------------- */
onMounted(() => {
loadDriverInfo()
loadTaskCounts()
loadCurrentTask()
loadRecentTasks()
onMounted(async () => {
await loadDriverInfo()
await Promise.all([loadTaskCounts(), loadTodayStats(), loadCurrentTask(), loadRecentTasks()])
})
/* ----------------- 方法 ----------------- */
function loadDriverInfo() {
driverInfo.value = {
id: 'driver001',
user_id: 'user001',
real_name: '李师傅',
id_card: '110101199001011234',
driver_license: 'C1',
vehicle_type: 1,
vehicle_number: '京A12345',
work_status: 1,
current_location: { lat: 39.9042, lng: 116.4074 },
service_areas: ['朝阳区', '东城区'],
rating: 4.9,
total_orders: 368,
auth_status: 1,
created_at: '2024-01-01',
updated_at: '2024-12-01'
}
}
function loadTaskCounts() {
taskCounts.value = { total: 25, pending: 3, ongoing: 1, completed: 21 }
}
function loadCurrentTask() {
currentTask.value = {
id: 'task001',
order_id: 'order001',
driver_id: 'driver001',
pickup_address: { address: '朝阳区建国门大街1号', lat: 39.9042, lng: 116.4074 },
delivery_address: { address: '朝阳区CBD核心区2号', lat: 39.9142, lng: 116.4174 },
distance: 2.5,
estimated_time: 15,
delivery_fee: 8.0,
status: 2,
pickup_time: null,
delivered_time: null,
delivery_code: 'DEL123',
remark: '联系电话: 13888888888',
created_at: '2024-12-01 14:30:00',
updated_at: '2024-12-01 14:30:00'
}
}
function loadRecentTasks() {
recentTasks.value = [
{
id: 'task002',
order_id: 'order002',
driver_id: 'driver001',
pickup_address: { address: '朝阳区国贸中心' },
delivery_address: { address: '朝阳区望京SOHO' },
distance: 12.5,
estimated_time: 35,
delivery_fee: 12.0,
status: 4,
pickup_time: '2024-12-01 13:00:00',
delivered_time: '2024-12-01 13:30:00',
delivery_code: 'DEL122',
remark: '',
created_at: '2024-12-01 12:45:00',
updated_at: '2024-12-01 13:30:00'
},
{
id: 'task003',
order_id: 'order003',
driver_id: 'driver001',
pickup_address: { address: '朝阳区三里屯太古里' },
delivery_address: { address: '朝阳区工体北路' },
distance: 1.8,
estimated_time: 8,
delivery_fee: 6.0,
status: 4,
pickup_time: '2024-12-01 11:30:00',
delivered_time: '2024-12-01 11:45:00',
delivery_code: 'DEL121',
remark: '',
created_at: '2024-12-01 11:15:00',
updated_at: '2024-12-01 11:45:00'
/* ----------------- 后端数据加载(使用 supa ----------------- */
async function loadDriverInfo() {
try {
const ready = await Promise.race([supaReady, new Promise(resolve => setTimeout(() => resolve(false), 1500))])
if (!ready) console.warn('supaReady timeout/failed in loadDriverInfo - proceeding')
const userId = getCurrentUserId()
if (!userId) return
let res = await supa.from('ml_delivery_drivers').select('*').eq('user_id', userId).limit(1).execute()
if (!(res && Array.isArray(res.data) && res.data.length > 0)) {
const akRes = await supa.from('ak_users').select('id').eq('auth_id', userId).limit(1).execute()
let akId = ''
if (akRes && Array.isArray(akRes.data) && akRes.data.length > 0) akId = (akRes.data[0] as any).id
if (akId) res = await supa.from('ml_delivery_drivers').select('*').eq('user_id', akId).limit(1).execute()
}
]
if (res && Array.isArray(res.data) && res.data.length > 0) {
driverInfo.value = Object.assign(driverInfo.value, res.data[0])
workStatus.value = driverInfo.value.work_status ?? workStatus.value
}
} catch (e) {
console.error('loadDriverInfo error', e)
}
}
async function loadTaskCounts() {
try {
const ready = await Promise.race([supaReady, new Promise(resolve => setTimeout(() => resolve(false), 1500))])
if (!ready) console.warn('supaReady timeout/failed in loadTaskCounts - proceeding')
const driverId = driverInfo.value.id || getCurrentUserId()
if (!driverId) return
const res = await supa.from('ml_delivery_tasks').select('id,status').eq('driver_id', driverId).execute()
if (res && Array.isArray(res.data)) {
const rows = res.data as Array<any>
const total = rows.length
const pending = rows.filter(r => Number(r.status) === 1).length
const ongoing = rows.filter(r => Number(r.status) > 1 && Number(r.status) < 5).length
const completed = rows.filter(r => Number(r.status) >= 5).length
taskCounts.value = { total, pending, ongoing, completed }
}
} catch (e) {
console.error('loadTaskCounts error', e)
}
}
async function loadTodayStats() {
try {
const ready = await Promise.race([supaReady, new Promise(resolve => setTimeout(() => resolve(false), 1500))])
if (!ready) console.warn('supaReady timeout/failed in loadTodayStats - proceeding')
const driverId = driverInfo.value.id || getCurrentUserId()
if (!driverId) return
const start = new Date(); start.setHours(0,0,0,0)
const end = new Date(); end.setHours(23,59,59,999)
const res = await supa.from('ml_delivery_tasks').select('delivery_fee,distance,status,created_at').eq('driver_id', driverId).gte('created_at', start.toISOString()).lte('created_at', end.toISOString()).execute()
if (res && Array.isArray(res.data)) {
const rows = res.data as Array<any>
const deliveries = rows.filter(r => Number(r.status) >= 5).length
const earnings = rows.reduce((s, r) => s + (Number(r.delivery_fee) || 0), 0)
const distance = rows.reduce((s, r) => s + (Number(r.distance) || 0), 0)
todayStats.value = { deliveries, earnings: earnings.toFixed(2), distance: Number(distance.toFixed(2)), efficiency: deliveries === 0 ? 0 : Math.round((deliveries / Math.max(1, rows.length)) * 10000)/100 }
}
} catch (e) {
console.error('loadTodayStats error', e)
}
}
async function loadCurrentTask() {
try {
const ready = await Promise.race([supaReady, new Promise(resolve => setTimeout(() => resolve(false), 1500))])
if (!ready) console.warn('supaReady timeout/failed in loadCurrentTask - proceeding')
const driverId = driverInfo.value.id || getCurrentUserId()
if (!driverId) { currentTask.value = null; return }
const res = await supa.from('ml_delivery_tasks').select('*').eq('driver_id', driverId).lt('status', 5).order('created_at', { ascending: false }).limit(1).execute()
if (res && Array.isArray(res.data) && res.data.length > 0) {
currentTask.value = res.data[0]
} else {
currentTask.value = null
}
} catch (e) {
console.error('loadCurrentTask error', e)
}
}
async function loadRecentTasks() {
try {
const ready = await Promise.race([supaReady, new Promise(resolve => setTimeout(() => resolve(false), 1500))])
if (!ready) console.warn('supaReady timeout/failed in loadRecentTasks - proceeding')
const driverId = driverInfo.value.id || getCurrentUserId()
if (!driverId) { recentTasks.value = []; return }
const res = await supa.from('ml_delivery_tasks').select('*').eq('driver_id', driverId).order('created_at', { ascending: false }).range(0, 9).execute()
if (res && Array.isArray(res.data)) {
recentTasks.value = res.data as Array<any>
} else {
recentTasks.value = []
}
} catch (e) {
console.error('loadRecentTasks error', e)
recentTasks.value = []
}
}
/* ----------------- 小工具 & 交互 ----------------- */
function getWorkStatus(): string {
const m: Record<number, string> = { 0: '休息中', 1: '工作中', 2: '忙碌中' }
return m[driverInfo.value.work_status] || '未知状态'
@@ -347,10 +339,11 @@ function getTaskStatusText(status: number): string {
}
function getAddressText(address: UTSJSONObject): string {
return (address['address'] as string) || '地址信息'
return (address && (address['address'] as string)) || (address && (address['detail'] as string)) || '地址信息'
}
function formatTime(dateStr: string): string {
if (!dateStr) return ''
const diff = Date.now() - new Date(dateStr).getTime()
const hours = Math.floor(diff / 36e5)
if (hours < 1) return '刚刚'
@@ -358,58 +351,27 @@ function formatTime(dateStr: string): string {
return `${Math.floor(hours / 24)}天前`
}
/* ----------------- 交互 ----------------- */
function toggleWorkStatus() {
workStatus.value = workStatus.value === 1 ? 0 : 1
driverInfo.value.work_status = workStatus.value
uni.showToast({
title: workStatus.value === 1 ? '已开始工作' : '已停止工作',
icon: 'success'
})
uni.showToast({ title: workStatus.value === 1 ? '已开始工作' : '已停止工作', icon: 'success' })
}
// --- 新增:重新定位逻辑 ---
function showRelocateConfirm() {
uni.showModal({
title: '重新定位',
content: '确定要更新当前位置吗?',
confirmText: '立即定位',
success: (res) => {
if (res.confirm) {
relocate()
}
}
})
uni.showModal({ title: '重新定位', content: '确定要更新当前位置吗?', confirmText: '立即定位', success: (res) => { if (res.confirm) relocate() } })
}
function relocate() {
uni.showLoading({
title: '获取位置中...',
mask: true
})
// 模拟定位耗时
uni.showLoading({ title: '获取位置中...', mask: true })
setTimeout(() => {
uni.hideLoading()
// 模拟定位成功更新位置实际项目中应调用API获取真实位置
currentLocation.value = '朝阳区建国门外大街附近'
uni.showToast({
title: '定位成功',
icon: 'success',
duration: 1500
})
uni.showToast({ title: '定位成功', icon: 'success', duration: 1500 })
}, 1200)
}
function contactCustomer() {
uni.showActionSheet({
itemList: ['拨打电话', '发送短信'],
success: res => {
if (res.tapIndex === 0) uni.makePhoneCall({ phoneNumber: '13888888888' })
}
})
uni.showActionSheet({ itemList: ['拨打电话', '发送短信'], success: res => { if (res.tapIndex === 0) uni.makePhoneCall({ phoneNumber: '13888888888' }) } })
}
function viewTaskDetail(taskId = '') {
@@ -417,31 +379,14 @@ function viewTaskDetail(taskId = '') {
uni.navigateTo({ url: `/pages/mall/delivery/task-detail?id=${id}` })
}
/* ----------------- 导航 ----------------- */
function editProfile() {
uni.navigateTo({ url: '/pages/mall/delivery/profile-edit' })
}
function goToSettings() {
uni.navigateTo({ url: '/pages/mall/delivery/settings' })
}
function goToTasks(type: string) {
uni.navigateTo({ url: `/pages/mall/delivery/tasks?type=${type}` })
}
function goToEarnings() {
uni.navigateTo({ url: '/pages/mall/delivery/earnings' })
}
function goToVehicle() {
uni.navigateTo({ url: '/pages/mall/delivery/vehicle' })
}
function goToRatings() {
uni.navigateTo({ url: '/pages/mall/delivery/ratings' })
}
function goToHelp() {
uni.navigateTo({ url: '/pages/mall/delivery/help-center' })
}
function goToFeedback() {
uni.navigateTo({ url: '/pages/mall/delivery/feedback' })
}
function editProfile() { uni.navigateTo({ url: '/pages/mall/delivery/profile-edit' }) }
function goToSettings() { uni.navigateTo({ url: '/pages/mall/delivery/settings' }) }
function goToTasks(type: string) { uni.navigateTo({ url: `/pages/mall/delivery/tasks?type=${type}` }) }
function goToEarnings() { uni.navigateTo({ url: '/pages/mall/delivery/earnings' }) }
function goToVehicle() { uni.navigateTo({ url: '/pages/mall/delivery/vehicle' }) }
function goToRatings() { uni.navigateTo({ url: '/pages/mall/delivery/ratings' }) }
function goToHelp() { uni.navigateTo({ url: '/pages/mall/delivery/help-center' }) }
function goToFeedback() { uni.navigateTo({ url: '/pages/mall/delivery/feedback' }) }
</script>
<style scoped>