| | |
| | | package com.dg.core.controller; |
| | | |
| | | import cn.hutool.core.date.DateTime; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson.TypeReference; |
| | | import com.dg.core.CorsConfig; |
| | | import com.dg.core.ResultData; |
| | | import com.dg.core.db.gen.AjaxResult; |
| | | import com.dg.core.file.FtpConstants; |
| | | import com.dg.core.file.MimeTypeUtils; |
| | | import com.dg.core.file.SFTPUtil; |
| | | import com.dg.core.util.TestDate; |
| | | import com.iceyyy.workday.WorkUtils; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | |
| | |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.InputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | import static com.dg.core.file.FileTypeUploadUtils.assertAllowed; |
| | | |
| | |
| | | @RequestMapping("/common") |
| | | public class CommonController extends BaseController { |
| | | |
| | | private static final String FILE_DELIMETER = ","; |
| | | private static final String FILE_DELIMETER = ","; |
| | | |
| | | @Autowired |
| | | private CorsConfig corsConfig; |
| | | @Autowired |
| | | private CorsConfig corsConfig; |
| | | |
| | | |
| | | // FTP 登录用户名 |
| | | @Value("${ftp.username}") |
| | | private String userName; |
| | | // FTP 登录密码 |
| | | @Value("${ftp.password}") |
| | | private String password; |
| | | // FTP 服务器地址IP地址 |
| | | @Value("${ftp.host}") |
| | | private String host; |
| | | // FTP 端口 |
| | | @Value("${ftp.port}") |
| | | private int port; |
| | | @Value("${ftp.url}") |
| | | private String url; |
| | | // FTP 登录用户名 |
| | | @Value("${ftp.username}") |
| | | private String userName; |
| | | // FTP 登录密码 |
| | | @Value("${ftp.password}") |
| | | private String password; |
| | | // FTP 服务器地址IP地址 |
| | | @Value("${ftp.host}") |
| | | private String host; |
| | | // FTP 端口 |
| | | @Value("${ftp.port}") |
| | | private int port; |
| | | @Value("${ftp.url}") |
| | | private String url; |
| | | |
| | | @Resource |
| | | RedisTemplate<String, Object> redisTemplate; |
| | | |
| | | |
| | | @RequestMapping("/hello") |
| | | @RequestMapping("/hello") |
| | | // @Authorization |
| | | public ResponseEntity<String> hello() { |
| | | return ResponseEntity.ok("hello world"); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 通用上传请求(多个) |
| | | */ |
| | | @ApiOperation("文件上传接口(通用)") |
| | | @PostMapping("/uploads") |
| | | public ResultData uploadFiles(List<MultipartFile> files) throws Exception |
| | | { |
| | | List<String> urls = new ArrayList<String>(); |
| | | List<String> fileNames = new ArrayList<String>(); |
| | | List<String> originalFilenames = new ArrayList<String>(); |
| | | for (MultipartFile file : files) { |
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | String name = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension; |
| | | try { |
| | | assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | InputStream is = file.getInputStream(); |
| | | sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is); |
| | | sftp.logout(); |
| | | |
| | | urls.add(url + "/idcard/" + name); |
| | | fileNames.add(name); |
| | | originalFilenames.add(file.getOriginalFilename()); |
| | | |
| | | } catch (Exception e) { |
| | | return ResultData.error("上传文件失败 "+e.getMessage()); |
| | | } |
| | | public ResponseEntity<String> hello() { |
| | | return ResponseEntity.ok("hello world"); |
| | | } |
| | | AjaxResult ajax = new AjaxResult(); |
| | | ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER)); |
| | | ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER)); |
| | | ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); |
| | | return ResultData.success(ajax); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 通用上传请求(多个) |
| | | */ |
| | | @ApiOperation("文件上传接口(通用)") |
| | | @PostMapping("/uploads") |
| | | public ResultData uploadFiles(List<MultipartFile> files) throws Exception { |
| | | List<String> urls = new ArrayList<String>(); |
| | | List<String> fileNames = new ArrayList<String>(); |
| | | List<String> originalFilenames = new ArrayList<String>(); |
| | | for (MultipartFile file : files) { |
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | String name = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension; |
| | | try { |
| | | assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | InputStream is = file.getInputStream(); |
| | | sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is); |
| | | sftp.logout(); |
| | | |
| | | urls.add(url + "/idcard/" + name); |
| | | fileNames.add(name); |
| | | originalFilenames.add(file.getOriginalFilename()); |
| | | |
| | | } catch (Exception e) { |
| | | return ResultData.error("上传文件失败 " + e.getMessage()); |
| | | } |
| | | } |
| | | AjaxResult ajax = new AjaxResult(); |
| | | ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER)); |
| | | ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER)); |
| | | ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); |
| | | return ResultData.success(ajax); |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 通用上传请求(多个) |
| | | */ |
| | | @ApiOperation("文件上传接口(通用)") |
| | | @PostMapping("/upload") |
| | | public ResultData uploadFile(MultipartFile file) throws Exception |
| | | { |
| | | List<String> urls = new ArrayList<String>(); |
| | | List<String> fileNames = new ArrayList<String>(); |
| | | List<String> originalFilenames = new ArrayList<String>(); |
| | | /** |
| | | * 通用上传请求(单个) |
| | | */ |
| | | @ApiOperation("文件上传接口(通用)") |
| | | @PostMapping("/upload") |
| | | public ResultData uploadFile(MultipartFile file) throws Exception { |
| | | List<String> urls = new ArrayList<String>(); |
| | | List<String> fileNames = new ArrayList<String>(); |
| | | List<String> originalFilenames = new ArrayList<String>(); |
| | | |
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | String name = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension; |
| | | try { |
| | | assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | InputStream is = file.getInputStream(); |
| | | sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is); |
| | | sftp.logout(); |
| | | String extension = FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | String name = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension; |
| | | try { |
| | | assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); |
| | | SFTPUtil sftp = new SFTPUtil(userName, password, host, port); |
| | | sftp.login(); |
| | | InputStream is = file.getInputStream(); |
| | | sftp.uploadMore(FtpConstants.FTPFILEPATH_IDCARD, name, is); |
| | | sftp.logout(); |
| | | |
| | | // urls.add(url + "/idcard/" + name); |
| | | // fileNames.add(name); |
| | | // originalFilenames.add(file.getOriginalFilename()); |
| | | return ResultData.success(url + "/idcard/" + name); |
| | | } catch (Exception e) { |
| | | return ResultData.error("上传文件失败 "+e.getMessage()); |
| | | } |
| | | |
| | | return ResultData.success(url + "/idcard/" + name); |
| | | } catch (Exception e) { |
| | | return ResultData.error("上传文件失败 " + e.getMessage()); |
| | | } |
| | | |
| | | // AjaxResult ajax = new AjaxResult(); |
| | | // ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER)); |
| | | // ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER)); |
| | | // ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); |
| | | } |
| | | |
| | | /** |
| | | * 判断当前时间是否是工作日,格式为“2022-10-16”,不传默认为今天 |
| | | */ |
| | | @ApiOperation("判断当前时间是否是工作日,格式为“20221016”,不传默认为今天") |
| | | @GetMapping("/isWeekday") |
| | | public ResultData isWeekday(@RequestParam(value = "dateTime", required = false) String dateTime) { |
| | | if (dateTime == null) { |
| | | Date date = new Date(); |
| | | SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd"); |
| | | dateTime = fmt.format(date); |
| | | } |
| | | if (WorkUtils.isWorkendDay(dateTime)) |
| | | return success("休息日或者周末"); |
| | | else |
| | | return success("工作日"); |
| | | } |
| | | |
| | | } |
| | | /** |
| | | * 获取天气默认为攀枝花 |
| | | */ |
| | | @ApiOperation("获取天气默认为攀枝花") |
| | | @GetMapping("/getWeather") |
| | | public ResultData getWeather(@RequestParam(value = "code", required = false) String code) { |
| | | TestDate td = new TestDate(); |
| | | Map weather; |
| | | if (code == null) {//默认为攀枝花 则从redis上获取redis上定时任务15分钟获取一次天气 |
| | | HashMap weather1 = (HashMap) redisTemplate.opsForValue().get("weather"); |
| | | return ResultData.success(weather1.get("lives")); |
| | | } else { |
| | | return ResultData.success(td.getWeather(code)); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |