OpenMockDelivery.vue 18.9 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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
<template>
  <div class="mock-delivery-page soft-page-stack">
    <a-card :bordered="false" class="soft-section-card">
      <div class="soft-section-header">
        <div class="soft-section-heading">
          <h3 class="soft-section-title">模拟推送配送单</h3>
          <p class="soft-section-subtitle">按开放应用模拟创建外卖配送单,并在发送前直接试算当前租户的配送费。</p>
        </div>
        <div class="soft-section-actions">
          <a-button @click="fillDemo">填充示例</a-button>
          <a-button @click="resetForm">重置</a-button>
          <a-button :loading="calcLoading" @click="handleCalcFee">按应用租户试算配送费</a-button>
          <a-button type="primary" :loading="saving" @click="handleSubmit">发送推单</a-button>
        </div>
      </div>

      <div class="soft-note-card">
        <strong>使用说明</strong>
        <p>系统会根据当前开放应用自动识别所属租户。推单页只覆盖外卖场景,门店可选填接入方门店编号,未填写时再手动补全门店信息。</p>
      </div>

      <div class="mock-form-section">
        <div class="soft-section-header">
          <div class="soft-section-heading">
            <h3 class="soft-section-title">应用与订单基础信息</h3>
            <p class="soft-section-subtitle">先确定开放应用、租户和外部订单标识。</p>
          </div>
        </div>
      <a-form layout="vertical">
        <a-row :gutter="16">
          <a-col :xs="24" :lg="12">
            <a-form-item label="开放应用">
              <a-select
                v-model:value="form.appId"
                placeholder="请选择应用"
                show-search
                :filter-option="filterAppOption"
                @change="handleAppChange"
              >
                <a-select-option v-for="item in appList" :key="item.id" :value="item.id">
                  {{ item.appName }}({{ item.appKey }})
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :lg="12">
            <a-form-item label="所属租户">
              <a-input :value="selectedTenantName" disabled />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="16">
          <a-col :xs="24" :lg="12">
            <a-form-item label="外部订单号">
              <a-input v-model:value="form.outOrderNo" placeholder="请输入外部订单号" />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :lg="12">
            <a-form-item label="期望配送时间戳(秒)">
              <a-input-number v-model:value="form.serviceTime" :min="0" style="width:100%" />
            </a-form-item>
          </a-col>
        </a-row>

        <div class="mock-form-section">
          <div class="soft-section-header">
            <div class="soft-section-heading">
              <h3 class="soft-section-title">发货方信息</h3>
              <p class="soft-section-subtitle">如果已同步门店,优先填写接入方门店编号;否则补全门店名称、地址和经纬度。</p>
            </div>
          </div>
          <a-row :gutter="16">
            <a-col :xs="24" :lg="12">
              <a-form-item label="接入方门店编号">
                <a-input v-model:value="form.outStoreId" placeholder="可选,填写后可自动补门店信息" />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :lg="12">
              <a-form-item label="发货门店名称">
                <a-input v-model:value="form.storeName" placeholder="未填写门店编号时必填" />
              </a-form-item>
            </a-col>
          </a-row>
          <a-form-item label="发货门店地址">
            <a-input v-model:value="form.storeAddr" placeholder="请输入发货门店地址" />
          </a-form-item>
          <a-row :gutter="16">
            <a-col :xs="24" :lg="12">
              <a-form-item label="发货经度">
                <a-input v-model:value="form.storeLng" placeholder="如:113.264385" />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :lg="12">
              <a-form-item label="发货纬度">
                <a-input v-model:value="form.storeLat" placeholder="如:23.129112" />
              </a-form-item>
            </a-col>
          </a-row>
        </div>

        <div class="mock-form-section">
          <div class="soft-section-header">
            <div class="soft-section-heading">
              <h3 class="soft-section-title">收件人信息</h3>
              <p class="soft-section-subtitle">用于试算距离和创建配送单,收件人地址与经纬度建议同时填写。</p>
            </div>
          </div>
          <a-row :gutter="16">
            <a-col :xs="24" :lg="12">
              <a-form-item label="收件人姓名">
                <a-input v-model:value="form.recipName" placeholder="请输入收件人姓名" />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :lg="12">
              <a-form-item label="收件人电话">
                <a-input v-model:value="form.recipPhone" placeholder="请输入收件人手机号" />
              </a-form-item>
            </a-col>
          </a-row>
          <a-form-item label="收件人地址">
            <a-input v-model:value="form.recipAddr" placeholder="请输入收件地址" />
          </a-form-item>
          <a-row :gutter="16">
            <a-col :xs="24" :lg="12">
              <a-form-item label="收件经度">
                <a-input v-model:value="form.recipLng" placeholder="如:113.270000" />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :lg="12">
              <a-form-item label="收件纬度">
                <a-input v-model:value="form.recipLat" placeholder="如:23.135000" />
              </a-form-item>
            </a-col>
          </a-row>
        </div>

        <div class="mock-form-section">
          <div class="soft-section-header">
            <div class="soft-section-heading">
              <h3 class="soft-section-title">订单信息</h3>
              <p class="soft-section-subtitle">当前只处理外卖场景,支持重量、整单备注和应用级回调。</p>
            </div>
          </div>
          <a-row :gutter="16">
            <a-col :xs="24" :lg="12">
              <a-form-item label="货物重量(kg)">
                <a-input-number v-model:value="form.weight" :min="0" :step="0.1" style="width:100%" />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :lg="12">
              <a-form-item label="订单级回调地址">
                <a-input v-model:value="form.callbackUrl" placeholder="可选,留空则使用应用级 Webhook" />
              </a-form-item>
            </a-col>
          </a-row>
          <a-form-item label="整单备注">
            <a-textarea v-model:value="form.remark" :rows="3" placeholder="如:请尽快送达" />
          </a-form-item>
          <a-form-item label="货物备注">
            <a-textarea v-model:value="form.itemRemark" :rows="2" placeholder="如:饮品分开装、轻拿轻放" />
          </a-form-item>
        </div>

        <div class="mock-form-section">
          <div class="soft-section-header">
            <div class="soft-section-heading">
              <h3 class="soft-section-title">货物清单</h3>
              <p class="soft-section-subtitle">件数会参与配送费试算,名称为空的货物不会提交到后端。</p>
            </div>
            <div class="soft-section-actions">
              <a-button type="dashed" @click="addItem">新增货物</a-button>
            </div>
          </div>
          <div v-if="form.items.length" class="soft-dashed-block">
          <a-row
            v-for="(item, index) in form.items"
            :key="index"
            :gutter="12"
            class="mock-item-row"
          >
            <a-col :xs="24" :lg="6">
              <a-form-item :label="index === 0 ? '名称' : ''">
                <a-input v-model:value="item.name" placeholder="如:招牌套餐" />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :lg="4">
              <a-form-item :label="index === 0 ? '数量' : ''">
                <a-input-number v-model:value="item.quantity" :min="1" style="width:100%" />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :lg="6">
              <a-form-item :label="index === 0 ? '规格' : ''">
                <a-input v-model:value="item.spec" placeholder="如:大杯 / 双拼" />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :lg="6">
              <a-form-item :label="index === 0 ? '备注' : ''">
                <a-input v-model:value="item.remark" placeholder="可选" />
              </a-form-item>
            </a-col>
            <a-col :xs="24" :lg="2">
              <a-form-item :label="index === 0 ? '操作' : ''">
                <a-button danger block @click="removeItem(index)">删除</a-button>
              </a-form-item>
            </a-col>
          </a-row>
          </div>
          <a-empty v-else description="暂无货物清单" />
        </div>
      </a-form>
      </div>
    </a-card>

    <div v-if="feeResult || result" class="soft-result-grid">
      <a-card v-if="feeResult" :bordered="false" class="soft-section-card">
        <div class="soft-section-header">
          <div class="soft-section-heading">
            <h3 class="soft-section-title">按应用租户试算结果</h3>
            <p class="soft-section-subtitle">试算时不会手填租户,系统会自动使用当前开放应用绑定的租户;实际推单金额以后端创建订单时实时计算为准。</p>
          </div>
        </div>
      <a-descriptions bordered :column="2">
        <a-descriptions-item label="配送费">¥{{ feeResult.totalFee }}</a-descriptions-item>
        <a-descriptions-item label="配送距离">{{ feeResult.distance }} km</a-descriptions-item>
        <a-descriptions-item label="预计送达">{{ feeResult.estimatedMinutes }} 分钟</a-descriptions-item>
        <a-descriptions-item label="基础费用">
          ¥{{ feeResult.moneyBasic }}{{ feeResult.moneyBasicTxt ? ` ${feeResult.moneyBasicTxt}` : '' }}
        </a-descriptions-item>
        <a-descriptions-item label="超距费用">
          ¥{{ feeResult.moneyDistance }}{{ feeResult.moneyDistanceTxt ? ` ${feeResult.moneyDistanceTxt}` : '' }}
        </a-descriptions-item>
        <a-descriptions-item label="超重费用">
          ¥{{ feeResult.moneyWeight }}{{ feeResult.moneyWeightTxt ? ` ${feeResult.moneyWeightTxt}` : '' }}
        </a-descriptions-item>
        <a-descriptions-item label="件数费用">
          ¥{{ feeResult.moneyPiece }}{{ feeResult.moneyPieceTxt ? ` ${feeResult.moneyPieceTxt}` : '' }}
        </a-descriptions-item>
        <a-descriptions-item label="时段附加费">¥{{ feeResult.moneyTime }}</a-descriptions-item>
        <a-descriptions-item label="保底费用">¥{{ feeResult.minFee }}{{ feeResult.minFeeApplied === 1 ? '(已命中)' : '' }}</a-descriptions-item>
        <a-descriptions-item label="计费重量">{{ feeResult.weight }} kg</a-descriptions-item>
        <a-descriptions-item label="计费件数">{{ feeResult.pieces || 0 }} 件</a-descriptions-item>
      </a-descriptions>
      </a-card>

      <a-card v-if="result" :bordered="false" class="soft-section-card">
        <div class="soft-section-header">
          <div class="soft-section-heading">
            <h3 class="soft-section-title">推单结果</h3>
            <p class="soft-section-subtitle">展示后台创建后的配送单编号、状态和费用结果。</p>
          </div>
        </div>
      <a-descriptions bordered :column="2">
        <a-descriptions-item label="配送单ID">{{ result.deliveryOrderId }}</a-descriptions-item>
        <a-descriptions-item label="平台订单号">{{ result.orderNo }}</a-descriptions-item>
        <a-descriptions-item label="外部订单号">{{ result.outOrderNo }}</a-descriptions-item>
        <a-descriptions-item label="状态">{{ statusMap[result.status] || result.status }}</a-descriptions-item>
        <a-descriptions-item label="配送费">¥{{ result.totalFee }}</a-descriptions-item>
        <a-descriptions-item label="配送距离">{{ result.distance }} km</a-descriptions-item>
        <a-descriptions-item label="基础费">¥{{ result.moneyBasic }}</a-descriptions-item>
        <a-descriptions-item label="超距费">¥{{ result.moneyDistance }}</a-descriptions-item>
        <a-descriptions-item label="时段附加费">¥{{ result.moneyTime }}</a-descriptions-item>
        <a-descriptions-item label="预计送达">{{ result.estimatedMinutes }} 分钟</a-descriptions-item>
      </a-descriptions>
      </a-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, reactive, ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { deliveryApi, openApi } from '@/api'
import { useRoleCityList } from '@/composables/useRoleCityList'

type OpenAppItem = {
  id: number
  appName: string
  appKey: string
  cityId: number
  status: number
}

type DeliveryItem = {
  name: string
  quantity: number
  spec: string
  remark: string
}

const appList = ref<OpenAppItem[]>([])
const saving = ref(false)
const calcLoading = ref(false)
const feeResult = ref<any>(null)
const result = ref<any>(null)
const { loadCities, getCityName } = useRoleCityList()

const statusMap: Record<number, string> = {
  2: '待接单',
  3: '已接单',
  4: '配送中',
  6: '已完成',
  10: '已取消',
}

function createDefaultItem(): DeliveryItem {
  return { name: '', quantity: 1, spec: '', remark: '' }
}

function createDefaultForm() {
  return {
    appId: undefined as number | undefined,
    outStoreId: '',
    storeName: '',
    storeAddr: '',
    storeLng: '',
    storeLat: '',
    recipName: '',
    recipPhone: '',
    recipAddr: '',
    recipLng: '',
    recipLat: '',
    outOrderNo: generateOutOrderNo(),
    weight: 0,
    serviceTime: 0,
    callbackUrl: '',
    remark: '',
    itemRemark: '',
    items: [createDefaultItem()],
  }
}

const form = reactive(createDefaultForm())

const selectedApp = computed(() => appList.value.find(item => item.id === form.appId))
const selectedTenantName = computed(() => getCityName(selectedApp.value?.cityId))

function generateOutOrderNo() {
  return `MOCK${Date.now()}`
}

function resetForm() {
  Object.assign(form, createDefaultForm())
  feeResult.value = null
  result.value = null
}

function fillDemo() {
  if (!form.appId && appList.value.length) {
    form.appId = appList.value[0].id
  }
  Object.assign(form, {
    ...form,
    outOrderNo: generateOutOrderNo(),
    outStoreId: '',
    storeName: '测试门店',
    storeAddr: '广州市天河区体育西路 100 号',
    storeLng: '113.327823',
    storeLat: '23.137466',
    recipName: '测试用户',
    recipPhone: '13800138000',
    recipAddr: '广州市天河区珠江新城测试地址',
    recipLng: '113.324590',
    recipLat: '23.119295',
    weight: 1,
    serviceTime: 0,
    callbackUrl: '',
    remark: '后台模拟推单',
    itemRemark: '少冰少辣',
  })
  form.items = [
    { name: '招牌奶茶', quantity: 2, spec: '大杯', remark: '少冰' },
    { name: '鸡腿饭', quantity: 1, spec: '标准', remark: '不要辣' },
  ]
  feeResult.value = null
  result.value = null
}

function addItem() {
  form.items.push(createDefaultItem())
}

function removeItem(index: number) {
  form.items.splice(index, 1)
  if (!form.items.length) {
    form.items.push(createDefaultItem())
  }
}

function handleAppChange() {
  feeResult.value = null
  result.value = null
}

function filterAppOption(input: string, option: any) {
  return String(option.children).toLowerCase().includes(input.toLowerCase())
}

function buildPayload() {
  return {
    appId: form.appId,
    outStoreId: form.outStoreId || undefined,
    storeName: form.storeName || undefined,
    storeAddr: form.storeAddr || undefined,
    storeLng: form.storeLng || undefined,
    storeLat: form.storeLat || undefined,
    recipName: form.recipName,
    recipPhone: form.recipPhone,
    recipAddr: form.recipAddr,
    recipLng: form.recipLng,
    recipLat: form.recipLat,
    outOrderNo: form.outOrderNo,
    weight: form.weight,
    serviceTime: form.serviceTime,
    callbackUrl: form.callbackUrl || undefined,
    remark: form.remark || undefined,
    itemRemark: form.itemRemark || undefined,
    items: form.items
      .filter(item => item.name.trim())
      .map(item => ({
        name: item.name.trim(),
        quantity: item.quantity || 1,
        spec: item.spec || undefined,
        remark: item.remark || undefined,
      })),
  }
}

function clearCalcResult() {
  feeResult.value = null
}

function buildCalcPayload() {
  return {
    cityId: selectedApp.value?.cityId,
    orderType: 6,
    startLng: form.storeLng,
    startLat: form.storeLat,
    endLng: form.recipLng,
    endLat: form.recipLat,
    weight: form.weight,
    pieces: form.items.reduce((sum, item) => sum + (item.quantity || 0), 0),
    serviceTime: form.serviceTime,
  }
}

async function loadApps() {
  const res: any = await openApi.list()
  appList.value = Array.isArray(res?.data) ? res.data.filter((item: OpenAppItem) => item.status === 1) : []
}

async function handleSubmit() {
  if (!form.appId) {
    message.error('请选择开放应用')
    return
  }
  if (!form.outOrderNo || !form.recipName || !form.recipPhone || !form.recipAddr || !form.recipLng || !form.recipLat) {
    message.error('请填写完整的收件人与订单信息')
    return
  }
  if (!form.outStoreId && (!form.storeName || !form.storeLng || !form.storeLat)) {
    message.error('未填写门店编号时,请补全发货门店名称和经纬度')
    return
  }

  saving.value = true
  try {
    const res: any = await openApi.mockDeliveryCreate(buildPayload())
    result.value = res.data
    message.success('模拟推单成功')
  } finally {
    saving.value = false
  }
}

async function handleCalcFee() {
  if (!form.appId || !selectedApp.value?.cityId) {
    message.error('请选择开放应用')
    return
  }
  if (!form.storeLng || !form.storeLat || !form.recipLng || !form.recipLat) {
    message.error('请填写完整的发货与收货经纬度')
    return
  }

  calcLoading.value = true
  try {
    const res: any = await deliveryApi.calc(buildCalcPayload())
    feeResult.value = res.data
    message.success('配送费计算完成')
  } finally {
    calcLoading.value = false
  }
}

onMounted(async () => {
  await Promise.all([loadApps(), loadCities()])
  if (appList.value.length) {
    form.appId = appList.value[0].id
  }
})

watch(
  () => [form.storeLng, form.storeLat, form.recipLng, form.recipLat, form.weight, form.serviceTime, form.outStoreId],
  () => {
    clearCalcResult()
  }
)
</script>

<style scoped>
.mock-delivery-page {
  gap: 16px;
}

.mock-form-section {
  margin-top: 8px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.mock-item-row {
  margin-bottom: 12px;
  align-items: flex-start;
}
</style>