CommonTest.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package com.izouma.yags;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.github.kevinsawicki.http.HttpRequest;
  6. import com.google.common.reflect.TypeToken;
  7. import com.google.gson.Gson;
  8. import com.izouma.yags.camp.api.ApiResponse;
  9. import com.izouma.yags.camp.api.QueryRole;
  10. import com.izouma.yags.dto.RaexUser;
  11. import com.izouma.yags.exception.BusinessException;
  12. import com.izouma.yags.utils.RaexUtils;
  13. import com.izouma.yags.utils.RecognizeUtil;
  14. import okhttp3.*;
  15. import org.apache.commons.io.FileUtils;
  16. import org.junit.jupiter.api.Test;
  17. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  18. import org.springframework.security.crypto.password.PasswordEncoder;
  19. import javax.imageio.ImageIO;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.net.URL;
  24. import java.util.Base64;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.regex.Pattern;
  28. public class CommonTest {
  29. static {
  30. RaexUtils.setBaseUrl("https://test.raex.vip");
  31. }
  32. @Test
  33. public void testPassword() {
  34. PasswordEncoder encoder = new BCryptPasswordEncoder(4);
  35. String encoded = encoder.encode("123456");
  36. System.out.println(encoded);
  37. System.out.println(new BCryptPasswordEncoder(4).matches("123456", encoded));
  38. }
  39. @Test
  40. public void exchange() {
  41. JSONObject jsonObject = new JSONObject();
  42. JSONObject q = new JSONObject();
  43. jsonObject.put("query", q);
  44. q.put("phone", "15077886171");
  45. JSONObject page = RaexUtils.post("/user/adminAll", jsonObject);
  46. JSONArray content = page.getJSONArray("content");
  47. if (content.size() != 1) {
  48. throw new BusinessException("手机号未注册");
  49. }
  50. JSONObject user = content.getJSONObject(0);
  51. JSONObject body = new JSONObject();
  52. body.put("name", "藏品兑换");
  53. body.put("type", "asset");
  54. body.put("collectionId", 8064603);
  55. JSONArray targets = new JSONArray();
  56. body.put("targets", targets);
  57. JSONObject target = new JSONObject();
  58. targets.add(target);
  59. target.put("userId", user.getString("id"));
  60. target.put("phone", user.getString("phone"));
  61. target.put("nickname", user.getString("nickname"));
  62. target.put("num", 1);
  63. JSONObject res = RaexUtils.post("/airDrop/save", body);
  64. System.out.println(res);
  65. }
  66. @Test
  67. public void reco() {
  68. String screenShot = "https://cdn.raex.vip/image/2022-08-05-17-38-33DogytANx.png";
  69. String format;
  70. if (Pattern.matches(".*\\.(jpg|jpeg)", screenShot.toLowerCase())) {
  71. format = "jpg";
  72. } else if (Pattern.matches(".*\\.png", screenShot.toLowerCase())) {
  73. format = "png";
  74. } else if (Pattern.matches(".*\\.bmp", screenShot.toLowerCase())) {
  75. format = "bmp";
  76. } else {
  77. throw new BusinessException("截图格式不正确");
  78. }
  79. try {
  80. ByteArrayOutputStream os = new ByteArrayOutputStream();
  81. ImageIO.write(ImageIO.read(new URL(screenShot)), format, os);
  82. JSONObject res = RecognizeUtil.recognize(Base64.getEncoder().encodeToString(os.toByteArray()));
  83. System.out.println(JSON.toJSONString(res, true));
  84. } catch (Exception e) {
  85. throw new BusinessException("图片识别失败");
  86. }
  87. }
  88. @Test
  89. public void searchByPhone() {
  90. JSONObject res = RaexUtils.post("/user/searchByPhoneAdmin", new HashMap<String, Object>() {{
  91. put("phone", "15077886171");
  92. }});
  93. List<RaexUser> users = JSON.parseArray(res.getJSONArray("users").toJSONString(), RaexUser.class);
  94. System.out.println(JSON.toJSONString(users, true));
  95. }
  96. @Test
  97. public void testPyRec() throws IOException {
  98. JSONObject res = JSON.parseObject(HttpRequest.post("http://120.77.252.240:9999/?threshold=0.4")
  99. .contentType("raw")
  100. .send(FileUtils.readFileToByteArray(new File("/Users/drew/Desktop/WechatIMG3615.jpeg")))
  101. .body());
  102. System.out.println(JSON.toJSONString(res, true));
  103. }
  104. @Test
  105. public void campApi() {
  106. OkHttpClient client = new OkHttpClient();
  107. RequestBody formBody = new FormBody.Builder()
  108. .add("friendUserId", "377195948")
  109. .add("gameId", "20001")
  110. .add("token", "YGyWAchJ")
  111. .add("userId", "436174868")
  112. .build();
  113. Request request = new Request.Builder()
  114. .addHeader("userId", "436174868")
  115. .addHeader("token", "YGyWAchJ")
  116. .url("https://ssl.kohsocialapp.qq.com:10001/game/battleprofile")
  117. .post(formBody)
  118. .build();
  119. try (Response response = client.newCall(request).execute()) {
  120. Gson gson = new Gson();
  121. String body = response.body().string();
  122. ApiResponse<QueryRole> res = gson.fromJson(body, new TypeToken<ApiResponse<QueryRole>>() {
  123. }.getType());
  124. System.out.println(JSON.toJSONString(res, true));
  125. } catch (IOException e) {
  126. throw new RuntimeException(e);
  127. }
  128. }
  129. }