/**
 * Copyright (C), 2018-2019, XXX有限公司
 * FileName: QRCodeTestController
 * Author:   pwb
 * Date:     2019/7/12 19:51
 * Description: 测试生成二维码
 * History:
 * <author>          <time>          <version>          <desc>
 * frank           修改时间           1.0.0              ycypApp
 */
package com.yx.face.controller.admin;


import com.yx.face.boot.uitls.QRCodeUtil;
import com.yx.face.model.vo.AdminVO;
import com.yx.face.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;


/**
 * 〈测试生成二维码〉
 *
 * @author pwb
 * @create 2019/7/12
 * @since 1.0.0
 */
@Api(tags = "B 二维码")
@RestController
@RequestMapping("code")
public class QRCodeController {
    @Value("${web.address}")
    private String address;
    @Autowired
    private AdminService adminService;

    @SneakyThrows
    @ApiOperation(value = "方式一 获取二维码")
    @GetMapping(value = "getQRCodeOne/{id}")
    public void getQRCodeOne(@PathVariable Integer id, HttpServletResponse response) {
        AdminVO adminVO = adminService.getAuthInfo(id);
        if (adminVO == null) {
            adminVO = new AdminVO();
        }
        //给相应添加头部信息，主要告诉浏览器返回的是图片流
        response.setHeader("Cache-Control", "no-store");
        // 不设置缓存
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/png");
        String content = address.substring(0, address.length() - 1).concat("?orgId=") + id + "&org=" + adminVO.getName();
        // 转换成短url
        // String shortUrl = ShortNetAddressUtil.generateShortUrl(longUrl);
        // 生成二维码
        BufferedImage qRcodeImg = QRCodeUtil.encode(content, null, true);
        // 将二维码输出到页面中
        ImageIO.write(qRcodeImg, "png", response.getOutputStream());
    }


}
