Administrator
2025-02-13 d248fcaf389ae5c763926955d7ba0cfa80fc002f
src/main/java/com/taoke/autopay/controller/WebApiController.java
@@ -48,48 +48,24 @@
    @Resource
    private SystemConfigService systemConfigService;
    @ResponseBody
    @RequestMapping(value = "submitKey")
    public String submitKey(String key) {
        if (1 > 0) {
            return JsonUtil.loadFalseResult(0, "接口已关闭");
        }
        if (StringUtil.isNullOrEmpty(key)) {
            return JsonUtil.loadFalseResult(0, "请上传key");
        }
        List<String> urllist = UrlUtils.parseUrlsFromText(key);
        if (urllist.isEmpty() || !urllist.get(0).contains("ur.alipay.com")) {
            return JsonUtil.loadFalseResult("支付宝口令不正确");
        }
        try {
            KeyOrder order = keyOrderService.addKeyOrder(new SubmitKeyInfo(key), null, TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMdd"));
            Long uid = keyOrderService.getCanDistributeUid();
            if (uid != null) {
                KeyOrder orderUpdate = new KeyOrder();
                orderUpdate.setId(order.getId());
                orderUpdate.setDistributeClientUid(uid);
                orderUpdate.setDistributeTime(new Date());
                keyOrderService.update(orderUpdate);
            }
            return JsonUtil.loadTrueResult("");
        } catch (KeyOrderException e) {
            e.printStackTrace();
            return JsonUtil.loadFalseResult(e.getMessage());
        } catch (WxOrderCountException e) {
            return JsonUtil.loadFalseResult("今日超过最大提交次数");
        }
    }
    private void addKey(SubmitKeyInfo keyInfo, Long wxUid) throws KeyVerifyException, KeyOrderException, WxOrderCountException {
        // 查询没有处理的数量
        long notProcessCount = keyOrderService.countUserNotDoOrder(wxUid, new Date(System.currentTimeMillis() - 1000*60*30));
        if(notProcessCount>0){
            throw new WxOrderCountException("上一个口令尚未处理完!");
        }
        // 解析链接
        List<String> urllist = UrlUtils.parseUrlsFromText(keyInfo.getKey());
        String verifyAlipayKey = systemConfigService.getValueCache(SystemConfigKeyEnum.ALIPAY_KEY_VERIFY);
        DYOrderDto orderDto =null;
        int orderType = Constant.ORDER_TYPE_UNKNOWN;
        if (verifyAlipayKey != null && verifyAlipayKey.trim().equalsIgnoreCase("1")) {
            try {
                // 需要验证支付宝口令
                if (urllist.size() < 1) {
                if (urllist.isEmpty()) {
                    throw new Exception("口令中不包含链接");
                }
                AlipayOrderUtil.AlipayOrderTradeInfo tradeInfo = AlipayOrderUtil.getTradeInfo(urllist.get(0));
@@ -108,8 +84,9 @@
                if (tradeInfo == null) {
                    throw new Exception("口令内容获取失败");
                }
                orderType = AlipayOrderUtil.getOrderType(tradeInfo.getGoodsTitle());
                // 验证内容
                DYOrderDto dto = keyOrderService.verifyKey(tradeInfo.getGoodsTitle(), orderStatus, tradeInfo.getItemRealAmount(), wxUid);
                orderDto = keyOrderService.verifyKey(tradeInfo.getGoodsTitle(), orderStatus, tradeInfo.getItemRealAmount(), wxUid, null);
            } catch (KeyVerifyException ee) {
                try {
                    verifyLogger.warn("校验不通过:【{}】-{}", keyInfo.getKey(), ee.getMessage());
@@ -120,10 +97,8 @@
                throw new KeyVerifyException(KeyVerifyException.CODE_COMMON, e.getMessage());
            }
        }
        KeyOrder order = keyOrderService.addKeyOrder(keyInfo, wxUid, TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMdd"));
        Long uid = keyOrderService.getCanDistributeUid();
        KeyOrder order = keyOrderService.addKeyOrder(keyInfo, wxUid, TimeUtil.getGernalTime(System.currentTimeMillis(), Constant.DB_DAY_FORMAT),orderType, orderDto);
        Long uid = keyOrderService.getCanDistributeUid(Constant.MAX_PAY_ACCOUNT_QUEUE_SIZE);
        if (uid != null) {
            KeyOrder orderUpdate = new KeyOrder();
            orderUpdate.setId(order.getId());
@@ -145,126 +120,7 @@
        }
    }
    @ResponseBody
    @RequestMapping(value = "submitKeyV2")
    public String submitKeyV2(SubmitKeyInfo keyInfo, HttpSession session) {
        WxUserInfo user = (WxUserInfo) session.getAttribute(Constant.SESSION_KEY_USER);
        if (StringUtil.isNullOrEmpty(keyInfo.getKey())) {
            return JsonUtil.loadFalseResult("请上传key");
        }
        try {
            verifySubmitKey(keyInfo.getKey());
        } catch (Exception e) {
            return JsonUtil.loadFalseResult("支付宝口令不正确");
        }
        if (user == null) {
            // 先保存KEY
//            SESSION_KEY_TEMP_ALIPAY_KEY
            session.setAttribute(Constant.SESSION_KEY_TEMP_ALIPAY_KEY, keyInfo);
            wxLogger.info("微信没有授权:" + session.getId());
            String redictLink = systemConfigService.getValueCache(SystemConfigKeyEnum.WX_REDIRECT_LINK);
            if (StringUtil.isNullOrEmpty(redictLink)) {
                return JsonUtil.loadFalseResult("无法获取到授权链接");
            }
            // 没有登录,返回登录链接
            JSONObject root = new JSONObject();
            root.put("link", redictLink);
            return JsonUtil.loadTrueResult(Constant.RESULT_CODE_NEED_LOGIN, root);
        }
        wxLogger.info("微信有授权:" + session.getId());
        try {
            addKey(keyInfo, user.getId());
            return JsonUtil.loadTrueResult("");
        } catch (KeyOrderException e) {
            e.printStackTrace();
            return JsonUtil.loadFalseResult(e.getMessage());
        } catch (WxOrderCountException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        } catch (KeyVerifyException e) {
            switch (e.getCode()) {
                case KeyVerifyException.CODE_KEY_MONEY_NOT_MATCH:
                    return JsonUtil.loadFalseResult("该笔订单有误,不予提交");
                case KeyVerifyException.CODE_ORDER_MONEY_NOT_MATCH:
                    return JsonUtil.loadFalseResult("提交金额不匹配");
            }
            logger.debug("口令验证结果异常:{}-{}", keyInfo.getKey(), e.getMessage());
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
    /**
     * @return java.lang.String
     * @author hxh
     * @description 带口令与金额的口令提交接口
     * @date 0:12 2024/7/9
     * @param: keyInfo
     * @param: session
     **/
    @ResponseBody
    @RequestMapping(value = "submitKeyV3")
    public String submitKeyV3(SubmitKeyInfo keyInfo, HttpSession session, HttpServletRequest request) {
        String referer = request.getHeader("Referer");
        keyInfo.setReferer(referer);
        WxUserInfo user = (WxUserInfo) session.getAttribute(Constant.SESSION_KEY_USER);
        if (StringUtil.isNullOrEmpty(keyInfo.getKey())) {
            return JsonUtil.loadFalseResult("请上传key");
        }
//        if (StringUtil.isNullOrEmpty(keyInfo.getMoney())) {
//            return JsonUtil.loadFalseResult("请上传money");
//        }
        try {
            verifySubmitKey(keyInfo.getKey());
        } catch (Exception e) {
            return JsonUtil.loadFalseResult("支付宝口令不正确");
        }
        // 验证提交时间
        String timeStr = systemConfigService.getValueCache(SystemConfigKeyEnum.KEY_SUBMIT_TIME_RANGE);
        if (StringUtil.isNullOrEmpty(timeStr)) {
            return JsonUtil.loadFalseResult("尚未配置生效时间");
        }
        String startTime = timeStr.split(",")[0].trim().replace(":", "");
        String endTime = timeStr.split(",")[1].trim().replace(":", "");
        String now = TimeUtil.getGernalTime(System.currentTimeMillis(), "HHmmss");
        if (Integer.parseInt(now) < Integer.parseInt(startTime) || Integer.parseInt(now) > Integer.parseInt(endTime)) {
            return JsonUtil.loadFalseResult(String.format("口令提交时间段为:%s-%s", timeStr.split(",")[0], timeStr.split(",")[1]));
        }
        if (user == null) {
            // 先保存KEY
//            SESSION_KEY_TEMP_ALIPAY_KEY
            session.setAttribute(Constant.SESSION_KEY_TEMP_ALIPAY_KEY, keyInfo);
            wxLogger.info("微信没有授权:" + session.getId());
            String redictLink = systemConfigService.getValueCache(SystemConfigKeyEnum.WX_REDIRECT_LINK);
            if (StringUtil.isNullOrEmpty(redictLink)) {
                return JsonUtil.loadFalseResult("无法获取到授权链接");
            }
            // 没有登录,返回登录链接
            JSONObject root = new JSONObject();
            root.put("link", redictLink);
            return JsonUtil.loadTrueResult(Constant.RESULT_CODE_NEED_LOGIN, root);
        }
        wxLogger.info("微信有授权:" + session.getId());
        try {
            addKey(keyInfo, user.getId());
            return JsonUtil.loadTrueResult("");
        } catch (KeyOrderException e) {
            e.printStackTrace();
            return JsonUtil.loadFalseResult(e.getMessage());
        } catch (WxOrderCountException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        } catch (KeyVerifyException e) {
            logger.debug("口令校验失败:{}-{}-{}", keyInfo.getKey(), e.getCode(), e.getMessage());
            switch (e.getCode()) {
                case KeyVerifyException.CODE_KEY_MONEY_NOT_MATCH:
                    return JsonUtil.loadFalseResult("该笔订单有误,不予提交");
                case KeyVerifyException.CODE_ORDER_MONEY_NOT_MATCH:
                    return JsonUtil.loadFalseResult("提交金额不匹配");
            }
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
    private Map<String, IPUtil.IPInfo> ipInfoMap=new HashMap<>();
    @ResponseBody
    @RequestMapping(value = "submitKeyV4")
    public String submitKeyV4(SubmitKeyInfo keyInfo, HttpSession session, HttpServletRequest request) {
@@ -273,6 +129,26 @@
        WxUserInfo user = (WxUserInfo) session.getAttribute(Constant.SESSION_KEY_USER);
        if (StringUtil.isNullOrEmpty(keyInfo.getKey())) {
            return JsonUtil.loadFalseResult("请上传key");
        }
        String ip = IPUtil.getRemotIP(request);
        keyInfo.setIp(ip);
        if(!ipInfoMap.containsKey(ip)){
            try {
                IPUtil.IPInfo ipInfo =  IPUtil.getLocalIPInfo(ip);
                ipInfoMap.put(ip, ipInfo);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        keyInfo.setIpInfo(ipInfoMap.get(ip));
        String citys =  systemConfigService.getValueCache(SystemConfigKeyEnum.FORBIDDEN_SUBMIT_CITYS);
        if(citys!=null&&ipInfoMap.get(ip)!=null){
           List<String> cityList = Arrays.asList(citys.split(","));
           if(cityList.contains(ipInfoMap.get(ip).getCity())){
               LogUtil.loggerDebug.warn("区域屏蔽:{}-{}", ip, new Gson().toJson(ipInfoMap.get(ip)));
               return JsonUtil.loadFalseResult("暂时无法上传口令");
           }
        }
        try {
@@ -308,6 +184,7 @@
            return JsonUtil.loadTrueResult(Constant.RESULT_CODE_NEED_LOGIN, root);
        }
        wxLogger.info("微信有授权:" + session.getId());
        LogUtil.loggerDebug.warn("GEO信息:{}-{}-{}", user.getId(),ip, new Gson().toJson(ipInfoMap.get(ip)));
        try {
            addKey(keyInfo, user.getId());
            return JsonUtil.loadTrueResult("");
@@ -323,6 +200,8 @@
                    return JsonUtil.loadFalseResult("该笔订单有误,不予提交");
                case KeyVerifyException.CODE_ORDER_MONEY_NOT_MATCH:
                    return JsonUtil.loadFalseResult("提交金额不匹配");
                case KeyVerifyException.CODE_ORDER_NO_REPEAT:
                    return JsonUtil.loadFalseResult("重复提交订单");
            }
            return JsonUtil.loadFalseResult(e.getMessage());
        }
@@ -367,6 +246,14 @@
                wxLogger.info("从session读取到key:{}", alipayKeyInfo);
                if (alipayKeyInfo != null) {
                    if(!ipInfoMap.containsKey(alipayKeyInfo.getIp())){
                        try {
                            IPUtil.IPInfo ipInfo =  IPUtil.getLocalIPInfo(alipayKeyInfo.getIp());
                            ipInfoMap.put(alipayKeyInfo.getIp(), ipInfo);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                    addKey(alipayKeyInfo, user.getId());
                }
                response.sendRedirect(successLink);