123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package com.yunzhi.fifteenpuzzle.controller;
-
- import com.alipay.api.AlipayApiException;
- import com.alipay.api.domain.AlipayTradePagePayModel;
- import com.alipay.api.response.AlipayTradePagePayResponse;
- import com.yunzhi.fifteenpuzzle.common.*;
- import com.yunzhi.fifteenpuzzle.entity.TaOrder;
- import com.yunzhi.fifteenpuzzle.service.ITaOrderService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.*;
-
- import java.text.DecimalFormat;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- @RestController
- @RequestMapping("/")
- public class TaOrderController extends BaseController {
-
- @Autowired
- ITaOrderService iTaOrderService;
-
- @Autowired
- AliPayUtil aliPayUtil;
-
- @Value("${yz.unitPrice}")
- Integer unitPrice;
-
- /**
- * 获取订单号
- */
- @RequestMapping(value="/order-no",method= RequestMethod.GET)
- public ResponseBean getOrderNo() throws Exception{
- return ResponseBean.success(StringUtils.uuid());
- }
-
- /**
- * 获取订单号
- */
- @RequestMapping(value="/order/{orderId}/answer",method= RequestMethod.GET)
- public ResponseBean getAnswer(@PathVariable String orderId) throws Exception{
- TaOrder taOrder = iTaOrderService.getById(orderId);
- String answer = null;
- if (null != taOrder && 1 == taOrder.getIsPaid()) {
- answer = taOrder.getRightAnswer();
- }
-
- return ResponseBean.success(answer);
- }
-
- /**
- * 提交订单
- * @param taOrder 实体对象
- * @return
- */
- @RequestMapping(value="/order-save",method= RequestMethod.POST)
- public ResponseBean save(@RequestBody TaOrder taOrder) throws Exception{
- if (StringUtils.isEmpty(taOrder.getOrderId())) {
- return ResponseBean.error("订单编号为空, 请刷新重试");
- }
-
- TaOrder origin = iTaOrderService.getById(taOrder.getOrderId());
- if (null != origin) {
- return ResponseBean.error("订单编号不正确, 请刷新重试");
- }
-
- if (!checkContent(taOrder.getGameContent())) {
- return ResponseBean.error("宫格内容错误, 请刷新重试");
- }
-
- // if (StringUtils.isEmpty(taOrder.getEmail())) {
- // return ResponseBean.error("请填写邮箱");
- // }
- //
- // if (!isEmail(taOrder.getEmail())) {
- // return ResponseBean.error("邮箱格式不正确");
- // }
-
- int[] f = new int[16];
- int space = getF(taOrder.getGameContent(), f);
-
- Ida.Puzzle pz = new Ida.Puzzle(f, space);
-
- try {
- Ida ida = new Ida();
- List<String> steps = ida.calc(pz);
-
- int charge = unitPrice == null ? 100 : unitPrice;
-
- taOrder.setCharges(charge);
- // taOrder.setRightAnswer(toHTML(steps));
- taOrder.setRightAnswer(String.join(",", steps));
- taOrder.setIsPaid(0);
-
- if (!iTaOrderService.save(taOrder)) {
- return ResponseBean.error("系统错误, 请重试");
- } else {
- AlipayTradePagePayModel model = new AlipayTradePagePayModel();
- model.setOutTradeNo(taOrder.getOrderId());
- double amount = taOrder.getCharges() / 100.00;
- model.setTotalAmount(new DecimalFormat("0.00").format(amount));
- model.setSubject("16宫格解密");
- model.setProductCode("FAST_INSTANT_TRADE_PAY"); // PC 场景
-
- try {
- AlipayTradePagePayResponse resp = aliPayUtil.pagePay(model, taOrder.getOrderId(), taOrder.getPayRtnUrl());
- return ResponseBean.success(resp.getBody());
- } catch (AlipayApiException e1) {
- return ResponseBean.error(e1.getMessage());
- }
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- return ResponseBean.error("输入数字格式错误, 或者有限步骤内无解");
- }
- }
-
- private String toHTML(List<String> steps) {
- String html = "<div>";
- for (int i = 0; i < steps.size(); i++) {
- String item = steps.get(i);
- String part = String.format(" %d <font color=\"red\"> %s </font> ", i+1, item);
- if (i % 16 == 0 && i > 0) {
- part += "<br>";
- }
- html += part;
- }
- html += "</div>";
- return html;
- }
-
- private int getF(String content, int[] f) {
- int space = -1;
- String[] items = content.split(",");
- for (int i = 0; i < items.length; i ++) {
- String item = items[i];
- if (StringUtils.isEmpty(item)) {
- f[i] = 16;
- space = i;
- } else {
- f[i] = Integer.parseInt(item);
- }
- }
- return space;
- }
-
- private boolean checkContent(String content) {
- if (StringUtils.isEmpty(content)) return false;
-
- String[] items = content.split(",");
- if (items.length != 16) return false;
-
- for (String item : items) {
- if (null == item || item.equals("")) continue;
-
- int i = Integer.parseInt(item);
- if (i < 1 || i > 16) {
- return false;
- }
- }
-
- return true;
- }
-
- boolean isEmail(String email){
- if (null==email || "".equals(email)){
- return false;
- }
- String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
- Pattern p = Pattern.compile(regEx1);
- Matcher m = p.matcher(email);
- return m.matches();
- }
- }
|