ccjgmwz 3 лет назад
Родитель
Сommit
59fc93e760

+ 132 - 39
unimall-admin-api/src/main/java/com/iotechn/unimall/admin/AdminCheckQuartz.java

@@ -1,6 +1,7 @@
 package com.iotechn.unimall.admin;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.baomidou.mybatisplus.mapper.Wrapper;
@@ -46,11 +47,18 @@ import org.apache.commons.httpclient.NameValuePair;
 import org.apache.commons.httpclient.cookie.CookiePolicy;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.impl.client.BasicCookieStore;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -159,63 +167,148 @@ public class AdminCheckQuartz {
             dangOrderDeal(shopAccount,shopOrder);
         }
     }
+    /**
+     * 订单数据定时轮训处理 每小时执行一次
+     */
+    @Scheduled(cron = "0 30 7-20 * * ? ")
+    @Transactional(rollbackFor = Exception.class)
+    public void pullOrderList() throws Exception {
+        List<ShopAccount> listAccount = shopAccountMapper.selectList(new EntityWrapper<ShopAccount>().eq("delete_flag",0));
+        for (ShopAccount shopAccount:listAccount
+        ) {
+            flushCookie(shopAccount);
+            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+            HttpGet get = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=1000&orderStatus=ShippedToReceipt");
+            //这里可以设置请求参数,token等
+            get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
+            get.addHeader("cookie",shopAccount.getFuziCookie());
+
+            CloseableHttpResponse response = httpClient.execute(get);//执行获取响应
+            try{
+
+                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//根据状态码处理
+                    String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
+                    //返回字符串
+                    String res = unicodeToString(strTmp);
+                    JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
+                    Boolean status = (Boolean) datas.get("status");//获取返回数据状态,get获取的字段需要根据提供的返回值去获取
+                    List<ShopOrder> data = JSONArray.parseArray(datas.get("data").toString(),ShopOrder.class);//"data"是根据返回值设定
+                    for (ShopOrder tmp: data
+                    ) {
+                        //正则表达式,用于匹配非数字串,+号用于匹配出多个非数字串
+                        String regEx="[^0-9]+";
+                        Pattern pattern = Pattern.compile(regEx);
+                        //用定义好的正则表达式拆分字符串,把字符串中的数字留出来
+                        String[] cs = pattern.split(tmp.getRemarkText());
+                        String dangOrder = "";
+                        for (String str:cs
+                        ) {
+                            dangOrder += str;
+                        }
+                        tmp.setDangOrder(dangOrder);
+                        tmp.setAccountId(shopAccount.getId());
+                        tmp.setDeleteFlag(0);
+                        tmp.setGmtUpdate(new Date());
+                        tmp.setGmtCreate(new Date());
+                        ShopOrder tt = new ShopOrder();
+                        tt.setOrderId(tmp.getOrderId());
+                        tt =  shopOrderMapper.selectOne(tt);
+                        if(tt == null){
+                            shopOrderMapper.insert(tmp);
+                        }
+                    }
+
+                }
+
+
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            finally {
+                response.close();
+                httpClient.close();
+            }
+        }
+    }
     public void flushCookie(ShopAccount shopAccount){
         try{
-            HttpClient httpClient = new HttpClient();
-            GetMethod getMethod = new GetMethod("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=30");
-            // 每次访问需授权的网址时需带上前面的 cookie 作为通行证
-            getMethod.setRequestHeader("cookie", shopAccount.getFuziCookie());
-            // 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
-            // 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
-            getMethod.setRequestHeader("Referer", "https://shop.kongfz.com/");
-            getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
-            httpClient.executeMethod(getMethod);
-            // 打印出返回数据,检验一下是否成功
-            String text = getMethod.getResponseBodyAsString();
+            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+            HttpGet getMethod = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=30");
+            getMethod.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
+            getMethod.addHeader("cookie",shopAccount.getFuziCookie());
+
+            httpClient.execute(getMethod);
+            CloseableHttpResponse response = httpClient.execute(getMethod);//执行获取响应
             try{
-                JSONObject jsonObject = JSONObject.parseObject(text);
-                System.out.println(jsonObject);
-                if(jsonObject.getBoolean("status")){
-                    return;
+                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//根据状态码处理
+                    String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
+                    //返回字符串
+                    String res = unicodeToString(strTmp);
+                    JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
+                    Boolean status = (Boolean) datas.get("status");//获取返回数据状态,get获取的字段需要根据提供的返回值去获取
+                    if(status){
+                        return;
+                    }
                 }
             }
             catch (Exception exception){
 
             }
+            finally {
+                response.close();
+                httpClient.close();
+            }
 
             System.out.println("原cookies = "+shopAccount.getFuziCookie());
 
             // 登陆 Url
             String loginUrl = "https://login.kongfz.com/Pc/Login/account";
-            httpClient = new HttpClient();
+            BasicCookieStore store= new BasicCookieStore();
+            httpClient = HttpClients.custom().setDefaultCookieStore(store).build();
             // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
-            PostMethod postMethod = new PostMethod(loginUrl);
-
-            // 设置登陆时要求的信息,用户名和密码
-            NameValuePair[] data = { new NameValuePair("loginName", shopAccount.getFuziAccount()), new NameValuePair("loginPass", shopAccount.getFuziPassword()) };
-            postMethod.setRequestBody(data);
-            try {
-                // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
-                httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
-                int statusCode=httpClient.executeMethod(postMethod);
-                text = unicodeToString(postMethod.getResponseBodyAsString());
-                System.out.println("登录返回值 = "+text);
-                if(statusCode == 302){
-                    // 获得登陆后的 Cookie
-                    Cookie[] cookies = httpClient.getState().getCookies();
-                    StringBuffer tmpcookies = new StringBuffer();
-                    for (Cookie c : cookies) {
-                        tmpcookies.append(c.toString() + ";");
+            HttpPost httpPost = new HttpPost(loginUrl);
+
+            // 装填参数
+            List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
+            nvps.add(new BasicNameValuePair("loginName", shopAccount.getFuziAccount()));
+            nvps.add(new BasicNameValuePair("loginPass", shopAccount.getFuziPassword()));
+            // 设置参数到请求对象中
+            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
+
+            // 设置header信息
+            // 指定报文头【Content-type】、【User-Agent】
+            httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
+
+            // 执行请求操作,并拿到结果(同步阻塞)
+            response = httpClient.execute(httpPost);
+            // 获取结果实体
+            HttpEntity entity = response.getEntity();
+            if (entity != null) {
+                String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
+                //返回字符串
+                String res = unicodeToString(strTmp);
+                JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
+                Boolean status = (Boolean) datas.get("status");//获取返回数据状态,get获取的字段需要根据提供的返回值去获取
+                if(status){
+                    List<org.apache.http.cookie.Cookie> cookielist = store.getCookies();
+                    String nCookie = "";
+                    for(org.apache.http.cookie.Cookie cookie: cookielist){
+                        String name=cookie.getName();
+                        String value=cookie.getValue();
+                        nCookie += name + "="+value+";";
                     }
-                    System.out.println("新cookies = "+tmpcookies.toString());
-                    shopAccount.setFuziCookie(tmpcookies.toString());
+                    System.out.println("nCookie:" + nCookie);
+                    shopAccount.setFuziCookie(nCookie);
                     shopAccountMapper.updateById(shopAccount);
-                    // 进行登陆后的操作
+                }
+                else{
+                    System.out.println("登录返回值:" + res);
                 }
             }
-            catch (Exception e) {
-                e.printStackTrace();
-            }
+            EntityUtils.consume(entity);
+            // 释放链接
+            response.close();
+
         }catch (Exception e) {
             e.printStackTrace();
         }

+ 156 - 44
unimall-admin-api/src/main/java/com/iotechn/unimall/admin/api/user/AdminUserServiceImpl.java

@@ -1,8 +1,11 @@
 package com.iotechn.unimall.admin.api.user;
-import org.apache.commons.httpclient.Cookie;
+import com.baomidou.mybatisplus.mapper.Wrapper;
+import com.iotechn.unimall.data.domain.shop.ShopAccount;
+import com.iotechn.unimall.data.domain.shop.ShopOrder;
+import com.iotechn.unimall.data.mapper.shop.ShopAccountMapper;
+import com.iotechn.unimall.data.mapper.shop.ShopOrderMapper;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.NameValuePair;
-import org.apache.commons.httpclient.cookie.CookiePolicy;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import com.alibaba.fastjson.JSONArray;
@@ -18,12 +21,18 @@ import com.iotechn.unimall.data.model.Page;
 import okhttp3.*;
 import org.apache.commons.codec.digest.Md5Crypt;
 import org.apache.commons.io.IOUtils;
+import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.impl.client.BasicCookieStore;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -36,6 +45,7 @@ import java.net.HttpURLConnection;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -54,6 +64,12 @@ public class AdminUserServiceImpl implements AdminUserService {
     @Autowired
     private UserMapper userMapper;
 
+    @Autowired
+    private ShopAccountMapper shopAccountMapper;
+
+    @Autowired
+    private ShopOrderMapper shopOrderMapper;
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Boolean addUser(Long adminId, UserDO user) throws ServiceException {
@@ -132,53 +148,149 @@ public class AdminUserServiceImpl implements AdminUserService {
     @Override
     public String test(String param) throws Exception {
 
-        String cookieVal = "shoppingCartSessionId=8ca49ed8d30b67e4c4136cfe52ca277f; kfz_uuid=97743462-cbc0-439b-b75d-1aa4bb0939ed; reciever_area=18005000000; PHPSESSID=267e4bbaafbbb5413cd4f7be3e601f79399a2a00; kfz_trace=97743462-cbc0-439b-b75d-1aa4bb0939ed|12179203|01e2236b05e0e5d2|; acw_tc=2760828c16537216057521740ed2e98094ffe4c6944af0e72de8dfc7e09466";
-
-        // 登陆 Url
-        String loginUrl = "https://login.kongfz.com/Pc/Login/account";
-        // 需登陆后访问的 Url
-//        String dataUrl = "http://hi.mop.com/?";
-        HttpClient httpClient = new HttpClient();
-        GetMethod getMethod = new GetMethod("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=30");
-        // 每次访问需授权的网址时需带上前面的 cookie 作为通行证
-        getMethod.setRequestHeader("cookie", param);
-        // 你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
-        // 例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
-        getMethod.setRequestHeader("Referer", "https://shop.kongfz.com/");
-        getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
-        httpClient.executeMethod(getMethod);
-        // 打印出返回数据,检验一下是否成功
-        String text = getMethod.getResponseBodyAsString();
-        JSONObject jsonObject = JSONObject.parseObject(text);
-        System.out.println(jsonObject);
-
-        // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
-        PostMethod postMethod = new PostMethod(loginUrl);
-
-        // 设置登陆时要求的信息,用户名和密码
-        NameValuePair[] data = { new NameValuePair("loginName", "18241771147"), new NameValuePair("loginPass", "Ccj841968545") };
-        postMethod.setRequestBody(data);
-        try {
-            // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
-            httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
-            int statusCode=httpClient.executeMethod(postMethod);
-            if(statusCode == 302){
-                // 获得登陆后的 Cookie
-                Cookie[] cookies = httpClient.getState().getCookies();
-                StringBuffer tmpcookies = new StringBuffer();
-                cookieVal = "";
-                for (Cookie c : cookies) {
-                    tmpcookies.append(c.toString() + ";");
+        List<ShopAccount> listAccount = shopAccountMapper.selectList(new EntityWrapper<ShopAccount>().eq("delete_flag",0));
+        for (ShopAccount shopAccount:listAccount
+             ) {
+            flushCookie(shopAccount);
+            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+            HttpGet get = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=1000&orderStatus=ShippedToReceipt");
+            //这里可以设置请求参数,token等
+            get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
+            get.addHeader("cookie",shopAccount.getFuziCookie());
+
+            CloseableHttpResponse response = httpClient.execute(get);//执行获取响应
+            try{
+
+                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//根据状态码处理
+                    String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
+                    //返回字符串
+                    String res = unicodeToString(strTmp);
+                    JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
+                    Boolean status = (Boolean) datas.get("status");//获取返回数据状态,get获取的字段需要根据提供的返回值去获取
+                    List<ShopOrder> data = JSONArray.parseArray(datas.get("data").toString(),ShopOrder.class);//"data"是根据返回值设定
+                    for (ShopOrder tmp: data
+                    ) {
+                        //正则表达式,用于匹配非数字串,+号用于匹配出多个非数字串
+                        String regEx="[^0-9]+";
+                        Pattern pattern = Pattern.compile(regEx);
+                        //用定义好的正则表达式拆分字符串,把字符串中的数字留出来
+                        String[] cs = pattern.split(tmp.getRemarkText());
+                        String dangOrder = "";
+                        for (String str:cs
+                        ) {
+                            dangOrder += str;
+                        }
+                        tmp.setDangOrder(dangOrder);
+                        tmp.setAccountId(shopAccount.getId());
+                        tmp.setDeleteFlag(0);
+                        tmp.setGmtUpdate(new Date());
+                        tmp.setGmtCreate(new Date());
+                        ShopOrder tt = new ShopOrder();
+                        tt.setOrderId(tmp.getOrderId());
+                        tt =  shopOrderMapper.selectOne(tt);
+                        if(tt == null){
+                            shopOrderMapper.insert(tmp);
+                        }
+                    }
+
                 }
-                cookieVal = tmpcookies.toString();
-                System.out.println("模拟登录成功"+cookieVal);
-                // 进行登陆后的操作
+
+
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            finally {
+                response.close();
+                httpClient.close();
             }
         }
-        catch (Exception e) {
+
+        return "";
+    }
+
+    public void flushCookie(ShopAccount shopAccount){
+        try{
+            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+            HttpGet getMethod = new HttpGet("https://shop.kongfz.com/buyer/order/index/?pageCurr=1&pageShow=30");
+            getMethod.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
+            getMethod.addHeader("cookie",shopAccount.getFuziCookie());
+
+            httpClient.execute(getMethod);
+            CloseableHttpResponse response = httpClient.execute(getMethod);//执行获取响应
+            try{
+                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//根据状态码处理
+                    String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
+                    //返回字符串
+                    String res = unicodeToString(strTmp);
+                    JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
+                    Boolean status = (Boolean) datas.get("status");//获取返回数据状态,get获取的字段需要根据提供的返回值去获取
+                    if(status){
+                        return;
+                    }
+                }
+            }
+            catch (Exception exception){
+
+            }
+            finally {
+                response.close();
+                httpClient.close();
+            }
+
+            System.out.println("原cookies = "+shopAccount.getFuziCookie());
+
+            // 登陆 Url
+            String loginUrl = "https://login.kongfz.com/Pc/Login/account";
+            BasicCookieStore store= new BasicCookieStore();
+            httpClient =HttpClients.custom().setDefaultCookieStore(store).build();
+            // 模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
+            HttpPost httpPost = new HttpPost(loginUrl);
+
+            // 装填参数
+            List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
+            nvps.add(new BasicNameValuePair("loginName", shopAccount.getFuziAccount()));
+            nvps.add(new BasicNameValuePair("loginPass", shopAccount.getFuziPassword()));
+            // 设置参数到请求对象中
+            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
+
+            // 设置header信息
+            // 指定报文头【Content-type】、【User-Agent】
+            httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
+
+            // 执行请求操作,并拿到结果(同步阻塞)
+            response = httpClient.execute(httpPost);
+            // 获取结果实体
+            HttpEntity entity = response.getEntity();
+            if (entity != null) {
+                String strTmp= EntityUtils.toString(response.getEntity(),"UTF-8");
+                //返回字符串
+                String res = unicodeToString(strTmp);
+                JSONObject datas = JSONObject.parseObject(res);//转换成JSON格式
+                Boolean status = (Boolean) datas.get("status");//获取返回数据状态,get获取的字段需要根据提供的返回值去获取
+                if(status){
+                    List<org.apache.http.cookie.Cookie> cookielist = store.getCookies();
+                    String nCookie = "";
+                    for(org.apache.http.cookie.Cookie cookie: cookielist){
+                        String name=cookie.getName();
+                        String value=cookie.getValue();
+                        nCookie += name + "="+value+";";
+                    }
+                    System.out.println("nCookie:" + nCookie);
+                    shopAccount.setFuziCookie(nCookie);
+                    shopAccountMapper.updateById(shopAccount);
+                }
+                else{
+                    System.out.println("登录返回值:" + res);
+                }
+            }
+            EntityUtils.consume(entity);
+            // 释放链接
+            response.close();
+
+        }catch (Exception e) {
             e.printStackTrace();
         }
-        return cookieVal;
+
     }
     /**
      * Unicode转 汉字字符串

+ 3 - 0
unimall-data/src/main/java/com/iotechn/unimall/data/domain/shop/ShopOrder.java

@@ -161,6 +161,9 @@ public class ShopOrder extends BaseDO{
     @TableField(exist = false)
     private ShopAccount shopAccount;
 
+    @TableField(exist = false)
+    private String remarkText;
+
     @TableField(exist = false)
     private List<ShopOrder> shopOrderList;