Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
guns-vip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chenjunxiong
guns-vip
Commits
60053ef4
Commit
60053ef4
authored
May 03, 2019
by
fengshuonan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新上传头像
parent
d74c0e7b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
29 deletions
+49
-29
src/main/java/cn/stylefeng/guns/modular/system/controller/SystemController.java
+26
-11
src/main/java/cn/stylefeng/guns/modular/system/service/FileInfoService.java
+3
-10
src/main/webapp/assets/modular/frame/user_info.js
+19
-7
src/main/webapp/pages/modular/frame/user_info.html
+1
-1
No files found.
src/main/java/cn/stylefeng/guns/modular/system/controller/SystemController.java
View file @
60053ef4
...
...
@@ -18,6 +18,7 @@ package cn.stylefeng.guns.modular.system.controller;
import
cn.hutool.core.bean.BeanUtil
;
import
cn.hutool.core.codec.Base64
;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.core.io.IoUtil
;
import
cn.stylefeng.guns.config.properties.GunsProperties
;
import
cn.stylefeng.guns.core.common.constant.DefaultAvatar
;
import
cn.stylefeng.guns.core.common.constant.factory.ConstantFactory
;
...
...
@@ -48,13 +49,13 @@ import org.springframework.web.multipart.MultipartFile;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLDecoder
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.UUID
;
/**
* 通用控制器
...
...
@@ -203,22 +204,20 @@ public class SystemController extends BaseController {
}
/**
*
上传
头像
*
更新
头像
*
* @author fengshuonan
* @Date 2018/11/9 12:45 PM
*/
@RequestMapping
(
"/up
load
Avatar"
)
@RequestMapping
(
"/up
date
Avatar"
)
@ResponseBody
public
Object
uploadAvatar
(
@RequestParam
String
avatar
)
{
public
Object
uploadAvatar
(
@RequestParam
(
"fileId"
)
String
fileId
)
{
if
(
ToolUtil
.
isEmpty
(
avatar
))
{
if
(
ToolUtil
.
isEmpty
(
fileId
))
{
throw
new
RequestEmptyException
(
"请求头像为空"
);
}
avatar
=
avatar
.
substring
(
avatar
.
indexOf
(
","
)
+
1
);
fileInfoService
.
uploadAvatar
(
avatar
);
fileInfoService
.
updateAvatar
(
fileId
);
return
SUCCESS_TIP
;
}
...
...
@@ -303,16 +302,32 @@ public class SystemController extends BaseController {
@ResponseBody
public
ResponseData
layuiUpload
(
@RequestPart
(
"file"
)
MultipartFile
picture
)
{
String
pictureName
=
UUID
.
randomUUID
().
toString
()
+
"."
+
ToolUtil
.
getFileSuffix
(
picture
.
getOriginalFilename
());
String
fileId
=
IdWorker
.
getIdStr
();
String
pictureName
=
fileId
+
"."
+
ToolUtil
.
getFileSuffix
(
picture
.
getOriginalFilename
());
try
{
//保存文件到指定目录
String
fileSavePath
=
gunsProperties
.
getFileUploadPath
();
picture
.
transferTo
(
new
File
(
fileSavePath
+
pictureName
));
File
file
=
new
File
(
fileSavePath
+
pictureName
);
picture
.
transferTo
(
file
);
//获取文件的base64编码
byte
[]
bytes
=
IoUtil
.
readBytes
(
new
FileInputStream
(
file
));
String
encode
=
Base64
.
encode
(
bytes
);
//保存文件信息
FileInfo
fileInfo
=
new
FileInfo
();
fileInfo
.
setFileId
(
fileId
);
fileInfo
.
setFileData
(
encode
);
fileInfoService
.
save
(
fileInfo
);
}
catch
(
Exception
e
)
{
log
.
error
(
"上传文件错误!"
,
e
);
throw
new
ServiceException
(
BizExceptionEnum
.
UPLOAD_ERROR
);
}
HashMap
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"fileId"
,
IdWorker
.
getIdStr
());
map
.
put
(
"fileId"
,
fileId
);
return
ResponseData
.
success
(
0
,
"上传成功"
,
map
);
}
...
...
src/main/java/cn/stylefeng/guns/modular/system/service/FileInfoService.java
View file @
60053ef4
...
...
@@ -7,7 +7,6 @@ import cn.stylefeng.guns.modular.system.entity.User;
import
cn.stylefeng.guns.modular.system.mapper.FileInfoMapper
;
import
cn.stylefeng.roses.kernel.model.exception.ServiceException
;
import
cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum
;
import
com.baomidou.mybatisplus.core.toolkit.IdWorker
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
...
...
@@ -29,13 +28,13 @@ public class FileInfoService extends ServiceImpl<FileInfoMapper, FileInfo> {
private
UserService
userService
;
/**
*
上传
头像
*
更新
头像
*
* @author fengshuonan
* @Date 2018/11/10 4:10 PM
*/
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
up
loadAvatar
(
String
avatar
)
{
public
void
up
dateAvatar
(
String
fileId
)
{
ShiroUser
currentUser
=
ShiroKit
.
getUser
();
if
(
currentUser
==
null
)
{
throw
new
ServiceException
(
CoreExceptionEnum
.
NO_CURRENT_USER
);
...
...
@@ -43,14 +42,8 @@ public class FileInfoService extends ServiceImpl<FileInfoMapper, FileInfo> {
User
user
=
userService
.
getById
(
currentUser
.
getId
());
//保存文件信息
FileInfo
fileInfo
=
new
FileInfo
();
fileInfo
.
setFileId
(
IdWorker
.
getIdStr
());
fileInfo
.
setFileData
(
avatar
);
this
.
save
(
fileInfo
);
//更新用户的头像
user
.
setAvatar
(
fileI
nfo
.
getFileId
()
);
user
.
setAvatar
(
fileI
d
);
userService
.
updateById
(
user
);
}
}
src/main/webapp/assets/modular/frame/user_info.js
View file @
60053ef4
...
...
@@ -30,13 +30,24 @@ layui.use(['form', 'upload', 'element', 'ax', 'laydate'], function () {
});
upload
.
render
({
elem
:
'#imgHead'
,
url
:
''
,
// 上传接口
done
:
function
(
res
)
{
// 上传完毕回调
},
error
:
function
()
{
// 请求异常回调
elem
:
'#imgHead'
,
url
:
Feng
.
ctxPath
+
'/system/upload'
,
before
:
function
(
obj
)
{
obj
.
preview
(
function
(
index
,
file
,
result
)
{
$
(
'#avatarPreview'
).
attr
(
'src'
,
result
);
});
}
,
done
:
function
(
res
)
{
var
ajax
=
new
$ax
(
Feng
.
ctxPath
+
"/system/updateAvatar"
,
function
(
data
)
{
Feng
.
success
(
res
.
message
);
},
function
(
data
)
{
Feng
.
error
(
"修改失败!"
+
data
.
responseJSON
.
message
+
"!"
);
});
ajax
.
set
(
"fileId"
,
res
.
data
.
fileId
);
ajax
.
start
();
}
,
error
:
function
()
{
Feng
.
error
(
"上传头像失败!"
);
}
});
});
\ No newline at end of file
src/main/webapp/pages/modular/frame/user_info.html
View file @
60053ef4
...
...
@@ -16,7 +16,7 @@
<div
class=
"layui-card-body"
style=
"padding: 25px;"
>
<div
class=
"text-center layui-text"
>
<div
class=
"user-info-head"
id=
"imgHead"
>
<img
src=
"${avatar}"
/>
<img
id=
"avatarPreview"
src=
"${avatar}"
/>
</div>
<h2
style=
"padding-top: 20px;"
>
${name}
</h2>
<p
style=
"padding-top: 8px;"
>
${roleName!} ${deptName!}
</p>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment