视图内容:
package com.immo.supervise.view;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.AbstractView;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.immo.framework.utils.HttpClientUtil;
import com.immo.framework.utils.IdUtils;
import com.immo.supervise.common.WxInfo;
import com.immo.supervise.entity.SuperviseQrcodeInfo;
import com.immo.supervise.mapper.SuperviseQrcodeInfoMapper;
/**
* 带参数微信二维码下载
*
* @author Linhai.Tan 2018-1-12
*
*/
public class WxParamQRcodeView extends AbstractView {
@Autowired
private SuperviseQrcodeInfoMapper superviseQrcodeInfoMapper;
/**
* 生成带参数二维码过程
*/
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String qrUrl = null;
Map<String, Object> params = new HashMap<>();
Map<String, Object> actionInfo = new HashMap<>();
Map<String, Object> sceneId = new HashMap<>();
// 查询数据库是否有此条件的二维码
String orgId = request.getParameter("orgId");
String userId = request.getParameter("userId");
String patientId = request.getParameter("patientId");
EntityWrapper<SuperviseQrcodeInfo> wrapper=new EntityWrapper<>();
if(StringUtils.isBlank(orgId)){
wrapper.isNull("org_id");
}else{
wrapper.eq("org_id",orgId);
}
if(StringUtils.isBlank(userId)){
wrapper.isNull("user_id");
}else{
wrapper.eq("user_id",userId);
}
if(StringUtils.isBlank(patientId)){
wrapper.isNull("patient_id");
}else{
wrapper.eq("patient_id",patientId);
}
List<SuperviseQrcodeInfo> list =superviseQrcodeInfoMapper.selectList(wrapper);
// 设置浏览器响应
response.setHeader("Content-Disposition",
"attachment;filename=" + new String("QRcode.png".getBytes(), "ISO-8859-1"));
if (list != null && list.size() == 1) {
qrUrl = WxInfo.getQRUrlByTicket(list.get(0).getTicket());
} else {
// 获取目前最大的序号
Integer sequence = superviseQrcodeInfoMapper.selectMaxOrder() + 1;
sceneId.put("scene_id", sequence);
actionInfo.put("scene", sceneId);
params.put("action_name", "QR_LIMIT_SCENE");
params.put("action_info", actionInfo);
String post = HttpClientUtil.JsonPost(WxInfo.getCreateQRcodeUrl(), params);
LoggerFactory.getLogger(this.getClass()).error(post);
Map map = JSONObject.parseObject(post, Map.class);
qrUrl = WxInfo.getQRUrlByTicket((String) map.get("ticket"));
// 保存此记录到数据库
SuperviseQrcodeInfo info = new SuperviseQrcodeInfo();
info.setSequence(sequence);
info.setTicket((String) map.get("ticket"));
info.setOrgId(orgId);
info.setUserId(userId);
info.setPatientId(patientId);
info.setId(IdUtils.getInstanse().getUID());
superviseQrcodeInfoMapper.insert(info);
}
URL url = new URL(qrUrl);
InputStream in = url.openStream();
ServletOutputStream out = response.getOutputStream();
// 3. 文件流----> response输出流
int len = -1;
byte[] b = new byte[1024];
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
in.close();
}
}备注为视图:
package com.immo.supervise.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.immo.supervise.view.QRcodeView;
import com.immo.supervise.view.WxParamQRcodeView;
/**
* 自定义视图配置
*
* @author Linhai.Tan 2018-1-5
*
*/
@Configuration
public class ViewConfig {
/**
* 下载微信带参数二维码
*
* @return
*/
@Bean(name = "wxParamQRcode")
public WxParamQRcodeView wxParamQRcode() {
return new WxParamQRcodeView();
}
}
访问视图:
/**
* 生成微信二维码接口视图,注意,这个是自定义视图
*
* @return
*/
@GetMapping("/wxParamQRcode")
public ModelAndView createQRCode() {
return new ModelAndView("wxParamQRcode");
}


被折叠的 条评论
为什么被折叠?



