Administrator
2025-04-23 595b7935a30e84fba1bc3561d05f9d19d3e32e1f
src/main/java/com/taoke/autopay/controller/WebApiController.java
@@ -6,18 +6,23 @@
import com.taoke.autopay.entity.KeyOrder;
import com.taoke.autopay.entity.SystemConfigKeyEnum;
import com.taoke.autopay.entity.WxUserInfo;
import com.taoke.autopay.entity.agent.ChannelAgent;
import com.taoke.autopay.exception.KeyOrderException;
import com.taoke.autopay.exception.KeyVerifyException;
import com.taoke.autopay.exception.WxOrderCountException;
import com.taoke.autopay.manager.PayCountVerifyManager;
import com.taoke.autopay.service.KeyOrderService;
import com.taoke.autopay.service.SystemConfigService;
import com.taoke.autopay.service.WxUserService;
import com.taoke.autopay.service.agent.ChannelAgentService;
import com.taoke.autopay.utils.*;
import com.taoke.autopay.vo.SubmitKeyInfo;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.UrlUtils;
@@ -47,25 +52,30 @@
    @Resource
    private SystemConfigService systemConfigService;
    @Resource
    private PayCountVerifyManager payCountVerifyManager;
    @Resource
    private ChannelAgentService channelAgentService;
    private void addKey(SubmitKeyInfo keyInfo, Long wxUid, String ip, IPUtil.IPInfo ipInfo) throws KeyVerifyException, KeyOrderException, WxOrderCountException {
    private KeyOrder addKey(SubmitKeyInfo keyInfo, Long wxUid) throws KeyVerifyException, KeyOrderException, WxOrderCountException {
        // 查询没有处理的数量
        long notProcessCount = keyOrderService.countUserNotDoOrder(wxUid, new Date(System.currentTimeMillis() - 1000*60*30));
        if(notProcessCount>0){
        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;
        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));
@@ -97,7 +107,7 @@
                throw new KeyVerifyException(KeyVerifyException.CODE_COMMON, e.getMessage());
            }
        }
        KeyOrder order = keyOrderService.addKeyOrder(keyInfo, wxUid, TimeUtil.getGernalTime(System.currentTimeMillis(), Constant.DB_DAY_FORMAT),orderType, orderDto);
        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();
@@ -106,8 +116,7 @@
            orderUpdate.setDistributeTime(new Date());
            keyOrderService.update(orderUpdate);
        }
        return order;
    }
    private void verifySubmitKey(String key) throws Exception {
@@ -120,7 +129,8 @@
        }
    }
    private Map<String, IPUtil.IPInfo> ipInfoMap=new HashMap<>();
    private Map<String, IPUtil.IPInfo> ipInfoMap = new HashMap<>();
    @ResponseBody
    @RequestMapping(value = "submitKeyV4")
    public String submitKeyV4(SubmitKeyInfo keyInfo, HttpSession session, HttpServletRequest request) {
@@ -132,22 +142,41 @@
        }
        String ip = IPUtil.getRemotIP(request);
        keyInfo.setIp(ip);
        if(!ipInfoMap.containsKey(ip)){
        if (!ipInfoMap.containsKey(ip)) {
            try {
                IPUtil.IPInfo ipInfo =  IPUtil.getLocalIPInfo(ip);
                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("暂时无法上传口令");
           }
        AntPathMatcher pathMatcher = new AntPathMatcher();
        Map<String, String> paramsMap = HttpUtil.getPramsFromUrl(keyInfo.getReferer());
        if (paramsMap.containsKey("a") && ipInfoMap.get(ip) != null) {
            String alias = paramsMap.get("a");
            ChannelAgent agent = channelAgentService.selectByAlias(alias);
            if (!StringUtil.isNullOrEmpty(agent.getShieldedAreas())) {
                String[] res = agent.getShieldedAreas().split(",");
                String ipInfoStr = ipInfoMap.get(ip).getProvince() + "/" + ipInfoMap.get(ip).getCity();
                boolean shield = false;
                for (String pattern : res) {
                    if (pathMatcher.match(pattern, ipInfoStr)) {
                        shield = true;
                        break;
                    }
                }
                if (shield) {
                    // 判断用户是否为白名单
                    if (user.getStatus() != WxUserInfo.STATUS_WHITE) {
                        // 不是白名单才限制
                        LogUtil.loggerDebug.warn("区域屏蔽:{}-{}", ip, new Gson().toJson(ipInfoMap.get(ip)));
                        return JsonUtil.loadFalseResult("暂时无法上传口令");
                    }
                }
            }
        }
        try {
@@ -183,10 +212,12 @@
            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)));
        LogUtil.loggerDebug.warn("GEO信息:{}-{}-{}", user.getId(), ip, new Gson().toJson(ipInfoMap.get(ip)));
        try {
            addKey(keyInfo, user.getId(),ip, ipInfoMap.get(ip));
            return JsonUtil.loadTrueResult("");
            KeyOrder order = addKey(keyInfo, user.getId());
            JSONObject data = new JSONObject();
            data.put("id", order.getId());
            return JsonUtil.loadTrueResult(data);
        } catch (KeyOrderException e) {
            e.printStackTrace();
            return JsonUtil.loadFalseResult(e.getMessage());
@@ -245,21 +276,20 @@
                wxLogger.info("从session读取到key:{}", alipayKeyInfo);
                if (alipayKeyInfo != null) {
                    if(!ipInfoMap.containsKey(alipayKeyInfo.getIp())){
                    if (!ipInfoMap.containsKey(alipayKeyInfo.getIp())) {
                        try {
                            IPUtil.IPInfo ipInfo =  IPUtil.getLocalIPInfo(alipayKeyInfo.getIp());
                            IPUtil.IPInfo ipInfo = IPUtil.getLocalIPInfo(alipayKeyInfo.getIp());
                            ipInfoMap.put(alipayKeyInfo.getIp(), ipInfo);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                    addKey(alipayKeyInfo, user.getId(),alipayKeyInfo.getIp(),ipInfoMap.get(alipayKeyInfo.getIp()));
                    addKey(alipayKeyInfo, user.getId());
                }
                response.sendRedirect(successLink);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            wxLogger.error("授权失败:{}", e.getMessage());
            if (!StringUtil.isNullOrEmpty(referer)) {
                Map<String, String> params = HttpUtil.getPramsFromUrl(referer);
@@ -270,4 +300,64 @@
        response.sendRedirect(failLink);
    }
    /**
     * 查询口令提交结果
     *
     * @param id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryKeyResult")
    public String queryKeyResult(String id, HttpServletRequest request) {
        KeyOrder keyOrder = keyOrderService.selectById(id);
        if (keyOrder == null) {
            return JsonUtil.loadFalseResult("口令不存在");
        }
        String desc = "";
        switch (keyOrder.getState()) {
            case KeyOrder.STATE_NOT_PROCESS:
                if (keyOrder.getDistributeClientUid() == null) {
                    desc = "口令已提交,等待支付中...";
                } else {
                    desc = "支付排队中...";
                }
                break;
            case KeyOrder.STATE_NOT_PAY:
                desc = "正在支付中,支付完成后才可继续提交口令。";
                break;
            case KeyOrder.STATE_PAY:
                desc = "支付成功";
                break;
            case KeyOrder.STATE_REJECT_PAY:
                desc = keyOrder.getStateDesc();
                break;
        }
        JSONObject data = new JSONObject();
        data.put("state_desc", desc);
        data.put("finish", keyOrder.getState() == KeyOrder.STATE_PAY || keyOrder.getState() == KeyOrder.STATE_REJECT_PAY ? 1 : 0);
        return JsonUtil.loadTrueResult(data);
    }
    /**
     * 查询支付剩余次数
     *
     * @param session
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryPayLeftCount")
    public String queryPayLeftCount(HttpSession session, HttpServletRequest request) {
        WxUserInfo user = (WxUserInfo) session.getAttribute(Constant.SESSION_KEY_USER);
        if (user == null || user.getId() == null) {
            return JsonUtil.loadFalseResult("用户尚未登录");
        }
        // 查询支付剩余次数
        Map<Integer, Long> map = payCountVerifyManager.computeUserLeftPayCount(user.getId());
        return JsonUtil.loadTrueResult(new Gson().toJson(map));
    }
}