Procházet zdrojové kódy

fix: 区分小程序环境

shc před 3 roky
rodič
revize
3283aa19fe

+ 5 - 0
hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/config/UpmsConfig.java

@@ -31,5 +31,10 @@ public class UpmsConfig {
 
 	private String formMail;
 
+	/**
+	 * 小程序环境
+	 */
+	private String wechatEnv;
+
 
 }

+ 2 - 2
hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/controller/ApiController.java

@@ -498,7 +498,7 @@ public class ApiController {
 		}
 
 		//获取小程序openid
-		Map<String, Object> opemMap = GetOpenIDUtil.oauth2GetOpenid(code);
+		Map<String, Object> opemMap = GetOpenIDUtil.oauth2GetOpenid(code,upmsConfig.getWechatEnv());
 		System.out.println("opemMap:" + opemMap);
 		if (null != opemMap) {
 			String openid = opemMap.get("openid") + "";
@@ -2962,7 +2962,7 @@ public class ApiController {
 	private String getReaderOpenId(String authCode) {
 		//获取小程序openid
 		log.info("小程序登录AUTH_CODE:{}", authCode);
-		Map<String, Object> resultMap = GetOpenIDUtil.oauth2GetOpenid(authCode.trim());
+		Map<String, Object> resultMap = GetOpenIDUtil.oauth2GetOpenid(authCode.trim(),upmsConfig.getWechatEnv());
 		log.warn("小程序登录结果信息:{}", resultMap);
 		return (String) resultMap.get("openid");
 	}

+ 37 - 11
hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/util/GetOpenIDUtil.java

@@ -12,30 +12,56 @@ import java.util.Map;
 
 /**
  * 微信小程序获取openid
+ *
  * @author Mr.Lin
  */
 public class GetOpenIDUtil {
-	public static final String appid="wxd03398e1bff2b241";
-	public static final String appsecret="f745fa594d51534845e8412433951a20";
+
+	// cos pro 环境
+	public static final String CSO_PRO_APPID = "wxd03398e1bff2b241";
+	public static final String CSO_PRO_APPSECRET = "f745fa594d51534845e8412433951a20";
+
+	// cos demo 环境
+	public static final String CSO_DEMO_APPID = "wx956bb32e73073426";
+	public static final String CSO_DEMO_APPSECRET = "cb722986c23cc352b7f87ff2dae4cc11";
+
+	public static Map<String, Object> oauth2GetOpenid(String code, String env) {
+		String appid;
+		String appsecret;
+		if ("cso_demo".equals(env)) {
+			appsecret = CSO_DEMO_APPSECRET;
+			appid = CSO_DEMO_APPID;
+		} else if ( "cso_prod".equals(env)) {
+			appsecret = CSO_PRO_APPSECRET;
+			appid = CSO_PRO_APPID;
+		} else {
+			appsecret = CSO_PRO_APPSECRET;
+			appid = CSO_PRO_APPID;
+		}
+		final String URL = String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appid, appsecret, code);
+
+		return oauth2GetOpenid(URL);
+
+	}
+
 
 	// 网页授权接口
 //    public final static String GetPageAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";//
 //    public final static String GetPageAccessTokenUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=CODE&grant_type=authorization_code";
-	public  static Map<String,Object> oauth2GetOpenid(String code) {
-		String requestUrl ="https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+appsecret+"&js_code="+code+"&grant_type=authorization_code";
+	private static Map<String, Object> oauth2GetOpenid(String requestUrl) {
 		HttpClient client = null;
-		Map<String,Object> result =new HashMap<String,Object>();
+		Map<String, Object> result = new HashMap<String, Object>();
 		try {
 			client = new DefaultHttpClient();
 			HttpGet httpget = new HttpGet(requestUrl);
 			ResponseHandler<String> responseHandler = new BasicResponseHandler();
 			String response = client.execute(httpget, responseHandler);
-			JSONObject OpenidJSONO=JSONObject.parseObject(response);
-			String openid =String.valueOf(OpenidJSONO.get("openid"));
-			String session_key=String.valueOf(OpenidJSONO.get("session_key"));
-			String unionid=String.valueOf(OpenidJSONO.get("unionid"));
-			String errcode=String.valueOf(OpenidJSONO.get("errcode"));
-			String errmsg=String.valueOf(OpenidJSONO.get("errmsg"));
+			JSONObject OpenidJSONO = JSONObject.parseObject(response);
+			String openid = String.valueOf(OpenidJSONO.get("openid"));
+			String session_key = String.valueOf(OpenidJSONO.get("session_key"));
+			String unionid = String.valueOf(OpenidJSONO.get("unionid"));
+			String errcode = String.valueOf(OpenidJSONO.get("errcode"));
+			String errmsg = String.valueOf(OpenidJSONO.get("errmsg"));
 
 			result.put("openid", openid);
 			result.put("sessionKey", session_key);