package com.izouma.yags; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.github.kevinsawicki.http.HttpRequest; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import com.izouma.yags.camp.api.ApiResponse; import com.izouma.yags.camp.api.QueryRole; import com.izouma.yags.dto.RaexUser; import com.izouma.yags.exception.BusinessException; import com.izouma.yags.utils.RaexUtils; import com.izouma.yags.utils.RecognizeUtil; import okhttp3.*; import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import javax.imageio.ImageIO; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.regex.Pattern; public class CommonTest { static { RaexUtils.setBaseUrl("https://test.raex.vip"); } @Test public void testPassword() { PasswordEncoder encoder = new BCryptPasswordEncoder(4); String encoded = encoder.encode("123456"); System.out.println(encoded); System.out.println(new BCryptPasswordEncoder(4).matches("123456", encoded)); } @Test public void exchange() { JSONObject jsonObject = new JSONObject(); JSONObject q = new JSONObject(); jsonObject.put("query", q); q.put("phone", "15077886171"); JSONObject page = RaexUtils.post("/user/adminAll", jsonObject); JSONArray content = page.getJSONArray("content"); if (content.size() != 1) { throw new BusinessException("手机号未注册"); } JSONObject user = content.getJSONObject(0); JSONObject body = new JSONObject(); body.put("name", "藏品兑换"); body.put("type", "asset"); body.put("collectionId", 8064603); JSONArray targets = new JSONArray(); body.put("targets", targets); JSONObject target = new JSONObject(); targets.add(target); target.put("userId", user.getString("id")); target.put("phone", user.getString("phone")); target.put("nickname", user.getString("nickname")); target.put("num", 1); JSONObject res = RaexUtils.post("/airDrop/save", body); System.out.println(res); } @Test public void reco() { String screenShot = "https://cdn.raex.vip/image/2022-08-05-17-38-33DogytANx.png"; String format; if (Pattern.matches(".*\\.(jpg|jpeg)", screenShot.toLowerCase())) { format = "jpg"; } else if (Pattern.matches(".*\\.png", screenShot.toLowerCase())) { format = "png"; } else if (Pattern.matches(".*\\.bmp", screenShot.toLowerCase())) { format = "bmp"; } else { throw new BusinessException("截图格式不正确"); } try { ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(ImageIO.read(new URL(screenShot)), format, os); JSONObject res = RecognizeUtil.recognize(Base64.getEncoder().encodeToString(os.toByteArray())); System.out.println(JSON.toJSONString(res, true)); } catch (Exception e) { throw new BusinessException("图片识别失败"); } } @Test public void searchByPhone() { JSONObject res = RaexUtils.post("/user/searchByPhoneAdmin", new HashMap() {{ put("phone", "15077886171"); }}); List users = JSON.parseArray(res.getJSONArray("users").toJSONString(), RaexUser.class); System.out.println(JSON.toJSONString(users, true)); } @Test public void testPyRec() throws IOException { JSONObject res = JSON.parseObject(HttpRequest.post("http://120.77.252.240:9999/?threshold=0.4") .contentType("raw") .send(FileUtils.readFileToByteArray(new File("/Users/drew/Desktop/WechatIMG3615.jpeg"))) .body()); System.out.println(JSON.toJSONString(res, true)); } @Test public void campApi() { OkHttpClient client = new OkHttpClient(); RequestBody formBody = new FormBody.Builder() .add("friendUserId", "377195948") .add("gameId", "20001") .add("token", "YGyWAchJ") .add("userId", "436174868") .build(); Request request = new Request.Builder() .addHeader("userId", "436174868") .addHeader("token", "YGyWAchJ") .url("https://ssl.kohsocialapp.qq.com:10001/game/battleprofile") .post(formBody) .build(); try (Response response = client.newCall(request).execute()) { Gson gson = new Gson(); String body = response.body().string(); ApiResponse res = gson.fromJson(body, new TypeToken>() { }.getType()); System.out.println(JSON.toJSONString(res, true)); } catch (IOException e) { throw new RuntimeException(e); } } }