From 0ef4d935573809e1556b850366f1c83aae321b07 Mon Sep 17 00:00:00 2001 From: long <18782126717@163.com> Date: Wed, 25 May 2022 11:32:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 202 ++++++++++ .../java/com/nft/ConfluxNftApplication.java | 15 + src/main/java/com/nft/util/AESUtil.java | 176 +++++++++ src/main/java/com/nft/util/AddressUtil.java | 27 ++ src/main/java/com/nft/util/App.java | 91 +++++ src/main/java/com/nft/util/CfxUtils.java | 149 +++++++ src/main/java/com/nft/util/KeyChainId.java | 7 + src/main/java/com/nft/util/OkHttpsUtils.java | 370 ++++++++++++++++++ .../java/com/nft/util/swagger2/Swagger2.java | 56 +++ src/main/resources/application.yml | 45 +++ 10 files changed, 1138 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/nft/ConfluxNftApplication.java create mode 100644 src/main/java/com/nft/util/AESUtil.java create mode 100644 src/main/java/com/nft/util/AddressUtil.java create mode 100644 src/main/java/com/nft/util/App.java create mode 100644 src/main/java/com/nft/util/CfxUtils.java create mode 100644 src/main/java/com/nft/util/KeyChainId.java create mode 100644 src/main/java/com/nft/util/OkHttpsUtils.java create mode 100644 src/main/java/com/nft/util/swagger2/Swagger2.java create mode 100644 src/main/resources/application.yml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c97918c --- /dev/null +++ b/pom.xml @@ -0,0 +1,202 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.0.RELEASE + + + com.nft + conflux-nft + 1.0-SNAPSHOT + conflux-nft + conflux-nft + + 8 + 8 + 1.8 + + + + org.springframework.boot + spring-boot-starter-aop + + + + io.springfox + springfox-swagger2 + 2.8.0 + + + + + io.springfox + springfox-swagger-ui + 2.8.0 + + + + cn.hutool + hutool-all + 5.0.6 + + + commons-codec + commons-codec + 1.10 + + + com.alibaba + fastjson + 1.2.57 + + + org.web3j + core + 4.5.11 + + + org.bitcoinj + bitcoinj-core + 0.15.3 + compile + + + org.springframework.boot + spring-boot-starter-data-redis + + + org.apache.commons + commons-lang3 + 3.4 + + + commons-collections + commons-collections + 3.2.1 + + + + + com.baomidou + mybatisplus-spring-boot-starter + 1.0.5 + + + + com.baomidou + mybatis-plus + 2.1.8 + + + + org.apache.velocity + velocity + 1.7 + + + + com.alibaba + druid + 1.0.26 + + + mysql + mysql-connector-java + 8.0.19 + + + + + org.springframework.boot + spring-boot-starter-web + + + + com.google.code.gson + gson + 2.8.2 + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.2 + + + + commons-collections + commons-collections + 3.2.1 + + + com.google.guava + guava + 21.0 + + + + org.projectlombok + lombok + 1.18.4 + + + + io.github.conflux-chain + conflux.web3j + 1.1.0 + + + com.squareup.okhttp3 + okhttp + 4.9.3 + + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.2.5 + + + org.mybatis + mybatis + + + + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + 4.13.2 + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + + true + + + + + \ No newline at end of file diff --git a/src/main/java/com/nft/ConfluxNftApplication.java b/src/main/java/com/nft/ConfluxNftApplication.java new file mode 100644 index 0000000..03bea51 --- /dev/null +++ b/src/main/java/com/nft/ConfluxNftApplication.java @@ -0,0 +1,15 @@ +package com.nft; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +@SpringBootApplication(scanBasePackages = {"com.nft"}) +@MapperScan({"com.nft.mapper"}) +@EnableScheduling +public class ConfluxNftApplication { + public static void main(String[] args) { + SpringApplication.run(ConfluxNftApplication.class, args); + } +} diff --git a/src/main/java/com/nft/util/AESUtil.java b/src/main/java/com/nft/util/AESUtil.java new file mode 100644 index 0000000..2184122 --- /dev/null +++ b/src/main/java/com/nft/util/AESUtil.java @@ -0,0 +1,176 @@ +package com.nft.util; + +import lombok.extern.slf4j.Slf4j; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; +import java.security.SecureRandom; + +/** + * AES加密解密工具类(提供对称加密) + */ +@Slf4j +public final class AESUtil { + // 加密密钥 + private static String KEY = "zyxXV3mYqgEuXk02IJQnLwQ7cb2LkOSv"; + // 默认字符集 + private static final String DEFAULT_CHARSET = "UTF-8"; + // 加密方式 + private static final String KEY_AES = "AES"; + // 加密模式 + private static final int EN_CODE = Cipher.ENCRYPT_MODE; + // 解密模式 + private static final int DE_CODE = Cipher.DECRYPT_MODE; + + /** + * k + * 加密 + * + * @param data 需要加密的数据 + * @return 返回加密后的结果 + */ + public static String encrypt(String data) throws Exception { + return doAES(data, KEY, EN_CODE); + } + + /** + * 解密 + * + * @param data 需要解密的数据 + * @return 返回解密后的结果 + */ + public static String decrypt(String data) throws Exception { + return doAES(data, KEY, DE_CODE); + } + + /** + * 加密 + * + * @param data 需要加密的数据 + * @param key 需要加密的密钥 + * @return 返回解密后的结果 + */ + public static String encrypt(String data, String key) throws Exception { + return doAES(data, key, EN_CODE); + } + + /** + * 解密 + * + * @param data 需要解密的数据 + * @param key 需要解密的密钥 + * @return 返回解密后的结果 + */ + public static String decrypt(String data, String key) throws Exception { + return doAES(data, key, DE_CODE); + } + + /** + * 加密或解密方法 + * + * @param data 需要加密或解密的内容 + * @param key 加密或解密的密钥 + * @param model 处理模式 + * @return 返回处理结果 + */ + private static String doAES(String data, String key, final int model) throws Exception { + try { + data = data == null ? "" : data; + key = key == null ? "" : key; + + if ("".equalsIgnoreCase(data) || "".equalsIgnoreCase(key)) { + return null; + } + + // 构造密钥生成器,指定为AES方式,不区分大小写 + KeyGenerator kgen = KeyGenerator.getInstance(KEY_AES); + + // 根据EcnodeRules规则初始化密钥生成器 + // 根据传入的字节数组,生成一个128位的随机源 + SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); + random.setSeed(key.getBytes("UTF-8")); + +// kgen.init(128, new SecureRandom(key.getBytes(DEFAULT_CHARSET))); + + kgen.init(128, random); + + // 产生原始对称密钥 + SecretKey secretKey = kgen.generateKey(); + + // 获得原始对称密钥的字节数组 + byte[] enCodeFormat = secretKey.getEncoded(); + + // 根据字节数组生成AES密钥 + SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, KEY_AES); + + // 根据指定的AES算法创建密码器 + Cipher cipher = Cipher.getInstance(KEY_AES); + + // 初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY + cipher.init(model, keySpec); + + byte[] content; // 用于存储加密内容 + byte[] result; + switch (model) { + case EN_CODE: + content = data.getBytes(DEFAULT_CHARSET); + result = cipher.doFinal(content); + + // 将二进制转换成16进制 + return parseByte2HexStr(result); + case DE_CODE: + content = parseHexStr2Byte(data); + result = cipher.doFinal(content); + + // 将二进制转为字符串 + return new String(result, DEFAULT_CHARSET); + default: + throw new RuntimeException("The Encrypt Model Is Wrong!"); + } + } catch (Exception e) { + e.printStackTrace(); + log.info("====error" + org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e)); + return null; + } + } + + /** + * 将二进制转换成16进制 + * + * @param buf 二进制数据 + * @return 返回16进制结果 + */ + public static String parseByte2HexStr(byte[] buf) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < buf.length; i++) { + String hex = Integer.toHexString(buf[i] & 0xFF); + if (hex.length() == 1) { + hex = '0' + hex; + } + sb.append(hex.toUpperCase()); + } + return sb.toString(); + } + + /** + * 将16进制转换为二进制 + * + * @param hexStr 16进制字符串 + * @return 返回二进制数据 + */ + public static byte[] parseHexStr2Byte(String hexStr) { + if (hexStr.length() < 1) { + return null; + } + byte[] result = new byte[hexStr.length() / 2]; + for (int i = 0; i < hexStr.length() / 2; i++) { + int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); + int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); + result[i] = (byte) (high * 16 + low); + } + return result; + } + +} diff --git a/src/main/java/com/nft/util/AddressUtil.java b/src/main/java/com/nft/util/AddressUtil.java new file mode 100644 index 0000000..f81e3b2 --- /dev/null +++ b/src/main/java/com/nft/util/AddressUtil.java @@ -0,0 +1,27 @@ +package com.nft.util; + +public class AddressUtil { + + + public static String getNewTypeAddress(String old, int chainId) { + conflux.web3j.types.Address a = new conflux.web3j.types.Address(old, chainId); + return a.getVerboseAddress(); + } + + public static String getNewAddress(String old, int chainId) { + conflux.web3j.types.Address a = new conflux.web3j.types.Address(old, chainId); + return a.getAddress(); + } + + public static String getNewAddress(String typeAddress) { + conflux.web3j.types.Address a = new conflux.web3j.types.Address(typeAddress); + return a.getAddress(); + } + + + public static String getHexAddress(String typeAddress) { + conflux.web3j.types.Address a = new conflux.web3j.types.Address(typeAddress); + return a.getHexAddress(); + } + +} diff --git a/src/main/java/com/nft/util/App.java b/src/main/java/com/nft/util/App.java new file mode 100644 index 0000000..dd0201f --- /dev/null +++ b/src/main/java/com/nft/util/App.java @@ -0,0 +1,91 @@ +package com.nft.util; + +import com.alibaba.fastjson.JSONObject; +import conflux.web3j.Account; +import conflux.web3j.AccountManager; +import conflux.web3j.Cfx; +import conflux.web3j.CfxUnit; +import conflux.web3j.contract.ContractCall; +import conflux.web3j.response.Receipt; +import conflux.web3j.response.Transaction; +import conflux.web3j.types.CfxAddress; +import conflux.web3j.types.RawTransaction; +import conflux.web3j.types.TransactionBuilder; +import conflux.web3j.types.Address; +import okhttp3.*; +import org.web3j.abi.datatypes.generated.Uint256; + +import java.io.IOException; +import java.math.BigInteger; +import java.util.Optional; + +public class App { + public static final Cfx cfx = Cfx.create("https://test.confluxrpc.com"); + public static final String privateKey="cfxtest:aapjjbzf3bumeatch25bduathv0xz9wk42e8yzd4r1"; + public static void main(String[] args) throws Exception { + getinit(); + + + } + + /** + * 获取初始数据 + */ + public static void getinit() { +// BigInteger currentEpochNumber = cfx.getEpochNumber().sendAndGet(); +// System.out.printf("Current epoch number %d--------->", currentEpochNumber); +// //获取账户余额和随机数 +// Address addr = new Address("cfxtest:aapjjbzf3bumeatch25bduathv0xz9wk42e8yzd4r1");//onwer +// //余额 +// BigInteger balance = cfx.getBalance(addr).sendAndGet(); +// // nonce will be used when sending transaction +// //随机数 +// BigInteger nonce = cfx.getNonce(addr).sendAndGet(); +// System.out.println("余额balance result:"+balance); +// System.out.println("随机数result:"+nonce); +// //获取交易和收据 + + } + // + public static void createAccountManager(){ + try { + AccountManager ac = new AccountManager(KeyChainId.CHAINID); + Address addr = new Address(privateKey); + String privatekey = ac.exportPrivateKey(addr, "cfxtest"); + + } catch (Exception e) { + e.printStackTrace(); + } + } + //创建一个账户 + public static void createAccount(){ + Account a1 = Account.create(cfx, privateKey); + } + //获取交易和收据 + public static void getReceipt() { + String txhash = "0x10c3c6191f2a1960a14899ce01969aee3b1eca519901a7a92cea092769596449";// deploy后的文件下 "stateRoot": "0x10c3c6191f2a1960a14899ce01969aee3b1eca519901a7a92cea092769596449", + Optional tx = cfx.getTransactionByHash(txhash).sendAndGet(); + Optional receipt = cfx.getTransactionReceipt(txhash).sendAndGet(); + System.out.println("交易:" + tx.toString()); + System.out.println("收据:" + receipt.toString()); + } + + //用ER721标准 + public static void getERinit() { +// ERC721 erc721 = new ERC721(cfx, new CfxAddress(nftAddress), account); + + } +// //转移资产 +// public static void move(){ +// Address addr = new Address("cfxtest:aak2rra2njvd77ezwjvx04kkds9fzagfe6d5r8e957"); +// // transfer 1 CFX to addr, the transaction hash will returned +// //String hash = account.transfer(addr, CfxUnit.cfx2Drip(1)); +// +// // An optional parameter can be used to specify other fields of a transaction +// Account.Option op = new Account.Option(); +// op.withGasPrice(CfxUnit.DEFAULT_GAS_PRICE); +// BigInteger nonce = cfx.getNonce(account.getAddress()).sendAndGet(); +// BigInteger value = CfxUnit.cfx2Drip(1); +// BigInteger currentEpochNumber = cfx.getEpochNumber().sendAndGet(); +// } +} diff --git a/src/main/java/com/nft/util/CfxUtils.java b/src/main/java/com/nft/util/CfxUtils.java new file mode 100644 index 0000000..0c2a197 --- /dev/null +++ b/src/main/java/com/nft/util/CfxUtils.java @@ -0,0 +1,149 @@ +package com.nft.util; + +import org.apache.commons.lang3.StringUtils; +import org.web3j.abi.EventEncoder; +import org.web3j.abi.TypeReference; +import org.web3j.abi.datatypes.*; +import org.web3j.abi.datatypes.generated.Bytes32; +import org.web3j.abi.datatypes.generated.Uint256; +import org.web3j.abi.datatypes.generated.Uint32; +import org.web3j.utils.Numeric; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; + +/** + * @author wangban + */ +public class CfxUtils { + + public static final String ADDRESS = "address"; + public static final String UINT256 = "uint256"; + public static final String UINT32 = "uint32"; + public static final String Utf8String = "string"; + public static final String BOOL = "bool"; + public static final String BYTES = "bytes"; + public static final String BYTES32 = "bytes32"; + public static final String ARRAY = "array"; + public static final String UINT256_ARRAY = "uint256Array"; + public static final String ADDRESS_ARRAY = "addressArray"; + + public static String getEventKeccak(String method, List inputParams) { + Event event = new Event(method, getTypes(inputParams)); + return EventEncoder.encode(event); + } + + public static String getUtf8String(String data) { + return new String(Numeric.hexStringToByteArray(data)).trim(); + } + + public static List getEventParams(String data) { + if (data.startsWith("0x")) { + data = data.substring(2); + } + List result = new ArrayList<>(); + result.add(data); + int length = data.length() / 64; + for (int i = 0; i < length; i++) { + String param = data.substring(i * 64, (i + 1) * 64); + BigInteger b = new BigInteger(param, 16); + result.add(b.toString(16)); + } +// System.out.println(result); + return result; + } + + public static String getAddress(String address) { + if (address.length() > 42) { + address = address.substring(26); + } + if (address.startsWith("0x")) { + return address; + } + if (StringUtils.isEmpty(address) || "0".equals(address)) { + return "0x0000000000000000000000000000000000000000"; + } + return "0x" + address; + } + + public static void main(String[] args) { + System.out.println(getAddress("0x0000000000000000000000001cba0557e0b38e595588b0f8a3d7c89de992e4e3")); + } + + public static BigInteger getChainId(BigInteger chainId){ + if(chainId.compareTo(BigInteger.ONE)==0){ + return new BigInteger("10001"); + }else { + return new BigInteger("11029"); + } + } + + public static BigInteger getNumber(String num) { + if (num.contains("0x")) { + num = num.substring(2); + } + return new BigInteger(num, 16); + } + + public static Integer getIntNumber(String num) { + if (num.contains("0x")) { + num = num.substring(2); + } + return new BigInteger(num, 16).intValue(); + } + + + private static List> getTypes(List inputParams) { + List> parameters = new ArrayList<>(); + for (int i = 0; i < inputParams.size(); i++) { + String in = inputParams.get(i); + switch (in) { + case ADDRESS: + parameters.add(new TypeReference
() { + }); + break; + case UINT256: + parameters.add(new TypeReference() { + }); + break; + case UINT32: + parameters.add(new TypeReference() { + }); + break; + case Utf8String: + parameters.add(new TypeReference() { + }); + break; + case BOOL: + parameters.add(new TypeReference() { + }); + break; + case BYTES: + parameters.add(new TypeReference() { + }); + break; + case BYTES32: + parameters.add(new TypeReference() { + }); + break; + case ARRAY: + parameters.add(new TypeReference() { + }); + break; + case UINT256_ARRAY: + parameters.add(new TypeReference>() { + }); + break; + case ADDRESS_ARRAY: + parameters.add(new TypeReference>() { + }); + break; + default: + break; + } + } + return parameters; + } + +} diff --git a/src/main/java/com/nft/util/KeyChainId.java b/src/main/java/com/nft/util/KeyChainId.java new file mode 100644 index 0000000..10a47f2 --- /dev/null +++ b/src/main/java/com/nft/util/KeyChainId.java @@ -0,0 +1,7 @@ +package com.nft.util; + +public class KeyChainId { + //chainid + public static final int CHAINID=1; + +} diff --git a/src/main/java/com/nft/util/OkHttpsUtils.java b/src/main/java/com/nft/util/OkHttpsUtils.java new file mode 100644 index 0000000..5e7c787 --- /dev/null +++ b/src/main/java/com/nft/util/OkHttpsUtils.java @@ -0,0 +1,370 @@ +package com.nft.util; + +import com.alibaba.fastjson.JSONObject; +import okhttp3.*; +import org.apache.commons.lang3.StringUtils; + +import javax.net.ssl.*; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.security.cert.X509Certificate; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + + +public class OkHttpsUtils { + + private static OkHttpClient mOkHttpClient; + + /** + * 网络访问连接池 + */ + private static ConnectionPool pool; + + /** + * get 请求 + * + * @param url 请求地址 + * @return 返回结果 + */ + public static String getRequest(String url, String appKey) { + try { + + Request.Builder builder = new Request.Builder(); + if (!StringUtils.isEmpty(appKey)) { + builder.addHeader("Authorization", "APPCODE" + appKey); + } + builder.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"); + builder.url(url); + final Request request = builder.build(); + //new call + Call call = getOkHttpInstance().newCall(request); + //请求加入调度 + Response response = call.execute(); + return response.body().string(); + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + } + + /** + * get 请求 InputStream + * + * @param url 请求地址 + * @return 返回结果 + */ + public static InputStream getInputStreamRequest(String url) { + try { + + Request.Builder builder = new Request.Builder(); + builder.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"); + builder.url(url); + final Request request = builder.build(); + //new call + Call call = getOkHttpInstance().newCall(request); + //请求加入调度 + Response response = call.execute(); + return response.body().byteStream(); + } catch (IOException e) { + System.err.println(e.getMessage()); + return null; + } + } + + private static OkHttpClient getOkHttpInstance() { + if (mOkHttpClient == null) { + synchronized (OkHttpsUtils.class) { + //创建okHttpClient对象 + mOkHttpClient = getUnsafeOkHttpClient(); + } + } + return mOkHttpClient; + } + + + //okHttp3添加信任所有证书 + + public static OkHttpClient getUnsafeOkHttpClient() { + + X509TrustManager x509 = new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[]{}; + } + }; + + try { + final TrustManager[] trustAllCerts = new TrustManager[]{x509}; + + final SSLContext sslContext = SSLContext.getInstance("SSL"); + sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); + final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); + OkHttpClient.Builder builder = new OkHttpClient.Builder() + .connectTimeout(3, TimeUnit.MINUTES) + .writeTimeout(3, TimeUnit.MINUTES) + .readTimeout(3, TimeUnit.MINUTES); + builder.sslSocketFactory(sslSocketFactory, x509); + + builder.hostnameVerifier(new HostnameVerifier() { + @Override + public boolean verify(String hostname, SSLSession session) { + return true; + } + }); + + return builder.build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + /** + * 获取网络访问连接池 + * + * @return + */ + private static ConnectionPool getConnectionPool() { + if (pool == null) { + synchronized (OkHttpsUtils.class) { + pool = new ConnectionPool(5, 10, TimeUnit.MINUTES); + } + } + return pool; + } + + + /** + * get 请求 含代理 + * + * @param url 请求地址 + * @return 返回结果 + */ + public static String getRequestByProxy(String url, String host, int port) { + try { + //创建okHttpClient对象 + OkHttpClient.Builder b = new OkHttpClient.Builder(); + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)); + b.proxy(proxy); + //创建一个Request + Request.Builder builder = new Request.Builder(); + builder.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"); + builder.url(url); + final Request request = builder.build(); + //new call + Call call = getOkHttpInstance().newCall(request); + //请求加入调度 + Response response = call.execute(); + return response.body().string(); + } catch (IOException e) { + System.err.println(e.getMessage()); + return ""; + } + } + + + public static String uploadObject(String url, File file, String token) throws IOException { + MediaType mediaType = MediaType.parse("image/*"); + RequestBody requestBody = RequestBody.create(mediaType, file); + + Request request = new Request.Builder() + .url(url) + .post(requestBody) + .addHeader("Authorization", token) + .build(); + + Call call = getOkHttpInstance().newCall(request); + Response response = call.execute(); + return response.body().string(); + } + + /** + * 一般post请求 + * + * @param url 请求路径 + * @param params 请求参数 + * @param token 消息头 + * @return 返回结果 + */ + public static String postRequest(String url, Map params, String token) { + try { + + FormBody.Builder formBodyBuilder = new FormBody.Builder(); + Set keySet = params.keySet(); + for (String key : keySet) { + String value = params.get(key); + formBodyBuilder.add(key, value); + } + FormBody formBody = formBodyBuilder.build(); + + Request.Builder builder = new Request.Builder(); + if (!StringUtils.isEmpty(token)) { + builder.addHeader("Authorization", token); + } + Request request = builder.url(url) + .post(formBody).build(); + + Call call = getOkHttpInstance().newCall(request); + + Response response = call.execute(); + return response.body().string(); + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + } + + /** + * 一般post请求 + * + * @param url 请求路径 + * @param params 请求参数 + * @param token 消息头 + * @return 返回结果 + */ + public static JSONObject postRequestObj(String url, Map params, String token) { + try { + + FormBody.Builder formBodyBuilder = new FormBody.Builder(); + Set keySet = params.keySet(); + for (String key : keySet) { + String value = params.get(key); + formBodyBuilder.add(key, value); + } + FormBody formBody = formBodyBuilder.build(); + + Request.Builder builder = new Request.Builder(); + if (!StringUtils.isEmpty(token)) { + builder.addHeader("Authorization", token); + } + Request request = builder.url(url) + .post(formBody).build(); + + Call call = getOkHttpInstance().newCall(request); + + Response response = call.execute(); + String s = response.body().string(); + + return JSONObject.parseObject(s); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + /** + * post传json参数 + * + * @param url 请求路径 + * @param json 请求的json数据 + * @return 返回结果 + */ + public static String postJsonAuth(String url, String json, String token) { + try { + + //MediaType 设置Content-Type 标头中包含的媒体类型值 + RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") + , json); + + Request.Builder builder = new Request.Builder(); + if (!StringUtils.isEmpty(token)) { + builder.addHeader("Authorization", token); + } + Request request = builder + //请求的url + .url(url) + .post(requestBody) + .build(); + + //创建/Call + Call call = getOkHttpInstance().newCall(request); + Response response = call.execute(); + return response.body().string(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + + /** + * post传json参数 + * + * @param url 请求路径 + * @param json 请求的json数据 + * @return 返回结果 + */ + public static String postJsonResponse(String url, String json) { + try { + + //MediaType 设置Content-Type 标头中包含的媒体类型值 + RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") + , json); + + Request request = new Request.Builder() + //请求的url + .url(url) + .post(requestBody) + .build(); + + //创建/Call + Call call = getOkHttpInstance().newCall(request); + Response response = call.execute(); + return response.body().string(); + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + } + + /** + * 一般post请求 + * + * @param url 请求路径 + * @param params 请求参数 + * @return 返回结果 + */ + public static String postRequest(String url, Map params) { + try { + + FormBody.Builder formBodyBuilder = new FormBody.Builder(); + Set keySet = params.keySet(); + Request.Builder builder = new Request.Builder(); + for (String key : keySet) { + String value = params.get(key); + if (!StringUtils.isEmpty(value)) { + builder.addHeader(key, value); + } + } + FormBody formBody = formBodyBuilder.build(); + + Request request = builder.url(url) + .post(formBody).build(); + + Call call = getOkHttpInstance().newCall(request); + + Response response = call.execute(); + String s = response.body().string(); + + return s; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + +} + diff --git a/src/main/java/com/nft/util/swagger2/Swagger2.java b/src/main/java/com/nft/util/swagger2/Swagger2.java new file mode 100644 index 0000000..31035af --- /dev/null +++ b/src/main/java/com/nft/util/swagger2/Swagger2.java @@ -0,0 +1,56 @@ +package com.nft.util.swagger2; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.service.Parameter; +import springfox.documentation.service.Tag; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +@ComponentScan("com.nft.api") +public class Swagger2 { + @Value("${swagger.show}") + private boolean swaggerShow; + + private Tag[] getTags() { + Tag[] tags = { +// new Tag("test", "测试服务"), + }; + return tags; + } + + @Bean + public Docket createRestApi() { + List pars = new ArrayList<>(); + return new Docket(DocumentationType.SWAGGER_2) + .enable(swaggerShow) + .apiInfo(apiInfo()) +// .tags(new Tag("test", "test"), getTags()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.nft.api")) + .paths(PathSelectors.any()) + .build() + .globalOperationParameters(pars); + } + + private ApiInfo apiInfo() { + Contact contact = new Contact("ban", "", ""); + return new ApiInfoBuilder().title("pl 接口列表") + .description("API调用") + .termsOfServiceUrl("http://") + //.contact(contact) + .version("1.0") + .build(); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..38aba91 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,45 @@ +server: + port: 9008 +spring: + application: + name: confluxNft + redis: + host: 127.0.0.1 + port: 6379 + password: + jedis: + pool: + max-active: 200 + max-wait: -1 + max-idle: 10 + database: 1 + datasource: + url: jdbc:mysql://182.92.73.21:3380/conflux_nft?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC + username: root + password: guolong09aa@1345225844@.@. + type: com.alibaba.druid.pool.DruidDataSource + servlet: + multipart: + max-file-size: 100MB + max-request-size: 100MB +mybatis: + mapper-locations: classpath*:/mapper/**/*.xml + configuration: + map-underscore-to-camel-case: true +#测试环境 +confluxNft: + #合约 + contract: cfxtest:acbctakv35gyzcepnh14k239hw6tcw2t7edv34adn3 + # 执行地址 + owner: cfxtest:aapjjbzf3bumeatch25bduathv0xz9wk42e8yzd4r1 + ###私钥位置 + privateKey: 0xa31115fa5cf8fefd57fa0dea7ff013e2d71b8cf13a4d6a7e276c9c5b21bf911b +swagger: + show: true +# mybatis-plus +mybatis-plus: + mapper-locations: classpath*:/mapper/**/*.xml + type-aliases-package: com.nft.entity + global-config: + logic-delete-value: 1 + logic-not-delete-value: 0