|
|
@@ -0,0 +1,188 @@
|
|
|
+package com.ruoyi.project.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.ruoyi.common.utils.TianheSignUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author tangrui
|
|
|
+ * @version 1.0
|
|
|
+ * @description: depositTest
|
|
|
+ * @date: 2023/3/21 17:12
|
|
|
+ */
|
|
|
+@Service("TianheService")
|
|
|
+public class TianheService {
|
|
|
+ @Value("${tianhe.appid}")
|
|
|
+ private String appid;
|
|
|
+ @Value("${tianhe.appkey}")
|
|
|
+ private String appkey;
|
|
|
+ // 测试环境
|
|
|
+// final static String test_address = "0xa7cf21881211d7b353b3acdf92453e99dccc4212";
|
|
|
+// final static String test_publicKey="04e8860b97ac4806bdda1debdb59847bf12b6fb5847bbab63a92bd2b5ad8d3cb67210f23c4b4ff0852e55ee4bc6fd7c9ea69128dd0caa69561dd0f59e963dfd48a";
|
|
|
+// // 正式环境 anderx账号
|
|
|
+// final static String address = "0x4cc4951ba7af50ddab07d417c4633522043d0984";
|
|
|
+// final static String publicKey="0465cbb9872e84c3d5cdc526020bff5b30a5bc9cf3855cfd61ea603e0aa3168ddf5555191f4f60fff31f2396fd7bca8e886204481aba4e74ec1cea04cd8db0f95d";
|
|
|
+ @Value("${tianhe.add_user_url}")
|
|
|
+ private String add_user_url ;
|
|
|
+ @Value("${tianhe.seve_text_url}")
|
|
|
+ private String seve_text_url;
|
|
|
+ @Value("${tianhe.seve_file_url}")
|
|
|
+ private String seve_file_url;
|
|
|
+ @Value("${tianhe.deposit_certificate_url}")
|
|
|
+ private String deposit_certificate_url;
|
|
|
+ @Value("${tianhe.deposit_query_url}")
|
|
|
+ private String deposit_query_url;
|
|
|
+ @Value("${tianhe.prefix}")
|
|
|
+ private String prefix;
|
|
|
+
|
|
|
+
|
|
|
+ public String addUser(String userId,String userKey) {
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("appId",appid);
|
|
|
+ body.put("appKey",appkey);
|
|
|
+ body.put("userId",prefix+userId);
|
|
|
+ body.put("userKey",userKey);
|
|
|
+ JSONObject jsonObject= JSONUtil.createObj();
|
|
|
+ jsonObject.putAll(body);
|
|
|
+
|
|
|
+ String post = HttpRequest.post(add_user_url).header("Content-Type", "application/json")
|
|
|
+ .body(jsonObject.toString()).execute().body();
|
|
|
+ return post;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String seveText(String address,String text,String operateId) {
|
|
|
+ //step1 拼接body
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("text", text);
|
|
|
+ body.put("address", address);
|
|
|
+ body.put("operateId", operateId);
|
|
|
+ body.put("noDigest", false);
|
|
|
+ JSONObject jsonObject = JSONUtil.createObj();
|
|
|
+ jsonObject.putAll(body);
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+ // 请求路径,仅截取域名后及 Query 参数前部分,例:"/v1/deposit/query";
|
|
|
+ String uri = "/v1/deposit/text/save";
|
|
|
+ //接口签名
|
|
|
+ String signature = TianheSignUtil.signRequest(uri, null, body, timestamp, appkey);
|
|
|
+ //hutool.http
|
|
|
+ String result2 = HttpRequest.post(seve_text_url)
|
|
|
+ .header("ti-appid", appid)
|
|
|
+ .header("ti-timestamp", String.valueOf(timestamp))
|
|
|
+ .header("ti-signature", signature)
|
|
|
+ .body(jsonObject.toString())
|
|
|
+ .execute().body();
|
|
|
+ System.out.println(result2);
|
|
|
+ return result2;
|
|
|
+ /**
|
|
|
+ * {"code":200,"success":true,"data":"aasdas121245","msg":"操作成功"}
|
|
|
+ */
|
|
|
+ }
|
|
|
+ public String seveFile(String address,String operateId,File file) {
|
|
|
+ //step1 拼接body
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ Map<String, Object> query = new HashMap<>();
|
|
|
+ body.put("file", file);
|
|
|
+ body.put("address", address);
|
|
|
+ body.put("operateId", operateId);
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+ System.out.println(timestamp);
|
|
|
+ // 请求路径,仅截取域名后及 Query 参数前部分,例:"/v1/deposit/query";
|
|
|
+ String uri = "/v1/deposit/file/save";
|
|
|
+ //接口签名
|
|
|
+ String signature = TianheSignUtil.signRequest(uri, body,query , timestamp, appkey);
|
|
|
+ System.out.println(signature);
|
|
|
+ //hutool.http
|
|
|
+ String result2 = HttpRequest.post(seve_file_url)
|
|
|
+ .header("ti-appid", appid)
|
|
|
+ .header("ti-timestamp", String.valueOf(timestamp))
|
|
|
+ .header("ti-signature", signature)
|
|
|
+// .header("Content-Type","application/form-data")
|
|
|
+ .form(body)
|
|
|
+ .execute().body();
|
|
|
+ System.out.println(result2);
|
|
|
+ return result2;
|
|
|
+ /**
|
|
|
+ * {"code":200,"success":true,"data":"aasdas121245","msg":"操作成功"}
|
|
|
+ */
|
|
|
+ }
|
|
|
+ public String seveFile(String address,String operateId,MultipartFile multipartFile) throws IOException {
|
|
|
+ File file=null;
|
|
|
+ String originalFilename = multipartFile.getOriginalFilename();
|
|
|
+ String [] fileName =originalFilename.split("\\.");
|
|
|
+ file=File.createTempFile(fileName[0],fileName[1]);
|
|
|
+ multipartFile.transferTo(file);
|
|
|
+ return seveFile(address,operateId,file);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public String depositQuery(String address,String operateId) {
|
|
|
+ //天河链控制台获取
|
|
|
+
|
|
|
+ Map<String, Object> query = new HashMap<>();
|
|
|
+ query.put("address", address);
|
|
|
+ query.put("operateId", operateId);
|
|
|
+ //step1 拼接body
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+ // 请求路径,仅截取域名后及 Query 参数前部分,例:"/v1/deposit/query";
|
|
|
+ String uri = "/v1/deposit/query";
|
|
|
+ //接口签名
|
|
|
+ String signature = TianheSignUtil.signRequest(uri, query, body, timestamp, appkey);
|
|
|
+ //hutool.http
|
|
|
+ String result2 = HttpRequest.get(deposit_query_url + "?address="+address+"&operateId="+operateId)
|
|
|
+ .header("ti-appid", appid)
|
|
|
+ .header("ti-timestamp", timestamp + "")
|
|
|
+ .header("ti-signature", signature)
|
|
|
+ .execute().body();
|
|
|
+ System.out.println(result2);
|
|
|
+ /**
|
|
|
+ * {"code":200,"success":true,"data":{"operateId":"aasdas121245","status":1,
|
|
|
+ * "depositContent":"这是一个测试文档数据","type":"TEXT","digest":"673b36d75cc544a07f706bfa734ed95bfab3123124985ec5306d0ab1190d5041",
|
|
|
+ * "txHash":"0x95abd4d4d138d32d121ff0afcd11e77d5746b61cd68815128aa5caa23f2abad4","createTime":"Mon Oct 16 14:43:03 CST 2023",
|
|
|
+ * "blockNumber":1803291,"blockHash":"0x3130f3cd927f74535b4d2ba1355a8dc54745f1b344c362804db543e25b511f47"},"msg":"操作成功"}
|
|
|
+ */
|
|
|
+ return result2;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getCertificate(String address,String operateId) {
|
|
|
+ //天河链控制台获取
|
|
|
+ Map<String, Object> query = new HashMap<>();
|
|
|
+ query.put("address", address);
|
|
|
+ query.put("operateId", operateId);
|
|
|
+ //step1 拼接body
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+ // 请求路径,仅截取域名后及 Query 参数前部分,例:"/v1/deposit/query";
|
|
|
+ String uri = "/v1/deposit/certificate";
|
|
|
+ //接口签名
|
|
|
+ String signature = TianheSignUtil.signRequest(uri, query, body, timestamp, appkey);
|
|
|
+ //hutool.http
|
|
|
+ String result2 = HttpRequest.get(deposit_certificate_url + "?address="+address+"&operateId="+operateId)
|
|
|
+ .header("ti-appid", appid)
|
|
|
+ .header("ti-timestamp", timestamp + "")
|
|
|
+ .header("ti-signature", signature)
|
|
|
+ .execute().body();
|
|
|
+ System.out.println(result2);
|
|
|
+ return result2;
|
|
|
+ /**
|
|
|
+ * {"code":200,"success":true,"data":"https://test.tichain.tianhecloud.com/#/certificate?
|
|
|
+ * depositContent=这是一个测试文档数据&type=TEXT&digest=673b36d75cc544a07f706bfa734ed95bfab3123124985ec5306d0ab1190d5041&
|
|
|
+ * txHash=0x95abd4d4d138d32d121ff0afcd11e77d5746b61cd68815128aa5caa23f2abad4&address=0xa7cf21881211d7b353b3acdf92453e99dccc4212&
|
|
|
+ * createTime=1697438585000","msg":"操作成功"}
|
|
|
+ */
|
|
|
+
|
|
|
+ }
|
|
|
+}
|