WxFeedbackService.java 5.44 KB
/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.wechat.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.JsonUtil;
import com.thinkgem.jeesite.modules.wechat.entity.WxFeedback;
import com.thinkgem.jeesite.modules.wechat.entity.WxOrder;

import net.sf.json.JSONObject;

import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.wechat.dao.WxFeedbackDao;

/**
 * 反馈意见Service
 * @author lzj
 * @version 2020-03-19
 */
@Service
@Transactional(readOnly = true)
public class WxFeedbackService extends CrudService<WxFeedbackDao, WxFeedback> {

	public WxFeedback get(String id) {
		return super.get(id);
	}
	
	public List<WxFeedback> findList(WxFeedback wxFeedback) {
		return super.findList(wxFeedback);
	}
	
	public Page<WxFeedback> findPage(Page<WxFeedback> page, WxFeedback wxFeedback) {
		return super.findPage(page, wxFeedback);
	}
	
	@Transactional(readOnly = false)
	public void save(WxFeedback wxFeedback) {
		super.save(wxFeedback);
	}
	
	@Transactional(readOnly = false)
	public void delete(WxFeedback wxFeedback) {
		super.delete(wxFeedback);
	}

	public Map rece_feedback(HttpServletRequest request) {
		Map map = new HashMap();
		String userid = request.getParameter("userid");
		String fdid = request.getParameter("fdid");
		String kes = request.getParameter("kes");
		String question = request.getParameter("question");
		
		WxFeedback feedback = getFdid(fdid);//反馈id查询是否存在,存在的话不再保存
		if(feedback == null) {
		WxFeedback order = new WxFeedback();
		order.setUserid(userid);
		order.setFdid(fdid);
		order.setKes(kes);
		order.setQuestion(question);
	
		super.save(order);
		map.put("resultMsg", "反馈信息成功保存");
		}else {
			map.put("resultMsg", "已存在反馈信息");
		}
		return map;
	}

	private WxFeedback getFdid(String fdid) {
		// TODO Auto-generated method stub
		 return dao.getFdid(fdid);
	}

	public Map answer(WxFeedback wxFeedback) {
			String url = Global.getWxUrl() + "/end/feedback";
			Map params = new HashMap();
			params.put("fdid", wxFeedback.getFdid());
			params.put("answer", wxFeedback.getAnswer());
			params.put("isopen", wxFeedback.getIsopen());
			String content = "";
			try {
				content = posturl(params, url);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			Map map = JsonUtil.jsonToMap(content);
			return map;
		}

		// post方式向微信端传递数据
		private String posturl(Map params, String url) throws IOException {

			Integer URLOUTTIME = 1000 * 60;
			String res = "";
			HttpPost httppost = null;
			CloseableHttpClient client = null;
			CloseableHttpResponse response = null;
			Exception exception = null;
			try {
				client = HttpClientBuilder.create().build();
				httppost = new HttpPost(url);
				RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(URLOUTTIME)
						.setConnectTimeout(URLOUTTIME).setSocketTimeout(URLOUTTIME).build();
				httppost.setConfig(requestConfig);
				List<NameValuePair> urlParameters = Lists.newArrayList();
				
				if(params != null && params.size()>0) {
					Iterator<String> it = params.keySet().iterator();
					while (it.hasNext()) {
						String key = it.next(); 
						urlParameters.add(new BasicNameValuePair(key,(String) params.get(key))); 
					}
					httppost.setEntity(new UrlEncodedFormEntity(urlParameters, "utf-8"));
				}
				httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
				response = client.execute(httppost);
				res = EntityUtils.toString(response.getEntity(), "utf-8");
				logger.info("返回信息:" + res);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (httppost != null) {
					httppost.releaseConnection();
				}
				if (response != null) {
					response.close();
				}
				if (client != null) {
					client.close();
				}
			}
			return res;
		}

		public Map open(WxFeedback wxFeedback) {
             String url = Global.getWxUrl() + "/open/feedback";
			Map params = new HashMap();
			params.put("fdid", wxFeedback.getFdid());
			params.put("isopen", wxFeedback.getIsopen());
			
			String content = "";
			try {
				content = posturl(params, url);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			Map map = JsonUtil.jsonToMap(content);
			return map;
		}
		
	
}