一基的服务与模板重新建立一个环境

This commit is contained in:
daihh
2022-10-15 11:54:53 +08:00
commit e8cb8ff892
1902 changed files with 111094 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
package com.xboe.system.api;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.xboe.core.CurrentUser;
import com.xboe.core.IAuthorizationToken;
import com.xboe.core.JsonResponse;
import com.xboe.core.api.ApiBaseController;
/**
* 后台管理系统初始化加载数据
*
*/
@RestController
@RequestMapping(value = "/xboe/sys/console")
public class SysConsoleApi extends ApiBaseController{
@Autowired
IAuthorizationToken authorizationToken;
/**
* 信息初始化
* @return
*/
@RequestMapping(value="/init",method = {RequestMethod.GET})
public JsonResponse<Map<String,Object>> init() {
Map<String,Object> map=new HashMap<String,Object>();
//获取当前用户数据
CurrentUser cu=super.getCurrent();
map.put("msg",2);//未读消息提示是0
map.put("aid",cu.getAccountId());//
map.put("name",cu.getName());//
map.put("avatar","");//头像,如果没有就提供一个默认的
return success(map);
}
/**
* 个人中心进入后的页面所需要的公共数据,比如学时总时长
* @param uname
* @param pass
* @return
*/
@RequestMapping(value="/user",method = {RequestMethod.GET})
public JsonResponse<Map<String,Object>> userInfo() {
Map<String,Object> map=new HashMap<String,Object>();
//模拟登录生成token返回
CurrentUser cu=super.getCurrent();
return success(map);
}
}

View File

@@ -0,0 +1,219 @@
package com.xboe.system.api;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.wf.captcha.SpecCaptcha;
import com.xboe.account.entity.Account;
import com.xboe.account.service.IAccountService;
import com.xboe.common.utils.MD5Util;
import com.xboe.common.utils.StringUtil;
import com.xboe.common.utils.WebUtil;
import com.xboe.constants.CacheName;
import com.xboe.constants.Constants;
import com.xboe.core.IAuthorizationToken;
import com.xboe.core.JsonResponse;
import com.xboe.core.api.ApiBaseController;
import com.xboe.system.logs.entity.SysLogLogin;
import com.xboe.system.logs.service.ISysLogLoginService;
import com.xboe.system.user.entity.SysManager;
import com.xboe.system.user.entity.User;
import com.xboe.system.user.service.ISysManagerService;
import com.xboe.system.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
/**
* 后台管理的用户登录.
* 需要查询是否是后台管理员
*/
@Slf4j
@RestController
@RequestMapping(value = "/xboe/system")
public class SysLoginApi extends ApiBaseController {
@Autowired
IAuthorizationToken authorizationToken;
@Autowired
IAccountService accountService;
@Autowired
IUserService userService;
@Autowired
ISysManagerService sysManagerService;
@Autowired
ISysLogLoginService sysLogLoginService;
@Autowired
StringRedisTemplate redisTemplate;
@GetMapping("/captcha")
public JsonResponse<Map<String, String>> captcha() {
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
String verCode = specCaptcha.text().toLowerCase();
String key = UUID.randomUUID().toString();
// 存入redis并设置过期时间为30分钟
//bladeRedis.setEx(CacheNames.CAPTCHA_KEY + key, verCode, Duration.ofMinutes(30));
redisTemplate.opsForValue().set(CacheName.NAME_AUTH + "::" + CacheName.CAPTCHA_KEY + key, verCode, 30, TimeUnit.MINUTES);
// 将key和base64返回给前端
Map<String, String> map = new HashMap<>();
map.put("key", key);
map.put("image", specCaptcha.toBase64());
return success(map);
}
/**
* 根据用户名和密码登录
*
* @param loginName
* @param password
* @return
*/
@RequestMapping(value = "/login", method = {RequestMethod.POST})
public JsonResponse<Map<String, Object>> login(HttpServletRequest request, String loginName, String password, String code, String key) {
try {
if (StringUtil.isBlank(loginName) || StringUtil.isBlank(password) || StringUtil.isBlank(code) || StringUtil.isBlank(key)) {
return error("缺少参数");
}
String verCode = redisTemplate.opsForValue().get(CacheName.NAME_AUTH + "::" + CacheName.CAPTCHA_KEY + key);
if (!code.toLowerCase().equals(verCode)) {
return error("验证码错误");
}
// 检查系统用户是否存在
Account account = accountService.check(loginName,null);
String passStr = "";
if (account != null && StringUtil.isNotBlank(account.getPassKey())) {
passStr = MD5Util.MD5Encode(password + account.getPassKey());
}
if (account == null || StringUtil.isBlank(passStr) || !passStr.equals(account.getPassValue())) {
return error("用户名或密码错误");
}
if (account.getStatus().equals(Constants.ACCOUNT_STATUS_DEACTIVATE)) {
return error("账号已停用");
}
SysManager sm = sysManagerService.get(account.getId());
if (sm == null) {
return error("该账号不是管理员");
}
Map<String, Object> map = this.createToken(account);
// 更新登录时间和登录日志
sysManagerService.updateLastTime(sm.getId());
// 更新用户的登录信息
userService.updateLastLoginInfo(account.getId(),WebUtil.getIpAddr(request));
SysLogLogin sll = new SysLogLogin();
sll.setAid(account.getId());
sll.setName(sm.getUser().getName());
sll.setDevice(WebUtil.getClientType(request));
sll.setClientOs(WebUtil.getClientOs(request));
sll.setClientBrowser(WebUtil.getClientBrowser(request));
sll.setLogInfo("登录后台管理系统");
sll.setLoginIp(WebUtil.getIpAddr(request));
sll.setLoginTime(LocalDateTime.now());
sysLogLoginService.save(sll);
return success(map);
} catch (Exception e) {
log.error("创建jwt token 错误", e);
return error("创建token失败", e.getMessage());
}
}
/**
* 旧token换新token
*
* @param token
* @return
*/
@RequestMapping(value = "/refreshtoken", method = {RequestMethod.POST})
public JsonResponse<Map<String, Object>> refreshToken(String token) {
try {
if (StringUtil.isBlank(token)) {
return error("缺少参数");
}
Map<String, String> tokenInfo = authorizationToken.readToken(token);
if (tokenInfo == null) {
return error("登录超时,请重新登录");
}
//检查系统用户是否存在
Account account = accountService.get(tokenInfo.get("aid"));
if (account.getStatus().equals(Constants.ACCOUNT_STATUS_DEACTIVATE)) {
return error("账号已停用");
}
SysManager sm = sysManagerService.get(account.getId());
if (sm == null) {
return error("该账号不是管理员");
}
Map<String, Object> map = this.createToken(account);
return success(map);
} catch (Exception e) {
log.error("创建jwt token 错误", e);
return error("创建token失败", e.getMessage());
}
}
/**
* 生成token
*
* @param account
* @return
* @throws Exception
*/
private Map<String, Object> createToken(Account account) throws Exception {
User user = userService.get(account.getId());
//模拟登录生成token返回
Map<String, String> data = new HashMap<String, String>();
Map<String, Object> map = new HashMap<String, Object>();
//模拟数据
data.put("aid", account.getId());
data.put("name", user.getName());
data.put("userNo", user.getUserNo());
data.put("departId", user.getDepartId());
data.put("userId",user.getSysId());
String token = authorizationToken.createToken(data);
map.put("expires_in", IAuthorizationToken.TOKEN_TIMEOUT);
map.put("scope", "boe");
map.put("access_token", token);
//System.out.println(token);
return map;
}
@RequestMapping(value = "/logout", method = {RequestMethod.GET})
public JsonResponse<Boolean> logout() {
//因为当前系统并没有后端记录登录状态所以这里直接返回true,
//当前对于用户在线状态的记录不能实现,
return success(true);
}
}

View File

@@ -0,0 +1,166 @@
package com.xboe.system.api;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xboe.common.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.xboe.core.JsonResponse;
import com.xboe.core.JsonResponseStatus;
import com.xboe.core.api.ApiBaseController;
import com.xboe.core.upload.IXUploadCallback;
import com.xboe.core.upload.XFileUploader;
import com.xboe.core.upload.XUploadResult;
import com.xboe.core.upload.XUploadSavePathType;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping(value = "/xboe/sys/xuploader")
public class SysUploaderApi extends ApiBaseController{
@Autowired(required=false)
IXUploadCallback uploadService;//上传成功后的处理服务
@Autowired
XFileUploader uploader;
@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
public JsonResponse<XUploadResult> save(HttpServletRequest request, String name,String dir) throws IOException {
//以下三项用于回调
String table = request.getParameter("t");//更新的表
String field = request.getParameter("f");//更新的字段
String id = request.getParameter("id");//更新的值的id
if(StringUtils.isBlank(name)){
name="file";
}
MultipartHttpServletRequest defaultRequest = (MultipartHttpServletRequest) request;
MultipartFile file = defaultRequest.getFile(name);
if (file == null ) {
return wrap(JsonResponseStatus.BAD_REQUEST, "未找到" + name + "对应的文件");
}
String type=".exe";
if(StringUtil.isBlank(file.getOriginalFilename()) ||file.getOriginalFilename().endsWith(type.toUpperCase()) || file.getOriginalFilename().endsWith(type)){
return badRequest("不支持此格式");
}
if(StringUtils.isBlank(dir)) {
dir="";
}
try {
XUploadResult xur =uploader.uploadFile(file, dir, XUploadSavePathType.YearMonth);
if(xur==null) {
return wrap(JsonResponseStatus.INTERNAL_SERVER_ERROR, "上传失败,请联系管理员" );
}
try{
if (uploadService!=null) {
byte[] bytes=file.getBytes();
boolean flag =uploadService.execute(bytes,xur.getDisplayName(),xur.getFilePath(),table, id, field);
if(!flag){
log.error("执行上传回调处理失败");
}
}
}catch(Exception ex){
// ex.printStackTrace();
log.error("处理失败",ex);
}
return wrap(xur);
} catch (Exception e) {
log.error("upload file error ",e);
return wrap(JsonResponseStatus.INTERNAL_SERVER_ERROR, "上传失败:" + e.getMessage());
}
}
@RequestMapping(value = "/file/del", method = RequestMethod.POST)
public JsonResponse<Boolean> save(HttpServletRequest request, String file) throws IOException {
try {
uploader.delFile(file);
return wrap(true);
} catch (Exception e) {
log.error("delete file error ",e);
return wrap(JsonResponseStatus.INTERNAL_SERVER_ERROR, "删除文件失败" + e.getMessage(),false);
}
}
/**
* 通过url和文件名下载文件
* @param res
* @param urlStr
* @param fileName
* @throws IOException
*/
@RequestMapping(value = "/url/download", method = RequestMethod.GET)
public void urlDownload(HttpServletResponse res,String urlStr,String fileName) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
//byte[] getData = readInputStream(inputStream);
res.setHeader("content-type", "application/octet-stream");
res.setContentType("application/octet-stream");
res.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
res.addHeader("charset", "utf-8");
res.addHeader("Pragma", "no-cache");
String encodeName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
res.setHeader("Content-Disposition", "attachment;filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
bis = new BufferedInputStream(inputStream);
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
log.error("关流异常",e);
}
}
}
//System.out.println("success");
}
}