|
@@ -3,6 +3,7 @@ package com.zhongzheng.framework.web.service;
|
|
|
|
|
|
import cn.hutool.core.lang.Validator;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
@@ -28,10 +29,14 @@ import com.zhongzheng.modules.user.domain.UserSchoolInfo;
|
|
|
import com.zhongzheng.modules.user.entity.ClientLoginUser;
|
|
|
import com.zhongzheng.modules.user.mapper.UserMapper;
|
|
|
import com.zhongzheng.modules.user.mapper.UserSchoolInfoMapper;
|
|
|
+import com.zhongzheng.modules.user.mapper.UserWxFollowMapper;
|
|
|
import com.zhongzheng.modules.user.service.IUserService;
|
|
|
+import com.zhongzheng.modules.user.service.IUserWxFollowService;
|
|
|
import com.zhongzheng.modules.user.vo.UserVo;
|
|
|
import com.zhongzheng.modules.wx.bo.WxLoginBody;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
@@ -41,7 +46,9 @@ import javax.annotation.Resource;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
@@ -52,6 +59,8 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Component
|
|
|
public class WxLoginService
|
|
|
{
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(WxLoginService.class);
|
|
|
+
|
|
|
@Autowired
|
|
|
private WxTokenService wxTokenService;
|
|
|
|
|
@@ -61,7 +70,6 @@ public class WxLoginService
|
|
|
@Value("${wx.small.appsecret}")
|
|
|
private String appsrcret;
|
|
|
|
|
|
-
|
|
|
private String wxAuthUrl = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
|
|
|
private String wxAuthParam = "appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
|
|
@@ -108,7 +116,8 @@ public class WxLoginService
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
@Autowired
|
|
|
- private UserSchoolInfoMapper userSchoolInfoMapper;
|
|
|
+ private IUserWxFollowService iUserWxFollowService;
|
|
|
+
|
|
|
|
|
|
public Map<String,String> test_login() {
|
|
|
String unionId = "oQ2yp56PgQ-PfwN4vxTZhR5eTpzk";
|
|
@@ -342,37 +351,69 @@ public class WxLoginService
|
|
|
public String getWxGzhAccessToken() {
|
|
|
String key = "WX_GZH_ACCESS_TOKEN";
|
|
|
String accessToken = redisCache.getCacheObject(key);
|
|
|
- // if(Validator.isEmpty(accessToken)){
|
|
|
+ if(Validator.isEmpty(accessToken)||accessToken.equals("null")){
|
|
|
String param = String.format(gzh_wxTokenParam, gzh_appid, gzh_appsrcret);
|
|
|
String resultString = HttpUtils.sendGet(gzh_wxTokenUrl,param);
|
|
|
//解析json
|
|
|
JSONObject jsonObject = (JSONObject) JSONObject.parse(resultString);
|
|
|
accessToken = String.valueOf(jsonObject.get("access_token"));
|
|
|
- if(Validator.isNotEmpty(accessToken)){
|
|
|
+ if(Validator.isNotEmpty(accessToken)&&!accessToken.equals("null")){
|
|
|
redisCache.setCacheObject(key, accessToken,7100, TimeUnit.SECONDS);//7200有效期
|
|
|
}
|
|
|
- // }
|
|
|
+ }
|
|
|
return accessToken;
|
|
|
}
|
|
|
|
|
|
- public String getWxGzhUserList() {
|
|
|
+ public String getWxGzhUserList(String nextOpenid) {
|
|
|
String accessToken = getWxGzhAccessToken();
|
|
|
- String nextOpenid = null;
|
|
|
+ if(Validator.isEmpty(accessToken)){
|
|
|
+ throw new CustomException("accessToken 错误");
|
|
|
+ }
|
|
|
String param = String.format(gzh_wxUserListParam, accessToken);
|
|
|
if(Validator.isNotEmpty(nextOpenid)){
|
|
|
param += "&next_openid="+nextOpenid;
|
|
|
+ }else{
|
|
|
+ iUserWxFollowService.cleanAllData();
|
|
|
}
|
|
|
String resultString = HttpUtils.sendGet(gzh_wxUserListUrl,param);
|
|
|
- System.out.println(resultString);
|
|
|
+ if(Validator.isNotEmpty(resultString)){
|
|
|
+ JSONObject userInfoJSON = null;
|
|
|
+ userInfoJSON = JSON.parseObject(resultString);
|
|
|
+ if(userInfoJSON.containsKey("total")){
|
|
|
+ Integer total = Integer.valueOf(String.valueOf(userInfoJSON.get("total")));
|
|
|
+ Integer count = Integer.valueOf(String.valueOf(userInfoJSON.get("count")));
|
|
|
+ String nextOpenidBack = String.valueOf(userInfoJSON.get("next_openid"));
|
|
|
+ if(Validator.isNotEmpty(nextOpenidBack)&&!nextOpenidBack.equals("")){
|
|
|
+ getWxGzhUserList(nextOpenidBack);
|
|
|
+ }
|
|
|
+ if(userInfoJSON.containsKey("data")){
|
|
|
+ List<String> openIdList = JSONArray.parseArray(userInfoJSON.getJSONObject("data").getJSONArray("openid").toJSONString(),String.class);
|
|
|
+ List<List<String>> lists = ToolsUtils.splitListBycapacity(openIdList, 1000);
|
|
|
+ for (List<String> listSub:lists) {
|
|
|
+ iUserWxFollowService.insertByBatchAddBo(listSub);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // System.out.println(resultString);
|
|
|
return resultString;
|
|
|
}
|
|
|
|
|
|
public String getWxGzhUserCgiInfo(String openid) {
|
|
|
String accessToken = getWxGzhAccessToken();
|
|
|
+ if(Validator.isEmpty(accessToken)){
|
|
|
+ throw new CustomException("accessToken 错误");
|
|
|
+ }
|
|
|
String param = String.format(gzh_wxUserCgiInfoParam, accessToken,openid);
|
|
|
String resultString = HttpUtils.sendGet(gzh_wxUserCgiInfoUrl,param);
|
|
|
- System.out.println(resultString);
|
|
|
- return resultString;
|
|
|
+ if(Validator.isNotEmpty(resultString)){
|
|
|
+ JSONObject userInfoJSON = null;
|
|
|
+ userInfoJSON = JSON.parseObject(resultString);
|
|
|
+ if(userInfoJSON.containsKey("unionid")){
|
|
|
+ return String.valueOf(userInfoJSON.get("unionid"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
//公众号获取用户信息
|