Files
tms-erp-web/src/views/business/common-route.vue
T
b2894lxlx 4b01316f64 ✨ 新增项目预付核销 UI,并修复常用线路新增不显示与付款提交后刷新
结算单附件增加项目预付核销;常用线路超管默认查全部组织并按筛选归属新增;付款申请提交后关页并刷新列表;平台公司选项 ID 按字符串绑定。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-16 02:02:00 +08:00

1229 lines
40 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<basic-container class="common-route-page">
<avue-crud
:option="option"
:table-loading="loading"
:data="data"
v-model:page="page"
v-model="form"
ref="crud"
:permission="permissionList"
:before-open="beforeOpen"
@row-save="rowSave"
@row-update="rowUpdate"
@row-del="rowDel"
@search-change="searchChange"
@search-reset="searchReset"
@selection-change="selectionChange"
@current-change="currentChange"
@size-change="sizeChange"
@refresh-change="refreshChange"
@on-load="onLoad"
>
<template #menu-left>
<el-button
type="primary"
icon="el-icon-plus"
v-if="hasPermission('common_route_add')"
@click="$refs.crud.rowAdd()"
>新建路线
</el-button>
<el-button
type="primary"
icon="el-icon-upload"
plain
v-if="config.importUrl && hasPermission('common_route_import')"
@click="handleImport"
>批量导入
</el-button>
<el-button
type="primary"
icon="el-icon-download"
plain
v-if="config.templateUrl && hasPermission('common_route_template')"
@click="handleTemplate"
>下载模板
</el-button>
<el-button
type="primary"
icon="el-icon-download"
plain
v-if="config.exportUrl && hasPermission('common_route_export')"
@click="handleExport"
>批量导出
</el-button>
<el-button
type="danger"
icon="el-icon-delete"
plain
v-if="hasPermission('common_route_delete')"
@click="handleDelete"
>批量删除
</el-button>
</template>
<template #departureAddress-form>
<div class="common-route-page__route-address">
<el-input
v-model="form.departureName"
readonly
placeholder="自动带出"
:disabled="dialogReadonly"
/>
<el-input
v-model="form.departureAddress"
placeholder="请输入并选择地址"
suffix-icon="el-icon-location"
:disabled="dialogReadonly"
@click="openRouteMapDialog('departure')"
/>
<el-tooltip content="选择常用地址" placement="top">
<el-button
type="primary"
:icon="List"
link
:disabled="dialogReadonly"
@click="openAddressDialog('departure')"
/>
</el-tooltip>
</div>
</template>
<template #arrivalAddress-form>
<div class="common-route-page__route-address">
<el-input
v-model="form.arrivalName"
readonly
placeholder="自动带出"
:disabled="dialogReadonly"
/>
<el-input
v-model="form.arrivalAddress"
placeholder="请输入并选择地址"
suffix-icon="el-icon-location"
:disabled="dialogReadonly"
@click="openRouteMapDialog('arrival')"
/>
<el-tooltip content="选择常用地址" placement="top">
<el-button
type="primary"
:icon="List"
link
:disabled="dialogReadonly"
@click="openAddressDialog('arrival')"
/>
</el-tooltip>
</div>
</template>
<template #menu="{ row, index }">
<el-link
type="primary"
v-if="hasPermission('common_route_view')"
@click="$refs.crud.rowView(row, index)"
>
查看
</el-link>
<el-link
type="primary"
v-if="hasPermission('common_route_edit') && !row.readonly"
@click="$refs.crud.rowEdit(row, index)"
>
编辑
</el-link>
<el-link
type="danger"
v-if="hasPermission('common_route_delete') && !row.readonly"
@click="rowDel(row)"
>
删除
</el-link>
</template>
</avue-crud>
<empty-pagination
:page="page"
@size-change="sizeChange"
@current-change="currentChange"
@load="onLoad(page, query)"
/>
<el-dialog :title="`${config.title}导入`" append-to-body v-model="excelBox" width="555px">
<avue-form :option="excelOptionConfig" v-model="excelForm">
<template #excelTemplate>
<el-button type="primary" @click="handleTemplate">
点击下载<i class="el-icon-download el-icon--right"></i>
</el-button>
</template>
</avue-form>
</el-dialog>
<el-dialog
title="选择常用地址"
append-to-body
v-model="addressDialogVisible"
width="1080px"
class="common-route-page__address-dialog"
@opened="loadAddressList"
>
<section-card>
<div class="common-route-page__address-search">
<el-form :model="addressQuery" label-position="right" label-width="86px">
<el-row :gutter="16">
<el-col :span="6">
<el-form-item label="地址名称">
<el-input v-model="addressQuery.addressName" clearable placeholder="请输入" />
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="地址类型">
<el-select
v-model="addressQuery.addressType"
clearable
placeholder="请选择"
style="width: 100%"
>
<el-option
v-for="item in addressTypeOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
</el-col>
<el-col :span="6">
<el-form-item label="详细地址">
<el-input v-model="addressQuery.detailAddress" clearable placeholder="请输入" />
</el-form-item>
</el-col>
<el-col :span="6" class="common-route-page__address-search-actions">
<el-button type="primary" @click="handleAddressSearch">查询</el-button>
<el-button @click="handleAddressReset">重置</el-button>
</el-col>
<el-col :span="6">
<el-form-item label="组织">
<el-select
v-model="addressQuery.deptId"
clearable
filterable
placeholder="请选择"
style="width: 100%"
>
<el-option
v-for="item in deptOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
</el-col>
</el-row>
</el-form>
</div>
<el-table
border
:data="addressData"
v-loading="addressLoading"
height="310"
highlight-current-row
@row-click="selectAddressRow"
>
<el-table-column type="index" label="序号" width="80" align="center" />
<el-table-column prop="addressName" label="地址名称" min-width="150" />
<el-table-column prop="addressCode" label="地址编号" min-width="150" />
<el-table-column prop="addressType" label="类型" min-width="150" />
<el-table-column label="站点编码" min-width="170">
<template #default="{ row }">
{{ row.addressType === '常规地址' ? '/' : row.siteCodeDisplay || row.siteCode || '' }}
</template>
</el-table-column>
<el-table-column prop="detailAddress" label="详细地址" min-width="220" />
<el-table-column label="操作" width="120" fixed="right">
<template #default="{ row }">
<el-link type="primary" @click.stop="selectAddressRow(row)">选择</el-link>
</template>
</el-table-column>
</el-table>
<div class="common-route-page__address-pagination">
<el-pagination
v-model:current-page="addressPage.currentPage"
v-model:page-size="addressPage.pageSize"
:page-sizes="[10, 20, 50, 100]"
layout="total, prev, pager, next, sizes"
:total="addressPage.total"
@size-change="handleAddressSizeChange"
@current-change="handleAddressCurrentChange"
/>
</div>
</section-card>
<template #footer>
<el-button @click="addressDialogVisible = false">关闭</el-button>
</template>
</el-dialog>
<el-dialog
title="地图选择地址"
append-to-body
v-model="routeMapVisible"
width="920px"
@opened="initRouteAmap"
>
<div class="common-route-page__map-toolbar">
<el-input
v-model="routeMapKeyword"
clearable
placeholder="输入地址关键词"
@keyup.enter="searchRouteMapKeyword"
/>
<el-button
type="primary"
:icon="Search"
:loading="routeMapLoading"
@click="searchRouteMapKeyword"
>
搜索
</el-button>
</div>
<div class="map-picker-content">
<div ref="routeAmap" class="common-route-page__map"></div>
<map-search-results :results="routeMapSearchResults" @select="selectRouteMapSearchResult" />
</div>
<div class="common-route-page__map-info">
<span>{{ routeMapStatus }}</span>
<span v-if="routeMapSelected.regionName"
>发货/收货地:{{ routeMapSelected.regionName }}</span
>
</div>
<template #footer>
<el-button @click="routeMapVisible = false">取消</el-button>
<el-button
type="primary"
:disabled="!routeMapSelected.longitude"
:loading="routeMapConfirmLoading"
@click="confirmRouteMapPick"
>
确定
</el-button>
</template>
</el-dialog>
</basic-container>
</template>
<script>
import * as api from '@/api/business/common-route';
import { getList as getCommonAddressList } from '@/api/base/common-address';
import { getDetail as getRegionDetail } from '@/api/base/region';
import { exportBlob } from '@/api/common';
import { getDeptTree } from '@/api/system/dept';
import { addressTypeOptions } from '@/option/base/common-address';
import { config, excelOption, option } from '@/option/business/common-route';
import { getToken, isAdministrator } from '@/utils/auth';
import { openImportDialog } from '@/utils/import-excel';
import { normalizeMapSearchResults, searchAmapPlaceText } from '@/utils/map-search';
import { downloadXls } from '@/utils/util';
import { isMobile } from '@/utils/validate';
import { List, Search } from '@element-plus/icons-vue';
import { mapGetters } from 'vuex';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
const AMAP_KEY = '653b7cf105ad7fb8ec9b2f5198ade315';
const AMAP_SECURITY_CODE = '5aab65c632e48ebae0d0e28f5b28e21a';
let amapLoader;
export default {
data() {
return {
List,
Search,
config,
option,
form: {},
query: {},
loading: true,
data: [],
dialogReadonly: false,
excelBox: false,
excelForm: {},
excelOptionConfig: excelOption,
addressTypeOptions,
addressDialogVisible: false,
addressTarget: '',
addressQuery: {},
addressData: [],
addressLoading: false,
routeMapVisible: false,
routeMapTarget: '',
routeMapLoading: false,
routeMapConfirmLoading: false,
routeMapKeyword: '',
routeMapStatus: '可搜索地址或点击地图选点',
routeMapSelected: {},
routeMapSearchResults: [],
routeAmap: null,
routeAmapMarker: null,
routeAmapGeocoder: null,
deptOptions: [],
page: {
pageSize: 10,
pageSizes: [10, 20, 50, 100],
currentPage: 1,
total: 0,
},
addressPage: {
pageSize: 10,
currentPage: 1,
total: 0,
},
selectionList: [],
};
},
computed: {
...mapGetters(['permission', 'userInfo']),
permissionList() {
return {
addBtn: this.hasPermission('common_route_add'),
};
},
isAdmin() {
return isAdministrator(this.userInfo);
},
ids() {
return this.selectionList.map(item => item.id).join(',');
},
},
created() {
// 超管默认查全部组织,不预填组织筛选;普通用户预填当前组织。
if (!this.isAdmin) {
this.applyDefaultDeptSearch();
}
this.initDeptTree();
},
methods: {
hasPermission(code) {
return this.isAdmin || this.validData(this.permission && this.permission[code], false);
},
initDeptTree() {
getDeptTree(this.userInfo.tenantId).then(res => {
const deptTree = res.data.data || [];
this.deptOptions = this.flattenDept(deptTree);
const deptColumn = this.findColumn(this.option.column, 'searchDeptId');
if (deptColumn) {
deptColumn.dicData = this.buildDeptTree(deptTree);
}
});
},
buildDeptTree(tree) {
return tree.map(item => {
// 雪花 ID 必须保持字符串,避免 JS Number 精度丢失导致筛错组织。
const id =
item.id === undefined || item.id === null || item.id === '' ? '' : String(item.id);
return {
label: item.title || item.deptName || item.name,
value: id,
children:
item.children && item.children.length ? this.buildDeptTree(item.children) : undefined,
};
}).filter(item => item.value);
},
flattenDept(tree, level = 0) {
const result = [];
tree.forEach(item => {
const id =
item.id === undefined || item.id === null || item.id === '' ? '' : String(item.id);
if (!id) return;
result.push({
label: `${' '.repeat(level)}${item.title || item.deptName || item.name}`,
value: id,
});
if (item.children && item.children.length) {
result.push(...this.flattenDept(item.children, level + 1));
}
});
return result;
},
defaultDeptId() {
const raw = (this.userInfo && (this.userInfo.deptId || this.userInfo.dept_id)) || '';
return (
String(raw)
.split(',')
.map(item => item.trim())
.filter(Boolean)[0] || ''
);
},
normalizeListQuery(params = {}) {
// 搜索条件优先于缓存的 query,避免默认组织 ID 覆盖用户新选择的组织。
const query = { ...this.query, ...params };
// 本次搜索显式带了组织字段时,以本次为准(含清空);否则沿用缓存。
const hasDeptInParams =
Object.prototype.hasOwnProperty.call(params, 'deptId') ||
Object.prototype.hasOwnProperty.call(params, 'searchDeptId');
const rawDeptId = hasDeptInParams
? params.deptId ?? params.searchDeptId
: query.deptId ?? query.searchDeptId;
delete query.searchDeptId;
if (rawDeptId !== undefined && rawDeptId !== null && String(rawDeptId).trim() !== '') {
// 级联可能回传路径数组,统一取末级并保持字符串,避免雪花 ID 精度丢失。
const deptValue = Array.isArray(rawDeptId) ? rawDeptId.at(-1) : rawDeptId;
query.deptId = String(deptValue ?? '')
.split(',')[0]
.trim();
if (!query.deptId) {
delete query.deptId;
}
} else {
delete query.deptId;
}
query.allDept = this.isAdmin ? 1 : 0;
return query;
},
applyDefaultDeptSearch() {
// 超管默认不限组织,避免预填组织后新增落在其他组织时列表看不见。
if (this.isAdmin) {
const deptColumn = this.findColumn(this.option.column, 'searchDeptId');
if (deptColumn) {
deptColumn.searchValue = undefined;
}
const nextQuery = { ...this.query };
delete nextQuery.deptId;
delete nextQuery.searchDeptId;
this.query = nextQuery;
this.$nextTick(() => {
if (this.$refs.crud?.searchForm) {
this.$refs.crud.searchForm.searchDeptId = undefined;
}
});
return;
}
const deptId = this.defaultDeptId();
const deptColumn = this.findColumn(this.option.column, 'searchDeptId');
if (!deptId || !deptColumn) return;
deptColumn.searchValue = deptId;
this.query = { ...this.query, deptId };
this.$nextTick(() => {
if (this.$refs.crud?.searchForm) {
this.$refs.crud.searchForm.searchDeptId = deptId;
}
});
},
resolveDeptName(deptId) {
if (!deptId) return '';
const matched = this.deptOptions.find(item => String(item.value) === String(deptId));
return matched ? String(matched.label || '').trim() : '';
},
resetAddressQuery() {
this.addressQuery = {
addressName: '',
addressType: '',
detailAddress: '',
deptId: this.defaultDeptId(),
};
},
openAddressDialog(target) {
if (this.dialogReadonly) return;
this.addressTarget = target;
this.addressPage.currentPage = 1;
this.resetAddressQuery();
this.addressDialogVisible = true;
},
handleAddressSearch() {
this.addressPage.currentPage = 1;
this.loadAddressList();
},
handleAddressReset() {
this.addressPage.currentPage = 1;
this.resetAddressQuery();
this.loadAddressList();
},
handleAddressSizeChange(pageSize) {
this.addressPage.pageSize = pageSize;
this.addressPage.currentPage = 1;
this.loadAddressList();
},
handleAddressCurrentChange(currentPage) {
this.addressPage.currentPage = currentPage;
this.loadAddressList();
},
loadAddressList() {
this.addressLoading = true;
getCommonAddressList(this.addressPage.currentPage, this.addressPage.pageSize, {
...this.addressQuery,
allDept: this.isAdmin ? 1 : 0,
})
.then(res => {
const data = res.data.data || {};
this.addressPage.total = data.total || 0;
this.addressData = data.records || [];
})
.finally(() => {
this.addressLoading = false;
});
},
async selectAddressRow(row) {
if (!row) return;
const applied = await this.applyAddress(this.addressTarget, row);
if (!applied) return;
this.addressDialogVisible = false;
},
async applyAddress(target, address) {
if (!target || !address) return;
const prefix = target === 'departure' ? 'departure' : 'arrival';
const regionIds = await this.resolveRegionIds(address.regionCode);
if (!this.hasCompleteRegionIds(regionIds)) {
this.$message.warning('该常用地址缺少完整的省、市、区ID,请先维护行政区划');
return false;
}
this.applyRegionIds(prefix, regionIds);
this.form[`${prefix}AddressId`] = address.id || '';
this.form[`${prefix}Name`] = address.addressName || '';
this.form[`${prefix}Address`] = address.detailAddress || '';
this.form[`${prefix}AddressCode`] = address.addressCode || '';
this.form[`${prefix}SiteCode`] =
address.addressType === '常规地址'
? '/'
: address.siteCodeDisplay || address.siteCode || '';
this.form[`${prefix}Longitude`] = address.longitude || '';
this.form[`${prefix}Latitude`] = address.latitude || '';
this.form[`${prefix}Contact`] = address.contactName || this.form[`${prefix}Contact`] || '';
this.form[`${prefix}Phone`] = address.contactPhone || this.form[`${prefix}Phone`] || '';
return true;
},
openRouteMapDialog(target) {
if (this.dialogReadonly) return;
const prefix = target === 'departure' ? 'departure' : 'arrival';
this.routeMapTarget = prefix;
this.routeMapKeyword = this.form[`${prefix}Address`] || this.form[`${prefix}Name`] || '';
this.routeMapSelected = this.buildRouteMapSelection({
lng: this.form[`${prefix}Longitude`],
lat: this.form[`${prefix}Latitude`],
address: this.form[`${prefix}Address`],
regionName: this.form[`${prefix}Name`],
regionCode: this.form[`${prefix}DistrictId`],
provinceId: this.form[`${prefix}ProvinceId`],
cityId: this.form[`${prefix}CityId`],
districtId: this.form[`${prefix}DistrictId`],
});
this.routeMapStatus = this.routeMapSelected.longitude
? '已加载当前选点,可重新选点'
: '可搜索地址或点击地图选点';
this.routeMapVisible = true;
},
loadAmap() {
if (window.AMap && window.AMap.Map) {
return Promise.resolve();
}
if (!amapLoader) {
amapLoader = new Promise((resolve, reject) => {
window._AMapSecurityConfig = {
securityJsCode: AMAP_SECURITY_CODE,
};
const script = document.createElement('script');
script.src = `https://webapi.amap.com/maps?v=2.0&key=${AMAP_KEY}`;
script.async = true;
script.onload = resolve;
script.onerror = () => reject(new Error('高德地图组件加载失败'));
document.body.appendChild(script);
});
}
return amapLoader;
},
initRouteAmap() {
this.loadAmap()
.then(() => {
this.$nextTick(() => {
if (!this.routeAmap) {
const center = this.toLngLat(
this.routeMapSelected.longitude || 116.40769,
this.routeMapSelected.latitude || 39.89945
);
this.routeAmap = new window.AMap.Map(this.$refs.routeAmap, {
center,
zoom: this.routeMapSelected.longitude ? 14 : 11,
});
window.AMap.plugin(['AMap.ToolBar', 'AMap.Geocoder'], () => {
this.routeAmap.addControl(new window.AMap.ToolBar());
if (!this.routeAmapGeocoder) {
this.routeAmapGeocoder = new window.AMap.Geocoder();
}
});
this.routeAmap.on('click', event => this.pickRouteMapPoint(event.lnglat));
} else if (typeof this.routeAmap.resize === 'function') {
this.routeAmap.resize();
}
if (this.routeMapSelected.longitude) {
this.renderRouteMapMarker(
this.toLngLat(this.routeMapSelected.longitude, this.routeMapSelected.latitude)
);
}
});
})
.catch(() => {
this.$message.error('高德地图组件加载失败,请稍后重试或重新打开弹窗');
this.routeMapVisible = false;
});
},
searchRouteMapKeyword() {
const keyword = String(this.routeMapKeyword || '').trim();
if (!keyword) {
this.$message.warning('请输入地址关键词');
return;
}
if (this.routeMapLoading) return;
this.routeMapSearchResults = [];
this.routeMapLoading = true;
this.loadAmap()
.then(() => searchAmapPlaceText(keyword))
.then(pois => {
this.routeMapSearchResults = normalizeMapSearchResults(pois, keyword);
const first = this.routeMapSearchResults[0];
if (!first?.location) {
this.routeMapStatus = '未找到匹配地址';
this.$message.warning('地图搜索无匹配地址');
return;
}
return this.$nextTick().then(() => {
if (typeof this.routeAmap?.resize === 'function') {
this.routeAmap.resize();
}
this.pickRouteMapPoint(first.location, first.address || first.name || keyword);
});
})
.catch(() => {
this.routeMapStatus = '未找到匹配地址';
this.$message.warning('地图搜索无匹配地址');
})
.finally(() => {
this.routeMapLoading = false;
});
},
selectRouteMapSearchResult(item) {
if (item && item.location) {
this.pickRouteMapPoint(item.location, item.address || item.name || '');
}
},
pickRouteMapPoint(lnglat, keyword) {
const longitude = this.getPointLng(lnglat);
const latitude = this.getPointLat(lnglat);
if (longitude === undefined || latitude === undefined) {
this.$message.warning('选点坐标无效');
return;
}
const point = this.toLngLat(longitude, latitude);
this.renderRouteMapMarker(point);
this.routeMapSelected = this.buildRouteMapSelection({
lng: longitude,
lat: latitude,
address: keyword,
});
this.routeMapStatus = '正在反查地址...';
this.ensureRouteAmapGeocoder()
.then(() => this.runAmapGeocode('address', point))
.then(result => {
const address = this.resolveMapAddress(result);
this.routeMapSelected = {
...this.routeMapSelected,
detailAddress: address.detailAddress || this.routeMapSelected.detailAddress,
regionName: address.regionName || this.routeMapSelected.regionName,
regionCode: address.regionCode || '',
};
this.routeMapKeyword = this.routeMapSelected.detailAddress || this.routeMapKeyword;
this.routeMapStatus = this.routeMapSelected.detailAddress || '已选点,可确认回填';
})
.catch(() => {
this.routeMapStatus = '反查地址失败';
this.$message.error('反查地址失败,请重新选点');
});
},
renderRouteMapMarker(point) {
if (!this.routeAmap || !window.AMap) return;
if (this.routeAmapMarker) {
this.routeAmapMarker.setMap(null);
}
this.routeAmapMarker = new window.AMap.Marker({ position: point });
this.routeAmapMarker.setMap(this.routeAmap);
this.routeAmap.setCenter(point);
},
async confirmRouteMapPick() {
if (!this.routeMapSelected.longitude) {
this.$message.warning('请先搜索或点击地图完成选点');
return;
}
const prefix = this.routeMapTarget;
this.routeMapConfirmLoading = true;
try {
const regionIds = await this.resolveRegionIds(this.routeMapSelected.regionCode);
if (!this.hasCompleteRegionIds(regionIds)) {
this.$message.warning('当前选点未解析到完整的省、市、区ID,请重新选择地址');
return;
}
this.applyRegionIds(prefix, regionIds);
this.form[`${prefix}AddressId`] = '';
this.form[`${prefix}AddressCode`] = '';
this.form[`${prefix}SiteCode`] = '';
this.form[`${prefix}Address`] =
this.routeMapSelected.detailAddress || this.form[`${prefix}Address`];
this.form[`${prefix}Name`] = this.routeMapSelected.regionName || this.form[`${prefix}Name`];
this.form[`${prefix}Longitude`] = this.routeMapSelected.longitude;
this.form[`${prefix}Latitude`] = this.routeMapSelected.latitude;
this.routeMapVisible = false;
this.$refs.crud?.validateField?.(`${prefix}Address`);
} finally {
this.routeMapConfirmLoading = false;
}
},
buildRouteMapSelection({
lng,
lat,
address,
regionName,
regionCode,
provinceId,
cityId,
districtId,
}) {
return {
longitude: this.formatCoordinate(lng),
latitude: this.formatCoordinate(lat),
detailAddress: address || '',
regionName: regionName || '',
regionCode: regionCode || '',
provinceId: provinceId || '',
cityId: cityId || '',
districtId: districtId || '',
};
},
applyRegionIds(prefix, regionIds = {}) {
this.form[`${prefix}ProvinceId`] = regionIds.provinceId || '';
this.form[`${prefix}CityId`] = regionIds.cityId || '';
this.form[`${prefix}DistrictId`] = regionIds.districtId || '';
},
hasCompleteRegionIds(regionIds = {}) {
return Boolean(regionIds.provinceId && regionIds.cityId && regionIds.districtId);
},
fallbackRegionIds(regionCode) {
const code = String(regionCode || '').trim();
if (!/^\d{6}$/.test(code)) {
return { provinceId: '', cityId: '', districtId: '' };
}
return {
provinceId: `${code.slice(0, 2)}0000`,
cityId: `${code.slice(0, 4)}00`,
districtId: code,
};
},
async resolveRegionIds(regionCode) {
const fallback = this.fallbackRegionIds(regionCode);
if (!regionCode) return fallback;
try {
const res = await getRegionDetail(regionCode);
const region = res.data.data || {};
return {
provinceId: String(region.provinceCode || fallback.provinceId || ''),
cityId: String(region.cityCode || fallback.cityId || ''),
districtId: String(region.districtCode || region.code || fallback.districtId || ''),
};
} catch (error) {
window.console.log(error);
return fallback;
}
},
formatCoordinate(value) {
if (value === undefined || value === null || value === '') return '';
const numberValue = Number(value);
return Number.isFinite(numberValue) ? numberValue.toFixed(6) : '';
},
toLngLat(lng, lat) {
return new window.AMap.LngLat(Number(lng), Number(lat));
},
getPointLng(point) {
if (!point) return undefined;
if (typeof point.getLng === 'function') return point.getLng();
return point.lng ?? point.lon;
},
getPointLat(point) {
if (!point) return undefined;
if (typeof point.getLat === 'function') return point.getLat();
return point.lat;
},
ensureRouteAmapGeocoder() {
if (this.routeAmapGeocoder) return Promise.resolve(this.routeAmapGeocoder);
return new Promise((resolve, reject) => {
if (!window.AMap || !window.AMap.plugin) {
reject(new Error('高德地图组件未就绪'));
return;
}
window.AMap.plugin(['AMap.Geocoder'], () => {
try {
this.routeAmapGeocoder = new window.AMap.Geocoder();
resolve(this.routeAmapGeocoder);
} catch (error) {
reject(error);
}
});
});
},
runAmapGeocode(action, input) {
return new Promise((resolve, reject) => {
const timer = window.setTimeout(() => {
reject(new Error('高德地图请求超时'));
}, 10000);
const done = (status, result) => {
window.clearTimeout(timer);
if (status === 'complete' && result) {
resolve(result);
return;
}
reject(new Error('高德地图请求失败'));
};
try {
if (action === 'location') {
this.routeAmapGeocoder.getLocation(input, done);
} else {
this.routeAmapGeocoder.getAddress(input, done);
}
} catch (error) {
window.clearTimeout(timer);
reject(error);
}
});
},
resolveMapPoint(result) {
if (!result) return null;
const geocode = Array.isArray(result.geocodes) ? result.geocodes[0] : null;
if (geocode && geocode.location) return geocode.location;
if (result.location) return result.location;
if (result.lnglat) return result.lnglat;
if (result.getLng || result.lng || result.lon) return result;
return null;
},
resolveMapAddress(result = {}) {
const regeocode = result.regeocode || {};
const component = regeocode.addressComponent || {};
const city = Array.isArray(component.city) ? '' : component.city;
return {
detailAddress: regeocode.formattedAddress || '',
regionName: [component.province, city, component.district].filter(Boolean).join(''),
regionCode: component.adcode || '',
};
},
normalizeRow(row) {
const submitRow = { ...row };
Object.keys(submitRow).forEach(key => {
if (typeof submitRow[key] === 'string') {
submitRow[key] = submitRow[key].trim();
}
});
return submitRow;
},
validateRow(row) {
if (!row.routeName) {
this.$message.warning('请输入线路名称');
return false;
}
if (row.routeName.length > 100) {
this.$message.warning('线路名称不能超过100个字符');
return false;
}
if (!row.departureName || !row.departureAddress) {
this.$message.warning('请选择发货地址');
return false;
}
if (!row.arrivalName || !row.arrivalAddress) {
this.$message.warning('请选择收货地址');
return false;
}
if (row.departureAddress.length > 255) {
this.$message.warning('发货地址不能超过255个字符');
return false;
}
if (row.arrivalAddress.length > 255) {
this.$message.warning('收货地址不能超过255个字符');
return false;
}
const invalidPhone = [
[row.departurePhone, '发货联系方式'],
[row.arrivalPhone, '收货联系方式'],
].find(([value]) => value && !isMobile(value));
if (invalidPhone) {
this.$message.warning(`${invalidPhone[1]}格式不正确`);
return false;
}
if (row.remark && row.remark.length > 500) {
this.$message.warning('备注不能超过500个字符');
return false;
}
return true;
},
stopSubmitLoading(loading) {
if (typeof loading === 'function') {
loading();
}
},
rowSave(row, done, loading) {
const submitRow = this.normalizeRow(row);
if (!this.validateRow(submitRow)) {
this.stopSubmitLoading(loading);
return;
}
api.submit(submitRow).then(
() => {
this.page.currentPage = 1;
this.onLoad(this.page, this.query);
this.$message({ type: 'success', message: '操作成功!' });
done();
},
error => {
window.console.log(error);
this.stopSubmitLoading(loading);
}
);
},
rowUpdate(row, index, done, loading) {
const submitRow = this.normalizeRow(row);
if (!this.validateRow(submitRow)) {
this.stopSubmitLoading(loading);
return;
}
api.submit(submitRow).then(
() => {
this.onLoad(this.page, this.query);
this.$message({ type: 'success', message: '操作成功!' });
done();
},
error => {
window.console.log(error);
this.stopSubmitLoading(loading);
}
);
},
rowDel(row) {
this.$confirm('确定将选择数据删除?', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => api.remove(row.id))
.then(res => {
this.afterRemove(res.data.data);
});
},
handleDelete() {
if (this.selectionList.length === 0) {
this.$message.warning('请选择至少一条数据');
return;
}
const selection = this.selectionList.filter(item => !item.readonly);
if (!selection.length) {
this.$message.warning('所选数据均为只读数据');
return;
}
this.$confirm('确定将选择数据删除?', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => api.remove(selection.map(item => item.id).join(',')))
.then(res => {
this.afterRemove(res.data.data);
});
},
afterRemove(result = {}) {
this.onLoad(this.page);
const successCount = result.successCount || 0;
const skippedCount = result.skippedCount || 0;
if (skippedCount) {
this.$message.warning(`成功删除${successCount}条,因业务引用跳过${skippedCount}条`);
} else {
this.$message({ type: 'success', message: '删除成功!' });
}
this.selectionClear();
},
beforeOpen(done, type) {
this.dialogReadonly = type === 'view';
if (type === 'add') {
const form = {};
// 超管若当前按某组织筛选,新增默认归属该组织,避免保存后被筛选条件滤掉。
if (this.isAdmin && this.query.deptId) {
form.deptId = String(this.query.deptId);
form.deptName = this.resolveDeptName(form.deptId);
}
this.form = form;
done();
return;
}
if (['edit', 'view'].includes(type)) {
api.getDetail(this.form.id).then(res => {
this.form = res.data.data || {};
done();
});
return;
}
done();
},
searchReset() {
this.query = {};
this.applyDefaultDeptSearch();
this.page.currentPage = 1;
this.onLoad(this.page, this.query);
},
searchChange(params, done) {
this.query = this.normalizeListQuery(params);
this.page.currentPage = 1;
this.onLoad(this.page, this.query);
done();
},
selectionChange(list) {
this.selectionList = list;
},
selectionClear() {
this.selectionList = [];
if (this.$refs.crud) {
this.$refs.crud.toggleSelection();
}
},
currentChange(currentPage) {
this.page.currentPage = currentPage;
},
sizeChange(pageSize) {
this.page.pageSize = pageSize;
},
refreshChange() {
this.onLoad(this.page, this.query);
},
onLoad(page, params = {}) {
this.loading = true;
const query = this.normalizeListQuery(params);
this.query = query;
api
.getList(page.currentPage, page.pageSize, query)
.then(res => {
const result = res.data.data;
this.page.total = result.total;
this.data = result.records;
this.selectionClear();
})
.finally(() => {
this.loading = false;
});
},
buildExportParams() {
return {
...this.normalizeListQuery(this.query),
ids: this.ids,
[this.website.tokenHeader]: getToken(),
};
},
handleExport() {
this.$confirm(`是否导出${config.exportName}?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}).then(() => {
NProgress.start();
exportBlob(config.exportUrl, this.buildExportParams(), { feedback: true })
.then(res => {
downloadXls(
res.data,
`${config.exportName}${this.$dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`
);
})
.finally(() => {
NProgress.done();
});
});
},
handleTemplate() {
NProgress.start();
exportBlob(
config.templateUrl,
{
[this.website.tokenHeader]: getToken(),
},
{ feedback: true }
)
.then(res => {
downloadXls(res.data, `${config.title}模板.xlsx`);
})
.finally(() => {
NProgress.done();
});
},
handleImport() {
const column = this.excelOptionConfig.column.find(item => item.prop === 'excelFile');
column.action = config.importUrl;
openImportDialog(this, config.title, () => this.onLoad(this.page, this.query));
},
},
};
</script>
<style lang="scss" scoped>
.common-route-page {
&__route-address {
display: flex;
gap: 10px;
align-items: center;
.el-input {
flex: 1;
min-width: 0;
}
.el-input:first-child {
flex: 0 0 42.5%;
}
.el-button {
flex: 0 0 40px;
font-size: 30px;
}
}
&__address-search {
padding: 4px 8px 10px;
}
&__address-search-actions {
display: flex;
align-items: flex-start;
justify-content: flex-end;
}
&__address-pagination {
display: flex;
justify-content: flex-end;
margin-top: 14px;
}
&__map-toolbar {
display: flex;
gap: 8px;
margin-bottom: 12px;
.el-input {
flex: 1;
min-width: 0;
}
}
&__map {
width: 100%;
height: 420px;
border: 1px solid #eff1f7;
}
&__map-info {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 10px;
color: #606266;
line-height: 1.6;
}
}
</style>