feat:财务和合同
This commit is contained in:
@@ -1,30 +1,97 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import { computed, onUnmounted, ref } from 'vue'
|
||||
import { onLoad } from '@dcloudio/uni-app'
|
||||
import {
|
||||
CONTRACT_SIGN_BODY,
|
||||
CONTRACT_SIGN_SIGNATURE_STORAGE_KEY,
|
||||
CONTRACT_SIGN_STEPS,
|
||||
} from '@/constants/contract-sign'
|
||||
import { getContract } from '@/constants/contract'
|
||||
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 signSteps = CONTRACT_SIGN_STEPS
|
||||
const contractBody = CONTRACT_SIGN_BODY
|
||||
const progress = ref<ContractSignProgressVo | null>(null)
|
||||
const previewTitle = ref('')
|
||||
const previewReady = ref(false)
|
||||
const previewOpened = ref(false)
|
||||
const signaturePadVisible = ref(false)
|
||||
const partyBSignaturePreview = 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
|
||||
|
||||
onLoad((options) => {
|
||||
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)) : '2'
|
||||
contract.value = getContract(id)
|
||||
|
||||
if (!contract.value) {
|
||||
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
|
||||
}
|
||||
})
|
||||
|
||||
@@ -32,7 +99,17 @@ 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
|
||||
}
|
||||
|
||||
@@ -40,25 +117,146 @@ function closeSignaturePad() {
|
||||
signaturePadVisible.value = false
|
||||
}
|
||||
|
||||
function handleSignConfirm(imagePath: string) {
|
||||
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
|
||||
|
||||
partyBSignaturePreview.value = true
|
||||
signaturePadVisible.value = false
|
||||
if (!validatePhone(mobile.value))
|
||||
return
|
||||
|
||||
if (imagePath) {
|
||||
try {
|
||||
uni.setStorageSync(CONTRACT_SIGN_SIGNATURE_STORAGE_KEY, imagePath)
|
||||
}
|
||||
catch {
|
||||
// ignore storage write errors
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
uni.redirectTo({
|
||||
url: `/pages/contract-center/sign-success/index?id=${encodeURIComponent(contract.value.id)}`,
|
||||
})
|
||||
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>
|
||||
|
||||
@@ -89,7 +287,7 @@ function handleSignConfirm(imagePath: string) {
|
||||
{{ contract.contractName }}
|
||||
</text>
|
||||
<text class="contract-sign__status">
|
||||
待签署
|
||||
{{ signStatusLabel }}
|
||||
</text>
|
||||
</view>
|
||||
|
||||
@@ -135,9 +333,17 @@ function handleSignConfirm(imagePath: string) {
|
||||
:show-scrollbar="false"
|
||||
>
|
||||
<view class="contract-sign__content">
|
||||
<text class="contract-sign__body">
|
||||
{{ contractBody }}
|
||||
</text>
|
||||
<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">
|
||||
@@ -152,9 +358,77 @@ function handleSignConfirm(imagePath: string) {
|
||||
</text>
|
||||
<view
|
||||
class="contract-sign__signature-box"
|
||||
:class="{ 'contract-sign__signature-box--done': partyBSignaturePreview }"
|
||||
: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>
|
||||
@@ -163,16 +437,17 @@ function handleSignConfirm(imagePath: string) {
|
||||
<view class="contract-sign__footer">
|
||||
<view
|
||||
class="contract-sign__submit-btn"
|
||||
@tap="openSignaturePad"
|
||||
:class="{ 'contract-sign__submit-btn--disabled': submitting || alreadySigned }"
|
||||
@tap="handleSubmit"
|
||||
>
|
||||
立即签署
|
||||
{{ alreadySigned ? '已签署' : '确认签署' }}
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<SignaturePad
|
||||
:visible="signaturePadVisible"
|
||||
@close="closeSignaturePad"
|
||||
@confirm="handleSignConfirm"
|
||||
@confirm="handleSignatureConfirm"
|
||||
/>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user