TaOrderController.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.yunzhi.fifteenpuzzle.controller;
  2. import com.alipay.api.AlipayApiException;
  3. import com.alipay.api.domain.AlipayTradePagePayModel;
  4. import com.alipay.api.response.AlipayTradePagePayResponse;
  5. import com.yunzhi.fifteenpuzzle.common.*;
  6. import com.yunzhi.fifteenpuzzle.entity.TaOrder;
  7. import com.yunzhi.fifteenpuzzle.service.ITaOrderService;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.web.bind.annotation.*;
  11. import java.text.DecimalFormat;
  12. import java.util.List;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. @RestController
  16. @RequestMapping("/")
  17. public class TaOrderController extends BaseController {
  18. @Autowired
  19. ITaOrderService iTaOrderService;
  20. @Autowired
  21. AliPayUtil aliPayUtil;
  22. @Value("${yz.unitPrice}")
  23. Integer unitPrice;
  24. /**
  25. * 获取订单号
  26. */
  27. @RequestMapping(value="/order-no",method= RequestMethod.GET)
  28. public ResponseBean getOrderNo() throws Exception{
  29. return ResponseBean.success(StringUtils.uuid());
  30. }
  31. /**
  32. * 获取订单号
  33. */
  34. @RequestMapping(value="/order/{orderId}/answer",method= RequestMethod.GET)
  35. public ResponseBean getAnswer(@PathVariable String orderId) throws Exception{
  36. TaOrder taOrder = iTaOrderService.getById(orderId);
  37. String answer = null;
  38. if (null != taOrder && 1 == taOrder.getIsPaid()) {
  39. answer = taOrder.getRightAnswer();
  40. }
  41. return ResponseBean.success(answer);
  42. }
  43. /**
  44. * 提交订单
  45. * @param taOrder 实体对象
  46. * @return
  47. */
  48. @RequestMapping(value="/order-save",method= RequestMethod.POST)
  49. public ResponseBean save(@RequestBody TaOrder taOrder) throws Exception{
  50. if (StringUtils.isEmpty(taOrder.getOrderId())) {
  51. return ResponseBean.error("订单编号为空, 请刷新重试");
  52. }
  53. TaOrder origin = iTaOrderService.getById(taOrder.getOrderId());
  54. if (null != origin) {
  55. return ResponseBean.error("订单编号不正确, 请刷新重试");
  56. }
  57. if (!checkContent(taOrder.getGameContent())) {
  58. return ResponseBean.error("宫格内容错误, 请刷新重试");
  59. }
  60. // if (StringUtils.isEmpty(taOrder.getEmail())) {
  61. // return ResponseBean.error("请填写邮箱");
  62. // }
  63. //
  64. // if (!isEmail(taOrder.getEmail())) {
  65. // return ResponseBean.error("邮箱格式不正确");
  66. // }
  67. int[] f = new int[16];
  68. int space = getF(taOrder.getGameContent(), f);
  69. Ida.Puzzle pz = new Ida.Puzzle(f, space);
  70. try {
  71. Ida ida = new Ida();
  72. List<String> steps = ida.calc(pz);
  73. int charge = unitPrice == null ? 100 : unitPrice;
  74. taOrder.setCharges(charge);
  75. // taOrder.setRightAnswer(toHTML(steps));
  76. taOrder.setRightAnswer(String.join(",", steps));
  77. taOrder.setIsPaid(0);
  78. if (!iTaOrderService.save(taOrder)) {
  79. return ResponseBean.error("系统错误, 请重试");
  80. } else {
  81. AlipayTradePagePayModel model = new AlipayTradePagePayModel();
  82. model.setOutTradeNo(taOrder.getOrderId());
  83. double amount = taOrder.getCharges() / 100.00;
  84. model.setTotalAmount(new DecimalFormat("0.00").format(amount));
  85. model.setSubject("16宫格解密");
  86. model.setProductCode("FAST_INSTANT_TRADE_PAY"); // PC 场景
  87. try {
  88. AlipayTradePagePayResponse resp = aliPayUtil.pagePay(model, taOrder.getOrderId(), taOrder.getPayRtnUrl());
  89. return ResponseBean.success(resp.getBody());
  90. } catch (AlipayApiException e1) {
  91. return ResponseBean.error(e1.getMessage());
  92. }
  93. }
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. return ResponseBean.error("输入数字格式错误, 或者有限步骤内无解");
  97. }
  98. }
  99. private String toHTML(List<String> steps) {
  100. String html = "<div>";
  101. for (int i = 0; i < steps.size(); i++) {
  102. String item = steps.get(i);
  103. String part = String.format("&nbsp; %d &nbsp; <font color=\"red\"> %s </font> &nbsp;", i+1, item);
  104. if (i % 16 == 0 && i > 0) {
  105. part += "<br>";
  106. }
  107. html += part;
  108. }
  109. html += "</div>";
  110. return html;
  111. }
  112. private int getF(String content, int[] f) {
  113. int space = -1;
  114. String[] items = content.split(",");
  115. for (int i = 0; i < items.length; i ++) {
  116. String item = items[i];
  117. if (StringUtils.isEmpty(item)) {
  118. f[i] = 16;
  119. space = i;
  120. } else {
  121. f[i] = Integer.parseInt(item);
  122. }
  123. }
  124. return space;
  125. }
  126. private boolean checkContent(String content) {
  127. if (StringUtils.isEmpty(content)) return false;
  128. String[] items = content.split(",");
  129. if (items.length != 16) return false;
  130. for (String item : items) {
  131. if (null == item || item.equals("")) continue;
  132. int i = Integer.parseInt(item);
  133. if (i < 1 || i > 16) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. boolean isEmail(String email){
  140. if (null==email || "".equals(email)){
  141. return false;
  142. }
  143. String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
  144. Pattern p = Pattern.compile(regEx1);
  145. Matcher m = p.matcher(email);
  146. return m.matches();
  147. }
  148. }