458 lines
13 KiB
Vue
458 lines
13 KiB
Vue
<script setup lang="ts">
|
||
import { computed, onUnmounted, ref } from 'vue'
|
||
import { onLoad } from '@dcloudio/uni-app'
|
||
import {
|
||
confirmContractSign,
|
||
getContractDetail,
|
||
getContractPreview,
|
||
getContractSignProgress,
|
||
initContractSign,
|
||
sendContractSignSmsCode,
|
||
} from '@/api/contract'
|
||
import { uploadRequirementAttachment } from '@/api/requirement'
|
||
import { CONTRACT_SIGN_SIGNATURE_STORAGE_KEY } from '@/constants/contract-sign'
|
||
import type { ContractItem } from '@/types/contract'
|
||
import type { ContractSignProgressVo } from '@/types/contract-api'
|
||
import { getLoginPhoneNumber } from '@/utils/auth-storage'
|
||
import { openContractPreview } from '@/utils/contract-preview'
|
||
import { mapContractVoToItem } from '@/utils/map-contract'
|
||
import {
|
||
mapContractSignStatusLabel,
|
||
mapContractSignSteps,
|
||
pickPartyBSignId,
|
||
} from '@/utils/map-contract-sign'
|
||
import { navigateBackWithFallback } from '@/utils/navigate-back'
|
||
import { validatePhone } from '@/utils/sms-code'
|
||
import SignaturePad from './components/SignaturePad.vue'
|
||
|
||
const contract = ref<ContractItem | null>(null)
|
||
const progress = ref<ContractSignProgressVo | null>(null)
|
||
const previewTitle = ref('')
|
||
const previewReady = ref(false)
|
||
const previewOpened = ref(false)
|
||
const signaturePadVisible = ref(false)
|
||
const signatureLocalPath = ref('')
|
||
const signaturePreview = ref(false)
|
||
const agreed = ref(false)
|
||
const mobile = ref('')
|
||
const smsCode = ref('')
|
||
const signId = ref('')
|
||
const countdown = ref(0)
|
||
const sendingCode = ref(false)
|
||
const submitting = ref(false)
|
||
let timer: ReturnType<typeof setInterval> | null = null
|
||
|
||
const signSteps = computed(() => mapContractSignSteps(progress.value))
|
||
const signStatusLabel = computed(() => mapContractSignStatusLabel(
|
||
progress.value?.signStatus || (contract.value?.signStatus === 'signed' ? 'SIGNED' : 'PENDING'),
|
||
))
|
||
const alreadySigned = computed(() => progress.value?.signStatus === 'SIGNED')
|
||
|
||
onLoad(async (options) => {
|
||
const rawId = options?.id
|
||
const id = rawId ? decodeURIComponent(String(rawId)) : ''
|
||
if (!id) {
|
||
uni.showToast({ title: '合同不存在', icon: 'none' })
|
||
setTimeout(() => uni.navigateBack(), 1500)
|
||
return
|
||
}
|
||
|
||
mobile.value = getLoginPhoneNumber()?.trim() || ''
|
||
|
||
try {
|
||
const [vo, preview, signProgress] = await Promise.all([
|
||
getContractDetail(id),
|
||
getContractPreview(id),
|
||
getContractSignProgress(id).catch(() => null),
|
||
])
|
||
contract.value = mapContractVoToItem(vo)
|
||
previewTitle.value = preview.title || vo.title || '合同正文'
|
||
previewReady.value = Boolean(preview.file)
|
||
progress.value = signProgress
|
||
const existingSignId = pickPartyBSignId(signProgress)
|
||
if (existingSignId)
|
||
signId.value = existingSignId
|
||
|
||
if (signProgress?.signStatus === 'SIGNED') {
|
||
uni.showToast({ title: '合同已签署', icon: 'none' })
|
||
setTimeout(() => {
|
||
uni.redirectTo({
|
||
url: `/pages/contract-center/sign-success/index?id=${encodeURIComponent(id)}`,
|
||
})
|
||
}, 800)
|
||
}
|
||
}
|
||
catch {
|
||
contract.value = null
|
||
setTimeout(() => uni.navigateBack(), 1500)
|
||
}
|
||
})
|
||
|
||
onUnmounted(() => {
|
||
if (timer) {
|
||
clearInterval(timer)
|
||
timer = null
|
||
}
|
||
})
|
||
|
||
function handleBack() {
|
||
navigateBackWithFallback('/pages/contract-center/index')
|
||
}
|
||
|
||
async function handleOpenFullContract() {
|
||
if (!contract.value)
|
||
return
|
||
const result = await openContractPreview(contract.value.id)
|
||
if (result)
|
||
previewOpened.value = true
|
||
}
|
||
|
||
function openSignaturePad() {
|
||
if (alreadySigned.value)
|
||
return
|
||
signaturePadVisible.value = true
|
||
}
|
||
|
||
function closeSignaturePad() {
|
||
signaturePadVisible.value = false
|
||
}
|
||
|
||
function handleSignatureConfirm(imagePath: string) {
|
||
signatureLocalPath.value = imagePath
|
||
signaturePreview.value = Boolean(imagePath)
|
||
signaturePadVisible.value = false
|
||
}
|
||
|
||
function startCountdown() {
|
||
countdown.value = 60
|
||
if (timer)
|
||
clearInterval(timer)
|
||
timer = setInterval(() => {
|
||
countdown.value -= 1
|
||
if (countdown.value <= 0 && timer) {
|
||
clearInterval(timer)
|
||
timer = null
|
||
}
|
||
}, 1000)
|
||
}
|
||
|
||
async function ensureSignInit() {
|
||
if (!contract.value)
|
||
throw new Error('合同不存在')
|
||
if (signId.value)
|
||
return signId.value
|
||
|
||
const mobileValue = mobile.value.trim()
|
||
const id = await initContractSign(contract.value.id, {
|
||
signType: 'sms',
|
||
mobile: mobileValue,
|
||
})
|
||
signId.value = String(id)
|
||
const latest = await getContractSignProgress(contract.value.id).catch(() => null)
|
||
if (latest)
|
||
progress.value = latest
|
||
return signId.value
|
||
}
|
||
|
||
async function handleSendCode() {
|
||
if (!contract.value || countdown.value > 0 || sendingCode.value || alreadySigned.value)
|
||
return
|
||
|
||
if (!validatePhone(mobile.value))
|
||
return
|
||
|
||
sendingCode.value = true
|
||
try {
|
||
await ensureSignInit()
|
||
await sendContractSignSmsCode(contract.value.id, {
|
||
mobile: mobile.value.trim(),
|
||
})
|
||
uni.showToast({ title: '验证码已发送', icon: 'none' })
|
||
startCountdown()
|
||
}
|
||
catch {
|
||
// request 统一提示
|
||
}
|
||
finally {
|
||
sendingCode.value = false
|
||
}
|
||
}
|
||
|
||
async function uploadSignatureUrl() {
|
||
if (!signatureLocalPath.value)
|
||
return ''
|
||
|
||
const uploaded = await uploadRequirementAttachment(signatureLocalPath.value)
|
||
const url = uploaded.url?.trim() || ''
|
||
if (!url)
|
||
throw new Error('签名上传失败')
|
||
if (url.length > 255)
|
||
throw new Error('签名地址过长,请重试')
|
||
return url
|
||
}
|
||
|
||
async function handleSubmit() {
|
||
if (!contract.value || submitting.value || alreadySigned.value)
|
||
return
|
||
|
||
if (!previewReady.value) {
|
||
uni.showToast({ title: '暂无合同文件,无法签署', icon: 'none' })
|
||
return
|
||
}
|
||
if (!previewOpened.value) {
|
||
uni.showToast({ title: '请先查看完整合同', icon: 'none' })
|
||
return
|
||
}
|
||
if (!agreed.value) {
|
||
uni.showToast({ title: '请勾选电子签服务协议', icon: 'none' })
|
||
return
|
||
}
|
||
if (!validatePhone(mobile.value))
|
||
return
|
||
if (!smsCode.value.trim()) {
|
||
uni.showToast({ title: '请输入验证码', icon: 'none' })
|
||
return
|
||
}
|
||
|
||
submitting.value = true
|
||
uni.showLoading({ title: '签署中...', mask: true })
|
||
try {
|
||
const currentSignId = await ensureSignInit()
|
||
let signatureImg = ''
|
||
try {
|
||
signatureImg = await uploadSignatureUrl()
|
||
}
|
||
catch (error) {
|
||
const message = error instanceof Error ? error.message : '签名上传失败'
|
||
uni.showToast({ title: message, icon: 'none' })
|
||
return
|
||
}
|
||
|
||
if (signatureLocalPath.value) {
|
||
try {
|
||
uni.setStorageSync(CONTRACT_SIGN_SIGNATURE_STORAGE_KEY, signatureLocalPath.value)
|
||
}
|
||
catch {
|
||
// ignore
|
||
}
|
||
}
|
||
|
||
const result = await confirmContractSign(contract.value.id, {
|
||
signId: currentSignId,
|
||
code: smsCode.value.trim(),
|
||
agreementFlag: true,
|
||
signatureImg: signatureImg || undefined,
|
||
signPosition: '乙方签字处',
|
||
})
|
||
progress.value = result
|
||
|
||
uni.redirectTo({
|
||
url: `/pages/contract-center/sign-success/index?id=${encodeURIComponent(contract.value.id)}`,
|
||
})
|
||
}
|
||
catch {
|
||
// request 统一提示
|
||
}
|
||
finally {
|
||
submitting.value = false
|
||
uni.hideLoading()
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<view
|
||
v-if="contract"
|
||
class="contract-sign"
|
||
>
|
||
<view class="contract-sign__navbar">
|
||
<view
|
||
class="contract-sign__back"
|
||
@tap="handleBack"
|
||
>
|
||
<image
|
||
class="contract-sign__back-icon"
|
||
src="/static/icons/icon-back.svg"
|
||
mode="aspectFit"
|
||
/>
|
||
</view>
|
||
<text class="contract-sign__navbar-title">
|
||
合同签署
|
||
</text>
|
||
<view class="contract-sign__navbar-placeholder" />
|
||
</view>
|
||
|
||
<view class="contract-sign__header">
|
||
<text class="contract-sign__contract-name">
|
||
{{ contract.contractName }}
|
||
</text>
|
||
<text class="contract-sign__status">
|
||
{{ signStatusLabel }}
|
||
</text>
|
||
</view>
|
||
|
||
<view class="contract-sign__steps">
|
||
<view
|
||
v-for="(step, index) in signSteps"
|
||
:key="step.key"
|
||
class="contract-sign__step"
|
||
>
|
||
<view class="contract-sign__step-track">
|
||
<view
|
||
class="contract-sign__step-node"
|
||
:class="{
|
||
'contract-sign__step-node--done': step.status === 'done',
|
||
'contract-sign__step-node--active': step.status === 'active',
|
||
}"
|
||
>
|
||
<text
|
||
v-if="step.status === 'done'"
|
||
class="contract-sign__step-check"
|
||
>
|
||
✓
|
||
</text>
|
||
</view>
|
||
<view
|
||
v-if="index < signSteps.length - 1"
|
||
class="contract-sign__step-line"
|
||
:class="{ 'contract-sign__step-line--done': step.status === 'done' }"
|
||
/>
|
||
</view>
|
||
<text
|
||
class="contract-sign__step-label"
|
||
:class="{ 'contract-sign__step-label--active': step.status === 'active' }"
|
||
>
|
||
{{ step.label }}
|
||
</text>
|
||
</view>
|
||
</view>
|
||
|
||
<scroll-view
|
||
class="contract-sign__scroll"
|
||
scroll-y
|
||
:show-scrollbar="false"
|
||
>
|
||
<view class="contract-sign__content">
|
||
<view
|
||
class="contract-sign__preview-card"
|
||
@tap="handleOpenFullContract"
|
||
>
|
||
<text class="contract-sign__preview-title">
|
||
{{ previewTitle || '合同正文' }}
|
||
</text>
|
||
<text class="contract-sign__preview-desc">
|
||
{{ previewReady ? (previewOpened ? '已查看,可再次打开' : '点击查看完整合同(签署前必读)') : '暂无合同预览文件' }}
|
||
</text>
|
||
</view>
|
||
|
||
<view class="contract-sign__signature-row">
|
||
<view class="contract-sign__signature-col">
|
||
<text class="contract-sign__signature-label">
|
||
甲方(签名/盖章):
|
||
</text>
|
||
<view class="contract-sign__signature-box contract-sign__signature-box--done" />
|
||
</view>
|
||
<view class="contract-sign__signature-col">
|
||
<text class="contract-sign__signature-label">
|
||
乙方(签名/盖章):
|
||
</text>
|
||
<view
|
||
class="contract-sign__signature-box"
|
||
:class="{ 'contract-sign__signature-box--done': signaturePreview }"
|
||
@tap="openSignaturePad"
|
||
>
|
||
<image
|
||
v-if="signatureLocalPath"
|
||
class="contract-sign__signature-image"
|
||
:src="signatureLocalPath"
|
||
mode="aspectFit"
|
||
/>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<view class="contract-sign__form">
|
||
<view class="contract-sign__field">
|
||
<text class="contract-sign__field-label">
|
||
签署手机号
|
||
</text>
|
||
<input
|
||
v-model="mobile"
|
||
class="contract-sign__input"
|
||
type="number"
|
||
maxlength="11"
|
||
placeholder="请输入注册手机号"
|
||
placeholder-class="contract-sign__placeholder"
|
||
:disabled="alreadySigned"
|
||
>
|
||
</view>
|
||
|
||
<view class="contract-sign__field">
|
||
<text class="contract-sign__field-label">
|
||
短信验证码
|
||
</text>
|
||
<view class="contract-sign__code-row">
|
||
<input
|
||
v-model="smsCode"
|
||
class="contract-sign__input contract-sign__input--code"
|
||
type="number"
|
||
maxlength="6"
|
||
placeholder="请输入验证码"
|
||
placeholder-class="contract-sign__placeholder"
|
||
:disabled="alreadySigned"
|
||
>
|
||
<view
|
||
class="contract-sign__code-btn"
|
||
:class="{ 'contract-sign__code-btn--disabled': countdown > 0 || sendingCode || alreadySigned }"
|
||
@tap="handleSendCode"
|
||
>
|
||
{{ countdown > 0 ? `${countdown}s` : '获取验证码' }}
|
||
</view>
|
||
</view>
|
||
</view>
|
||
|
||
<view
|
||
class="contract-sign__agreement"
|
||
@tap="agreed = !agreed"
|
||
>
|
||
<view
|
||
class="contract-sign__checkbox"
|
||
:class="{ 'contract-sign__checkbox--checked': agreed }"
|
||
>
|
||
<text
|
||
v-if="agreed"
|
||
class="contract-sign__checkmark"
|
||
>
|
||
✓
|
||
</text>
|
||
</view>
|
||
<text class="contract-sign__agreement-text">
|
||
我已阅读并同意电子签服务协议,确认签署本合同
|
||
</text>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</scroll-view>
|
||
|
||
<view class="contract-sign__footer">
|
||
<view
|
||
class="contract-sign__submit-btn"
|
||
:class="{ 'contract-sign__submit-btn--disabled': submitting || alreadySigned }"
|
||
@tap="handleSubmit"
|
||
>
|
||
{{ alreadySigned ? '已签署' : '确认签署' }}
|
||
</view>
|
||
</view>
|
||
|
||
<SignaturePad
|
||
:visible="signaturePadVisible"
|
||
@close="closeSignaturePad"
|
||
@confirm="handleSignatureConfirm"
|
||
/>
|
||
</view>
|
||
</template>
|
||
|
||
<style scoped lang="scss">
|
||
@import './index.scss';
|
||
</style>
|