安全教育平台微信小程序(hbuilder打开后运行到微信开发者程序)
祖安之光
2025-11-05 f4ed2c4a1412f7256614e04e18683ca15a89bb25
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<template>
    <view class="signature-container">
        <view class="canvas-container">
            <canvas canvas-id="signatureCanvas" id="signatureCanvas" class="signature-canvas"
                @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd"
                disable-scroll></canvas>
            <view class="toast" v-if="toastVisible">请签写您的名字</view>
        </view>
        <view class="controls">
            <view></view>
            <view class="btn-group">
                <button class="btn btn-clear" @tap="clearCanvas" :disabled="uploading">清空</button>
                <button class="btn btn-confirm" @tap="saveSignature" :disabled="uploading || !hasDrawn">
                    {{ uploading ? '上传中...' : '确定' }}
                </button>
            </view>
        </view>
    </view>
</template>
 
<script>
    import VUE_APP_BASE_URL from "../../common/constant";
    import {
        postSignaure
    } from "../../api/review.js"
    export default {
        name: 'SignaturePad',
        data() {
            return {
                canvas: null,
                ctx: null,
                isDrawing: false,
                hasDrawn: false,
                lastX: 0,
                lastY: 0,
                toastVisible: true,
                systemInfo: null,
                uploading: false,
                id: null
            }
        },
        onReady() {
            this.initCanvas();
        },
        onLoad(e) {
            this.getSystemInfo();
            this.id = e.id && JSON.parse(decodeURIComponent(e.id))
        },
        onUnload() {
            this.cleanup();
        },
        methods: {
            getSystemInfo() {
                const that = this;
                wx.getSystemInfo({
                    success(res) {
                        that.systemInfo = res;
                        that.initCanvas();
                    }
                });
            },
 
            initCanvas() {
                if (!this.systemInfo) return;
                this.ctx = wx.createCanvasContext('signatureCanvas', this);
                this.ctx.setLineCap('round');
                this.ctx.setLineJoin('round');
                this.ctx.setStrokeStyle('#1A1A1A');
                this.ctx.setLineWidth(2);
                this.ctx.setFillStyle('#1A1A1A');
                this.clearCanvas();
            },
 
            handleTouchStart(e) {
                if (!this.ctx) return;
 
                const touch = e.touches[0];
                this.isDrawing = true;
                [this.lastX, this.lastY] = [touch.x, touch.y];
                this.drawDot(this.lastX, this.lastY);
                this.hideToast();
                this.hasDrawn = true;
            },
 
            handleTouchMove(e) {
                if (!this.isDrawing || !this.ctx) return;
 
                const touch = e.touches[0];
                const x = touch.x;
                const y = touch.y;
 
                // 绘制线条
                this.ctx.beginPath();
                this.ctx.moveTo(this.lastX, this.lastY);
                this.ctx.lineTo(x, y);
                this.ctx.stroke();
                this.ctx.draw(true);
 
                this.drawDot(x, y);
 
                [this.lastX, this.lastY] = [x, y];
            },
 
            handleTouchEnd() {
                this.isDrawing = false;
            },
 
            drawDot(x, y) {
                if (!this.ctx) return;
 
                this.ctx.beginPath();
                this.ctx.arc(x, y, 1, 0, 2 * Math.PI);
                this.ctx.fill();
                this.ctx.draw(true);
            },
 
            clearCanvas() {
                if (!this.ctx) return;
 
 
                this.ctx.clearRect(0, 0, 1000, 1000);
                this.ctx.setFillStyle('#F5F7FB');
                this.ctx.fillRect(0, 0, 1000, 1000);
                this.ctx.draw(true);
 
                this.hasDrawn = false;
                this.showToast();
            },
 
            showToast() {
                this.toastVisible = true;
            },
 
            hideToast() {
                this.toastVisible = false;
            },
 
            async saveSignature() {
                if (!this.hasDrawn) {
                    wx.showToast({
                        title: '您还未签名!',
                        icon: 'none',
                        duration: 2000
                    });
                    return;
                }
                if (this.uploading) return;
                this.uploading = true;
                try {
                    const tempFilePath = await this.canvasToTempFile();
                    const uploadResult = await this.uploadToServer(tempFilePath);
                    await this.handleUploadSuccess(uploadResult);
 
                } catch (error) {
                    this.handleUploadError(error);
                } finally {
                    this.uploading = false;
                }
            },
 
            canvasToTempFile() {
                return new Promise((resolve, reject) => {
                    wx.canvasToTempFilePath({
                        canvasId: 'signatureCanvas',
                        quality: 1,
                        fileType: 'png',
                        success: (res) => {
                            resolve(res.tempFilePath);
                        },
                        fail: (err) => {
                            reject(new Error('生成图片失败:' + JSON.stringify(err)));
                        }
                    }, this);
                });
            },
 
            uploadToServer(tempFilePath) {
                return new Promise((resolve, reject) => {
                    wx.uploadFile({
                        url: `${VUE_APP_BASE_URL}/system/common/uploadFile`, // 替换为你的上传接口
                        filePath: tempFilePath,
                        name: 'file',
                        formData: {},
                        header: {
                            'Authorization': uni.getStorageSync('tk'), // 如果有token认证
                            'Content-Type': 'multipart/form-data'
                        },
                        success: (res) => {
                            if (res.statusCode === 200) {
                                try {
                                    const data = JSON.parse(res.data);
                                    resolve(data);
                                } catch (e) {
                                    reject(new Error('解析响应数据失败'));
                                }
                            } else {
                                reject(new Error(`上传失败,状态码:${res.statusCode}`));
                            }
                        },
                        fail: (err) => {
                            reject(new Error('网络请求失败:' + JSON.stringify(err)));
                        }
                    });
                });
            },
 
            async handleUploadSuccess(res) {
                try {
                    if (!res.data || !res.data.path) {
                        throw new Error('未获取到文件路径');
                    }
                    const filePath = res.data.path;
                    const submitResult = await this.submitSignatureInfo(filePath);
                    this.handleFinalSuccess(submitResult);
                } catch (error) {
                    throw new Error('提交签名信息失败:' + error.message);
                }
            },
 
            async submitSignatureInfo(path) {
                const res = await postSignaure({
                    id: this.id,
                    sign: path
                })
                if (res.code == 200) {
                    return res
                } else {
                    reject(new Error(`提交失败,状态码:${res.code}`));
                }
            },
 
            handleFinalSuccess(result) {
                wx.showToast({
                    title: '签名提交成功',
                    icon: 'success',
                    duration: 2000
                });
                setTimeout(() => {
                    // const pages = getCurrentPages();
                    // if (pages.length > 1) {
                    //     const prevPage = pages[pages.length - 2];
                    //     if (prevPage) {
                    //         prevPage.needRefresh = true;
                    //     }
                    //     wx.navigateBack({
                    //         delta: 1,
                    //         success: () => {
                    //             console.log('返回成功,页面应该刷新');
                    //         }
                    //     });
                    // } else {
                        wx.reLaunch({
                            url: '/pages/review/index'
                        });
                    // }
                }, 1500);
            },
 
            handleUploadError(error) {
                console.error('上传失败:', error);
 
                wx.showToast({
                    title: '上传失败,请重试',
                    icon: 'none',
                    duration: 2000
                });
            },
 
            cleanup() {
                this.ctx = null;
                this.isDrawing = false;
            }
        }
    }
</script>
 
<style scoped>
    .signature-container {
        width: 100vw;
        height: 100vh;
        display: flex;
        flex-direction: column;
        background: #ffffff;
    }
 
    .canvas-container {
        flex: 1;
        position: relative;
        background: #F5F7FB;
        border: 1px solid rgba(0, 0, 0, 0.08);
        border-radius: 4px;
        margin: 10px;
        overflow: hidden;
    }
 
    .signature-canvas {
        width: 100%;
        height: 100%;
        background: #F5F7FB;
    }
 
    .toast {
        position: absolute;
        top: 50%;
        left: 40%;
        transform: translate(-50%, -50%);
        transform: rotate(90deg);
        font-size: 40rpx;
        color: rgba(58, 65, 85, 0.4);
        pointer-events: none;
        z-index: 100;
    }
 
    .controls {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 15px 32px;
        background: #ffffff;
        border-top: 1px solid #e5e7eb;
    }
 
    .btn {
        padding: 8px 20px;
        border: none;
        border-radius: 4px;
        font-size: 14px;
        cursor: pointer;
        transition: all 0.3s ease;
    }
 
    .btn-clear {
        background: rgba(103, 149, 255, 0.2);
        color: #3670F5;
        margin-right: 12px;
    }
 
    .btn-clear:hover {
        background: rgba(103, 149, 255, 0.3);
    }
 
    .btn-confirm {
        background: #3670F5;
        color: #FFFFFF;
    }
 
    .btn-confirm:hover {
        background: #2563eb;
    }
 
    .btn-group {
        display: flex;
        gap: 10px;
    }
 
    /* 微信小程序适配样式 */
    button {
        margin: 0;
        padding: 8px 20px;
        border-radius: 4px;
        font-size: 14px;
    }
 
    button::after {
        border: none;
    }
 
    /* 横竖屏适配 */
    @media (orientation: landscape) {
        .signature-container {
            flex-direction: column;
        }
 
        .canvas-container {
            height: calc(100vh - 80px);
        }
    }
 
    @media (orientation: portrait) {
        .signature-container {
            flex-direction: column;
        }
 
        .canvas-container {
            height: calc(100vh - 120px);
        }
    }
</style>