|
@ -0,0 +1,111 @@
|
|
|
package com.yihu.wlyy.service.weixin.openid;
|
|
|
|
|
|
import com.yihu.wlyy.entity.wechat.WxOpenidTemp;
|
|
|
import com.yihu.wlyy.repository.wechat.WxOpenidTempDao;
|
|
|
import com.yihu.wlyy.util.HttpUtil;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONObject;
|
|
|
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.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* Created by Trick on 2018/6/28.
|
|
|
*/
|
|
|
@Service
|
|
|
@Transactional
|
|
|
public class WxOpenidTempService {
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(WxOpenidTempService.class);
|
|
|
|
|
|
@Value("${wechat.appId}")
|
|
|
private String appId;
|
|
|
@Value("${wechat.appSecret}")
|
|
|
private String appSecret;
|
|
|
@Autowired
|
|
|
private HttpUtil HttpUtil;
|
|
|
@Autowired
|
|
|
private WxOpenidTempDao wxOpenidTempDao;
|
|
|
|
|
|
public String loadWeChatOpenIDList(){
|
|
|
|
|
|
//微信开发者文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140840
|
|
|
|
|
|
/**
|
|
|
* 返回成功格式:
|
|
|
* {"total":2,
|
|
|
"count":2,
|
|
|
"data":{
|
|
|
"openid":["OPENID1","OPENID2"]},
|
|
|
"next_openid":"NEXT_OPENID"
|
|
|
}
|
|
|
*/
|
|
|
/**
|
|
|
* 返回失败格式:
|
|
|
*
|
|
|
* {"errcode":40013,"errmsg":"invalid appid"}
|
|
|
*/
|
|
|
String openidList_url = "https://api.weixin.qq.com/cgi-bin/user/get";
|
|
|
String params ="access_token="+getToken();
|
|
|
String result = HttpUtil.sendGet(openidList_url, params);
|
|
|
JSONObject json = new JSONObject(result);
|
|
|
if (json.has("data")) {
|
|
|
JSONArray array = json.getJSONArray("openid");
|
|
|
List<WxOpenidTemp> list = new ArrayList<>();
|
|
|
for(int i=0;i< array.length();i++){
|
|
|
WxOpenidTemp wx = new WxOpenidTemp();
|
|
|
String openid = (String) array.get(i);
|
|
|
wx.setOpenid(openid);
|
|
|
list.add(wx);
|
|
|
}
|
|
|
wxOpenidTempDao.save(list);
|
|
|
String nextOpenid = json.getString("next_openid");
|
|
|
//递归调用获取openid并存储
|
|
|
if(StringUtils.isNotBlank(nextOpenid)){
|
|
|
nextOpenIDList(nextOpenid);
|
|
|
}
|
|
|
}
|
|
|
return "1";
|
|
|
}
|
|
|
|
|
|
public void nextOpenIDList(String nextOpenid){
|
|
|
String openidList_url = "https://api.weixin.qq.com/cgi-bin/user/get";
|
|
|
String params ="access_token="+getToken()+"&next_openid="+nextOpenid;
|
|
|
String result = HttpUtil.sendGet(openidList_url, params);
|
|
|
JSONObject json = new JSONObject(result);
|
|
|
if (json.has("data")) {
|
|
|
JSONArray array = json.getJSONArray("openid");
|
|
|
List<WxOpenidTemp> list = new ArrayList<>();
|
|
|
for(int i=0;i< array.length();i++){
|
|
|
WxOpenidTemp wx = new WxOpenidTemp();
|
|
|
String openid = (String) array.get(i);
|
|
|
wx.setOpenid(openid);
|
|
|
list.add(wx);
|
|
|
}
|
|
|
wxOpenidTempDao.save(list);
|
|
|
String next = json.getString("next_openid");
|
|
|
if(StringUtils.isNotBlank(nextOpenid)){
|
|
|
nextOpenIDList(next);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String getToken(){
|
|
|
String token_url = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
String params = "grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
|
|
|
String result = HttpUtil.sendGet(token_url, params);
|
|
|
JSONObject json = new JSONObject(result);
|
|
|
if (json.has("access_token")) {
|
|
|
String token = json.get("access_token").toString();
|
|
|
return token;
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
}
|