# 文档查看
文档查看
<template>
<FaLayout v-loading="loading" :right="240" :left="false">
<AppHeader slot="header" />
<FaContract ref="viewer" :docs="docs" :doc-index.sync="docIndex" :page-src="getPageImg" />
<FaThumbs slot="right" :viewer="viewer" :docs="docs" :doc-index.sync="docIndex" />
</FaLayout>
</template>
<script lang="ts">
import { defineComponent, onMounted, reactive, Ref, ref, toRefs } from 'vue'
import { DocData, Viewer } from 'fdd-contract'
import AppHeader from '../components/AppHeader.vue'
import { getDetail, getPageImg } from '../utils/mock'
/** 文档查看 */
export default defineComponent({
name: 'ContractView',
components: { AppHeader },
setup() {
/** 合同控件 */
const viewer = ref(null) as Ref<Viewer | null>
/** 详情数据 */
const detail = reactive({
/** 文档列表 */
docs: [] as DocData[]
})
/** 加载中 */
const loading = ref(false)
/** 当前文档索引 */
const docIndex = ref(0)
/** 详情加载 */
async function load() {
try {
loading.value = true
const { docs } = await getDetail()
Object.assign(detail, { docs })
} finally {
loading.value = false
}
}
onMounted(load)
return {
...toRefs(detail),
/** 合同控件 */
viewer,
/** 获取文档页面图片地址 */
getPageImg,
/** 加载中 */
loading,
/** 当前文档索引 */
docIndex
}
}
})
</script>
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
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
<template>
<div class="app-header">
<h2 class="app-header__title">{{ appTitle }}</h2>
<div class="app-header__action">
<slot />
</div>
</div>
</template>
<script lang="ts">
import { computed, defineComponent } from 'vue'
import router from '../router'
export default defineComponent({
name: 'AppHeader',
props: {
/** 标题 */
title: {
type: String,
default: ''
}
},
setup(props) {
return {
appTitle: computed(() => props.title || router.currentRoute.meta?.title || '')
}
}
})
</script>
<style lang="scss" scoped>
.app-header {
position: relative;
z-index: 1;
height: 48px;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
&__title {
margin: 0;
font-size: 16px;
line-height: 48px;
text-align: center;
}
&__action {
position: absolute;
top: 0;
right: 16px;
display: flex;
align-items: center;
height: 100%;
}
}
</style>
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
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
import FddContract, { createId, PageData, ActorData, SignData, DocData, Widget } from 'fdd-contract'
import { mockAjax } from '.'
import { createDateSign, createSignature, getImg } from './img'
/** 页面尺寸 */
const pageSizes = (() => {
const { width, height } = FddContract.config.page
return {
/** A4 */
a4: {
w: width,
h: height
},
/** A4 横版 */
a4c: {
w: height,
h: width
},
/** A5 */
a5: {
w: 530,
h: 820
},
/** A5 横版 */
a5c: {
w: 820,
h: 530
}
}
})()
/** 创建文档 */
function createDoc(name: string, count = 1, sizes = Object.keys(pageSizes) as (keyof typeof pageSizes)[]): DocData {
return {
id: createId(),
name,
pages: Array.from(new Array(count), (item, p) => {
const pageType = sizes[Math.floor(Math.random() * sizes.length)]
const { w, h } = pageSizes[pageType]
return {
p,
w,
h
} as PageData
})
}
}
/** 文档 */
export const docs = [
createDoc('7页 A4 合同', 7, ['a4']),
createDoc('1页 A4 合同', 1, ['a4']),
createDoc('2页 A4 合同', 2, ['a4']),
createDoc('3页 A4 合同', 3, ['a4']),
createDoc('20页异形合同', 20),
createDoc('100页 A4 合同', 100, ['a4']),
createDoc('20000页合同', 20000, ['a4'])
]
/** 参与方 */
export const actors: ActorData[] = [
{
id: createId(),
name: '深圳市法大大有限公司-小智',
index: 0,
isSender: true,
fill: true,
sign: true
},
{
id: createId(),
name: '深圳市桔子有限公司-小度',
index: 1,
fill: true,
sign: false
},
{
id: createId(),
name: '深圳市大米有限公司-小爱',
index: 2,
fill: false,
sign: true
},
{
id: createId(),
name: '小白',
index: 3,
isPerson: true,
fill: true,
sign: true
},
{
id: createId(),
name: '小丽',
index: 4,
isPerson: true,
fill: false,
sign: true
}
]
/** 印章 */
const seals: SignData[] = [
{
id: createId(),
name: '法大大',
img: getImg('seal-fadada.svg'),
default: true,
specs: '42mm * 42mm',
w: 160,
h: 160
},
{
id: createId(),
name: '桔子',
img: getImg('seal-juzi.svg'),
specs: '60mm * 60mm',
w: 228,
h: 228,
tag: '标签'
},
{
id: createId(),
name: '大米',
img: getImg('seal-dami.svg'),
specs: '38mm * 38mm',
w: 142,
h: 142,
tag: { text: '标签', type: 'success' }
},
{
id: createId(),
name: '大米',
img: getImg('seal-dami.svg'),
disabled: true,
specs: '40mm * 40mm',
w: 150,
h: 150
}
]
/** 签名 */
const signatures: SignData[] = [
{
id: createId(),
name: '小智',
img: createSignature('小智'),
default: true,
specs: '30mm * 16mm'
},
{
id: createId(),
name: '小度',
img: createSignature('小度'),
specs: '20mm * 11mm',
w: 114,
h: 64,
tag: { text: '标签', type: 'danger', effect: 'dark' }
},
{
id: createId(),
name: '小爱',
img: createSignature('小爱'),
specs: '16mm * 16mm',
w: 92,
h: 92,
tag: { text: '标签', effect: 'normal' }
},
{
id: createId(),
name: '小明',
img: createSignature('小明'),
disabled: true
}
]
/** 日期章 */
const dateSign = {
img: createDateSign()
} as SignData
/** 签章信息 */
export const signs = {
/** 印章 */
seals,
/** 签名 */
signatures,
/** 日期章 */
dateSign
}
const DETAIL_STORE_KEY = 'demo-detail'
interface Detail {
/** 文档列表 */
docs: DocData[]
/** 参与方列表 */
actors: ActorData[]
/** 签章信息 */
signs: typeof signs
}
/** 获取详情数据 */
export function getDetail() {
return mockAjax(() => {
const cache = sessionStorage.getItem(DETAIL_STORE_KEY)
if (cache) {
return JSON.parse(cache) as Detail
}
const detail: Detail = {
docs,
actors,
signs
}
sessionStorage.setItem(DETAIL_STORE_KEY, JSON.stringify(detail))
return detail
})
}
/** 获取文档页面图片地址 */
export function getPageImg(doc: DocData, page: PageData) {
const { w, h, p } = page
const n = p + 1
return `//fakeimg.pl/${w}x${h}/fff?text=${n}`
// return `//picsum.photos/seed/picsum/${w}/${h}`
// return `//fpoimg.com/${w}x${h}?bg_color=fff&text=${n}`
// return `//temp.im/${w}x${h}/fff/aaa`
// return `//dummyimage.com/${w}x${h}.png/fff/aaa&text=${n}`
// return `//via.placeholder.com/${w}x${h}/fff?text=${n}`
}
/** 获取日期章图片地址 */
export function getDateSignImg(widget: Widget) {
const { fontSize, dateFormat: formate } = widget.sign
return createDateSign({
fontSize,
formate
})
}
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
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
import { MessageBox } from 'element-ui'
import { awaitTimeout, Widget } from 'fdd-contract'
/** 模拟 Ajax 请求 */
export async function mockAjax<T extends () => void>(callback: T): Promise<ReturnType<T>> {
return awaitTimeout(Math.random() * 1000, callback)
}
/** 控件数据提示 */
export function alertWidgetsRaw(data: Widget[]) {
MessageBox.alert(
`<pre class="demo-alert-data">${JSON.stringify(data.map(item => item.getRawField()))}</pre>`,
'提交数据',
{
dangerouslyUseHTMLString: true
}
)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { DateFormat } from 'fdd-contract'
import formatDateString from '../../src/widgets/date-fill/formatDateString'
/** 获取图片地址 */
export function getImg(src: string) {
// Vite 下获取静态资源地址
return new URL(`../assets/img/${src}`, import.meta.url).href
}
/** svg 图片 base64 */
export function svg2Base64(svg: string) {
return `data:image/svg+xml;base64,${window.btoa(unescape(encodeURIComponent(svg)))}`
}
/** 生成签署日期 base64 图片 */
export function createDateSign({
date = new Date(),
fontSize = 16,
formate = DateFormat.LOCAL
}: {
date?: Date
fontSize?: number
formate?: DateFormat
} = {}) {
const format = (n: number) => (n < 10 ? `0${n}` : `${n}`)
const [w, h] = [170, 28]
const opacity = '0.8'
const text = formatDateString(
`${date.getFullYear()}/${format(date.getMonth() + 1)}/${format(date.getDate())}`,
formate
)
return svg2Base64(`<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="${w}" height="${h}" viewbox="0 0 ${w} ${h}" fill-opacity="${opacity}" stroke-opacity="${opacity}">
<text x="${w / 2}" y="${
h / 2
}" fill="black" font-family="宋体,SimSun" font-size="${fontSize}" style="text-anchor: middle; dominant-baseline: central;">${text}</text>
</svg>`)
}
/** 生成签名 base64 图片 */
export function createSignature(text: string) {
const [w, h] = [171, 92]
const fontSize = 56
const opacity = 0.8
return svg2Base64(`<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="${w}" height="${h}" viewbox="0 0 ${w} ${h}" fill-opacity="${opacity}" stroke-opacity="${opacity}" >
<text font-family="楷体,KaiTi" font-size="${fontSize}" x="${w / 2}" y="${
h / 2
}" fill="black" style="text-anchor: middle; dominant-baseline: central;">${text}</text>
<text x="130" y="86" fill="#888" font-size="12">E-Sign</text>
</svg>`)
}
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
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