Browse Source

消息推送相关修改!

8 years ago
parent
commit
52ab2747da

+ 1 - 1
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/job/message/FollowupPlanJob.java

@ -83,7 +83,7 @@ public class FollowupPlanJob implements Job {
                    message.setReadonly(1);//是否只读消息
                    list.add(message);
                    // 推送消息给医生
                    PushMsgTask.url= url+"api/v1/chats/sm";
                    PushMsgTask.url= url;
                    // 推送消息给医生
                    PushMsgTask.getInstance().put(doctor,"4",title,content,"");
                }

+ 1 - 1
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/job/message/HealthMessageJob.java

@ -128,7 +128,7 @@ public class HealthMessageJob implements Job {
            dbStorage.saveLog(quartzJobLog);
            // 推送消息给医生
            PushMsgTask.url=url+"api/v1/chats/sm";
            PushMsgTask.url=url;
            // 推送消息给医生
            PushMsgTask.getInstance().put(jsonArray);
        }catch (Exception e){

+ 1 - 1
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/job/message/NoticeJob.java

@ -124,7 +124,7 @@ public class NoticeJob implements Job {
                jsonArray.add(json);
            }
            // 推送消息给医生
            PushMsgTask.url = url + "/api/v1/chats/sm";
            PushMsgTask.url = url;
            // 推送消息给医生
            PushMsgTask.getInstance().put(jsonArray);
            //保存日志

+ 17 - 7
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/task/PushMsgTask.java

@ -1,6 +1,7 @@
package com.yihu.wlyy.statistics.task;
import com.yihu.wlyy.statistics.util.HttpClientUtil;
import com.yihu.wlyy.statistics.util.ImUtill;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.NameValuePair;
@ -125,13 +126,22 @@ public class PushMsgTask {
	 */
	public static boolean pushMessage(String receiver, String msgType, String title, String msg, String data) {
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("to", receiver));
			params.add(new BasicNameValuePair("content", msg));
			params.add(new BasicNameValuePair("contentType", msgType));
			params.add(new BasicNameValuePair("title", title));
			params.add(new BasicNameValuePair("summary", data));
			String response = HttpClientUtil.post(url, params, "UTF-8");
			//List<NameValuePair> params = new ArrayList<NameValuePair>();
			//params.add(new BasicNameValuePair("to", receiver));
			//params.add(new BasicNameValuePair("content", msg));
			//params.add(new BasicNameValuePair("contentType", msgType));
			//params.add(new BasicNameValuePair("title", title));
			//params.add(new BasicNameValuePair("summary", data));
			//String response = HttpClientUtil.post(url, params, "UTF-8");
			org.json.JSONObject participants = new org.json.JSONObject();
			participants.put("system",0);
			participants.put(receiver,0);
			org.json.JSONObject sessionObj  = ImUtill.createSession(url,participants,"2","系统消息","");
			if(sessionObj.getInt("status")==-1){
				throw  new RuntimeException(sessionObj.getString("message"));
			}
			org.json.JSONObject session = sessionObj.getJSONObject("data");
			ImUtill.sendImMsg(url,"system","系统",session.getString("id"),"1", msg,msgType);
			return true;
		} catch (Exception e) {
			logger.error("push message error:", e);

+ 26 - 0
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/util/HttpClientUtil.java

@ -11,6 +11,10 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@ -99,4 +103,26 @@ public class HttpClientUtil {
		return null;
	}
	public static String postBody(String url,JSONObject params){
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
		String ret = restTemplate.postForObject(url, formEntity, String.class);
		return ret;
	}
	public static void putBody(String url,JSONObject params){
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
		restTemplate.put(url, formEntity, String.class);
	}
}

+ 221 - 0
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/util/ImUtill.java

@ -0,0 +1,221 @@
package com.yihu.wlyy.statistics.util;
import org.json.JSONObject;
/**
 * Created by 卓 on 2017/1/13.
 */
public class ImUtill {
    /**
     * 发送消息给IM
     *
     * @param from        来自
     * @param contentType 1文字 2图片消息
     * @param content     内容
     */
    public static String sendImMsg(String baseUrl,String from,String fromName, String sessionId, String contentType, String content,String businessType) {
        String imAddr = baseUrl + "/api/v2/sessions/"+sessionId+"/messages";
        JSONObject params = new JSONObject();
        params.put("sender_id", from);
        params.put("sender_name", fromName);
        params.put("content_type", contentType);
        params.put("content", content);
        params.put("session_id", sessionId);
        params.put("business_type", businessType);
        String response = HttpClientUtil.postBody(imAddr, params);
        return response;
    }
    /**
     * 发送消息给IM
     *
     * @param from        来自
     * @param contentType 1文字 2图片消息
     * @param content     内容
     */
    public static String sendTopicIM(String baseUrl,String from,String fromName, String topicId, String contentType, String content) {
        String url = baseUrl+ ("api/v2/sessions/topic/"+topicId+"/messages");
        JSONObject params = new JSONObject();
        params.put("sender_id", from);
        params.put("sender_name", fromName);
        params.put("content_type", contentType);
        params.put("content", content);
        params.put("topic_id", topicId);;
        String response = HttpClientUtil.postBody(url, params);
        return response;
    }
    /**
     * 结束议题
     *
     * @param topicId  议题ID
     * @param endUser  结束人
     * @param endUserName 结束人名字
     * @param sessionId 会话ID
     */
    public static JSONObject endTopics(String baseUrl,String sessionId,String endUser, String endUserName,String topicId) {
        String imAddr = baseUrl + "api/v2/sessions/"+sessionId+"/topics/"+topicId+"/ended";
        JSONObject params = new JSONObject();
        params.put("session_id", sessionId);
        params.put("end_user", endUser);
        params.put("end_user_name",endUserName);
        params.put("topic_id", topicId);
        String ret = HttpClientUtil.postBody(imAddr,params);
        JSONObject obj  = null;
        try{
            obj = new JSONObject(ret);
        }catch (Exception e){
            return null;
        }
        return obj;
    }
    /**
     * 议题邀请人员
     * @param user 结束人名字
     * @param sessionId 会话ID
     */
    public static void updateTopicUser(String baseUrl,String sessionId,String user) {
        String imAddr = baseUrl + "api/v2/sessions/"+sessionId+"/participants/"+user;
        JSONObject params = new JSONObject();
        params.put("user", user+":"+0);
        HttpClientUtil.putBody(imAddr,params);
    }
    /**
     * 创建议题
     *
     * @param topicId  议题ID
     * @param topicName  议题名称
     * @param participants 成员
     */
    public static JSONObject createTopics(String baseUrl,String sessionId, String topicId, String topicName, JSONObject participants, JSONObject messages, String sessionType) {
        String imAddr = baseUrl + "api/v2/sessions/"+topicId+"/topics";
        JSONObject params = new JSONObject();
        params.put("topic_id", topicId);
        params.put("topic_name", topicName);
        params.put("participants", participants.toString());
        params.put("messages", messages.toString());
        params.put("session_id", sessionId);
        params.put("session_type", sessionType);
        String ret = HttpClientUtil.postBody(imAddr,params);
        JSONObject obj  = null;
        try{
            obj = new JSONObject(ret);
        }catch (Exception e){
            return null;
        }
        return obj;
    }
    /**
     * 创建会话(system)
     *
     */
    public static JSONObject createSession(String baseUrl,JSONObject participants,String sessionType,String sessionName,String sessionId) {
        String imAddr = baseUrl + "api/v2/sessions";
        JSONObject params = new JSONObject();
        params.put("participants", participants.toString());
        params.put("session_name", sessionName);
        params.put("session_type", sessionType);
        params.put("session_id", sessionId);
        String ret = HttpClientUtil.postBody(imAddr,params);
        JSONObject obj  = null;
        try{
            obj = new JSONObject(ret);
        }catch (Exception e){
            return null;
        }
        return obj;
    }
    /**
     * 获取会话实例的消息对象
     * @param senderId
     * @param senderName
     * @param title
     * @param description
     * @param images
     * @return
     */
    public static JSONObject getCreateTopicMessage(String senderId,String senderName,String title,String description,String images){
        JSONObject messages = new JSONObject();
        messages.put("description",description);
        messages.put("title",title);
        messages.put("img",images);
        messages.put("sender_id",senderId);
        messages.put("sender_name",senderName);
        return messages;
    }
    public static JSONObject getTopicMessage(String baseUrl,String topicId,String startMsgId,String endMsgId,int page,int pagesize,String uid){
        String url = baseUrl
                + "api/v2/sessions/topic/"+topicId+"/messages?topic_id="+topicId+"&end="+startMsgId
                +"&start="+(endMsgId==null?"":endMsgId)+"&page="+page+"&pagesize="+pagesize+"&user="+uid;
        try{
            String ret = HttpClientUtil.get(url, "UTF-8");
            JSONObject obj = new JSONObject(ret);
            if(obj.getInt("status")==-1){
                throw new RuntimeException(obj.getString("message"));
            }else{
                return  obj.getJSONObject("data");
            }
        }catch (Exception e){
            return null;
        }
    }
    /**
     * 删除对应的成员信息在MUC模式中
     * @param userId
     * @param oldUserId
     * @param sessionId
     * @return
     */
    public static JSONObject deleteMucUser(String baseUrl,String userId,String oldUserId,String sessionId) throws  Exception{
        String url = baseUrl
                + "api/v2/sessions/"+sessionId+"/participant/update";
        try{
            JSONObject params = new JSONObject();
            params.put("user_id", userId);
            params.put("old_user_id", oldUserId);
            params.put("session_id", sessionId);
            String ret = HttpClientUtil.postBody(url,params);
            JSONObject obj = new JSONObject(ret);
            if(obj.getInt("status")==-1){
                throw new RuntimeException("人员更换失败!");
            }else{
                return obj;
            }
        }catch (Exception e){
           throw new RuntimeException("人员更换失败!");
        }
    }
    /**
     * 获取议题
     * @param topicId
     * @return
     */
    public static JSONObject getTopic(String baseUrl,String topicId) throws  Exception{
        String url =baseUrl
                + "api/v2/sessions/topics/"+topicId+"?topic_id="+topicId;
        try{
            String ret = HttpClientUtil.get(url,"utf-8");
            JSONObject obj = new JSONObject(ret);
            if(obj.getInt("status")==-1){
                throw new RuntimeException("获取议题失败!");
            }else{
                return obj;
            }
        }catch (Exception e){
            throw new RuntimeException("获取议题失败!");
        }
    }
}