Optimize front-end resource loading

1. Optimize front-end resource loading

jsHow to load resources:
1.1. Use inline pictures, that is, convert pictures to base64 encoded data-url. This method actually integrates the image information into the css file, avoiding the separate loading of image resources. But the image inlining will increase the size of the css file and increase the rendering time of the first screen.

1.2. Use js code to preload images.


preloadImage() {
    
    
    const imgList = [
        require('@/assets/imgs/error.png'),
        require('@/assets/imgs/ticket_bg.png')
    ];
    for (let i = 0; i < imgList.length; i++) {
    
    
        const newIMG = new Image();
        newIMG.src = imgList[i];
    }
}

1.3. preload/prefetchThey can assist the browser in optimizing the order and timing of resource loading and improve page performance

<head>
    ...
    <link rel="prefetch" href="static/img/ticket_bg.a5bb7c33.png">
    ...
</head>

<head>
    ...
    <link rel="preload" as="font" href="<%= require('/assets/fonts/AvenirNextLTPro-Demi.otf') %>" crossorigin>
    <link rel="preload" as="font" href="<%= require('/assets/fonts/AvenirNextLTPro-Regular.otf') %>" crossorigin>
    ...
</head>
const PreloadWebpackPlugin = require('preload-webpack-plugin');
...
plugins: [
  new PreloadWebpackPlugin({
    
    
    rel: 'preload'as(entry) {
    
      //资源类型
      if (/\.css$/.test(entry)) return 'style';
      if (/\.woff$/.test(entry)) return 'font';
      if (/\.png$/.test(entry)) return 'image';
      return 'script';
    },
    include: 'asyncChunks', // preload模块范围,还可取值'initial'|'allChunks'|'allAssets',
    fileBlacklist: [/\.svg/] // 资源黑名单
    fileWhitelist: [/\.script/] // 资源白名单
  })
]

2. OCR tools

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;

import javax.imageio.ImageIO;

import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.spiderflow.ocr.model.Ocr;

import com.baidu.aip.ocr.AipOcr;

public class OcrUtil {
    
    
	
	private static HashMap<String, AipOcr> aipOcrMap = new HashMap<String, AipOcr>();
	
	private static HashMap<String, String> options = new HashMap<String, String>();
	
	private static class ErrorCode {
    
    
		
		/**
		 * 图片格式不对
		 */
		private static final int IMAGE_FORMAT_ERROR = 216201;
		
		/**
		 * 响应无效
		 */
		private static final int URL_RESPONSE_INVALID = 282113;
		
	}
	
	static {
    
    
	    options.put("detect_direction", "true");
	    options.put("detect_language", "true");
	    options.put("probability", "true");
	}
	
	public static AipOcr getAipOcr(Ocr ocr) {
    
    
		String ocrMapKey = ocr.getAppId()+ocr.getApiKey()+ocr.getSecretKey();
		AipOcr aipOcr = aipOcrMap.get(ocrMapKey);
		if(aipOcr == null) {
    
    
			aipOcr = new AipOcr(ocr.getAppId(), ocr.getApiKey(), ocr.getSecretKey());
			aipOcrMap.put(ocrMapKey, aipOcr);
		}
		return aipOcr;
	}
	
	public static int getErrorCode(JSONObject bgJson) {
    
    
		if(!bgJson.isNull("error_code")) {
    
    
			int errorCode = bgJson.getInt("error_code");
			return errorCode;
		}
		return -996;
	}
	
	public static boolean isImageFormatError(int errorCode) {
    
    
		if(errorCode != -996) {
    
    
			// 图片格式不对
			if(errorCode == ErrorCode.IMAGE_FORMAT_ERROR) {
    
    
				return true;
			}
		}
		return false;
	}
	
	public static boolean isUrlResponseInvalid(int errorCode) {
    
    
		if(errorCode != -996) {
    
    
			if(errorCode == ErrorCode.URL_RESPONSE_INVALID) {
    
    
				return true;
			}
		}
		return false;
	}
	
	public static byte[] imgConvert(byte[] bytes) throws IOException {
    
    
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		ByteArrayInputStream input = new ByteArrayInputStream(bytes);
		ImageIO.write(ImageIO.read(input), "png", output);
		return output.toByteArray();
	}
	
	public static JSONObject ocrIdentify(Ocr ocr,byte[] bytes) throws Exception {
    
    
		try {
    
    
			AipOcr aipOcr = getAipOcr(ocr);
			JSONObject bgJson = aipOcr.basicGeneral(bytes, options);
			if(isImageFormatError(getErrorCode(bgJson))) {
    
    
				return aipOcr.basicGeneral(imgConvert(bytes), options);
			}
			return bgJson;
		} catch (Exception e) {
    
    
			throw e;
		}
	}
	
	public static JSONObject ocrIdentify(Ocr ocr,String url) throws IOException {
    
    
		AipOcr aipOcr = getAipOcr(ocr);
		JSONObject bgJson = aipOcr.basicGeneralUrl(url, options);
		if(isUrlResponseInvalid(getErrorCode(bgJson))) {
    
    
			return aipOcr.basicGeneral(imgConvert(Jsoup.connect(url).ignoreContentType(true).execute().bodyAsBytes()), options);
		}
		return bgJson;
	}
	
}

Three, underscores turn to hump

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class FieldUtils {
    
    

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    public static List<String> allFields(Class clazz){
    
    
        return Arrays.stream(clazz.getDeclaredFields()).map(item->humpToLine2(item.getName())).collect(Collectors.toList());
    }

    public static List<String> updateFields(Class clazz){
    
    
        return Arrays.stream(clazz.getDeclaredFields())
                .filter(item->item.getAnnotation(ApiUpdateField.class) != null)
                .map(item-> humpToLine2(item.getName())).collect(Collectors.toList());
    }

    /**
     * 下划线转驼峰
     */
    public static String underlineToCamel(String name){
    
    
        StringBuilder sb = new StringBuilder(name.length());
        for (int i = 0; i < name.length(); i++) {
    
    
            char c = name.charAt(i);
            if ('_' == c) {
    
    
                if (++i < name.length()){
    
    
                    sb.append(Character.toUpperCase(name.charAt(i)));
                }
            }else {
    
    
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰转下划线
     */
    public static String humpToLine2(String str) {
    
    
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
    
    
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiUpdateField {
    
    
}

Four, table structure analysis

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 表结构解析
 */
public class SqlUtils {
    
    
    public static String getByPattern(String sql, String pattern, int group) {
    
    
        Pattern compile = Pattern.compile(pattern);
        Matcher matcher = compile.matcher(sql);
        while (matcher.find()) {
    
    
            return matcher.group(group);
        }
        return null;
    }

    public static List<String> getColumnSqls(String sql) {
    
    
        List<String> lines = new ArrayList<>();
        Scanner scanner = new Scanner(sql);
        boolean start = false;
        while (scanner.hasNextLine()) {
    
    
            String nextLine = scanner.nextLine();
            if (nextLine.indexOf("CREATE TABLE") != -1) {
    
    
                start = true;
                continue;
            }
            if (nextLine.indexOf("PRIMARY KEY") != -1 || nextLine.indexOf("ENGINE=") != -1) {
    
    
                start = false;
                continue;
            }
            if (start) {
    
    
                lines.add(nextLine);
            }
        }
        return lines;
    }
}

Guess you like

Origin blog.csdn.net/qq_32447301/article/details/109265771