package com.ruoyi.web.work.api;

import com.google.code.kaptcha.Producer;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ImageCompressUtil;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.web.work.api.config.BaseController;
import com.ruoyi.web.work.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/app/common")
public class Api_CommonController extends BaseController {

    @Autowired
    private RedisCache redisCache;

    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;

    @Autowired
    IUserService userService;

    //图片上传
    @PostMapping("/upload")
    public AjaxResult upload(MultipartHttpServletRequest multiReq) {
        try {
            AjaxResult ajax = AjaxResult.success();
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            String fileName = FileUploadUtils.upload(filePath, multiReq.getFile("file"));

            String suffix = ".JPEG .jpeg .JPG .jpg .png .PNG .gif .GIF";
            String fileType = fileName.substring(fileName.lastIndexOf("."));
            //图片才压缩
            if (suffix.indexOf(fileType) != -1) {
                String oldFile = filePath + fileName.replace("/profile/upload", "");
                String imagName = System.currentTimeMillis() + ".jpg";
                String newFile = filePath + "/" + DateUtils.datePath() + "/" + imagName;
                ImageCompressUtil.zipImageFile(new File(oldFile), new File(newFile));
                //删掉压缩前的图片
                FileUtils.deleteFile(oldFile);
                fileName = "/profile/upload/" + DateUtils.datePath() + "/" + imagName;
            }
            ajax.put("fileName", fileName);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    //图形验证码
    @GetMapping("/captcha")
    public AjaxResult captcha() {
        String uuid = IdUtils.simpleUUID();
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
        // 生成验证码
        String capText = captchaProducerMath.createText();
        String capStr = capText.substring(0, capText.lastIndexOf("@"));
        String code = capText.substring(capText.lastIndexOf("@") + 1);
        BufferedImage image = captchaProducerMath.createImage(capStr);
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpg", os);
            redisCache.setCacheObject(verifyKey, code, 2, TimeUnit.MINUTES);
        } catch (IOException e) {
            return AjaxResult.error(e.getMessage());
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("uuid", uuid);
        ajax.put("img", "data:image/gif;base64," + Base64.encode(os.toByteArray()));
        return AjaxResult.success(ajax);
    }
}

