Browse Source

优化:获取用户unionid。

lym 4 years ago
parent
commit
98481a5f05
1 changed files with 101 additions and 1 deletions
  1. 101 1
      src/main/java/com/hw/controller/WechatOpenidController.java

+ 101 - 1
src/main/java/com/hw/controller/WechatOpenidController.java

@@ -22,7 +22,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -246,6 +249,7 @@ public class WechatOpenidController extends BaseController {
                                 infoFrom.setOldOpenid(object.getString("ori_openid"));
                                 infoFrom.setNewOpenid(object.getString("new_openid"));
                                 infoFrom.setOldAppid(appid);
+                                infoFrom.setNewAppid(newAppId);
                                 editList.add(infoFrom);
                             }
                         }
@@ -304,4 +308,100 @@ public class WechatOpenidController extends BaseController {
         }
         return editNum;
     }
+
+    @ApiOperation(value = "获取unionid",notes = "获取unionid")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "appid",value = "获取unionid公众号appid",dataType = "String"),
+    })
+    @ApiResponse(code = 0,message = "操作成功")
+    @GetMapping("/getUnionids")
+    public ResponseBase getUnionids(String appid){
+        if (StringUtils.isEmpty(appid)){
+            appid = oldAppId;
+        }
+        int editNum = getUnionid(appid,1,100,0);
+        ResultVO resultVO = new ResultVO();
+        if (editNum < 1){
+            resultVO.setMsg("openid没有获取对应的unionid!");
+        }else {
+            resultVO.setMsg("拉取保存用户unionid"+ editNum +"条");
+        }
+        return responseSuccess(resultVO);
+    }
+
+    /**
+     * 获取unionid
+     * lym
+     * @param appid
+     * @param editNum
+     * @return
+     */
+    private int getUnionid(String appid,int pageNo,int pageSize,int editNum) {
+        WechatInfoForm from = new WechatInfoForm();
+        from.setOldAppid(appid);
+        from.setPageNo(pageNo);
+        from.setPageSize(pageSize); //因一次只能获取100个,所以取100个
+        PageInfo<List<String>> listPageInfo = wechatInfoService.selectOldOpenids(from);
+        int total = (int) listPageInfo.getTotal();
+        if (total > 0) {
+            List<List<String>> openidList = listPageInfo.getList();
+
+            List<Object> paramList = new ArrayList<>();
+            Map param = null;
+            for (int i = 0;i < openidList.size();i++){
+                param = new HashMap();
+                param.put("openid", openidList.get(i));
+                paramList.add(param);
+            }
+            //获取新公众号access_token
+            String accessToken = getAccessToken(appid, oldSecret, CARBONNEWWHAT_ACCESS_TOKEN);
+            Map map = new HashMap();
+            map.put("user_list", paramList);
+            //获取用户信息
+            String result = EasyHttpUtils.LoadInstance().post(WECHAT_API_URl + "/user/info/batchget?access_token=" + accessToken, (JSONObject) JSONObject.toJSON(map));
+            log.info(result);
+            if (StringUtils.isNotEmpty(result)) {
+                JSONObject jsonObject = JSONObject.parseObject(result);
+                if (!jsonObject.containsKey("errcode") && jsonObject.containsKey("user_info_list")) {
+                    JSONArray resultList = (JSONArray) jsonObject.get("user_info_list");
+                    if (resultList != null && resultList.size() > 0) {
+                        List<WechatInfoForm> editList = new ArrayList<>();
+                        WechatInfoForm infoFrom = null;
+                        for (int i = 0; i < resultList.size(); i++) {
+                            JSONObject object = resultList.getJSONObject(i);
+                            if (object.containsKey("openid") && object.containsKey("unionid")) {
+                                infoFrom = new WechatInfoForm();
+                                infoFrom.setOldOpenid(object.getString("openid"));
+                                infoFrom.setOldUnionid(object.getString("unionid"));
+                                infoFrom.setOldAppid(appid);
+                                editList.add(infoFrom);
+                            }
+                        }
+
+                        if (editList != null && editList.size() > 0) {
+                            int i = wechatInfoService.batchUpdate(editList); //修改迁移对应openid
+                            if (i > 0) {
+                                editNum += i;
+                            }
+                        }
+
+                        int pageNum = total / pageSize;
+                        int yNum = total % pageSize;
+                        if (yNum > 0) { //不整除分页加1
+                            pageNum += 1;
+                        }
+                        if (pageNo < pageNum) {
+                            pageNo += 1;
+                            getUnionid(appid, pageNo, pageSize, editNum);
+                        }
+                    }
+                }
+                if (jsonObject.containsKey("errcode")) {
+                    throw new GlobalException("获取unionid失败,请检查公众号配置!!");
+                }
+            }
+
+        }
+        return  editNum;
+    }
 }