IndexController.java 3.1 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
package com.boco.nbd.wios.manage.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.boco.nbd.cams.core.config.RedisClient;
import com.boco.nbd.wios.manage.util.CaptchaUtilsEx;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

/**
 * 首页
 *
 * @author lilin
 * @version [版本号, 2018年11月28日]
 */
@RestController
public class IndexController {

    @Autowired
    private RedisClient redisClient;

    /**
     * 版本号
     */
    private static String version;

    static {

        try {
            version = "v" + new Manifest(IndexController.class.getResourceAsStream("/META-INF/MANIFEST.MF")).getMainAttributes()
                    .get(Attributes.Name.IMPLEMENTATION_VERSION).toString();
        } catch (Exception e) {
            version = "unknow";
        }

        version = "<html><head></head><body>wios version: " + version + ".</body></html>";
    }

    /**
     * 首页展示页
     *
     * @return 首页展示页
     */
    @RequestMapping("/")
    public String index() {
        return version;
    }

    /**
     * 健康检查
     *
     * @return success
     */
    @RequestMapping("/healthCheck.do")
    public String healthCheck() {
        return "success";
    }

    /**
     * 验证码
     *
     * @param deviceid 如果是前后端分离,页面也需要传(可以使用uuid,验证时使用相同的uuid)
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/jcaptcha.do")
    public void jcaptcha(String deviceid, HttpServletRequest request,
                         HttpServletResponse response) throws Exception {

        CaptchaUtilsEx captchaUtils = null;

        boolean isMobileCaptcha = StringUtils.isNotBlank(deviceid);

        if (isMobileCaptcha) {
            captchaUtils = new CaptchaUtilsEx(130, 58, 4);
        } else {
            captchaUtils = new CaptchaUtilsEx(98, 39, 4, true);
        }

        // 如果是手机登录,则保存缓存
        if (isMobileCaptcha) {
            redisClient.put(RedisClient.REDIS_IMAGE_CAPTCHA_PREFIX + deviceid, captchaUtils.getImageCode(), 600);
        } else {
            request.getSession().setAttribute("app.jcaptcha", captchaUtils.getImageCode());
        }

        // 输出图象到页面
        ImageIO.write(captchaUtils.getImage(), "JPEG", response.getOutputStream());
    }

    @RequestMapping("/generateQrcode.do")
    public void generateQrcode(String url, Integer width, Integer height, HttpServletRequest request, HttpServletResponse response) throws Exception {

        width = width == null ? 300 : width;
        height = height == null ? 300 : height;

        // 输出图象到页面
        ImageIO.write(QrCodeUtil.generate(url, width, height), "JPEG", response.getOutputStream());
    }

}