WeChatController.java 4.82 KB
package com.phxl.modules.wechat.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.phxl.common.util.Global;
import com.phxl.common.utils.Encodes;
import com.phxl.modules.wechat.constants.WeChatConstants;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.taskdefs.Exec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
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 com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.phxl.modules.wechat.service.WeChatService;
import com.phxl.modules.wechat.utils.WeChatUtils;
import org.springframework.web.servlet.ModelAndView;

/**
 * 微信端
 * @author 何
 *
 */
@Controller
@RequestMapping(value = "/weChat")
public class WeChatController {

	private Logger logger=LoggerFactory.getLogger(WeChatController.class);
	@Autowired
	@Qualifier("weChatServiceImpl")
	private WeChatService weChatService;
	
	/**
	 * 微信认证
	 * @param signature
	 * @param timestamp
	 * @param nonce
	 * @param echostr
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/verificate", method = RequestMethod.GET)
    public void weChatVerification(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce, @RequestParam String echostr,
            HttpServletRequest request, HttpServletResponse response) {
		try {
			 PrintWriter printWriter = response.getWriter();
			 //String checkWx = WeChatUtils.checkWx(timestamp, nonce);
			 printWriter.print(echostr);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
    }

	/**
	 * 获得加密后得密码信息
	 * @param password
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/entryptPassword", method = RequestMethod.POST)
	public Map<String,Object> entryptPassword(@RequestParam String password) {
		String pwd =  WeChatUtils.encode(password);
		Map<String,Object> map =new HashMap<String, Object>();
		map.put("password", pwd);
		return map;
	}
	
	
	/**
	 * 登录
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(HttpServletRequest request, HttpServletResponse response) {
		return weChatService.login(request,response);
	}
	
	
	/**
	  * 接受spd推送给微信端数据
	 * @param request
	 * @param params
	 * @return
	 */
	@RequestMapping("/pushData")
	@ResponseBody
	public String pushWeChatData(HttpServletRequest request, @RequestBody String params){
		logger.info("推送微信端接口数据:{}"+params);
		String url = getServiceIp(request);
		return weChatService.pushData(params,url);
	}
	/**
	 * 查询数据
	 * @param jsonObject
	 * @return
	 */
	 @ResponseBody
	 @RequestMapping(value = "/findDatas", method = RequestMethod.POST)
     public JSONObject findDatas(HttpServletRequest request, HttpServletResponse response,@RequestBody JSONObject jsonObject) throws IOException {
		    return weChatService.findDataList(jsonObject);
     }
	
	 
	 /**
	  * 通过商品名称查询商品信息
	  */
	 @ResponseBody
	 @RequestMapping(value = "/queryParam", method = RequestMethod.POST)
	 public JSONObject queryParams(@RequestBody JSONObject jsonObject) {
		 return weChatService.queryDrugInfo(jsonObject);
	 }


	/**
	 * 登录
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request, HttpServletResponse response) {
		return weChatService.logout(request,response);
	}


	/**
	 * 通过商品名称查询商品信息
	 */
	@ResponseBody
	@RequestMapping(value = "/createMenu", method = RequestMethod.GET)
	public String createMenu() {
		return weChatService.createMenu();
	}

	/**
	 * 通过商品名称查询商品信息
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteMenu", method = RequestMethod.GET)
	public String deleteMenu() {
		return weChatService.deleteMenu();
	}

	/**
	 * 返回当前服务ip地址和端口信息
	 */
	public String getServiceIp(HttpServletRequest request){
		//得到请求协议
		String scheme = request.getScheme();
		//得到具体ip
		String ip = request.getHeader("host");
		return scheme+"://"+ip;
	}


}