tom
2023-12-06 9e968679ed2e6937aeb7b50a6c450d5d19251f42
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
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>电子签名生成图片</title>
        <script src="../../js/jquery-3.5.1.min.js"></script>
        <script src="./js/jSignature.js"></script>
        
        <script src="../../js/vue/vue.js"></script>
        <script src="../../js/vue/axios/dist/axios.min.js"></script>
        <script src="../../js/vue/element-ui/lib/index.js"></script>
        <script src="../../js/common.js"></script>
        <script src="../../js/myelement.js"></script>
        <script src="../../js/vue/page.js"></script>
        
        <link href="../../js/vue/element-ui/lib/theme-chalk/index.css" rel="stylesheet">
        <link href="../../css/myelement.css" rel="stylesheet">
        <link href="../../css/iconfont.css" rel="stylesheet">
        <link href="../../css/page.css" rel="stylesheet">
        <link href="//at.alicdn.com/t/font_2374495_mkoo966o0uo.css" rel="stylesheet">
        
    </head>
    <body>
        <div id="vbody">
            <div id="page_root">
                <div ref="popup_body" style="width: 100vw; height: 100vh;">
                    <div class="el-dialog__header">
                        <div class="dialog-title">
                            <span>{{title}}</span>
                            <div style="float: right;">
                                <el-button-group style="margin-left: 3px;">
                                    <el-button type="primary" @click="saveImg">保 存</el-button>
                                    <el-button type="default" @click="saveAfter">取 消</el-button>
                                </el-button-group>
                            </div>
                        </div>
                    </div>
                    <div class="versionNo" >
                        <div id="writers" >
                            <div id='signature' style='background-color: #d2d2e8;'></div>
                            <!-- <div>
                                <el-image
                                    v-if="isSignature"
                                    style="height: 300px; width: 100%;" 
                                    :src="file_img"
                                    fit="contain">
                                </el-image>
                            </div> -->
                            <div style="width: 100%; padding: 5px;" align="center">
                                <el-button @click="rewrite">重 写</el-button>
                                <el-button @click="confirm">预 览</el-button>
                            </div>
                            <div id="someelement" ><img :src="file_img" id="images"></div>
                            
                        </div>
                    </div>
                    
                </div>
            </div>
            <div id="page_loading" style="position: absolute; top:0px; width: 100vw; height: 100vh;">
                <div class="spinner">
                    <div class="cube1"></div>
                    <div class="cube2"></div>
                </div>
            </div>
        </div>
        
        <script>
            new Vue({
                el: "#vbody",
                data: {
                    title: localStorage.getItem("emp_name") + "签名",
                    popupParames: {},
                    conversions: null,
                    isSignature:false,
                    file_img:"",
                },
                created() {
                    this.popupParames = clone(Root.popupParames);
                    this.initData();
                },
                mounted() {
                    this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
                        document.getElementById('page_root').style.display = "block";
                        document.getElementById('page_loading').style.display = "none";
                        this.initJSignature();
                        //重新设置弹窗宽高
                        this.$nextTick(function() {
                            let w_ = this.$refs.popup_body.offsetWidth + "px";
                            let h_ = this.$refs.popup_body.offsetHeight + "px";
                            Root.setPopupWH(w_, h_);
                        })
                    });
                },
            
                methods: {
                    closeDialog() {
                        if (this.popupParames.totab){
                            Root.tab.removeItem(Root.tab.selected);
                            Root.tab.open(me.popupParames.parentOption, false); 
                        }
                        else {
                            Root.hidePopup();
                        }
                    },
                    initData() {
                        var param_ = {
                            isClientMode: false,
                            dataname:"user",
                            filter:"id = '" + localStorage.getItem("id") + "'"
                        };
                        let me = this;
                        Server.call("root/data/getEntity", param_, function(result) {
                            if (result && result.success) {
                                if (result.data.entity.signature) {
                                    me.isSignature = true;
                                    me.getPDF(result.data.entity.signature);
                                } else{
                                    me.isSignature = false;
                                }
                            } else{
                                Root.message({
                                    type:"warning",
                                    message:"查询签名信息失败"
                                })
                            }
                        });
                    },
                    getPDF(fileid) {
                        var me =this;
                        getDownloadFileUrl(fileid, true, function(result_) {
                            
                            if (result_.data && result_.data.path) {
                                var file_img_ = "";
                                var path_ = result_.data.path;
                                if (path_.substring(0, 5) == "root/") {
                                    file_img_ = window.top.RootSetting.url_file + path_.substring(5);
                                }
                                me.file_img = file_img_;
                            }
                        });
                        
                    },
                    saveAfter() {
                        var me = this;
                        if(this.popupParames.callback) {
                            let obj = {
                                signature: this.isSignature
                            }
                            this.popupParames.callback(obj, function() {
                                if (me.popupParames.totab){
                                    Root.tab.removeItem(Root.tab.selected);
                                    Root.tab.open(me.popupParames.parentOption, false); 
                                }
                                else {
                                    Root.hidePopup();
                                }
                            });
                        }
                        else {
                            alert("该按键事件缺失!")
                        }
                    },
                    
                    saveImg() {
                        var me = this;
                        
                        this.confirm();
                        
                        var formData = new FormData();
                        formData.append("upfile", this.conversions,"image.png");
                        me.uploadFile(me, formData, result_ => {
                            var ids = result_.data.data.ids;
                            var paramObj = {};
                            paramObj.id = localStorage.getItem("id");
                            
                            paramObj.signature = ids[0];
                            var param_ = paramObj;
                            Server.call("root/data/updateEntity/" + "user", param_, function(result) {
                                console.log(result);
                                Root.message({
                                    type: 'success',
                                    message: '已保存'
                                }); 
                                me.isSignature = true;
                                me.saveAfter();
                            });
                            
                            
                        })
                    },
                    uploadFile(vue, formData, callback, error_callback) {
                        var user_id = localStorage.getItem("id");
                        uploadAxios.post(baseUrl +'file/exec?userId=' + user_id, formData).then(data_ => {
                            vue.$message({
                                showClose: true,
                                message: '上传成功!',
                                type: 'success'
                            });
                            if (callback) {
                                callback(data_);
                            }
                        }).catch(error => {
                                vue.$message({
                                    showClose: true,
                                    message:  ':请求出现错误:' + error,
                                    type: 'error'
                                });
                                console.error(error);
                                if (error_callback) {
                                    error_callback(error);
                                }
                            });
                    },
                    //初始化插件
                    initJSignature() {
                        var arguments = {
                            width: '100%',
                            signatureLine: false,//去除默认画布上那条横线
                            lineWidth: '5'
                        };
                                
                        $("#signature").jSignature(arguments);
                    },
                    
                    //重置画布,可以进行重新作画
                    rewrite() {
                        this.file_img = ""
                        $("#signature").jSignature("reset");
                        $("#images").attr('src','');
                        
                    },
                    
                    //点击确定按钮,把签名的转成图片,然后把数据放进图片中,最后把图片中的数据传到后台
                    confirm() {
                        //将画布内容转换为图片
                        var $signature = $("#signature")
                        var datapair = $signature.jSignature("getData", "image");
                        $("#images").attr('src','data:' + datapair[0] + "," + datapair[1]);
                        var file = $("#images").attr('src');//拿到图片中的src,这就是我们需要的base64
                        console.info(file);
                        
                        //let file = img.src; // 把整个base64给file
                        var filename = localStorage.getItem("emp_name") + "签名";
                        let name = filename + ".png"; // 定义文件名字(例如:cober.png)
                        var type = "image/png"; // 定义图片类型(canvas转的图片一般都是png,也可以指定其他类型)
                        this.conversions = this.base64ToBlob(file, type); // 调用base64转图片方法
                    },
                    // conversions 就是转化之后的图片文件,
                    base64ToBlob(urlData, type) {
                        let arr = urlData.split(',');
                        let mime = arr[0].match(/:(.*?);/)[1] || type;
                        // 去掉url的头,并转化为byte
                        let bytes = window.atob(arr[1]);
                        // 处理异常,将ascii码小于0的转换为大于0
                        let ab = new ArrayBuffer(bytes.length);
                        // 生成视图(直接针对内存):8位无符号整数,长度1个字节
                        let ia = new Uint8Array(ab);
                        for (let i = 0; i < bytes.length; i++) {
                            ia[i] = bytes.charCodeAt(i);
                        }
                        return new Blob([ab], {
                            type: mime
                        });
                    },
                    
                    
                    
                }
            });
            
        </script>
        
    </body>
</html>