Newer
Older
newfiber-termite / newfiber-termites / newfiber-termites-package / src / test / java / CommonTest.java
@xiongkai xiongkai on 17 Oct 5 KB bug修复
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.newfiber.common.core.constant.SecurityConstants;
import com.newfiber.common.core.utils.JwtUtils;
import com.newfiber.common.core.utils.file.FileUtils;
import com.newfiber.common.core.utils.uuid.IdUtils;
import com.newfiber.common.security.utils.PwdUtil;
import com.newfiber.common.security.utils.SecurityUtils;
import com.newfiber.termite.domain.jingchu.JingChuUpload;
import com.newfiber.termite.domain.request.deviceInfo.DeviceInfoStateSyncRequest;
import com.newfiber.termite.util.CosUtil;
import io.jsonwebtoken.Claims;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
public class CommonTest {

    @Test
    public void fileUploadTest(){
        MultipartFile file = FileUtils.getMultipartFile(new File("C:\\Users\\Administrator\\Pictures\\201210133348076.jpg"));
        System.out.println(CosUtil.putFile(file));
    }

    @Test
    public void jingchuTest(){
        String url = "https://sk.hubeishuiyi.cn/bylr";
        String token = "eyJraWQiOiJrMSIsImFsZyI6IlJTMjU2In0.eyJpc3MiOiJhZG1pbkBnaXN1bmkuY29tIiwiYXVkIjoidXVtcyIsImp0aSI6IjYwMGZiYmMyLWE1NDgtNGZmNC04ZGNhLTZkNTdkZjY1OTYxNyIsImlhdCI6MTcxNzcyNDA1MywibmJmIjoxNzE3NzIzNDUzLCJzdWIiOiJzdXBlciIsImV4cCI6MTcxNzc4NTQ5Mywia2V5IjoiMmQ5ZWY2MTNlYTkxNDc4MDhlOGZiYTMxYWNlOTE0NmEiLCJpZCI6IjEiLCJuYW1lIjoic3VwZXIiLCJuaWNrTmFtZSI6Iui2hee6p-euoeeQhuWRmCIsImFjY291bnQiOiJzdXBlciIsInR5cGUiOiIyIiwiY2xpZW50IjpudWxsLCJjbGllbnRfaWQiOiIiLCJ0b2tlblR5cGUiOiJVU0VSIn0.VzETse76m1rkRsMjst9kDr1oCnHrPq81pRs-34bjVXPDRiPT6ekVCm-63hLfoH4GAp25oTL9dWA1ax-t1KWlSzHUn82myEBokwCVWlkZZPVMzFMCgP_SNdFeYlvguVhige1nQ6B0wo5crGYKKsfllt0_bp98TFg3I3zF03WsORk7HlsZfsY7ROwYkXXkIgbdw5gIRYlZJ6W1MiMET9TOyAzWGJvEyTPewaW4XMoq-okZoAe_7gV2u_tylngqRCohxTSuA7xf59gK8OrstwTeId_qQp4remoEDdLJfYyzLXCX5xBKns8gw2i1MZfpq9rizDT-Qdz23VQDQdmeHWK7zQ";

        JingChuUpload jingChuUpload = new JingChuUpload();
        jingChuUpload.setMnNo("4202813006");
        jingChuUpload.setObDate(DateUtil.formatDateTime(new Date()).replace(" ", "T"));
        jingChuUpload.setObTime(DateUtil.format(new Date(), "yyMMddHHmm"));
        jingChuUpload.setOrder("123456");
        jingChuUpload.setReceiveType(0);
        jingChuUpload.setStationId(1625);
        jingChuUpload.setStationName("大晚垅水库");
        jingChuUpload.setDeviceId(5858);
        jingChuUpload.setDeviceName("大晚垅水库白蚁监测站");

        List<JingChuUpload> jingChuUploadList = new ArrayList<>();
        jingChuUploadList.add(jingChuUpload);
        HttpResponse httpResponse = HttpUtil.createPost(url.concat("/waterDataTermites/insertDataBatch")).
                body(JSONObject.toJSONString(jingChuUploadList)).timeout(1000).
                header("Authorization", token).execute();
        log.info("同步荆楚监测数据.请求:{},响应:{}", JSONObject.toJSONString(jingChuUploadList), httpResponse.body());

    }

    @Test
    public void imgUtil(){
        Font font = new Font("微软雅黑", Font.BOLD, 50);

//        ImgUtil.pressText(new File("C:\\Users\\Administrator\\Downloads\\capture_202407.jpg"),
//                new File("C:\\Users\\Administrator\\Downloads\\capture_202407_ex.jpg"),
//                "Hello\r\n" + "World", Color.WHITE, font, -1, -1, 1f);

        Image srcImage = ImgUtil.read(new File("C:\\Users\\Administrator\\Downloads\\capture_202407.jpg"));
        Img img = Img.from(srcImage);
        img.setPositionBaseCentre(false);
        img = img.pressText("项目:皇田水库", Color.WHITE, font, 0, 900, 1f);
        img = img.pressText("位置:3-5", Color.WHITE, font, 0, 1000, 1f);
        img = img.pressText("时间:2024/05/01 13:00", Color.WHITE, font, 0, 1100, 1f);

        ImgUtil.write(img.getImg(), new File("C:\\Users\\Administrator\\Downloads\\capture_202407_ex.jpg"));
    }

    @Test
    public void jsonTest(){
        String str = "{\"sn\":\"123\",\"AP\":\"123\"}";
        DeviceInfoStateSyncRequest request = JSONObject.parseObject(str, DeviceInfoStateSyncRequest.class);
        System.out.println(1);
    }

    @Test
    public void jwtTest(){
        Map<String, Object> claimsMap = new HashMap<String, Object>();
        claimsMap.put(SecurityConstants.USER_KEY, IdUtils.fastUUID());
        claimsMap.put(SecurityConstants.DETAILS_USER_ID, 1);
        claimsMap.put(SecurityConstants.DETAILS_USERNAME, "admin");

        // 接口返回信息
        Map<String, Object> rspMap = new HashMap<String, Object>();
        rspMap.put("access_token", JwtUtils.createToken(claimsMap));
        rspMap.put("expires_in", 1);

        System.out.println(rspMap.get("access_token"));
    }

    @Test
    public void checkToken(){
        Claims claims = JwtUtils.parseToken("eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjJkNWVlNTM1LWViMDAtNGE3My05ZjY5LWE3YmJhZjY5ZDBhOCIsInVzZXJuYW1lIjoiYWRtaW4ifQ.lNVhnljLEsJKQCtvWazCDpWHDPKCshz6XUgAnpVkCbN0fwM3wZ_wU3aKl8YS3ravoZ1ifW7MVFArphDaQeIAQg");

        System.out.println("1");
    }

    @Test
    public void passTest(){
        String password = SecurityUtils.transferAesEncrypt("Q+AX0BXUxw/bn9mZRVib2w==");
        PwdUtil.passwordCheck(password);
        System.out.println(SecurityUtils.encryptPassword(password));
    }
}