From 7ac0b5be02902a96bd1feb658e41a9b69fa50738 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期六, 29 六月 2024 01:37:31 +0800
Subject: [PATCH] 功能完善

---
 src/main/java/com/taoke/autopay/controller/WebApiController.java |  113 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 98 insertions(+), 15 deletions(-)

diff --git a/src/main/java/com/taoke/autopay/controller/WebApiController.java b/src/main/java/com/taoke/autopay/controller/WebApiController.java
index dafd7b6..995f124 100644
--- a/src/main/java/com/taoke/autopay/controller/WebApiController.java
+++ b/src/main/java/com/taoke/autopay/controller/WebApiController.java
@@ -1,11 +1,18 @@
 package com.taoke.autopay.controller;
 
+import com.taoke.autopay.dto.WXAppInfoDto;
 import com.taoke.autopay.entity.KeyOrder;
+import com.taoke.autopay.entity.SystemConfigKeyEnum;
+import com.taoke.autopay.entity.WxUserInfo;
 import com.taoke.autopay.exception.KeyOrderException;
+import com.taoke.autopay.exception.WxOrderCountException;
 import com.taoke.autopay.factory.OrderFactory;
 import com.taoke.autopay.service.KeyOrderService;
-import com.taoke.autopay.utils.JsonUtil;
-import com.taoke.autopay.utils.StringUtil;
+import com.taoke.autopay.service.SystemConfigService;
+import com.taoke.autopay.service.WxUserOrderCountService;
+import com.taoke.autopay.service.WxUserService;
+import com.taoke.autopay.utils.*;
+import net.sf.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Controller;
@@ -14,6 +21,9 @@
 import org.yeshi.utils.UrlUtils;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Date;
 import java.util.List;
@@ -23,26 +33,33 @@
 public class WebApiController {
     Logger logger = LoggerFactory.getLogger(WebApiController.class);
 
+    Logger wxLogger = LoggerFactory.getLogger("wxLogger");
+
+
     @Resource
     private KeyOrderService keyOrderService;
 
+    @Resource
+    private WxUserService wxUserService;
+
+    @Resource
+    private SystemConfigService systemConfigService;
 
     @ResponseBody
     @RequestMapping(value = "submitKey")
-    public String submitKey(String  key) {
-        if(StringUtil.isNullOrEmpty(key)){
-           return JsonUtil.loadFalseResult(0,"璇蜂笂浼爇ey");
+    public String submitKey(String key) {
+        if (StringUtil.isNullOrEmpty(key)) {
+            return JsonUtil.loadFalseResult(0, "璇蜂笂浼爇ey");
         }
-       List<String> urllist =  UrlUtils.parseUrlsFromText(key);
-       if(urllist.isEmpty()||!urllist.get(0).contains("ur.alipay.com")){
-           return JsonUtil.loadFalseResult("鏀粯瀹濆彛浠や笉姝g‘");
-       }
+        List<String> urllist = UrlUtils.parseUrlsFromText(key);
+        if (urllist.isEmpty() || !urllist.get(0).contains("ur.alipay.com")) {
+            return JsonUtil.loadFalseResult("鏀粯瀹濆彛浠や笉姝g‘");
+        }
         try {
-            KeyOrder order =   keyOrderService.addKeyOrder(key);
-
-            Long uid =  keyOrderService.getCanDistributeUid();
-            if(uid!=null){
-                KeyOrder orderUpdate=new KeyOrder();
+            KeyOrder order = keyOrderService.addKeyOrder(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());
@@ -51,8 +68,74 @@
             return JsonUtil.loadTrueResult("");
         } catch (KeyOrderException e) {
             e.printStackTrace();
-           return JsonUtil.loadFalseResult(e.getMessage());
+            return JsonUtil.loadFalseResult(e.getMessage());
+        } catch (WxOrderCountException e) {
+            return JsonUtil.loadFalseResult("浠婃棩瓒呰繃鏈�澶ф彁浜ゆ鏁�");
         }
     }
 
+    @ResponseBody
+    @RequestMapping(value = "submitKeyV2")
+    public String submitKeyV2(String key, HttpSession session) {
+        WxUserInfo user = (WxUserInfo) session.getAttribute(Constant.SESSION_KEY_USER);
+        if (StringUtil.isNullOrEmpty(key)) {
+            return JsonUtil.loadFalseResult(0, "璇蜂笂浼爇ey");
+        }
+        List<String> urllist = UrlUtils.parseUrlsFromText(key);
+        if (urllist.isEmpty() || !urllist.get(0).contains("ur.alipay.com")) {
+            return JsonUtil.loadFalseResult("鏀粯瀹濆彛浠や笉姝g‘");
+        }
+
+        if (user == null) {
+            wxLogger.info("寰俊娌℃湁鎺堟潈");
+            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);
+        }
+
+        try {
+            KeyOrder order = keyOrderService.addKeyOrder(key, user.getId(), 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("浠婃棩瓒呰繃鏈�澶ф彁浜ゆ鏁�");
+        }
+    }
+
+    @RequestMapping(value = "wxLogin")
+    public void wxLogin(String code, String state, HttpServletResponse response, HttpSession session) throws IOException {
+        // 鏍规嵁code鑾峰彇openid
+        String failLink = systemConfigService.getValueCache(SystemConfigKeyEnum.WX_LOGIN_FAIL_LINK);
+        try {
+            WXAppInfoDto wxApp = systemConfigService.getWxAppInfoCache();
+            String successLink = systemConfigService.getValueCache(SystemConfigKeyEnum.WX_LOGIN_SUCCESS_LINK);
+            WxApiUtil.WXAccessTokenInfo tokenInfo = WxApiUtil.getAcessTokenInfo(code, wxApp);
+            if (tokenInfo != null && !StringUtil.isNullOrEmpty(tokenInfo.getOpenid())) {
+                WxUserInfo user = wxUserService.login(tokenInfo.getOpenid());
+                session.setAttribute(Constant.SESSION_KEY_USER, user);
+                response.sendRedirect(successLink);
+                return;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            wxLogger.error("鎺堟潈澶辫触锛歿}", e.getMessage());
+        }
+        response.sendRedirect(failLink);
+    }
+
 }

--
Gitblit v1.8.0