Pārlūkot izejas kodu

增加添加站点对外接口

Jack 11 mēneši atpakaļ
vecāks
revīzija
77c8784fe6

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 3 - 2938
.idea/workspace.xml


+ 114 - 14
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/okki/site/controller/OkkiSiteController.java

@@ -1,27 +1,36 @@
 package org.jeecg.modules.api.okki.site.controller;
 
-import java.util.Arrays;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.jeecg.common.api.vo.Result;
-import org.jeecg.common.system.query.QueryGenerator;
-import org.jeecg.modules.api.okki.site.entity.OkkiSite;
-import org.jeecg.modules.api.okki.site.service.IOkkiSiteService;
-
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
-
+import org.apache.shiro.authz.annotation.RequiresPermissions;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.common.aspect.annotation.AutoLog;
 import org.jeecg.common.system.base.controller.JeecgController;
+import org.jeecg.common.system.query.QueryGenerator;
+import org.jeecg.modules.api.okki.site.entity.OkkiSite;
+import org.jeecg.modules.api.okki.site.service.IOkkiSiteService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.ModelAndView;
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiOperation;
-import org.jeecg.common.aspect.annotation.AutoLog;
-import org.apache.shiro.authz.annotation.RequiresPermissions;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.TreeMap;
 
 /**
  * @Description: 站点
@@ -36,6 +45,10 @@ import org.apache.shiro.authz.annotation.RequiresPermissions;
 public class OkkiSiteController extends JeecgController<OkkiSite, IOkkiSiteService> {
 	@Autowired
 	private IOkkiSiteService okkiSiteService;
+
+	private final static String URL = "https://cms.dev.xiaoman.cn/shop-api/external/site";
+
+	private final static String CLIENT_SECRET = "";
 	
 	/**
 	 * 分页列表查询
@@ -161,4 +174,91 @@ public class OkkiSiteController extends JeecgController<OkkiSite, IOkkiSiteServi
         return super.importExcel(request, response, OkkiSite.class);
     }
 
+	/**
+	 * 完成装修
+	 * @param okkiSite
+	 * @return
+	 */
+	@PostMapping(value = "status")
+	public Result<String> changeStatus(@RequestBody OkkiSite okkiSite) {
+		// TODO:: 请求okki平台接口
+		return Result.OK();
+	}
+
+
+	public static void main(String[] args) throws Exception {
+		String url = "https://cms.dev.xiaoman.cn/shop-api/External/site";
+		String clientSecret = "rAqZAp9oo0crNariGVVpt5AvPeVhCKXJ";
+		Map<String, String> query = new TreeMap<>();
+		query.put("sign_method", "hmac-md5");
+		query.put("timestamp", String.valueOf(System.currentTimeMillis()));
+		query.put("site_id", "1000002");
+		query.put("method", "update_site_status");
+
+		Map<String, Object> postData = new HashMap<>();
+		postData.put("status", 3);
+
+		String queryStr = buildQueryString(query);
+		String body = toJsonString(postData);
+		String signStr = queryStr + body;
+		query.put("signature", generateHmacMD5(signStr, clientSecret));
+
+		String okkiUrl = url + "?" + buildQueryString(query);
+		String res = doPost(okkiUrl, body);
+		System.out.println(res);
+	}
+
+	private static String buildQueryString(Map<String, String> params) {
+		StringBuilder query = new StringBuilder();
+		for (Map.Entry<String, String> entry : params.entrySet()) {
+			if (query.length() > 0) {
+				query.append("&");
+			}
+			query.append(entry.getKey()).append("=").append(entry.getValue());
+		}
+		return query.toString();
+	}
+
+	private static String generateHmacMD5(String data, String secret) throws Exception {
+		SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacMD5");
+		Mac mac = Mac.getInstance("HmacMD5");
+		mac.init(keySpec);
+		byte[] hmacBytes = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
+		StringBuilder sb = new StringBuilder(hmacBytes.length * 2);
+		for (byte b : hmacBytes) {
+			sb.append(String.format("%02x", b));
+		}
+		return sb.toString();
+	}
+
+	private static String toJsonString(Object object) throws Exception {
+		com.google.gson.Gson gson = new com.google.gson.Gson();
+		return gson.toJson(object);
+	}
+
+	private static String doPost(String url, String data) throws Exception {
+		java.net.URL apiUrl = new URL(url);
+		System.out.println(url);
+		HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
+		connection.setRequestMethod("POST");
+		connection.setRequestProperty("Content-Type", "application/json");
+		connection.setDoOutput(true);
+		try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
+			outputStream.write(data.getBytes(StandardCharsets.UTF_8));
+			outputStream.flush();
+		}
+
+		int responseCode = connection.getResponseCode();
+		if (responseCode == HttpURLConnection.HTTP_OK) {
+			try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
+				String line;
+				StringBuilder response = new StringBuilder();
+				while ((line = in.readLine()) != null) {
+					response.append(line);
+				}
+				return response.toString();
+			}
+		}
+		throw new Exception("HTTP request failed with response code: " + responseCode);
+	}
 }

+ 2 - 5
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/okki/site/controller/ShopApiController.java

@@ -4,14 +4,11 @@ import org.jeecg.common.api.vo.Result;
 import org.jeecg.modules.api.okki.site.entity.OkkiSite;
 import org.jeecg.modules.api.okki.site.service.IOkkiSiteService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestHeader;
-import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.Map;
 
-@RequestMapping("sites")
+@RequestMapping("website")
 public class ShopApiController {
 
     @Autowired

+ 5 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/okki/site/entity/OkkiSite.java

@@ -96,4 +96,9 @@ public class OkkiSite implements Serializable {
 	@Excel(name = "备注", width = 15)
     @ApiModelProperty(value = "备注")
     private String remark;
+    /**状态*/
+    @Excel(name = "状态", width = 15, dicCode = "site_status")
+    @Dict(dicCode = "site_status")
+    @ApiModelProperty(value = "状态")
+    private java.lang.Integer status;
 }

+ 17 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/okki/site/param/BaseParam.java

@@ -0,0 +1,17 @@
+package org.jeecg.modules.api.okki.site.param;
+
+import lombok.Data;
+
+@Data
+public class BaseParam {
+
+    private String mothod;
+
+    private String timestamp;
+
+    private Integer site_id;
+
+    private String sign_method;
+
+    private String signature;
+}

+ 9 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/okki/site/param/ChangeSiteStatusParam.java

@@ -0,0 +1,9 @@
+package org.jeecg.modules.api.okki.site.param;
+
+import lombok.Data;
+
+@Data
+public class ChangeSiteStatusParam {
+
+    private Integer status;
+}

+ 161 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/okki/utils/HttpClientUtils.java

@@ -0,0 +1,161 @@
+package org.jeecg.modules.api.okki.utils;
+
+import org.apache.http.Consts;
+import org.apache.http.NameValuePair;
+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.client.utils.URIBuilder;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * 调用第三方接口工具类
+ */
+public class HttpClientUtils {
+
+    /**
+     * 带参数的get请求
+     * @param url
+     * @param param
+     * @return String
+     */
+    public static String doGet(String url, Map<String, String> param) {
+        // 创建Httpclient对象
+        CloseableHttpClient httpclient = HttpClients.createDefault();
+
+        String resultString = "";
+        CloseableHttpResponse response = null;
+        try {
+            // 创建uri
+            URIBuilder builder = new URIBuilder(url);
+            if (param != null) {
+                for (String key : param.keySet()) {
+                    builder.addParameter(key, param.get(key));
+                }
+            }
+            URI uri = builder.build();
+            // 创建http GET请求
+            HttpGet httpGet = new HttpGet(uri);
+            // 执行请求
+            response = httpclient.execute(httpGet);
+            // 判断返回状态是否为200
+            if (response.getStatusLine().getStatusCode() == 200) {
+                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                if (response != null) {
+                    response.close();
+                }
+                httpclient.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return resultString;
+    }
+
+    /**
+     * 不带参数的get请求
+     * @param url
+     * @return String
+     */
+    public static String doGet(String url) {
+        return doGet(url, null);
+    }
+
+    /**
+     * 带参数的post请求
+     * @param url
+     * @param param
+     * @return String
+     */
+    public static String doPost(String url, Map<String, String> param) {
+        // 创建Httpclient对象
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        CloseableHttpResponse response = null;
+        String resultString = "";
+        try {
+            // 创建Http Post请求
+            HttpPost httpPost = new HttpPost(url);
+            // 创建参数列表
+            if (param != null) {
+                List<NameValuePair> paramList = new ArrayList<>();
+                for (String key : param.keySet()) {
+                    paramList.add(new BasicNameValuePair(key, param.get(key)));
+                }
+                // 模拟表单
+                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, Consts.UTF_8);
+                httpPost.setEntity(entity);
+            }
+            // 执行http请求
+            response = httpClient.execute(httpPost);
+            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                response.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return resultString;
+    }
+
+    /**
+     * 不带参数的post请求
+     * @param url
+     * @return String
+     */
+    public static String doPost(String url) {
+        return doPost(url, null);
+    }
+
+    /**
+     * 传送json类型的post请求
+     * @param url
+     * @param json
+     * @return String
+     */
+    public static String doPostJson(String url, String json) {
+        // 创建Httpclient对象
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        CloseableHttpResponse response = null;
+        String resultString = "";
+        try {
+            // 创建Http Post请求
+            HttpPost httpPost = new HttpPost(url);
+            // 创建请求内容
+            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
+            httpPost.setEntity(entity);
+            // 执行http请求
+            response = httpClient.execute(httpPost);
+            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                response.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return resultString;
+    }
+}

+ 93 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/api/okki/utils/SignUtils.java

@@ -0,0 +1,93 @@
+package org.jeecg.modules.api.okki.utils;
+
+import cn.hutool.crypto.SecureUtil;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+
+public class SignUtils {
+
+    private static final String MD5 = "md5";
+
+    public static String sign(final Map<String, Object> data, String token) {
+        Set<String> keySet = data.keySet();
+        String[] keyArray = keySet.toArray(new String[0]);
+        Arrays.sort(keyArray);
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < keyArray.length; i++) {
+            if (String.valueOf(data.get(keyArray[i])).length() > 0) {
+                sb.append(keyArray[i]).append(data.get(keyArray[i]));
+            }
+        }
+        sb.append(token);
+        return SecureUtil.md5(sb.toString());
+    }
+
+    /**
+     * Java实现PHP中的http_build_query()效果
+     * @param array
+     *        key=value形式的二位数组
+     * @return
+     */
+    public static String http_build_query(Map<String ,String> array){
+        String reString = null;
+        //遍历数组形成akey=avalue&bkey=bvalue&ckey=cvalue形式的的字符串
+        Iterator it = array.entrySet().iterator();
+        while (it.hasNext()){
+            Map.Entry<String,String> entry =(Map.Entry) it.next();
+            String key = entry.getKey();
+            String value = entry.getValue();
+            reString += key+"="+value+"&";
+        }
+        reString = reString.substring(0, reString.length()-1);
+        //将得到的字符串进行处理得到目标格式的字符串
+        try {
+            reString = java.net.URLEncoder.encode(reString,"utf-8");
+        } catch (UnsupportedEncodingException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        reString = reString.replace("%3D", "=").replace("%26", "&");
+        return reString;
+    }
+
+    /**
+     * 使用 HMAC-SHA1 签名算法对data进行签名
+     * @param data 被签名的字符串
+     * @param key  密钥
+     * @return 加密后的字符串
+     */
+    public static String getSignature(String data, String key) throws Exception {
+        //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
+        SecretKeySpec signinKey = new SecretKeySpec(key.getBytes(), MD5);
+        //生成一个指定 Mac 算法 的 Mac 对象
+        Mac mac = Mac.getInstance(MD5);
+        //用给定密钥初始化 Mac 对象
+        mac.init(signinKey);
+        //完成 Mac 操作
+        byte[] rawHmac = mac.doFinal(data.getBytes());
+        String hexBytes = byte2hex(rawHmac);
+        return hexBytes;
+    }
+
+    private static String byte2hex(final byte[] b) {
+        String hs = "";
+        String stmp = "";
+        for (int n = 0; n < b.length; n++) {
+            // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
+            stmp = (java.lang.Integer.toHexString(b[n] & 0xFF));
+            if (stmp.length() == 1) {
+                hs = hs + "0" + stmp;
+            } else {
+                hs = hs + stmp;
+            }
+        }
+        return hs;
+    }
+}

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels