<template>
|
<view class="client-visit-detail">
|
<PageHeader title="客户拜访详情"
|
@back="goBack" />
|
<u-form @submit="handleSignIn"
|
ref="formRef"
|
label-width="90">
|
<!-- 客户信息 -->
|
<u-cell-group title="客户信息">
|
<u-form-item label="客户名称"
|
prop="customerName"
|
required
|
border-bottom>
|
<u-input v-model="form.customerName"
|
placeholder="请输入客户名称" />
|
</u-form-item>
|
<u-form-item label="联系人"
|
prop="contact"
|
border-bottom>
|
<u-input v-model="form.contact"
|
placeholder="请输入联系人" />
|
</u-form-item>
|
<u-form-item label="联系电话"
|
prop="contactPhone"
|
border-bottom>
|
<u-input v-model="form.contactPhone"
|
placeholder="请输入联系电话" />
|
</u-form-item>
|
</u-cell-group>
|
<!-- 拜访信息 -->
|
<u-cell-group title="拜访信息">
|
<u-form-item label="拜访目的"
|
prop="purposeVisit"
|
required
|
border-bottom>
|
<u-input v-model="form.purposeVisit"
|
placeholder="请输入拜访目的" />
|
</u-form-item>
|
<u-form-item label="拜访时间"
|
prop="purposeDate"
|
required
|
border-bottom>
|
<u-input v-model="form.purposeDate"
|
placeholder="请选择拜访时间"
|
@click="showTimePicker" />
|
<template #right>
|
<up-icon name="arrow-right"
|
@click="showTimePicker"></up-icon>
|
</template>
|
</u-form-item>
|
<u-form-item label="拜访地点"
|
prop="visitAddress"
|
required
|
border-bottom>
|
<u-input v-model="form.visitAddress"
|
placeholder="请输入拜访地点">
|
<template #suffix>
|
<u-icon name="map"
|
@click="getCurrentLocation"
|
class="location-icon" />
|
</template>
|
</u-input>
|
</u-form-item>
|
<u-form-item label="拜访照片">
|
<AlbumImageUpload
|
v-model="form.storageBlobDTO"
|
@change="onVisitImageChange"
|
:limit="6"
|
:enableWatermark="true"
|
:watermarkText="getVisitWatermarkText"
|
:beforeUpload="beforeUploadVisitImages"
|
action="/file/upload"
|
tipText=""
|
/>
|
</u-form-item>
|
</u-cell-group>
|
<!-- 备注信息 -->
|
<u-cell-group title="备注信息">
|
<u-form-item label="备注"
|
prop="remark"
|
border-bottom>
|
<u-textarea v-model="form.remark"
|
placeholder="请输入备注信息"
|
:maxlength="200"
|
count
|
:autoHeight="true" />
|
</u-form-item>
|
</u-cell-group>
|
<!-- 提交按钮 -->
|
<view class="footer-btns">
|
<u-button class="cancel-btn"
|
@click="goBack">取消</u-button>
|
<u-button class="sign-btn"
|
type="primary"
|
@click="handleSignIn"
|
:loading="loading">签到</u-button>
|
</view>
|
</u-form>
|
<!-- 时间选择器 -->
|
<up-datetime-picker :show="showTime"
|
v-model="currentTime"
|
@confirm="onTimeConfirm"
|
@cancel="showTime = false"
|
mode="datetime" />
|
</view>
|
</template>
|
|
<script setup>
|
// 替换 toast 方法
|
defineOptions({ name: "client-visit-detail" });
|
const showToast = message => {
|
uni.showToast({
|
title: message,
|
icon: "none",
|
});
|
};
|
|
import { ref, onMounted } from "vue";
|
import PageHeader from "@/components/PageHeader.vue";
|
import AlbumImageUpload from "@/components/AlbumImageUpload/index.vue";
|
import { normalizeFileUrl } from "@/utils/filePreview";
|
import config from "@/config.js";
|
import {
|
clientVisitSignIn,
|
clientVisitUpdate,
|
} from "@/api/cooperativeOffice/clientVisit";
|
import useUserStore from "@/store/modules/user";
|
import dayjs from "dayjs";
|
import { onLoad } from "@dcloudio/uni-app";
|
|
const userStore = useUserStore();
|
|
// 表单数据
|
const form = ref({
|
customerName: "",
|
contact: "",
|
contactPhone: "",
|
visitingPeople: "",
|
purposeVisit: "",
|
purposeDate: "",
|
visitAddress: "",
|
latitude: "",
|
longitude: "",
|
locationAddress: "",
|
remark: "",
|
// 临时文件id列表(提交用)
|
tempFileIds: [],
|
// 正式文件列表(回显用,后端查询会返回)
|
commonFileList: [],
|
// 上传组件回传的文件列表(后端若不接收可忽略;先随表单提交)
|
storageBlobDTO: [],
|
});
|
|
// 页面状态
|
const loading = ref(false);
|
const formRef = ref(null);
|
|
// 时间相关
|
const currentTime = ref(Date.now());
|
const showTime = ref(false);
|
|
// 返回上一页
|
const goBack = () => {
|
// 返回时清除本地存储的ID
|
uni.removeStorageSync("clientVisit");
|
uni.navigateBack();
|
};
|
|
// 显示时间选择器
|
const showTimePicker = () => {
|
showTime.value = true;
|
};
|
|
// 确认时间选择
|
const onTimeConfirm = e => {
|
console.log(e);
|
form.value.purposeDate = dayjs(e.value).format("YYYY-MM-DD HH:mm:ss");
|
currentTime.value = e.value;
|
showTime.value = false;
|
};
|
|
// 获取当前位置
|
const getCurrentLocation = () => {
|
uni.showLoading({ title: "获取位置中..." });
|
|
uni.getLocation({
|
type: "gcj02",
|
success: res => {
|
form.value.latitude = res.latitude;
|
form.value.longitude = res.longitude;
|
|
// 使用逆地理编码获取地址信息
|
uni.request({
|
url: `https://restapi.amap.com/v3/geocode/regeo?key=c120a5dc69a9f61839f7763e6057005f&location=${res.longitude},${res.latitude}&radius=1000&extensions=all`,
|
success: geoRes => {
|
uni.hideLoading();
|
if (geoRes.data.status === "1" && geoRes.data.regeocode) {
|
const regeocode = geoRes.data.regeocode;
|
const address = regeocode.formatted_address;
|
|
// 优先显示详细地址
|
if (address) {
|
form.value.visitAddress = address;
|
showToast("位置获取成功");
|
} else {
|
// 如果没有详细地址,尝试组合地址信息
|
const addressComponent = regeocode.addressComponent;
|
const combinedAddress = `${addressComponent.province}${addressComponent.city}${addressComponent.district}${addressComponent.township}`;
|
form.value.visitAddress = combinedAddress;
|
showToast("位置获取成功");
|
}
|
} else {
|
// API调用成功但没有返回地址信息
|
const fallbackAddress = `位置: ${res.latitude.toFixed(
|
4
|
)}, ${res.longitude.toFixed(4)}`;
|
form.value.visitAddress = fallbackAddress;
|
showToast("获取到位置,但地址解析失败");
|
}
|
},
|
fail: err => {
|
uni.hideLoading();
|
console.error("逆地理编码失败:", err);
|
|
// 逆地理编码失败时,显示简化的位置信息
|
const fallbackAddress = `位置: ${res.latitude.toFixed(
|
4
|
)}, ${res.longitude.toFixed(4)}`;
|
form.value.visitAddress = fallbackAddress;
|
showToast("位置获取成功,但地址解析失败");
|
},
|
});
|
},
|
fail: err => {
|
uni.hideLoading();
|
console.error("获取位置失败:", err);
|
|
// 显示错误提示并引导用户检查权限
|
showToast("获取位置失败,请检查定位权限");
|
|
// 引导用户检查权限设置
|
uni.showModal({
|
title: "位置权限提示",
|
content:
|
"获取位置失败,可能是因为位置权限未开启,请在设备设置中检查并开启位置权限。",
|
confirmText: "知道了",
|
cancelText: "取消",
|
success: res => {
|
if (res.confirm) {
|
// 可以尝试打开设置页面(如果支持)
|
if (uni.openSetting) {
|
uni.openSetting({
|
success: settingRes => {
|
console.log("设置结果:", settingRes);
|
},
|
});
|
}
|
}
|
},
|
});
|
|
// 失败时显示错误信息
|
form.value.visitAddress = "位置获取失败";
|
},
|
});
|
};
|
|
// 提交签到
|
const handleSignIn = async () => {
|
if (!form.value.customerName) {
|
showToast("请输入客户名称");
|
return;
|
}
|
|
if (!form.value.purposeVisit) {
|
showToast("请输入拜访目的");
|
return;
|
}
|
|
if (!form.value.purposeDate) {
|
showToast("请选择拜访时间");
|
return;
|
}
|
if (!form.value.visitAddress) {
|
showToast("请获取当前位置");
|
return;
|
}
|
|
try {
|
loading.value = true;
|
|
// 使用安全浅拷贝,避免对象展开在某些运行时抛错
|
const source =
|
form.value && typeof form.value === "object" ? form.value : {};
|
const submitData = {};
|
Object.keys(source).forEach(k => {
|
submitData[k] = source[k];
|
});
|
// 从上传列表提取 tempFileIds(兼容 tempId / tempFileId / id)
|
if (Array.isArray(submitData.storageBlobDTO)) {
|
submitData.tempFileIds = submitData.storageBlobDTO
|
.map(f => f?.tempId ?? f?.tempFileId ?? f?.id)
|
.filter(v => v !== undefined && v !== null && v !== "");
|
} else {
|
submitData.tempFileIds = Array.isArray(submitData.tempFileIds)
|
? submitData.tempFileIds
|
: [];
|
}
|
// 兼容后端只接收 URL 字符串的场景:额外补一个 visitImageUrls
|
if (Array.isArray(submitData.storageBlobDTO)) {
|
submitData.visitImageUrls = submitData.storageBlobDTO
|
.map(f => f?.url)
|
.filter(Boolean)
|
.join(",");
|
}
|
console.log("submitData", submitData);
|
if (isEdit.value) {
|
const { code } = await clientVisitUpdate(submitData);
|
if (code === 200) {
|
showToast("修改成功");
|
setTimeout(() => {
|
goBack();
|
}, 500);
|
} else {
|
loading.value = false;
|
showToast("签到失败,请重试");
|
}
|
} else {
|
const { code } = await clientVisitSignIn(submitData);
|
if (code === 200) {
|
showToast("签到成功");
|
setTimeout(() => {
|
goBack();
|
}, 500);
|
} else {
|
loading.value = false;
|
showToast("签到失败,请重试");
|
}
|
}
|
} catch (e) {
|
loading.value = false;
|
console.error("签到失败:", e);
|
}
|
};
|
const isEdit = ref(false);
|
const getVisitFileKey = file => {
|
return (
|
file?.tempId ??
|
file?.tempFileId ??
|
file?.id ??
|
file?.url ??
|
file?.downloadUrl ??
|
""
|
);
|
};
|
const onVisitImageChange = list => {
|
const nextList = Array.isArray(list) ? list.slice() : [];
|
form.value.storageBlobDTO = nextList;
|
|
// 同步提交用 tempFileIds,避免删除后仍携带旧 ID
|
form.value.tempFileIds = nextList
|
.map(f => f?.tempId ?? f?.tempFileId ?? f?.id)
|
.filter(v => v !== undefined && v !== null && v !== "");
|
|
// 编辑场景下同步 commonFileList,避免提交时残留已删除文件
|
if (Array.isArray(form.value.commonFileList)) {
|
const keepKeys = new Set(nextList.map(getVisitFileKey).filter(Boolean));
|
form.value.commonFileList = form.value.commonFileList.filter(item =>
|
keepKeys.has(getVisitFileKey(item))
|
);
|
}
|
};
|
const isLikelyPathValue = value => {
|
const v = String(value || "").trim();
|
if (!v) return false;
|
if (/^(https?:)?\/\//i.test(v)) return true;
|
if (/^(blob:|data:|wxfile:|file:|content:)/i.test(v)) return true;
|
if (v.includes("/") || v.includes("\\")) return true;
|
if (/\.[a-zA-Z0-9]{2,8}($|\?)/.test(v)) return true;
|
return false;
|
};
|
const buildVisitFilePreviewUrl = file => {
|
const candidates = [
|
file?.url,
|
file?.path,
|
file?.filePath,
|
file?.tempPath,
|
file?.urlFull,
|
file?.downloadUrl,
|
].filter(Boolean);
|
|
for (const raw of candidates) {
|
if (!isLikelyPathValue(raw)) continue;
|
const normalized = normalizeFileUrl(raw);
|
if (normalized) return normalized;
|
}
|
|
for (const raw of candidates) {
|
const value = String(raw || "").trim();
|
if (!value) continue;
|
// 兜底走 common/download,兼容某些后端只返回相对存储路径
|
return (
|
(config.baseUrl || "") +
|
"/common/download?fileName=" +
|
encodeURIComponent(value) +
|
"&delete=false"
|
);
|
}
|
return "";
|
};
|
onLoad(() => {
|
// 编辑拜访时,从本地存储获取拜访记录
|
const visit = uni.getStorageSync("clientVisit");
|
if (visit) {
|
form.value = visit;
|
isEdit.value = true;
|
console.log("form.value", form.value);
|
|
// 回显:若后端/列表页塞了 commonFileList,则转成上传组件可识别的 storageBlobDTO
|
const commonFileList = Array.isArray(visit?.commonFileList)
|
? visit.commonFileList
|
: [];
|
if (commonFileList.length && (!Array.isArray(visit?.storageBlobDTO) || !visit.storageBlobDTO.length)) {
|
form.value.commonFileList = commonFileList;
|
form.value.storageBlobDTO = commonFileList
|
.map(f => {
|
const previewUrl = buildVisitFilePreviewUrl(f);
|
return {
|
...f,
|
tempId: f?.tempId ?? f?.tempFileId ?? f?.id,
|
id: f?.id ?? f?.tempId ?? f?.tempFileId,
|
url: previewUrl,
|
downloadUrl: previewUrl,
|
};
|
})
|
.filter(x => x?.url);
|
}
|
|
// 编辑场景:同步一份 tempFileIds,便于后端需要时直接使用
|
if (!Array.isArray(form.value.tempFileIds) || !form.value.tempFileIds.length) {
|
form.value.tempFileIds = Array.isArray(form.value.storageBlobDTO)
|
? form.value.storageBlobDTO
|
.map(f => f?.tempId ?? f?.tempFileId ?? f?.id)
|
.filter(Boolean)
|
: [];
|
}
|
} else {
|
isEdit.value = false;
|
}
|
});
|
|
// 初始化页面数据
|
const initPageData = () => {
|
// 设置默认拜访时间为当前时间
|
form.value.purposeDate = dayjs().format("YYYY-MM-DD HH:mm:ss");
|
currentTime.value = Date.now();
|
|
// 设置拜访人为当前登录用户的昵称
|
form.value.visitingPeople = userStore.nickName || "";
|
};
|
|
onMounted(() => {
|
initPageData();
|
});
|
|
const beforeUploadVisitImages = () => {
|
const name = (form.value.customerName || "").trim();
|
if (!name) {
|
showToast("请先填写客户名称后上传");
|
return false;
|
}
|
const visitTime = (form.value.purposeDate || "").trim();
|
if (!visitTime) {
|
showToast("请先选择拜访时间后上传");
|
return false;
|
}
|
return true;
|
};
|
|
const getVisitWatermarkText = () => {
|
const name = (form.value.customerName || "").trim();
|
// 水印时间改为“拜访时间”
|
const visitTime = (form.value.purposeDate || "").trim();
|
const time = visitTime || dayjs().format("YYYY-MM-DD HH:mm:ss");
|
return name ? `${name}\n${time}` : `\n${time}`;
|
};
|
</script>
|
|
<style scoped lang="scss">
|
@import "@/static/scss/form-common.scss";
|
.client-visit-detail {
|
min-height: 100vh;
|
background: #f8f9fa;
|
padding-bottom: calc(5rem + env(safe-area-inset-bottom));
|
}
|
|
.footer-btns {
|
position: fixed;
|
left: 0;
|
right: 0;
|
bottom: calc(env(safe-area-inset-bottom) + 8px);
|
background: #fff;
|
display: flex;
|
justify-content: space-around;
|
align-items: center;
|
padding: 0.75rem 0;
|
box-shadow: 0 -0.125rem 0.5rem rgba(0, 0, 0, 0.05);
|
z-index: 1000;
|
}
|
|
.cancel-btn {
|
font-weight: 400;
|
font-size: 1rem;
|
color: #666;
|
background: #f5f5f5;
|
border: 1px solid #ddd;
|
width: 45%;
|
height: 2.5rem;
|
border-radius: 2.5rem 2.5rem 2.5rem 2.5rem;
|
}
|
|
.sign-btn {
|
font-weight: 500;
|
font-size: 1rem;
|
color: #fff;
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
border: none;
|
width: 45%;
|
height: 2.5rem;
|
border-radius: 2.5rem 2.5rem 2.5rem 2.5rem;
|
}
|
|
.location-icon {
|
color: #1989fa;
|
font-size: 1.2rem;
|
}
|
</style>
|