package cn.com.capcloud.api;

import cn.com.capcloud.api.action.NoSessionAction;
import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.api.action.service.util.ResponseUtil;
import cn.com.capcloud.ccui.core.common.json.JackJSON;
import cn.com.capcloud.core.util.ReadProperty;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

@CrossOrigin//支持跨域请求
@RestController
@RequestMapping("/nosession")
public class NoSessionController {

    @Autowired
    private NoSessionAction noSessionAction;
    @Autowired
    private ReadProperty readProperty;
    @Autowired
    private ApiService apiService;

    @RequestMapping("/data/myAction")
    public KHCResponseEntity myAction(@RequestBody String jsonStr, HttpServletRequest request) {
        String retStr= null;
        try {
            retStr = noSessionAction.myAction(jsonStr,request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        KHCResponseEntity KHCResponseEntity = JackJSON.parseObject(retStr, KHCResponseEntity.class);
        return KHCResponseEntity;
    }
    @RequestMapping("/file/downFileYZM")
    public void downFileYZM(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String fileDiskName = "/"+request.getParameter("fileDiskName");
        String suffix = "jpg";

        if (suffix != null && suffix.length() > 1) {
            suffix = suffix.substring(1, suffix.length());
        }
        String fileDir=readProperty.getCaptchaFilePath();
        fileDiskName=fileDiskName.replace("..", "");
        File file = new File(fileDir+fileDiskName);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        inputStream.read(data);
        inputStream.close();
        ResponseUtil.setContentType(response,suffix);
        String downloadFileName = request.getParameter("downFileName");
        if (downloadFileName == null || downloadFileName.length() == 0) {
            downloadFileName = fileDiskName;
        }
        response.addHeader("Content-Disposition",
                "attachment; filename=" + URLEncoder.encode(downloadFileName, "utf-8"));
        OutputStream stream = response.getOutputStream();
        stream.write(data);
        stream.flush();
        stream.close();
    }
    //模板示例：
    /*
    @RequestMapping("/test/ip")
    public ResponseEntity testIp(HttpServletRequest request) {
        String ip= IPUtil.getIpAddress(request);
        Map<String, Object> retMap=apiService.getReturnDataByCode("ok");
        retMap.put("message",ip);
        String retStr=JSONObject.fromObject(retMap).toString();
        ResponseEntity responseEntity= JSON.parseObject(retStr, ResponseEntity.class);
        return responseEntity;
    }*/
}

