路泰科技体检小程序UI设计新版本
:qx
qx
2025-08-06 e3afb7ab25d2709d4e2fe8859bb23ef56b5a1360
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
<template>
    <view class="container">
 
        <!-- 输入区域 -->
        <view class="input-group">
            <text class="label">
                <uni-icons type="compose" size="16" color="#2c3e50" />
                输入文本或URL:
            </text>
            <textarea v-model="inputText" placeholder="在此输入文本、网址或其他信息..." />
        </view>
 
        <!-- 示例按钮 -->
        <view class="examples">
            <view v-for="(example, index) in examples" :key="index" class="example-btn" @tap="setExample(example.text)">
                {{ example.label }}
            </view>
        </view>
 
        <!-- 生成按钮 -->
        <button class="generate-btn" @tap="generateQRCode">
            <uni-icons type="bolt" size="20" color="#fff" />
            <text>{{generating ? "生成中...." :"生成二维码" }}</text>
        </button>
 
        <!-- 二维码显示区域 -->
        <view class="qrcode-container">
 
            <canvas v-if="qrCodeGenerated" ref="qrcodeCanvas" canvas-id="qrcodeCanvas" class="qrcode-canvas"
                @longpress="handleLongPress" width="200" height="200" />
            <view v-else class="placeholder">
                <uni-icons type="qrcode" size="48" color="#95a5a6" />
                <text>二维码将在此处显示</text>
            </view>
        </view>
    </view>
</template>
 
 
 
<script>
    import UQRCode from '@uqrcode/js'; // npm install @uqrcode/js
    export default {
        data() {
            return {
                inputText: 'https://www.example.com',
                qrCodeGenerated: false,
                isMiniprogram: false,
                qrCodeDataUrl: null,
                platformName: 'H5',
                platformIcon: 'circle',
                error: null,
                generating: false,
                saveButtonText: '保存二维码',
                examples: [{
                        label: '示例网址',
                        text: 'https://www.example.com'
                    },
                    {
                        label: '示例文本',
                        text: 'Hello, World!'
                    },
                    {
                        label: '联系方式',
                        text: '微信:example_user'
                    },
                    {
                        label: 'WiFi连接',
                        text: 'WIFI:T:WPA;S:MyNetwork;P:password123;;'
                    }
                ]
            };
        },
        mounted() {
            // this.detectPlatform();
        },
        methods: {
            // detectPlatform() {
            //     // 判断是否在小程序环境中
            //     // #ifdef MP-WEIXIN
            //     this.isMiniprogram = true;
            //     this.platformName = '微信小程序';
            //     this.platformIcon = 'weixin';
            //     this.saveButtonText = '保存到相册';
            //     // #endif
 
            //     // #ifdef APP-PLUS
            //     this.isMiniprogram = true;
            //     this.platformName = 'App';
            //     this.platformIcon = 'mobile';
            //     this.saveButtonText = '保存到相册';
            //     // #endif
            // },
 
            setExample(text) {
                this.inputText = text;
                this.generateQRCode();
            },
 
            // 生成二维码 - 使用 canvas-id 替代 ref
            async generateQRCode() {
                const text = this.inputText.trim();
 
                if (!text) {
                    this.error = '请输入要生成二维码的内容!';
                    return;
                }
 
                this.generating = true;
                this.error = null;
                this.qrCodeGenerated = true;
                try {
                    const qr = new UQRCode();
                    qr.data = text;
                    qr.size = 200;
                    qr.make();
                    const ctx = uni.createCanvasContext('qrcodeCanvas',
                    this); // 组件内调用需传this,vue3 中 this 为 getCurrentInstance()?.proxy
                    qr.canvasContext = ctx;
                    qr.drawCanvas();
                    this.generating = false;
                } catch (e) {
                    console.error('二维码生成异常:', e);
                    alert('生成二维码时出错!');
                    this.qrCodeGenerated = false;
 
                }
            },
 
            // 保存二维码
            saveQRCode() {
                if (!this.qrCodeGenerated) {
                    alert('请先生成二维码');
                    return;
                }
 
                const canvas = this.$refs.qrcodeCanvas;
                if (!canvas) return;
 
                // 在不同平台使用不同的保存方式
                if (this.isMiniprogram) {
                    // 小程序环境保存
                    this.saveInMiniprogram(canvas);
                } else {
                    // H5环境保存
                    this.saveInH5(canvas);
                }
            },
            // 在H5环境中保存二维码
            saveInH5(canvas) {
                const dataURL = canvas.toDataURL('image/png');
                const link = document.createElement('a');
                link.href = dataURL;
                link.download = `qrcode-${new Date().getTime()}.png`;
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
 
                alert('二维码已保存');
            },
 
            // 在小程序环境中保存二维码
            saveInMiniprogram(canvas) {
                // 模拟Uniapp保存图片到相册
                if (typeof uni !== 'undefined') {
                    uni.canvasToTempFilePath({
                        canvas: canvas,
                        success: (res) => {
                            uni.saveImageToPhotosAlbum({
                                filePath: res.tempFilePath,
                                success: () => {
                                    uni.showToast({
                                        title: '保存成功',
                                        icon: 'success'
                                    });
                                },
                                fail: () => {
                                    uni.showToast({
                                        title: '保存失败',
                                        icon: 'none'
                                    });
                                }
                            });
                        },
                        fail: (err) => {
                            console.error('保存失败:', err);
                            uni.showToast({
                                title: '保存失败',
                                icon: 'none'
                            });
                        }
                    });
                }
                // 微信原生小程序保存
                else if (typeof wx !== 'undefined') {
                    wx.canvasToTempFilePath({
                        canvas: canvas,
                        success: (res) => {
                            wx.saveImageToPhotosAlbum({
                                filePath: res.tempFilePath,
                                success: () => {
                                    wx.showToast({
                                        title: '保存成功',
                                        icon: 'success'
                                    });
                                },
                                fail: () => {
                                    wx.showToast({
                                        title: '保存失败',
                                        icon: 'none'
                                    });
                                }
                            });
                        },
                        fail: (err) => {
                            console.error('保存失败:', err);
                            wx.showToast({
                                title: '保存失败',
                                icon: 'none'
                            });
                        }
                    });
                }
            },
 
 
            // 小程序长按事件
            handleLongPress() {
                if (this.isMiniprogram) {
                    this.saveQRCode();
                }
            }
        }
    };
</script>
 
<style lang="scss">
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
    }
 
    textarea {
        width: 100%;
        height: 120px;
        padding: 15px;
        border: 2px solid #ddd;
        border-radius: 12px;
        font-size: 1rem;
        resize: none;
        transition: all 0.3s ease;
    }
 
    textarea:focus {
        border-color: #3498db;
        outline: none;
        box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
    }
 
    .generate-btn {
        width: 100%;
        padding: 15px;
        background: linear-gradient(to right, #3498db, #2c3e50);
        color: white;
        border: none;
        border-radius: 12px;
        font-size: 1.2rem;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        margin-bottom: 25px;
        display: flex;
        justify-content: center;
        align-items: center;
        gap: 10px;
    }
 
    .generate-btn:hover {
        background: linear-gradient(to right, #2980b9, #1a252f);
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    }
 
    .qrcode-container {
        text-align: center;
        margin-bottom: 25px;
        padding: 20px;
        border-radius: 15px;
        background: #f8f9fa;
        box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.05);
        min-height: 260px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
 
    .qrcode-canvas {
        width: 200px;
        height: 200px;
        margin: 0 auto;
        padding: 10px;
        background: white;
        border-radius: 10px;
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    }
 
    .placeholder {
        color: #95a5a6;
        font-size: 1.1rem;
    }
    .examples {
        display: flex;
        gap: 15px;
        flex-wrap: wrap;
        margin-top: 15px;
        justify-content: center;
    }
 
    .example-btn {
        padding: 8px 15px;
        background: #e0e7ff;
        color: #4f46e5;
        border-radius: 20px;
        font-size: 0.9rem;
        cursor: pointer;
        transition: all 0.2s;
    }
 
    .example-btn:hover {
        background: #c7d2fe;
    }
 
 
 
    @media (max-width: 500px) {
        #app {
            padding: 20px;
        }
 
        .header h1 {
            font-size: 1.8rem;
        }
 
        .header p {
            font-size: 1rem;
        }
 
        .qrcode-canvas {
            width: 180px;
            height: 180px;
        }
    }
</style>