import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import net.iharder.Base64;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class JMessageUtils {
public final static String JMESSAGE_URL = "https://api.im.jpush.cn";
private static String appKey;
private static String masterSecret;
@Autowired
private Environment environment;
@PostConstruct
public void init() {
JMessageUtils.appKey = environment.getProperty("config.jpush.appkey");
JMessageUtils.masterSecret = environment.getProperty("config.jpush.mastersecret");
}
/**
* 註冊用戶:一次批量註冊最多支持500個用戶
* <p>
* username 用戶登錄名(必填)
* password 登錄密碼(必填)
* appkey 用戶所屬於的應用的appkey
* nickname 用戶暱稱
* birthday 生日 yyyy-MM-dd HH:mm:ss
* gender 性別 0 - 未知, 1 - 男 ,2 - 女
* signature 用戶簽名
* region 用戶所屬地區
* address 用戶詳細地址
* ctime 用戶創建時間
* mtime 用戶最後修改時間
* extras
*/
public String insertUser(JSONArray users) throws IOException, Exception {
String url = JMESSAGE_URL + "/v1/users/";
return httpPostOrGet(url, users.toJSONString());
}
/**
* 管理員註冊 (管理員api發送消息接口的權限)
*/
public String adminRegister(JSONObject admin) throws IOException, Exception {
String url = JMESSAGE_URL + "/v1/admins/";
return httpPostOrGet(url, admin.toJSONString());
}
/**
* 獲取應用管理員列表
*
* @param start 起始記錄位置 從0開始
* @param count 查詢條數 最多支持500條
*/
public String getAdminsListByAppkey(Integer start, Integer count) throws IOException, Exception {
String url = JMESSAGE_URL + "/v1/admins?start=" + start + "&count=" + count;
return httpPostOrGet(url);
}
/**
* 獲取用戶信息
*/
public String getUserInfo(String username) throws IOException, Exception {
String url = JMESSAGE_URL + "/v1/users/" + username;
return httpPostOrGet(url);
}
/**
* 用戶在線狀態查詢(不適用於多端在線)
*
* @return {"login": true,"online": false}
*/
public String getUserStat(String username) throws IOException, Exception {
String url = JMESSAGE_URL + "/v1/users/" + username + "/userstat";
return httpPostOrGet(url);
}
/**
* 批量用戶在線狀態查詢
*
* @return [{ "devices": [],"username": "caiyh01"},
* {"devices": [{"login": false, "online": false,"platform": "a" }],"username": "Rauly" }]
* <p>devices: 設備登陸狀態數組,沒有登陸過數組爲空; platform: SDK各平臺:a-Android,i-iOS,j-JS,w-Windows</p>
*/
public String getUserStat(String... username) throws IOException, Exception {
String url = JMESSAGE_URL + "/v1/users/userstat";
return httpPostOrGet(url, username);
}
/**
* 批量用戶在線狀態查詢2
*
* @return [{"devices": [], "username": "caiyh01","statusmsg": "no_exist", "statuscode": 1 // 用戶不存在,會有statusmsg }, {
* "devices": [{ "login": false, "online": false,"platform": "a" }], "username": "Rauly", "statuscode": 0 // 用戶存在}]
*/
public String getUserStat2(String... username) throws IOException, Exception {
String url = JMESSAGE_URL + "/v2/users/statuser";
return httpPostOrGet(url, username);
}
/**
* 修改密碼
*/
public String updatePassword(String username, String newPassword) throws IOException, Exception {
// PUT
String url = JMESSAGE_URL + "/v1/users/" + username + "/password";
JSONObject password = new JSONObject();
password.put("new_password", newPassword);
return httpPostOrGet(url, password.toJSONString());
}
/**
* 刪除用戶
*/
public String deleteUser(String username) throws IOException, Exception {
// DELETE
String url = JMESSAGE_URL + "/v1/users/" + username;
return httpPostOrGet(url);
}
/**
* 刪除用戶(最多支持同時刪除100個用戶)
*/
public String deleteUsers(String... username) throws IOException, Exception {
// DELETE
String url = JMESSAGE_URL + "/v1/users/";
return httpPostOrGet(url, username);
}
/**
* 添加黑名單
*/
public String addUserBlacklist(String username) throws IOException, Exception {
// HttpMethod.PUT
// PUT
String url = JMESSAGE_URL + "/v1/users/" + username + "/blacklist";
return httpPostOrGet(url);
}
/**
* 添加黑名單
*/
public String addUserBlacklist(String... username) throws IOException, Exception {
// PUT
String url = JMESSAGE_URL + "/v1/users/" + username + "/blacklist";
return httpPostOrGet(url);
}
private String httpPostOrGet(String url) throws IOException, Exception {
return httpPostOrGet(url, null);
}
/**
* @param url 請求URL
* @param payload 請求體爲null 時,使用 GET請求方式
*/
private String httpPostOrGet(String url, Object payload) throws IOException, Exception {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", Base64.encodeObject(appKey + ":" + masterSecret));
String response;
if (null == payload) {
response = HttpUtils.get(url, headers);
} else {
response = HttpUtils.postBody(url, headers, "application/json", payload);
}
return response;
}
}