审计记录

src/org/theyeasy/weixin/service/impl/WxOpenMpServiceImpl.java 8.7 KB
zxt@theyeasy.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
package org.theyeasy.weixin.service.impl;

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

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.theyeasy.weixin.service.WxOpenMpService;
import org.theyeasy.weixin.util.WxTokenServiceConstant;

import redis.clients.jedis.Jedis;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.w1hd.zzhnc.util.PropertiesFileUtil;
import com.w1hd.zzhnc.util.RedisUtil;

import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

/**
 * 微信公众号服务类
 * @author Administrator
 *
 */
public class WxOpenMpServiceImpl extends WxMpServiceImpl {

	private static final JsonParser JSON_PARSER = new JsonParser();
	private String componentAppId;
	private String appId;
	
	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	/**
	 * 这是获取的第三方平台的AccessToken,不是公众号的Token(mark by lcc 171028)
	 */
	public String getComponentAccessToken() {
		String open_token_key=WxTokenServiceConstant.WX_OPEN_TOKEN_SERVICE.concat("_").concat(this.getComponentAppId());

		return RedisUtil.get3rdToken(open_token_key);
	}
	
	
	public WxOpenMpServiceImpl(String component_appId, String appId) {
		// TODO Auto-generated constructor stub
		this.componentAppId = component_appId;
		this.appId = appId;
		final WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
		// final WxMpInRedisConfigStorage config=new WxMpInRedisConfigStorage();
		config.setAppId(appId);// 设置微信公众号的appid
		this.setWxMpConfigStorage(config);
	}

	/**
	 * 这是获取公众号的Token,不是获取第三方平台的OpenToken(mark by lcc 171028)
	 */
	@Override
	public String getAccessToken(boolean forceRefresh) throws WxErrorException {

		String open_mp_token_key = WxTokenServiceConstant.WX_OPEN_AUTHOR_TOKEN_SERVICE.concat("_").concat(getComponentAppId()).concat("_").concat(this.getAppId());

		String open_mp_token = RedisUtil.get3rdToken(open_mp_token_key);

		System.out.println(open_mp_token);

		return open_mp_token;
	}

	public String getComponentAppId() {
		return componentAppId;
	}

	@Override
	public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {

		if (forceRefresh) {
			this.getWxMpConfigStorage().expireJsapiTicket();
		}

		if (this.getWxMpConfigStorage().isJsapiTicketExpired()) {
			String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
			String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
			JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
			JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
			String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
			int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
			this.getWxMpConfigStorage().updateJsapiTicket(jsapiTicket, expiresInSeconds);
		}
		return this.getWxMpConfigStorage().getJsapiTicket();
	}

	public void setComponentAppId(String componentAppId) {
		this.componentAppId = componentAppId;
	}

	public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
		try {
			T result = executeInternal(executor, uri, data);
			this.log.debug("\n[URL]:  {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, result);
			return result;
		} catch (WxErrorException e) {
			log.error(e.getMessage(), e);
			return null;
		}
	}

	protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
		if (uri.indexOf("access_token=") != -1) {
			throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
		}
		String accessToken = getAccessToken(false);

		String uriWithAccessToken = uri;
		uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;

		try {
			return executor.execute(getHttpclient(), this.getHttpProxy(), uriWithAccessToken, data);
		} catch (WxErrorException e) {
			WxError error = e.getError();

			if (error.getErrorCode() != 0) {
				this.log.error("\n[URL]:  {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, error);
				throw new WxErrorException(error);
			}
			return null;
		} catch (IOException e) {
			this.log.error("\n[URL]:  {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uri, data, e.getMessage());
			throw new RuntimeException(e);
		}
	}

	@Override
	public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
		StringBuilder url = new StringBuilder();
		url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
		url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
		url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
		url.append("&response_type=code");
		url.append("&scope=").append(scope);
		if (state != null) {
			url.append("&state=").append(state);
		}
		url.append("&component_appid=").append(getComponentAppId());
		url.append("#wechat_redirect");
		return url.toString();
		
				
	}

	private WxMpOAuth2AccessToken getOAuth2AccessToken(StringBuilder url) throws WxErrorException {
		try {
			RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
			String responseText = executor.execute(this.getHttpclient(), this.getHttpProxy(), url.toString(), null);
			return WxMpOAuth2AccessToken.fromJson(responseText);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
		StringBuilder url = new StringBuilder();
		url.append("https://api.weixin.qq.com/sns/oauth2/component/access_token?");
		url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
		url.append("&code=").append(code);
		url.append("&&grant_type=").append("authorization_code");
		url.append("&component_appid=").append(this.getComponentAppId());
		url.append("&component_access_token=").append(this.getComponentAccessToken());

		return this.getOAuth2AccessToken(url);
	}

	@Override
	public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
		StringBuilder url = new StringBuilder();
		url.append("https://api.weixin.qq.com/sns/oauth2/component/refresh_token?");
		url.append("appid=").append(this.getWxMpConfigStorage().getAppId());
		url.append("&grant_type=refresh_token");
		url.append("&refresh_token=").append(refreshToken);
		url.append("&component_appid=").append(this.getComponentAppId());
		url.append("&component_access_token=").append(this.getComponentAccessToken());

		return this.getOAuth2AccessToken(url);
	}

	@Override
	public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, String lang) throws WxErrorException {
		StringBuilder url = new StringBuilder();
		url.append("https://api.weixin.qq.com/sns/userinfo?");
		url.append("access_token=").append(oAuth2AccessToken.getAccessToken());
		url.append("&openid=").append(oAuth2AccessToken.getOpenId());
		if (lang == null) {
			url.append("&lang=zh_CN");
		} else {
			url.append("&lang=").append(lang);
		}

		try {
			RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
			String responseText = executor.execute(getHttpclient(), this.getHttpProxy(), url.toString(), null);
			return WxMpUser.fromJson(responseText);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken oAuth2AccessToken) {
		StringBuilder url = new StringBuilder();
		url.append("https://api.weixin.qq.com/sns/auth?");
		url.append("access_token=").append(oAuth2AccessToken.getAccessToken());
		url.append("&openid=").append(oAuth2AccessToken.getOpenId());

		try {
			RequestExecutor<String, String> executor = new SimpleGetRequestExecutor();
			executor.execute(getHttpclient(), this.getHttpProxy(), url.toString(), null);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (WxErrorException e) {
			return false;
		}
		return true;
	}

	
	
	 
}