mitao
2024-08-16 d7dc4db8d005a58f51d21d35147317762a16373f
ruoyi-modules/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/LogisticsServiceImpl.java
@@ -1,6 +1,9 @@
package com.ruoyi.order.service.impl;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.core.IBaseClient;
@@ -13,7 +16,18 @@
import com.ruoyi.system.api.domain.Logistics;
import com.ruoyi.system.api.domain.dto.LogisticsDTO;
import com.ruoyi.system.api.domain.vo.Express100VO;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
 * <p>
@@ -27,9 +41,15 @@
public class LogisticsServiceImpl extends ServiceImpl<LogisticsMapper, Logistics> implements
        ILogisticsService {
    private String key="BltjQodT7186";
    private String customer="56DE8E9E3D58CE73C60755C8B7483043";
    private static final String key = "BltjQodT7186";
    private static final String kye1 = "GMtjjhxp9pdEpfz37M";
    private static final String customer = "56DE8E9E3D58CE73C60755C8B7483043";
    private static final String AUTONUMBER_AUTO_URL = "http://www.kuaidi100.com/autonumber/auto?num=NUM&key=KEY";
    @Autowired
    private RestTemplate restTemplate;
    @Override
@@ -55,6 +75,76 @@
        Gson gson = new Gson();
        String responseBody = execute.getBody();
        Express100VO response = gson.fromJson(responseBody, Express100VO.class);
        LambdaQueryWrapper<Logistics> wrapper= Wrappers.lambdaQuery();
        wrapper.eq(Logistics::getLogisticsNum,logisticsDTO.getCompany());
        Logistics one = this.getOne(wrapper);
        if (Objects.nonNull(one)) {
            response.setLogisticsName(one.getLogisticsName());
        }
        return response;
    }
    @Override
    public Boolean isLogisticsOne(LogisticsDTO logisticsDTO) {
        Boolean b=false;
        Map<String,String> params=new HashMap();
        params.put("secret_key", "GMtjjhxp9pdEpfz37M");
        params.put("secret_code", "53514ac63f0447d188423221550b0c3e");
        params.put("secret_sign", "8265A3C04DDA73E5B899F7F750099BB0");
        params.put("num", logisticsDTO.getPostid());
        String string=post(params);
        if (string.equals("[]")){
            return b;
        }else{
                return b=true;
            }
    }
    public String post(Map<String,String> params){
        StringBuilder response=new StringBuilder("");
        BufferedReader reader = null;
        try {
            StringBuilder builder =new StringBuilder();
            for(Map.Entry param:params.entrySet()){
                if(builder.length()>0){
                    builder.append('&');
                }
                builder.append(URLEncoder.encode(param.getKey().toString(),"UTF-8"));
                builder.append('=');
                builder.append(URLEncoder.encode(String.valueOf(param.getValue()),"UTF-8"));
            }
            byte[]bytes=builder.toString().getBytes("UTF-8");
            URL url=new URL("http://cloud.kuaidi100.com/api");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("accept","*/*");
            conn.setRequestProperty("connection","Keep-Alive");
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length",String.valueOf(bytes.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(bytes);
            reader=new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line = "";
            while ((line =reader.readLine())!=null){
                response.append(line);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if (null!=reader){
                    reader.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return response.toString();
    }
}