Przeglądaj źródła

个人签章上传和删除

laiguoran 3 lat temu
rodzic
commit
80f8247a9f

+ 38 - 13
app/controller/profile_controller.js

@@ -247,6 +247,7 @@ module.exports = app => {
 
             const renderData = {
                 accountData,
+                fujianOssPath: ctx.app.config.fujianOssPath,
             };
             await this.layout('profile/sign.ejs', renderData);
         }
@@ -351,12 +352,22 @@ module.exports = app => {
 
                 // 获取账号数据
                 const accountData = await ctx.service.projectAccount.getDataByCondition({ id: sessionUser.accountId });
-
-                if (accountData.sign_path === '') {
-                    throw '不存在签名';
+                const data = JSON.parse(ctx.request.body.data);
+                let result = false;
+                if (data.type && data.type === 'stamp') {
+                    if (!accountData.stamp_path) {
+                        throw '不存在签章';
+                    }
+                    // 删除oss文件
+                    await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + accountData.stamp_path);
+                    // 删除库
+                    result = await ctx.service.projectAccount.update({ stamp_path: null }, { id: sessionUser.accountId });
+                } else {
+                    if (accountData.sign_path === '') {
+                        throw '不存在签名';
+                    }
+                    result = await ctx.service.projectAccount.update({ sign_path: '' }, { id: sessionUser.accountId });
                 }
-
-                const result = await ctx.service.projectAccount.update({ sign_path: '' }, { id: sessionUser.accountId });
                 if (!result) {
                     throw '移除签名失败';
                 }
@@ -412,15 +423,29 @@ module.exports = app => {
                 const stream = await ctx.getFileStream();
                 const create_time = Date.parse(new Date()) / 1000;
                 const fileInfo = path.parse(stream.filename);
-                const dirName = 'public/upload/sign';
-                const fileName = moment().format('YYYYMMDD') + '_sign_' + create_time + fileInfo.ext;
-                await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', dirName, fileName));
-                await sendToWormhole(stream);
-                const result = await ctx.service.projectAccount.update({ sign_path: fileName }, { id: ctx.session.sessionUser.accountId });
-                if (result) {
-                    responseData.data = { sign_path: fileName };
+                if (stream.fields && stream.fields.type && stream.fields.type === 'stamp') {
+                    // const dirName = 'app/public/upload/sign/profile';
+                    // const fileName = moment().format('YYYYMMDD') + '_sign_' + create_time + fileInfo.ext;
+                    const filepath = `app/public/upload/sign/profile/qianzhang_${create_time + fileInfo.ext}`;
+                    await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
+                    await sendToWormhole(stream);
+                    const result = await ctx.service.projectAccount.update({ stamp_path: filepath }, { id: ctx.session.sessionUser.accountId });
+                    if (result) {
+                        responseData.data = { stamp_path: filepath };
+                    } else {
+                        throw '添加数据库失败';
+                    }
                 } else {
-                    throw '添加数据库失败';
+                    const dirName = 'public/upload/sign';
+                    const fileName = moment().format('YYYYMMDD') + '_sign_' + create_time + fileInfo.ext;
+                    await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', dirName, fileName));
+                    await sendToWormhole(stream);
+                    const result = await ctx.service.projectAccount.update({ sign_path: fileName }, { id: ctx.session.sessionUser.accountId });
+                    if (result) {
+                        responseData.data = { sign_path: fileName };
+                    } else {
+                        throw '添加数据库失败';
+                    }
                 }
             } catch (err) {
                 this.log(err);

+ 31 - 2
app/public/js/profile.js

@@ -115,7 +115,15 @@ $(document).ready(function() {
         $('#delete-sign').click(function () {
             postData('/profile/sign/delete', {}, function (result) {
                 $('#sign-show').html('');
-                toast('移除成功', 'success');
+                toastr.success('移除成功');
+            })
+        });
+
+        // 移除签章
+        $('#delete-stamp').click(function () {
+            postData('/profile/sign/delete', { type: 'stamp' }, function (result) {
+                $('#stamp-show').html('');
+                toastr.success('移除成功');
             })
         });
 
@@ -136,7 +144,7 @@ $(document).ready(function() {
             const ext = file.name.toLowerCase().split('.').splice(-1)[0];
             const imgStr = /(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
             if (!imgStr.test(ext)) {
-                toast('请上传正确的图片格式文件','error');
+                toastr.error('请上传正确的图片格式文件');
                 return
             }
             if ($(this).val()) {
@@ -149,6 +157,27 @@ $(document).ready(function() {
                 });
             }
         })
+
+        // 上传签章
+        $('#stamp-upload').change(function () {
+            const file = this.files[0];
+            const ext = file.name.toLowerCase().split('.').splice(-1)[0];
+            const imgStr = /(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;
+            if (!imgStr.test(ext)) {
+                toastr.error('请上传正确的图片格式文件');
+                return
+            }
+            if ($(this).val()) {
+                const formData = new FormData();
+                formData.append('type', 'stamp');
+                formData.append('file', this.files[0]);
+                postDataWithFile('/profile/sign/upload', formData, function (result) {
+                    const html = '<img src="'+ fujianOssPath + result.stamp_path +'" width="90">';
+                    $('#stamp-show').html(html);
+                    $('#stamp-upload').val('');
+                });
+            }
+        })
     } catch (error) {
         console.log(error);
     }

+ 12 - 0
app/view/profile/sign.ejs

@@ -40,6 +40,12 @@
                             </div>
                             <button type="button" class="btn btn-danger btn-sm" id="delete-sign">移除签名</button>
                             <div class="form-group">
+                                <label for="stamp">上传签章</label>
+                                <input type="file" class="form-control-file" id="stamp-upload">
+                                <small class="form-text text-danger">图片大小为600x300,格式PNG透明背景。</small>
+                            </div>
+                            <button type="button" class="btn btn-sm btn-danger" id="delete-stamp">移除签章</button>
+                            <div class="form-group">
                                 <label>签名图预览</label>
                                 <div>
                                     <div class="position-relative">
@@ -49,6 +55,11 @@
                                                 <img src="/public/upload/sign/<%= accountData.sign_path %>" width="90">
                                             <% } %>
                                         </div>
+                                        <div class="position-absolute fixed-top" id="stamp-show" style="left:300px;top:260px">
+                                            <% if (accountData.stamp_path !== '') { %>
+                                                <img src="<%- fujianOssPath + accountData.stamp_path %>" width="90">
+                                            <% } %>
+                                        </div>
                                     </div>
                                 </div>
                             </div>
@@ -61,5 +72,6 @@
 </div>
 <script type="text/javascript">
     const csrf = '<%= ctx.csrf %>';
+    const fujianOssPath = JSON.parse(unescape('<%- escape(JSON.stringify(fujianOssPath)) %>'));
 </script>
 <script type="text/javascript" src="/public/js/profile.js"></script>

+ 2 - 0
sql/update.sql

@@ -1,3 +1,5 @@
 ALTER TABLE `zh_material` ADD `is_new` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '是否是新建的调差,用于区分清单新建规则' AFTER `in_time`;
 
 ALTER TABLE `zh_change_plan` ADD `expr` TEXT NULL DEFAULT NULL COMMENT '工程量数量计算式' AFTER `memo`;
+
+ALTER TABLE `zh_project_account` ADD `stamp_path` VARCHAR(255) NULL DEFAULT NULL COMMENT '用户签章oss地址' AFTER `sign_path`;