獲取關注了關注了公衆號微信用戶的openid


import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class WechatUserService {

    private final static String GET_TAG_USERS_URL = "https://api.weixin.qq.com/cgi-bin/user/tag/get";
    private final static String GET_ALL_USER_OPENID_URL = "https://api.weixin.qq.com/cgi-bin/user/get";
    private static final String GET_ACCESS_TOKEN_URL ="https://api.weixin.qq.com/sns/oauth2/access_token";
    @Autowired
    private OfficialAccountsManager officialAccountsManager;
    @Autowired
    private AccessTokenManagerCenter accessTokenManagerCenter;


    @Autowired
    private RestTemplate restTemplate;


    public List<String> getOpenIdsByTag(String tagId, String nextOpenId) {
        Map<String, Object> body = new HashMap<>();
        body.put("tagid", tagId);
        body.put("next_openid", nextOpenId);
        String result = restTemplate.postForObject(getTagUsersCompleteUrl(), body, String.class);
        if (result != null && result.contains("count")) {
            JSONObject data = JSON.parseObject(result).getJSONObject("data");
            return data.getJSONArray("openid").toJavaList(String.class);
        }
        throw new WechatRequestException("獲取標籤用戶列表失敗:" + result);
    }


    private String getTagUsersCompleteUrl() {
        return UriComponentsBuilder.fromHttpUrl(GET_TAG_USERS_URL)
                .queryParam("access_token", accessTokenManagerCenter.getAccessToken())
                .build().toUri().toString();
    }

    private String getAllUserCompleteUrl() {
        return UriComponentsBuilder.fromHttpUrl(GET_ALL_USER_OPENID_URL)
                .queryParam("access_token", accessTokenManagerCenter.getAccessToken())
                .build().toUri().toString();
    }


    private String accessTokenUrl(String code) {
        return UriComponentsBuilder.fromHttpUrl(GET_ACCESS_TOKEN_URL)
                .queryParam("appid", officialAccountsManager.getAppId())
                .queryParam("secret", officialAccountsManager.getAppSecret())
                .queryParam("code", code)
                .queryParam("grant_type", "authorization_code")
                .build().toUri().toString();
    }

     public String getAccessTokenByCode(String code) {
         String url = accessTokenUrl(code);
         return restTemplate.getForObject(url, String.class);
     }

    public List<String> getAllOpenId(String next_openid) {
        //return getOpenIdsByTag("100","");
        Map<String, Object> requestParams = Maps.newHashMap();
        requestParams.put("next_openid", next_openid);
        String result = restTemplate.postForObject(getAllUserCompleteUrl(), requestParams, String.class);
        if (result != null && result.contains("count")) {
            JSONObject data = JSON.parseObject(result).getJSONObject("data");
            return data.getJSONArray("openid").toJavaList(String.class);
        }
        return Collections.emptyList();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章