在微信开发过程中,难免遇到公众号更换主体这种坑爹的事情,如果遇到这种事情,虽然你可以把产品干掉然而你还是要结局问题!!!!!
我这边就遇到了这种问题,最后想出了一个较为麻烦,对用户体验并不好的方法. 如果各位观看后有思路或者方案希望能留言提出来,谢谢!
我这边目前是针对web端微信的,如果是app 我这边并不是通过微信 而是通过用户的手机号进行相关的绑定,
我这种方法是在用户登录的时候进行,会对用户体验较为不好.
首先微信web端登录的时候,需要把登录发起请求交给后端服务器,这样我们做后端的可以进行控制跳转.
我们先假设用户表 user_id open_id union_id 假设只是一个用户的数据 我们在这个基础上加上 一个old_user 字段来分辨当前用户是否有被刷新成新公众号数据
现在用户表就变成 user_id open_id union_id old_user
第二部就是进行后台微信登录接口,让前端在登录的时候调用后端的微信登录接口
伪代码
进行微信登录
第一次访问接口 : http://url/weixinlogin?key=xxxxx
只能携带 key 也就是要跳转的url
在Controller里面直接进行第二次跳转url进行拼接
用旧的微信公众号appid state=old 生成微信登录url 进行请求
第二次访问接口 : http://url/weixinlogin?key=KEY&code=XXX&state=old&user_id=USER_ID
在这里需要进入service层 在这里进行判断
通过旧微信公众号登录拿到的code 来找openid 和 unid 再找旧用户
找到旧用户后把旧用户的user_id 带出来
在用新微信公总号app_id state=new user_id = old_user_id 生成微信登录url 进行请求
第三次访问接口 : http://url/weixinlogin?key=KEY&code=XXX&state=new&user_id=old_user_id
拿到新微信公众号登录code 找出 用户针对新微信公众号 openid 和 unid
然后根据传过来的old_user_id 进行刷新该用户的 openid 和 unid
再把old_user 改成新用户
=====================================结束=================================================
这里是一些配置
code = 微信回调时的code
state = 微信回调时可以带的自定义参数
key = 自定义参数 主要用来 Base64 进行加密的url 进行跳转的url
user_id = 老用户的user_id
authorization_url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECTURL&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
base_authorization_url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECTURL&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
appid = 新微信web appid
old_appid = 老的微信 web appid
new_redirect_url=http://[回调网址]?user_id=USER_ID&key=KEY
/**新
* 微信公众号授权
*
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "/weixinlogin", method = RequestMethod.GET)
public void weChatLogin(
@RequestParam(value = "code", defaultValue = "") String code,
@RequestParam(value = "state", defaultValue = "") String state,
@RequestParam(value = "key", defaultValue = "KEY") String paramKey,
@RequestParam(value = "user_id", defaultValue = "USER_ID") String userId,
HttpServletRequest request, HttpServletResponse response) throws Exception {
//第一次进行跳
if (MiscUtils.isEmpty(state)) {
String authorization_url = MiscUtils.getConfigByKey("base_authorization_url");//静默授权
String appid = MiscUtils.getConfigByKey("old_appid");
String redireceUrl = MiscUtils.getConfigByKey("new_redirect_url");
if (paramKey != null) {
redireceUrl = redireceUrl.replace("KEY", paramKey);
}
String encodeUrl = URLEncoder.encode(redireceUrl, "utf-8");
String authorizationUrl = authorization_url.replace("APPID", appid).replace("REDIRECTURL", encodeUrl).replace("STATE", "old");//修改参数
response.sendRedirect(authorizationUrl);
return;
}
if (state.equals("old")) {
RequestEntity requestEntity = this.createResponseEntity("CommonServer", "newWeixinCodeUserLogin", null, "");
Map<String, String> map = new HashMap<>();
map.put("code", code);
map.put("state", state);
requestEntity.setParam(map);
map.put("code", code);
map.put("user_id", userId);
map.put("state", state);
requestEntity.setParam(map);
ResponseEntity responseEntity = this.process(requestEntity, serviceManger, message);
Map<String, Object> resultMap = (Map<String, Object>) responseEntity.getReturnData();
Integer key = Integer.valueOf(resultMap.get("key").toString());
if (key == 1) {
String authorization_url = MiscUtils.getConfigByKey("base_authorization_url");//静默授权url
String appid = MiscUtils.getConfigByKey("appid");
String redireceUrl = MiscUtils.getConfigByKey("new_redirect_url");
redireceUrl = redireceUrl.replace("KEY", paramKey);
if (!MiscUtils.isEmpty(resultMap.get("user_id"))) {
String user_id = resultMap.get("user_id").toString();
redireceUrl = redireceUrl.replace("USER_ID", user_id);
}
String encodeUrl = URLEncoder.encode(redireceUrl, "utf-8");
String authorizationUrl = authorization_url.replace("APPID", appid).replace("REDIRECTURL", encodeUrl).replace("STATE", "new");//修改参数
response.sendRedirect(authorizationUrl);
return;
}
//没有拿到旧账号的openid 重新登录
logger.info("没有拿到openId 或者 unionid 跳到手动授权页面");
String authorization_url = MiscUtils.getConfigByKey("authorization_url");//手动授权url
String appid = MiscUtils.getConfigByKey("old_appid");
String redireceUrl = MiscUtils.getConfigByKey("new_redirect_url");
redireceUrl = redireceUrl.replace("KEY", paramKey);
if (!MiscUtils.isEmpty(resultMap.get("user_id"))) {
String user_id = resultMap.get("user_id").toString();
redireceUrl = redireceUrl.replace("USER_ID", user_id);
}
String encodeUrl = URLEncoder.encode(redireceUrl, "utf-8");
String authorizationUrl = authorization_url.replace("APPID", appid).replace("REDIRECTURL", encodeUrl).replace("STATE", "old");
;//修改参数
response.sendRedirect(authorizationUrl);
return;
}
//新登路
if (state.equals("new")) {
RequestEntity requestEntity = this.createResponseEntity("CommonServer", "newWeixinCodeUserLogin", null, "");
Map<String, String> map = new HashMap<>();
map.put("code", code);
map.put("user_id", userId);
map.put("state", state);
requestEntity.setParam(map);
ResponseEntity responseEntity = this.process(requestEntity, serviceManger, message);
Map<String, Object> resultMap = (Map<String, Object>) responseEntity.getReturnData();
Integer key = Integer.valueOf(resultMap.get("key").toString());
if (key == 1) {
String userWeixinAccessToken = (String) resultMap.get("access_token");
if (StringUtils.isNotEmpty(paramKey) && !paramKey.equals("KEY")) {
String newUrl = new String(Base64.decode(paramKey));
response.sendRedirect(newUrl + "token=" + userWeixinAccessToken);
} else {
response.sendRedirect(MiscUtils.getConfigByKey("web_index") + userWeixinAccessToken);
}
return;
}
String authorization_url = MiscUtils.getConfigByKey("authorization_url");//手动授权url
String appid = MiscUtils.getConfigByKey("appid");
String redireceUrl = MiscUtils.getConfigByKey("new_redirect_url");
redireceUrl = redireceUrl.replace("KEY", paramKey);
if (!MiscUtils.isEmpty(resultMap.get("user_id"))) {
String user_id = resultMap.get("user_id").toString();
redireceUrl = redireceUrl.replace("USER_ID", user_id);
}
String encodeUrl = URLEncoder.encode(redireceUrl, "utf-8");
String authorizationUrl = authorization_url.replace("APPID", appid).replace("REDIRECTURL", encodeUrl).replace("STATE", "new");//修改参数
response.sendRedirect(authorizationUrl);
return;
}
}
总结:
其实就相当于两次登录
第一次使用旧微信相关配置进行登录,找到旧用户信息,如果找不到就不进行创建
第二次使用新微信相关配置进行登录,需要携带第一次找的旧用户信息,登录进来后刷新旧用户数据中微信相关的数据.
这个目前只针对web端,而且这里新旧公众号 指的是主体不一样 也就是说不在一个微信开放平台下面 ,所以会造成 新旧 公众号的 unid 不一样没办法进行关联
而且微信的openid 是指每个公众号针对每个用户都是不同的 也没有办法进行关联
PS: 如果能帮到你记得点个赞,如果真的遇到了这种事情记得好好的宰一下产品!!
我这边就遇到了这种问题,最后想出了一个较为麻烦,对用户体验并不好的方法. 如果各位观看后有思路或者方案希望能留言提出来,谢谢!
我这边目前是针对web端微信的,如果是app 我这边并不是通过微信 而是通过用户的手机号进行相关的绑定,
我这种方法是在用户登录的时候进行,会对用户体验较为不好.
首先微信web端登录的时候,需要把登录发起请求交给后端服务器,这样我们做后端的可以进行控制跳转.
我们先假设用户表 user_id open_id union_id 假设只是一个用户的数据 我们在这个基础上加上 一个old_user 字段来分辨当前用户是否有被刷新成新公众号数据
现在用户表就变成 user_id open_id union_id old_user
第二部就是进行后台微信登录接口,让前端在登录的时候调用后端的微信登录接口
伪代码
进行微信登录
第一次访问接口 : http://url/weixinlogin?key=xxxxx
只能携带 key 也就是要跳转的url
在Controller里面直接进行第二次跳转url进行拼接
用旧的微信公众号appid state=old 生成微信登录url 进行请求
第二次访问接口 : http://url/weixinlogin?key=KEY&code=XXX&state=old&user_id=USER_ID
在这里需要进入service层 在这里进行判断
通过旧微信公众号登录拿到的code 来找openid 和 unid 再找旧用户
找到旧用户后把旧用户的user_id 带出来
在用新微信公总号app_id state=new user_id = old_user_id 生成微信登录url 进行请求
第三次访问接口 : http://url/weixinlogin?key=KEY&code=XXX&state=new&user_id=old_user_id
拿到新微信公众号登录code 找出 用户针对新微信公众号 openid 和 unid
然后根据传过来的old_user_id 进行刷新该用户的 openid 和 unid
再把old_user 改成新用户
=====================================结束=================================================
这里是一些配置
code = 微信回调时的code
state = 微信回调时可以带的自定义参数
key = 自定义参数 主要用来 Base64 进行加密的url 进行跳转的url
user_id = 老用户的user_id
authorization_url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECTURL&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
base_authorization_url=https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECTURL&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
appid = 新微信web appid
old_appid = 老的微信 web appid
new_redirect_url=http://[回调网址]?user_id=USER_ID&key=KEY
/**新
* 微信公众号授权
*
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "/weixinlogin", method = RequestMethod.GET)
public void weChatLogin(
@RequestParam(value = "code", defaultValue = "") String code,
@RequestParam(value = "state", defaultValue = "") String state,
@RequestParam(value = "key", defaultValue = "KEY") String paramKey,
@RequestParam(value = "user_id", defaultValue = "USER_ID") String userId,
HttpServletRequest request, HttpServletResponse response) throws Exception {
//第一次进行跳
if (MiscUtils.isEmpty(state)) {
String authorization_url = MiscUtils.getConfigByKey("base_authorization_url");//静默授权
String appid = MiscUtils.getConfigByKey("old_appid");
String redireceUrl = MiscUtils.getConfigByKey("new_redirect_url");
if (paramKey != null) {
redireceUrl = redireceUrl.replace("KEY", paramKey);
}
String encodeUrl = URLEncoder.encode(redireceUrl, "utf-8");
String authorizationUrl = authorization_url.replace("APPID", appid).replace("REDIRECTURL", encodeUrl).replace("STATE", "old");//修改参数
response.sendRedirect(authorizationUrl);
return;
}
if (state.equals("old")) {
RequestEntity requestEntity = this.createResponseEntity("CommonServer", "newWeixinCodeUserLogin", null, "");
Map<String, String> map = new HashMap<>();
map.put("code", code);
map.put("state", state);
requestEntity.setParam(map);
map.put("code", code);
map.put("user_id", userId);
map.put("state", state);
requestEntity.setParam(map);
ResponseEntity responseEntity = this.process(requestEntity, serviceManger, message);
Map<String, Object> resultMap = (Map<String, Object>) responseEntity.getReturnData();
Integer key = Integer.valueOf(resultMap.get("key").toString());
if (key == 1) {
String authorization_url = MiscUtils.getConfigByKey("base_authorization_url");//静默授权url
String appid = MiscUtils.getConfigByKey("appid");
String redireceUrl = MiscUtils.getConfigByKey("new_redirect_url");
redireceUrl = redireceUrl.replace("KEY", paramKey);
if (!MiscUtils.isEmpty(resultMap.get("user_id"))) {
String user_id = resultMap.get("user_id").toString();
redireceUrl = redireceUrl.replace("USER_ID", user_id);
}
String encodeUrl = URLEncoder.encode(redireceUrl, "utf-8");
String authorizationUrl = authorization_url.replace("APPID", appid).replace("REDIRECTURL", encodeUrl).replace("STATE", "new");//修改参数
response.sendRedirect(authorizationUrl);
return;
}
//没有拿到旧账号的openid 重新登录
logger.info("没有拿到openId 或者 unionid 跳到手动授权页面");
String authorization_url = MiscUtils.getConfigByKey("authorization_url");//手动授权url
String appid = MiscUtils.getConfigByKey("old_appid");
String redireceUrl = MiscUtils.getConfigByKey("new_redirect_url");
redireceUrl = redireceUrl.replace("KEY", paramKey);
if (!MiscUtils.isEmpty(resultMap.get("user_id"))) {
String user_id = resultMap.get("user_id").toString();
redireceUrl = redireceUrl.replace("USER_ID", user_id);
}
String encodeUrl = URLEncoder.encode(redireceUrl, "utf-8");
String authorizationUrl = authorization_url.replace("APPID", appid).replace("REDIRECTURL", encodeUrl).replace("STATE", "old");
;//修改参数
response.sendRedirect(authorizationUrl);
return;
}
//新登路
if (state.equals("new")) {
RequestEntity requestEntity = this.createResponseEntity("CommonServer", "newWeixinCodeUserLogin", null, "");
Map<String, String> map = new HashMap<>();
map.put("code", code);
map.put("user_id", userId);
map.put("state", state);
requestEntity.setParam(map);
ResponseEntity responseEntity = this.process(requestEntity, serviceManger, message);
Map<String, Object> resultMap = (Map<String, Object>) responseEntity.getReturnData();
Integer key = Integer.valueOf(resultMap.get("key").toString());
if (key == 1) {
String userWeixinAccessToken = (String) resultMap.get("access_token");
if (StringUtils.isNotEmpty(paramKey) && !paramKey.equals("KEY")) {
String newUrl = new String(Base64.decode(paramKey));
response.sendRedirect(newUrl + "token=" + userWeixinAccessToken);
} else {
response.sendRedirect(MiscUtils.getConfigByKey("web_index") + userWeixinAccessToken);
}
return;
}
String authorization_url = MiscUtils.getConfigByKey("authorization_url");//手动授权url
String appid = MiscUtils.getConfigByKey("appid");
String redireceUrl = MiscUtils.getConfigByKey("new_redirect_url");
redireceUrl = redireceUrl.replace("KEY", paramKey);
if (!MiscUtils.isEmpty(resultMap.get("user_id"))) {
String user_id = resultMap.get("user_id").toString();
redireceUrl = redireceUrl.replace("USER_ID", user_id);
}
String encodeUrl = URLEncoder.encode(redireceUrl, "utf-8");
String authorizationUrl = authorization_url.replace("APPID", appid).replace("REDIRECTURL", encodeUrl).replace("STATE", "new");//修改参数
response.sendRedirect(authorizationUrl);
return;
}
}
总结:
其实就相当于两次登录
第一次使用旧微信相关配置进行登录,找到旧用户信息,如果找不到就不进行创建
第二次使用新微信相关配置进行登录,需要携带第一次找的旧用户信息,登录进来后刷新旧用户数据中微信相关的数据.
这个目前只针对web端,而且这里新旧公众号 指的是主体不一样 也就是说不在一个微信开放平台下面 ,所以会造成 新旧 公众号的 unid 不一样没办法进行关联
而且微信的openid 是指每个公众号针对每个用户都是不同的 也没有办法进行关联
PS: 如果能帮到你记得点个赞,如果真的遇到了这种事情记得好好的宰一下产品!!