Commit f0211db0 authored by dqjdda's avatar dqjdda
Browse files

优化代码

parent a8230bbd
......@@ -2,30 +2,23 @@ package me.zhengjie.utils;
import me.zhengjie.exception.BadRequestException;
import org.hibernate.exception.ConstraintViolationException;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* 异常工具
* 异常工具 2019-01-06
* @author Zheng Jie
* @date 2019-01-06
*/
public class ThrowableUtil {
/**
* 获取堆栈信息
* @param throwable
* @return
*/
public static String getStackTrace(Throwable throwable){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try {
try (PrintWriter pw = new PrintWriter(sw)) {
throwable.printStackTrace(pw);
return sw.toString();
} finally {
pw.close();
}
}
......@@ -34,9 +27,10 @@ public class ThrowableUtil {
while ((t != null) && !(t instanceof ConstraintViolationException)) {
t = t.getCause();
}
if (t instanceof ConstraintViolationException) {
if (t != null) {
throw new BadRequestException(msg);
}
assert false;
throw new BadRequestException("删除失败:" + t.getMessage());
}
}
package me.zhengjie.utils;
import cn.hutool.json.JSONArray;
import lombok.var;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
......@@ -26,7 +24,7 @@ public class TranslatorUtil {
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
......@@ -39,14 +37,11 @@ public class TranslatorUtil {
}
private static String parseResult(String inputJson) throws Exception {
JSONArray jsonArray = new JSONArray(inputJson);
JSONArray jsonArray2 = (JSONArray) jsonArray.get(0);
String result ="";
for(var i = 0; i < jsonArray2.size(); i ++){
result += ((JSONArray) jsonArray2.get(i)).get(0).toString();
JSONArray jsonArray2 = (JSONArray) new JSONArray(inputJson).get(0);
StringBuilder result = new StringBuilder();
for (Object o : jsonArray2) {
result.append(((JSONArray) o).get(0).toString());
}
return result;
return result.toString();
}
}
......@@ -12,7 +12,6 @@ public class ValidationUtil{
/**
* 验证空
* @param optional
*/
public static void isNull(Optional optional, String entity,String parameter , Object value){
if(!optional.isPresent()){
......@@ -25,14 +24,12 @@ public class ValidationUtil{
/**
* 验证是否为邮箱
* @param string
* @return
*/
public static boolean isEmail(String string) {
if (string == null){
return false;
}
String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
String regEx1 = "^([a-z0-9A-Z]+[-|.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
return string.matches(regEx1);
}
}
......@@ -22,7 +22,7 @@ spring:
redis:
#数据库索引
database: 0
database: 1
host: 127.0.0.1
port: 6379
password:
......
......@@ -11,6 +11,7 @@ public class EladminSystemApplicationTests {
@Test
public void contextLoads() {
}
}
......
......@@ -18,66 +18,46 @@ public class AlipayConfig implements Serializable {
@Id
private Long id;
/**
* 应用ID,APPID,收款账号既是APPID对应支付宝账号
*/
// 应用ID,APPID,收款账号既是APPID对应支付宝账号
@NotBlank
@Column(name = "app_id")
private String appID;
/**
* 商户私钥,您的PKCS8格式RSA2私钥
*/
// 商户私钥,您的PKCS8格式RSA2私钥
@NotBlank
@Column(name = "private_key", columnDefinition = "text")
private String privateKey;
/**
* 支付宝公钥
*/
// 支付宝公钥
@NotBlank
@Column(name = "public_key", columnDefinition = "text")
private String publicKey;
/**
* 签名方式,固定格式
*/
// 签名方式,固定格式
@Column(name = "sign_type")
private String signType="RSA2";
/**
* 支付宝开放安全地址,一般不会变
*/
// 支付宝开放安全地址,一般不会变
@Column(name = "gateway_url")
private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
/**
* 编码,固定格式
*/
// 编码,固定格式
private String charset= "utf-8";
/**
* 异步通知地址
*/
// 异步通知地址
@NotBlank
@Column(name = "notify_url")
private String notifyUrl;
/**
* 订单完成后返回的页面
*/
// 订单完成后返回的页面
@NotBlank
@Column(name = "return_url")
private String returnUrl;
/**
* 类型,固定格式
*/
// 类型,固定格式
private String format="JSON";
/**
* 商户号
*/
// 商户号
@NotBlank
@Column(name = "sys_service_provider_id")
private String sysServiceProviderId;
......
......@@ -19,30 +19,22 @@ public class EmailConfig implements Serializable {
@Id
private Long id;
/**
*邮件服务器SMTP地址
*/
// 邮件服务器SMTP地址
@NotBlank
private String host;
/**
* 邮件服务器SMTP端口
*/
// 邮件服务器SMTP端口
@NotBlank
private String port;
/**
* 发件者用户名,默认为发件人邮箱前缀
*/
// 发件者用户名,默认为发件人邮箱前缀
@NotBlank
private String user;
@NotBlank
private String pass;
/**
* 收件人
*/
// 收件人
@NotBlank
@Column(name = "from_user")
private String fromUser;
......
......@@ -32,9 +32,6 @@ public class Picture implements Serializable {
private String width;
/**
* delete URl
*/
@Column(name = "delete_url")
private String delete;
......
......@@ -18,23 +18,17 @@ public class QiniuConfig implements Serializable {
@Id
private Long id;
/**
* 一个账号最多拥有两对密钥(Access/Secret Key)
*/
// 一个账号最多拥有两对密钥(Access/Secret Key)
@NotBlank
@Column(name = "access_key", columnDefinition = "text")
private String accessKey;
/**
* 一个账号最多拥有两对密钥(Access/Secret Key)
*/
// 一个账号最多拥有两对密钥(Access/Secret Key)
@NotBlank
@Column(name = "secret_key", columnDefinition = "text")
private String secretKey;
/**
* 存储空间名称作为唯一的 Bucket 识别符
*/
// 存储空间名称作为唯一的 Bucket 识别符
@NotBlank
private String bucket;
......@@ -49,14 +43,10 @@ public class QiniuConfig implements Serializable {
@NotBlank
private String zone;
/**
* 外链域名,可自定义,需在七牛云绑定
*/
// 外链域名,可自定义,需在七牛云绑定
@NotBlank
private String host;
/**
* 空间类型:公开/私有
*/
// 空间类型:公开/私有
private String type = "公开";
}
......@@ -21,37 +21,25 @@ public class QiniuContent implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* 文件名,如qiniu.jpg
*/
// 文件名
@Column(name = "name",unique = false)
private String key;
/**
* 空间名
*/
// 空间名
private String bucket;
/**
* 大小
*/
// 大小
private String size;
/**
* 文件地址
*/
// 文件地址
private String url;
private String suffix;
/**
* 空间类型:公开/私有
*/
// 空间类型:公开/私有
private String type = "公开";
/**
* 更新时间
*/
// 更新时间
@UpdateTimestamp
@Column(name = "update_time")
private Timestamp updateTime;
......
......@@ -26,32 +26,21 @@ public class VerificationCode {
private String code;
/**
* 使用场景,自己定义
*/
// 使用场景,自己定义
private String scenes;
/**
* true 为有效,false 为无效,验证时状态+时间+具体的邮箱或者手机号
*/
// true 为有效,false 为无效,验证时状态+时间+具体的邮箱或者手机号
private Boolean status = true;
/**
* 类型 :phone 和 email
*/
// 类型 :phone 和 email
@NotBlank
private String type;
/**
* 具体的phone与email
*/
// 具体的phone与email
@NotBlank
private String value;
/**
* 创建日期
*/
// 创建日期
@CreationTimestamp
@Column(name = "create_time")
private Timestamp createTime;
......
......@@ -18,9 +18,7 @@ import java.util.List;
@NoArgsConstructor
public class EmailVo {
/**
* 收件人,支持多个收件人,用逗号分隔
*/
// 收件人,支持多个收件人,用逗号分隔
@NotEmpty
private List<String> tos;
......
......@@ -15,51 +15,35 @@ import java.sql.Timestamp;
@Data
public class TradeVo {
/**
* (必填)商品描述
*/
// (必填)商品描述
@NotBlank
private String body;
/**
* (必填)商品名称
*/
// (必填)商品名称
@NotBlank
private String subject;
/**
* (必填)商户订单号,应该由后台生成
*/
// (必填)商户订单号,应该由后台生成
@ApiModelProperty(hidden = true)
private String outTradeNo;
/**
* (必填)第三方订单号
*/
// (必填)第三方订单号
@ApiModelProperty(hidden = true)
private String tradeNo;
/**
* (必填)价格
*/
// (必填)价格
@NotBlank
private String totalAmount;
/**
* 订单状态,已支付,未支付,作废
*/
// 订单状态,已支付,未支付,作废
@ApiModelProperty(hidden = true)
private String state;
/**
* 创建时间,存入数据库时需要
*/
// 创建时间,存入数据库时需要
@ApiModelProperty(hidden = true)
private Timestamp createTime;
/**
* 作废时间,存入数据库时需要
*/
// 作废时间,存入数据库时需要
@ApiModelProperty(hidden = true)
private Date cancelTime;
}
......@@ -8,5 +8,5 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* @author Zheng Jie
* @date 2019-09-05
*/
public interface LocalStorageRepository extends JpaRepository<LocalStorage, Long>, JpaSpecificationExecutor {
public interface LocalStorageRepository extends JpaRepository<LocalStorage, Long>, JpaSpecificationExecutor<LocalStorage> {
}
\ No newline at end of file
......@@ -8,5 +8,5 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* @author Zheng Jie
* @date 2018-12-27
*/
public interface PictureRepository extends JpaRepository<Picture,Long>, JpaSpecificationExecutor {
public interface PictureRepository extends JpaRepository<Picture,Long>, JpaSpecificationExecutor<Picture> {
}
......@@ -12,6 +12,6 @@ import org.springframework.data.jpa.repository.Query;
public interface QiNiuConfigRepository extends JpaRepository<QiniuConfig,Long> {
@Modifying
@Query(value = "update qiniu_content set type = ?1", nativeQuery = true)
@Query(value = "update QiniuConfig set type = ?1")
void update(String type);
}
......@@ -8,12 +8,12 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* @author Zheng Jie
* @date 2018-12-31
*/
public interface QiniuContentRepository extends JpaRepository<QiniuContent,Long>, JpaSpecificationExecutor {
public interface QiniuContentRepository extends JpaRepository<QiniuContent,Long>, JpaSpecificationExecutor<QiniuContent> {
/**
* 根据key查询
* @param key
* @return
* @param key 文件名
* @return QiniuContent
*/
QiniuContent findByKey(String key);
}
package me.zhengjie.rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.aop.log.Log;
......@@ -16,6 +17,7 @@ import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
......@@ -24,22 +26,26 @@ import java.util.Map;
*/
@Slf4j
@RestController
@RequestMapping("/api")
@RequestMapping("/api/aliPay")
@Api(tags = "支付宝支付管理")
public class AliPayController {
@Autowired
AlipayUtils alipayUtils;
private final AlipayUtils alipayUtils;
@Autowired
private AlipayService alipayService;
private final AlipayService alipayService;
@GetMapping(value = "/aliPay")
public AliPayController(AlipayUtils alipayUtils, AlipayService alipayService) {
this.alipayUtils = alipayUtils;
this.alipayService = alipayService;
}
@GetMapping
public ResponseEntity<AlipayConfig> get(){
return new ResponseEntity<>(alipayService.find(),HttpStatus.OK);
}
@Log("配置支付宝")
@PutMapping(value = "/aliPay")
@PutMapping
public ResponseEntity payConfig(@Validated @RequestBody AlipayConfig alipayConfig){
alipayConfig.setId(1L);
alipayService.update(alipayConfig);
......@@ -48,7 +54,7 @@ public class AliPayController {
@Log("支付宝PC网页支付")
@ApiOperation(value = "PC网页支付")
@PostMapping(value = "/aliPay/toPayAsPC")
@PostMapping(value = "/toPayAsPC")
public ResponseEntity<String> toPayAsPC(@Validated@RequestBody TradeVo trade) throws Exception{
AlipayConfig alipay = alipayService.find();
trade.setOutTradeNo(alipayUtils.getOrderCode());
......@@ -58,7 +64,7 @@ public class AliPayController {
@Log("支付宝手机网页支付")
@ApiOperation(value = "手机网页支付")
@PostMapping(value = "/aliPay/toPayAsWeb")
@PostMapping(value = "/toPayAsWeb")
public ResponseEntity<String> toPayAsWeb(@Validated @RequestBody TradeVo trade) throws Exception{
AlipayConfig alipay = alipayService.find();
trade.setOutTradeNo(alipayUtils.getOrderCode());
......@@ -67,7 +73,7 @@ public class AliPayController {
}
@ApiIgnore
@GetMapping("/aliPay/return")
@GetMapping("/return")
@ApiOperation(value = "支付之后跳转的链接")
public ResponseEntity<String> returnPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
AlipayConfig alipay = alipayService.find();
......@@ -75,47 +81,39 @@ public class AliPayController {
//内容验签,防止黑客篡改参数
if(alipayUtils.rsaCheck(request,alipay)){
//商户订单号
String outTradeNo = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
String outTradeNo = new String(request.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//支付宝交易号
String tradeNo = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
String tradeNo = new String(request.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
System.out.println("商户订单号"+outTradeNo+" "+"第三方交易号"+tradeNo);
/**
* 根据业务需要返回数据,这里统一返回OK
*/
// 根据业务需要返回数据,这里统一返回OK
return new ResponseEntity<>("payment successful",HttpStatus.OK);
}else{
/**
* 根据业务需要返回数据
*/
// 根据业务需要返回数据
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
@ApiIgnore
@RequestMapping("/aliPay/notify")
@RequestMapping("/notify")
@ApiOperation(value = "支付异步通知(要公网访问),接收异步通知,检查通知内容app_id、out_trade_no、total_amount是否与请求中的一致,根据trade_status进行后续业务处理")
public ResponseEntity notify(HttpServletRequest request) throws Exception{
AlipayConfig alipay = alipayService.find();
Map<String, String[]> parameterMap = request.getParameterMap();
StringBuilder notifyBuild = new StringBuilder("/****************************** pay notify ******************************/\n");
parameterMap.forEach((key, value) -> notifyBuild.append(key + "=" + value[0] + "\n") );
//内容验签,防止黑客篡改参数
if (alipayUtils.rsaCheck(request,alipay)) {
//交易状态
String tradeStatus = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
String tradeStatus = new String(request.getParameter("trade_status").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
// 商户订单号
String outTradeNo = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
String outTradeNo = new String(request.getParameter("out_trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//支付宝交易号
String tradeNo = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
String tradeNo = new String(request.getParameter("trade_no").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//付款金额
String totalAmount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"),"UTF-8");
String totalAmount = new String(request.getParameter("total_amount").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
//验证
if(tradeStatus.equals(AliPayStatusEnum.SUCCESS.getValue())||tradeStatus.equals(AliPayStatusEnum.FINISHED.getValue())){
/**
* 验证通过后应该根据业务需要处理订单
*/
}
// if(tradeStatus.equals(AliPayStatusEnum.SUCCESS.getValue())||tradeStatus.equals(AliPayStatusEnum.FINISHED.getValue())){
// // 验证通过后应该根据业务需要处理订单
// }
return new ResponseEntity(HttpStatus.OK);
}
return new ResponseEntity(HttpStatus.BAD_REQUEST);
......
package me.zhengjie.rest;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.aop.log.Log;
import me.zhengjie.domain.EmailConfig;
import me.zhengjie.domain.vo.EmailVo;
import me.zhengjie.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
......@@ -16,30 +16,34 @@ import org.springframework.web.bind.annotation.*;
* @author 郑杰
* @date 2018/09/28 6:55:53
*/
@Slf4j
@RestController
@RequestMapping("api")
@RequestMapping("api/email")
@Api(tags = "邮件管理")
public class EmailController {
@Autowired
private EmailService emailService;
private final EmailService emailService;
@GetMapping(value = "/email")
public EmailController(EmailService emailService) {
this.emailService = emailService;
}
@GetMapping
public ResponseEntity get(){
return new ResponseEntity(emailService.find(),HttpStatus.OK);
return new ResponseEntity<>(emailService.find(),HttpStatus.OK);
}
@Log("配置邮件")
@PutMapping(value = "/email")
@PutMapping
@ApiOperation(value = "配置邮件")
public ResponseEntity emailConfig(@Validated @RequestBody EmailConfig emailConfig){
emailService.update(emailConfig,emailService.find());
return new ResponseEntity(HttpStatus.OK);
}
@Log("发送邮件")
@PostMapping(value = "/email")
@PostMapping
@ApiOperation(value = "发送邮件")
public ResponseEntity send(@Validated @RequestBody EmailVo emailVo) throws Exception {
log.warn("REST request to send Email : {}" +emailVo);
emailService.send(emailVo,emailService.find());
return new ResponseEntity(HttpStatus.OK);
}
......
......@@ -4,7 +4,6 @@ import me.zhengjie.aop.log.Log;
import me.zhengjie.domain.LocalStorage;
import me.zhengjie.service.LocalStorageService;
import me.zhengjie.service.dto.LocalStorageQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -20,28 +19,31 @@ import org.springframework.web.multipart.MultipartFile;
*/
@Api(tags = "本地存储管理")
@RestController
@RequestMapping("api")
@RequestMapping("/api/localStorage")
public class LocalStorageController {
@Autowired
private LocalStorageService localStorageService;
private final LocalStorageService localStorageService;
public LocalStorageController(LocalStorageService localStorageService) {
this.localStorageService = localStorageService;
}
@ApiOperation(value = "查询文件")
@GetMapping(value = "/localStorage")
@GetMapping
@PreAuthorize("hasAnyRole('ADMIN','LOCALSTORAGE_ALL','LOCALSTORAGE_SELECT')")
public ResponseEntity getLocalStorages(LocalStorageQueryCriteria criteria, Pageable pageable){
return new ResponseEntity(localStorageService.queryAll(criteria,pageable),HttpStatus.OK);
return new ResponseEntity<>(localStorageService.queryAll(criteria,pageable),HttpStatus.OK);
}
@ApiOperation(value = "上传文件")
@PostMapping(value = "/localStorage")
@PostMapping
@PreAuthorize("hasAnyRole('ADMIN','LOCALSTORAGE_ALL','LOCALSTORAGE_CREATE')")
public ResponseEntity create(@RequestParam String name, @RequestParam("file") MultipartFile file){
return new ResponseEntity(localStorageService.create(name, file),HttpStatus.CREATED);
return new ResponseEntity<>(localStorageService.create(name, file),HttpStatus.CREATED);
}
@ApiOperation(value = "修改文件")
@PutMapping(value = "/localStorage")
@PutMapping
@PreAuthorize("hasAnyRole('ADMIN','LOCALSTORAGE_ALL','LOCALSTORAGE_EDIT')")
public ResponseEntity update(@Validated @RequestBody LocalStorage resources){
localStorageService.update(resources);
......@@ -49,20 +51,16 @@ public class LocalStorageController {
}
@ApiOperation(value = "删除文件")
@DeleteMapping(value = "/localStorage/{id}")
@DeleteMapping(value = "/{id}")
@PreAuthorize("hasAnyRole('ADMIN','LOCALSTORAGE_ALL','LOCALSTORAGE_DELETE')")
public ResponseEntity delete(@PathVariable Long id){
localStorageService.delete(id);
return new ResponseEntity(HttpStatus.OK);
}
/**
* 删除多张图片
* @param ids
* @return
*/
@Log("删除图片")
@DeleteMapping(value = "/localStorage")
@Log("多选删除")
@DeleteMapping
@ApiOperation(value = "多选删除")
public ResponseEntity deleteAll(@RequestBody Long[] ids) {
localStorageService.deleteAll(ids);
return new ResponseEntity(HttpStatus.OK);
......
Supports Markdown
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