com.mingsoft.weixin.util.GroupUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.mingsoft.weixin.util.GroupUtils.java

Source

/**
The MIT License (MIT) * Copyright (c) 2016 (mingsoft.net)
    
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
    
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
    
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.mingsoft.weixin.util;

import java.io.IOException;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.mingsoft.util.JsonUtil;
import com.mingsoft.weixin.http.HttpClientConnectionManager;

/**
 * 
 * 
 * @author ??(qq:330216230)
 * @version 
 * ?100-000-000<br/>
 * 2014-4-1<br/>
 * ?<br/>
 */
public class GroupUtils extends BaseUtils {

    /**
     * ???</br>
     * access_token:???????
     */
    private final static String GROUP_QUERY_GET = "get?access_token=";

    /**
     * POST??Id
     * access_token:???????
     */
    private final static String GROUP_QUERY_USER_GETID = "getid?access_token=";

    /**
     * POST
     * access_token:???????
     */
    private final static String GROUP_SAVE_CREATE = "create?access_token=";

    /**
     * POST
     * access_token:???????
     */
    private final static String GROUP_UPDATE = "update?access_token=";

    /**
     * POST
     * access_token:???????
     */
    private final static String GROUP_UPDATE_MOBILE = "groups/members/update?access_token=";

    /**
     * JSON???
     */
    private final static String GROUP_ERRMSG = "errmsg";

    /**
     * ??Id
     */
    private final static String GROUP_ID = "groupid";

    /**
     * JSON???
     */
    private final static String GROUP_ERRMSG_OK = "ok";

    /**
     * ??????
     * @param appid ??????
     * @param secret ??????
     */
    public GroupUtils(String appid, String secret) {
        super(appid, secret);
        // TODO Auto-generated constructor stub
    }

    /**
     * ???
     * @return {"groups": [{ </br>
     *             "id":id?, </br>
     *             "name": "??UTF8?", </br>
     *             "count": ?}]}, 
     */
    public String queryGrouping() {
        //??accessToken?
        String pathUrl = GROUP_URL + GROUP_QUERY_GET + getAccessToken();
        HttpGet get = HttpClientConnectionManager.getGetMethod(pathUrl);
        DefaultHttpClient HTTPCLIENT = new DefaultHttpClient();
        try {
            HttpResponse responses = (HttpResponse) HTTPCLIENT.execute(get);
            String jsonStr = EntityUtils.toString(responses.getEntity(), "utf-8");
            logger.debug("JSON:" + jsonStr);
            return jsonStr;
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 
     * @param openId ?
     * @return ID {"groupid": id?}
     */
    @SuppressWarnings("unchecked")
    public String queryUserGroup(String openId) {
        String url = GROUP_URL + GROUP_QUERY_USER_GETID + getAccessToken();
        String json = "{\"openid\":\"" + openId + "\"}";
        Map object = JsonUtil.getMap4Json(getPost(json, url));
        return object.get(GROUP_ID).toString();
    }

    /**
     * 
     * @param groupName ??:?30
     * @return ?{ "group": { "id": 107,  "name": "test" }}
     */
    public String saveGroup(String groupName) {
        if (!(groupName.length() > 30)) {
            String url = GROUP_URL + GROUP_SAVE_CREATE + getAccessToken();
            String json = "{\"group\":{\"name\":\"" + groupName + "\"}}";
            return getPost(json, url);
        } else {
            return "";
        }
    }

    /**
     * ????
     * @param updateGroupName ???(??:?30)
     * @param groupId ?ID
     * @return true ?:{"errcode": 0, "errmsg": "ok"}
     */
    @SuppressWarnings("unchecked")
    public boolean updateGroup(String updateGroupName, int groupId) {
        if (!(updateGroupName.length() > 30)) {
            String url = GROUP_URL + GROUP_UPDATE + getAccessToken();
            String json = "{\"group\":{\"id\":" + groupId + ",\"name\":\"" + updateGroupName + "\"}}";
            Map object = JsonUtil.getMap4Json(getPost(json, url));
            String accessToken = object.get(GROUP_ERRMSG).toString();
            if (accessToken.equals(GROUP_ERRMSG_OK)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 
     * @param openId ??
     * @param groupId ?ID
     * @return true ?:{"errcode": 0, "errmsg": "ok"}
     */
    @SuppressWarnings("unchecked")
    public boolean mobileGroup(String openId, int groupId) {
        String url = GROUP_URL + GROUP_UPDATE_MOBILE + getAccessToken();
        String json = "{\"openid\":\"" + openId + "\",\"to_groupid\":" + groupId + "}";
        Map object = JsonUtil.getMap4Json(getPost(json, url));
        String accessToken = object.get(GROUP_ERRMSG).toString();
        if (accessToken.equals(GROUP_ERRMSG_OK)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * ??JSON?POST
     * @param json JSON?
     * @param url ?
     * @return JSON
     */
    private String getPost(String json, String url) {
        HttpPost httpost = HttpClientConnectionManager.getPostMethod(url);
        try {
            httpost.setEntity(new StringEntity(json, "UTF-8"));
            HttpResponse response = HTTPCLIENT.execute(httpost);
            String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
            String obj = JSON.toJSONString(jsonStr);

            logger.debug("POST?:" + obj);
            return obj;
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
}