CommonController.java 7.23 KB
package com.dili.titan.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.ueditor.ActionEnter;
import com.dili.titan.common.utils.web.BaseController;
import com.dili.titan.domain.base.Result;
import com.dili.titan.rpc.ConfigRPC;
import com.dili.titan.rpc.ImageRPC;
import com.diligrp.website.web.interfaces.domain.output.PickUpPointResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <B>Copyright</B> Copyright (c) 2014 www.diligrp.com All rights reserved. <br />
 * 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br />
 * <B>Company</B> 地利集团
 * @createTime May 28, 2014 2:48:29 PM
 * @author wujianjun
 */
@Controller
@RequestMapping("/common")
public class CommonController extends BaseController {

    @Autowired
    private ImageRPC imageRPC;

    @Autowired
    private ConfigRPC configRPC;

    /**
     * 上传图片
     * @return
     * @createTime May 28, 2014 2:49:11 PM
     * @author wujianjun
     */
    @RequestMapping(value = "/uploadImg",method = RequestMethod.POST)
    public ModelAndView uploadImage(@RequestParam MultipartFile image,
            HttpServletResponse resp) {
        Result result = Result.create();
        String json =  imageRPC.uploadProductImage(image);
        result.addResult("json", json);
        return toVMSkipLayout("common/uploadImgForUE", result.getAllResult());
    }

    /**
     * 供应图片
     * @return
     * @createTime May 28, 2014 2:49:11 PM
     * @author chenzhiwei
     */
    @RequestMapping(value = "/supplyUploadImg",method = RequestMethod.POST,produces = "application/text;charset=UTF-8")
    @ResponseBody
    public String supplyUploadImg(@RequestParam MultipartFile image) {
        String upload = imageRPC.uploadSupplyImage(image);
        JSONObject jsonObject = JSONObject.parseObject(upload);
        if(jsonObject.getBoolean("success")) {
            upload = jsonObject.getString("message");
        }
        return upload;
    }


    /**
     * 富文本编辑框上传图片
     * @return
     * @createTime May 28, 2014 2:49:11 PM
     * @author wujianjun
     */
    @RequestMapping(value = "/uploadImgForUE",method = RequestMethod.POST)
    public ModelAndView uploadForUE(@RequestParam MultipartFile image,
            HttpServletResponse resp) throws IOException{
        Result result = Result.create();
    	String upload = imageRPC.uploadDescImage(image,true);
        JSONObject json = JSONObject.parseObject(upload);
        JSONObject returnjson = new JSONObject();
        if (json.getBoolean("success")) {
            returnjson.put("state", "SUCCESS");
            returnjson.put("url", json.getString("message"));
            returnjson.put("title", image.getName());
            returnjson.put("original", image.getOriginalFilename());
        } else {
            returnjson.put("state", json.getString("message"));
        }
        result.addResult("json", returnjson.toJSONString());
        return toVMSkipLayout("common/uploadImgForUE", result.getAllResult());
    }

    /**
     * 富文本编辑框上传图片  无水印
     *
     * @return
     * @createTime May 28, 2014 2:49:11 PM
     * @author wujianjun
     */
    @RequestMapping(value = "/uploadImgNoMark", method = RequestMethod.POST)
    public ModelAndView uploadImgNoMark(@RequestParam MultipartFile image) {
        String upload = imageRPC.uploadDescImage(image,false);
        Result result = Result.create();
        result.addResult("json", upload);
        return toVMSkipLayout("common/uploadImgForUE", result.getAllResult());
    }

    /**
     * 富文本编辑框上传图片  无水印
     *
     * @return
     * @createTime May 28, 2014 2:49:11 PM
     * @author wujianjun
     */
    @RequestMapping(value = "/uploadCateIcon", method = RequestMethod.POST)
    public ModelAndView uploadCateIcon(@RequestParam MultipartFile image) {
        JSONObject returnjson = new JSONObject();
        if(image.getSize()>100*1024){
            returnjson.put("state", "上传图片不能大于100KB!");
        }else {
            String upload = imageRPC.uploadAuthImage(image);
            JSONObject json = JSONObject.parseObject(upload);
            if (json.getBoolean("success")) {
                returnjson.put("url", json.getString("message"));
                returnjson.put("state", "SUCCESS");
                returnjson.put("title", "上传成功");
            } else {
                returnjson.put("state", json.getString("message"));
            }
        }
        Result result = Result.create();
        result.addResult("json", returnjson.toJSONString());
        return toVMSkipLayout("common/uploadImgForUE", result.getAllResult());
    }

    /**
     * 搜索供应商
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/searchSupplier", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String searchAttrbute(String keyword, Model view){
      return "";
    }

    /**
     * this method is  富文本编辑框上传图片
     *
     * @param request
     * @param response
     * @createTime 2015年1月14日 上午11:21:30
     * @author yangjianjun
     */
    @RequestMapping(value = "/uploadUmeditor")
    public void uploadUmeditor(HttpServletRequest request,
            HttpServletResponse response) {
        try {
            request.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", "text/html");

            String rootPath = request.getRealPath("/assets/umeditor");
            response.getWriter()
                    .write(new ActionEnter(request, rootPath).exec());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	@RequestMapping(value="/uploadCategoryIcon",method=RequestMethod.POST)
	@ResponseBody
	public String uploadAuthIcon(@RequestParam MultipartFile image) throws Exception{
		Map<String,Object> result = new HashMap<String,Object>();
		String authIcon = imageRPC.uploadCategoryImage(image);
        JSONObject jsonObject = JSONObject.parseObject(authIcon);
        result.put("success", jsonObject.getBoolean("success"));
        result.put("message", jsonObject.getString("message"));
        return JSON.toJSONString(result);
	}

    /**
     * 搜索市场
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/searchSc", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String searchSc(String keyword, Model view){
        List<PickUpPointResp> list = configRPC.getPickUpInfoByName(keyword);
        return JSON.toJSONString(list);
    }
}