WxController.java 3.79 KB
package com.example.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.wink.client.ClientResponse;
import org.apache.wink.client.Resource;
import org.apache.wink.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.service.http.ClientConfigService;
import com.example.service.http.ClientConfigServiceImpl;
import com.example.support.model.MessageEntity;
import com.fasterxml.jackson.databind.ObjectMapper;

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

	@Value("${wx.sign.base.url}")
	String wxSignBaseUrl;

	// String jssdkurl =
	// "http://gfwp.gac-toyota.com.cn/GTMCfamily/index.php/campaign/api2/getjssdk";
	//
	// String getopenidurl =
	// "http://gfwp.gac-toyota.com.cn/GTMCfamily/index.php/campaign/api2/getjssdk";
	//
	// @Autowired
	// private HttpPostService httpPostService;
	//
	//
	// @RequestMapping("/getjssdk")
	// public MessageEntity getjssdk(HttpServletRequest request, @RequestParam
	// String url) {
	// MessageEntity.Builder builder = new MessageEntity.Builder(request);
	// String newUrl = jssdkurl + "?url="+ url;
	// Map<String, Object> map = null;
	// try {
	// map = httpPostService.httpPost(newUrl, null);
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// return builder.success(true).code("2000").content(map).create();
	// }

	@RequestMapping("/getUser")
	public MessageEntity oauth(HttpServletRequest request, @RequestParam String access_token, String openid) {
		MessageEntity.Builder builder = new MessageEntity.Builder(request);
		ClientConfigService configService = new ClientConfigServiceImpl();
		RestClient restClient = new RestClient(configService.getClientConfig());
		Resource resource = restClient.resource("https://api.weixin.qq.com/sns/userinfo");
		resource.queryParam("access_token", access_token);
		resource.queryParam("openid", openid);
		resource.queryParam("lang", "zh_CN");
		ClientResponse response = resource.get();
		String responseEntity = response.getEntity(String.class);
		Map readValue = null;
		// System.out.println("responseEntity:::"+responseEntity);
		ObjectMapper mapper = new ObjectMapper();
		try {
			readValue = mapper.readValue(responseEntity, Map.class);
			if (null != readValue.get("errcode")) {
				return builder.success(false).code("999").content(readValue).create();
			}
		} catch (IOException e) {
			e.printStackTrace();
			;
		}
		return builder.success(true).code("2000").content(readValue).create();
	}

	@RequestMapping("/wxSign")
	public MessageEntity jsSign(HttpServletRequest request, @RequestParam String url)
			throws UnsupportedEncodingException {
		MessageEntity.Builder builder = new MessageEntity.Builder(request);
		ClientConfigService configService = new ClientConfigServiceImpl();
		RestClient restClient = new RestClient(configService.getClientConfig());
		Resource resource = restClient.resource(wxSignBaseUrl + "?url=" + URLEncoder.encode(url, "utf-8"));
		ClientResponse response = resource.get();
		String responseEntity = response.getEntity(String.class);
		Map readValue = null;
		// System.out.println("responseEntity:::"+responseEntity);
		ObjectMapper mapper = new ObjectMapper();
		try {
			readValue = mapper.readValue(responseEntity, Map.class);
			if (null != readValue.get("errcode")) {
				return builder.success(false).code("999").content(readValue).create();
			}
		} catch (IOException e) {
			e.printStackTrace();
			;
		}
		return builder.success(true).code("2000").content(readValue).create();
	}

}