Commit 7d2a5e66 by fengshuonan

更新头像相关的配置

parent 01a6325f
......@@ -373,6 +373,12 @@ INSERT INTO `sys_user` VALUES ('44', null, 'test', '45abb7879f6a8268f1ef600e6038
INSERT INTO `sys_user` VALUES ('45', null, 'boss', '71887a5ad666a18f709e1d4e693d5a35', '1f7bf', '老板', '2017-12-04 00:00:00', '1', '', '', '1', '24', '1', '2017-12-04 22:24:02', null);
INSERT INTO `sys_user` VALUES ('46', null, 'manager', 'b53cac62e7175637d4beb3b16b2f7915', 'j3cs9', '经理', '2017-12-04 00:00:00', '1', '', '', '1', '24', '1', '2017-12-04 22:24:24', null);
DROP TABLE IF EXISTS `sys_file_info`;
CREATE TABLE `sys_file_info` (
`id` varchar(50) NOT NULL COMMENT '主键id',
`file_data` text COMMENT 'base64编码的文件',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='文件信息表\n';
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
......
......@@ -18,7 +18,6 @@ package cn.stylefeng.guns.core.interceptor;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.core.util.DefaultImages;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
......@@ -55,13 +54,7 @@ public class AttributeSetInteceptor extends HandlerInterceptorAdapter {
} else {
modelAndView.addObject("menus", user.getMenus());
modelAndView.addObject("name", user.getName());
if (ToolUtil.isEmpty(user.getAvatar())) {
modelAndView.addObject("avatar", DefaultImages.defaultAvatar());
} else {
modelAndView.addObject("avatar", user.getAvatar());
}
modelAndView.addObject("avatar", DefaultImages.defaultAvatarUrl());
modelAndView.addObject("email", user.getEmail());
}
}
......
package cn.stylefeng.guns.core.util;
import cn.stylefeng.guns.core.common.constant.DefaultAvatar;
import cn.stylefeng.guns.core.listener.ConfigListener;
/**
......@@ -27,8 +26,8 @@ public class DefaultImages {
* @author fengshuonan
* @Date 2018/10/30 5:51 PM
*/
public static String defaultAvatar() {
return DefaultAvatar.BASE_64_AVATAR;
public static String defaultAvatarUrl() {
return ConfigListener.getConf().get("contextPath") + "/system/previewAvatar";
}
/**
......
......@@ -15,15 +15,20 @@
*/
package cn.stylefeng.guns.modular.system.controller;
import cn.hutool.core.codec.Base64;
import cn.stylefeng.guns.core.common.constant.DefaultAvatar;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.modular.system.model.FileInfo;
import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.guns.modular.system.service.IFileInfoService;
import cn.stylefeng.guns.modular.system.service.IUserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
......@@ -31,6 +36,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
......@@ -42,6 +49,7 @@ import java.net.URLDecoder;
*/
@Controller
@RequestMapping("/system")
@Slf4j
public class SystemController extends BaseController {
private String PREFIX = "/common/";
......@@ -49,6 +57,9 @@ public class SystemController extends BaseController {
@Autowired
private IUserService userService;
@Autowired
private IFileInfoService fileInfoService;
/**
* 通用的树列表选择器
*/
......@@ -86,16 +97,49 @@ public class SystemController extends BaseController {
throw new RequestEmptyException("请求头像为空");
}
avatar = avatar.substring(avatar.indexOf(",") + 1);
fileInfoService.uploadAvatar(avatar);
return SUCCESS_TIP;
}
/**
* 预览头像
*
* @author fengshuonan
* @Date 2018/11/9 12:45 PM
*/
@RequestMapping("/previewAvatar")
@ResponseBody
public Object previewAvatar(HttpServletResponse response) {
ShiroUser currentUser = ShiroKit.getUser();
if (currentUser == null) {
throw new ServiceException(CoreExceptionEnum.NO_CURRENT_USER);
}
User user = userService.selectById(currentUser.getId());
user.setAvatar(avatar);
userService.updateById(user);
return SUCCESS_TIP;
String avatar = user.getAvatar();
if (ToolUtil.isEmpty(avatar)) {
avatar = DefaultAvatar.BASE_64_AVATAR;
} else {
FileInfo fileInfo = fileInfoService.selectById(avatar);
avatar = fileInfo.getFileData();
}
//输出图片的文件流
try {
response.setContentType("image/jpeg");
byte[] decode = Base64.decode(avatar);
response.getOutputStream().write(decode);
} catch (IOException e) {
log.error("获取图片的流错误!", avatar);
throw new ServiceException(CoreExceptionEnum.SERVICE_ERROR);
}
return null;
}
}
/**
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.stylefeng.guns.modular.system.dao;
import cn.stylefeng.guns.modular.system.model.FileInfo;
import com.baomidou.mybatisplus.mapper.BaseMapper;
/**
* <p>
* 文件信息 Mapper 接口
* </p>
*
* @author stylefeng
* @since 2017-07-11
*/
public interface FileInfoMapper extends BaseMapper<FileInfo> {
}
\ No newline at end of file
package cn.stylefeng.guns.modular.system.model;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import lombok.Data;
import java.io.Serializable;
/**
* <p>
* 字典表
* </p>
*
* @author stylefeng
* @since 2017-07-11
*/
@TableName("sys_file_info")
@Data
public class FileInfo extends Model<FileInfo> {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
@TableId(value = "id")
private String id;
@TableField("file_data")
private String fileData;
@Override
protected Serializable pkVal() {
return id;
}
}
/**
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.stylefeng.guns.modular.system.service;
import cn.stylefeng.guns.modular.system.model.FileInfo;
import com.baomidou.mybatisplus.service.IService;
/**
* <p>
* 文件信息 服务类
* </p>
*
* @author stylefeng123
* @since 2018-02-22
*/
public interface IFileInfoService extends IService<FileInfo> {
/**
* 上传头像
*
* @author fengshuonan
* @Date 2018/11/10 4:10 PM
*/
void uploadAvatar(String avatar);
}
/**
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.stylefeng.guns.modular.system.service.impl;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.modular.system.dao.FileInfoMapper;
import cn.stylefeng.guns.modular.system.model.FileInfo;
import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.guns.modular.system.service.IFileInfoService;
import cn.stylefeng.guns.modular.system.service.IUserService;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* <p>
* 文件信息 服务实现类
* </p>
*
* @author stylefeng123
* @since 2018-02-22
*/
@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements IFileInfoService {
@Autowired
private IUserService userService;
@Override
@Transactional(rollbackFor = Exception.class)
public void uploadAvatar(String avatar) {
ShiroUser currentUser = ShiroKit.getUser();
if (currentUser == null) {
throw new ServiceException(CoreExceptionEnum.NO_CURRENT_USER);
}
User user = userService.selectById(currentUser.getId());
//保存文件信息
FileInfo fileInfo = new FileInfo();
fileInfo.setId(IdWorker.getIdStr());
fileInfo.setFileData(avatar);
this.insert(fileInfo);
//更新用户的头像
user.setAvatar(fileInfo.getId());
userService.updateById(user);
}
}
server:
port: 8080
tomcat:
max-http-header-size: 10000 #单位:字节
max-http-header-size: 10240 #单位:字节
guns:
swagger-open: true #是否开启swagger (true/false)
kaptcha-open: false #是否开启登录时验证码 (true/false)
# file-upload-path: d:/tmp #文件上传目录(不配置的话为java.io.tmpdir目录)
spring-session-open: false #是否开启spring session,如果是多机环境需要开启(true/false)
session-invalidate-time: 1800 #session失效时间(只在单机环境下生效,多机环境在SpringSessionConfig类中配置) 单位:秒
session-validation-interval: 900 #多久检测一次失效的session(只在单机环境下生效) 单位:秒
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment