ImgInfoController.java 4.95 KB
package com.example.controller;

import com.example.models.ImgInfo;
import com.example.service.ImgInfoService;
import com.example.support.model.MessageEntity;
import com.example.utils.file.DefaultFileUpload;
import com.example.utils.file.FileUploadInfo;
import com.example.utils.md5.MD5Utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Map;

/**
 * Created by JA on 17/7/3.
 */
@RequestMapping("/kmr/img")
@RestController
public class ImgInfoController {

    @Autowired
    private ImgInfoService imgInfoService;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public MessageEntity upload(HttpServletRequest request,@RequestPart final MultipartFile file,@ModelAttribute ImgInfo imgInfo,
            String access_token, String state, String timestamp, String rands, String jsondata, String checksum, String secret) {
        MessageEntity.Builder builder = new MessageEntity.Builder(request);
        String code = "9000";
        String openid = imgInfo.getOpenid();
        boolean flag = false;
        if(StringUtils.isNotBlank(jsondata)){
            jsondata = URLDecoder.decode(jsondata);
            ObjectMapper objectMapper = new ObjectMapper();
            Map map = null;
            try {
                map = objectMapper.readValue(jsondata, Map.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
//            if(map != null && map.containsKey("gtmc_code") && map.get("gtmc_code").toString().equals("200")){
//                String checksum1 = MD5Utils.hash(access_token + openid + state + timestamp + rands + jsondata + secret);
//                if(checksum1.equals(checksum)){
                    imgInfo.setGtmcUid(map.get("gtmc_uid").toString());
                    imgInfo.setGtmcDepartment(map.get("gtmc_department").toString());
                    imgInfo.setGtmcName(map.get("gtmc_name").toString());
                    FileUploadInfo info = new DefaultFileUpload().upload("/data/home/app/4Q0i/uimg", file);
                    String url = info.getUrl();
                    imgInfo.setImgUrl(url);
                    boolean b = this.imgInfoService.create(imgInfo);
                    if(b){
                        code = "2000";
                        flag = true;
                    }
//                }
//            }
        }

        return builder.success(flag).content(imgInfo).code(code).create();
    }


    @RequestMapping(value = "/upload/base64", method = RequestMethod.POST)
    public MessageEntity uploadbase64(HttpServletRequest request,@RequestBody String base64,@ModelAttribute ImgInfo imgInfo,
                                String access_token, String state, String timestamp, String rands, String jsondata, String checksum, String secret) {
        MessageEntity.Builder builder = new MessageEntity.Builder(request);
        String code = "9000";
        String openid = imgInfo.getOpenid();
        boolean flag = false;
        if(StringUtils.isNotBlank(jsondata)){
            jsondata = URLDecoder.decode(jsondata);
            ObjectMapper objectMapper = new ObjectMapper();
            Map map = null;
            try {
                map = objectMapper.readValue(jsondata, Map.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
//            if(map != null && map.containsKey("gtmc_code") && map.get("gtmc_code").toString().equals("200")){
//                String checksum1 = MD5Utils.hash(access_token + openid + state + timestamp + rands + jsondata + secret);
//                if(checksum1.equals(checksum)){
                    imgInfo.setGtmcUid(map.get("gtmc_uid").toString());
                    imgInfo.setGtmcDepartment(map.get("gtmc_department").toString());
                    imgInfo.setGtmcName(map.get("gtmc_name").toString());
                    FileUploadInfo info = new DefaultFileUpload().uploadBase64("/data/home/app/4Q0i/uimg", base64);
                    String url = info.getUrl();
                    imgInfo.setImgUrl(url);
                    boolean b = this.imgInfoService.create(imgInfo);
                    if(b){
                        code = "2000";
                        flag = true;
                    }
//                }
//            }
        }

        return builder.success(flag).content(imgInfo).code(code).create();
    }

    @RequestMapping("/getbyopenid")
    public MessageEntity getbyopenid(HttpServletRequest request, @RequestParam String openid) {
        MessageEntity.Builder builder = new MessageEntity.Builder(request);
        ImgInfo imgInfo = this.imgInfoService.getNewByOpenid(openid);
        return builder.success(true).code("2000").content(imgInfo).create();
    }

}