BrowserController.java 3.63 KB
package cn.com.glsx.base.modules.controller;

import cn.com.glsx.admin.common.util.fileupload.FileConstants;
import cn.com.glsx.admin.common.util.fileupload.FileUploadServerData;
import cn.com.glsx.admin.common.util.fileupload.FileUploadUtil;
import com.glsx.plat.common.utils.SnowFlake;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.page.ElementHandle;
import com.ruiyun.jvppeteer.core.page.Page;
import com.ruiyun.jvppeteer.options.PageNavigateOptions;
import com.ruiyun.jvppeteer.options.ScreenshotOptions;
import com.ruiyun.jvppeteer.options.WaitForSelectorOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/api/browser")
public class BrowserController {

    @Autowired
    private Browser browser;

    @GetMapping("/screenshot")
    public R<String> screenshot(@RequestParam("url") String url,
                                String selector,
                                String filename,
                                Integer timeout,
                                Integer quality) throws IOException, InterruptedException {

        //设置默认参数
        selector = StringUtils.isNotEmpty(selector) ? selector : "selector";
        filename = StringUtils.isNotEmpty(filename) ? filename : SnowFlake.nextSerialNumber();
        timeout = timeout != null ? timeout : 1000 * 10;
        quality = quality != null ? quality : 80;
        log.info("screenshot params: url={},selector={},filename={},timeout={},quality={}", url, selector, filename, timeout, quality);

        PageNavigateOptions navigateOptions = new PageNavigateOptions();
        List<String> waitUntil = Lists.newArrayList("load", "networkidle0");
        navigateOptions.setWaitUntil(waitUntil);
        navigateOptions.setTimeout(timeout);

        Page page = browser.newPage();
        page.setDefaultTimeout(timeout);
        page.goTo(url, navigateOptions, true);

        //根据dom判断是否加载完
        WaitForSelectorOptions waitForSelectorOptions = new WaitForSelectorOptions();
        waitForSelectorOptions.setTimeout(timeout);
        waitForSelectorOptions.setVisible(Boolean.TRUE);
        ElementHandle elementHandle = page.waitForSelector(selector, waitForSelectorOptions);

        ScreenshotOptions screenshotOptions = new ScreenshotOptions();
        screenshotOptions.setType(MediaType.IMAGE_JPEG.getSubtype());
        screenshotOptions.setQuality(quality);

        //设置截图范围
        screenshotOptions.setFullPage(Boolean.TRUE);

        //保存到本地
        //screenshotOptions.setPath(filename + ".jpg");

        String base64Str = page.screenshot(screenshotOptions);

        page.close();

        byte[] buffer = Base64.getDecoder().decode(base64Str);

        FileUploadServerData uploadServerData = FileUploadUtil.uploadToFileSystem(filename, buffer, FileConstants.JPG_TYPE);

        Map<String, String> rtnMap = Maps.newHashMap();
        rtnMap.put("url", uploadServerData.getSecureUrl());
        rtnMap.put("base64", base64Str);
        return R.ok().data(rtnMap);
    }

}