package com.yihu.platform.api; import com.yihu.platform.apiservice.IHealthService; import com.yihu.platform.utils.ApiUtil; import com.yihu.platform.utils.StringUtil; import com.yihu.wsgw.api.InterfaceMessage; import net.sf.json.JSONException; import net.sf.json.JSONObject; /** * Created by jcl on 2017/9/12. */ public class MessageApi { /** * 微信推送 * @param msg * @return */ public String push(InterfaceMessage msg) { try { JSONObject json = JSONObject.fromObject(msg.getParam()); String userId = StringUtil.isEmpty(json.get("userId")) ? null : json.getString("userId"); String handlerId = StringUtil.isEmpty(json.get("handlerId")) ? null : json.getString("handlerId"); String content = StringUtil.isEmpty(json.get("content")) ? null : json.getString("content"); String detailUrl = StringUtil.isEmpty(json.get("detailUrl")) ? null : json.getString("detailUrl"); String doctorUserId = StringUtil.isEmpty(json.get("doctorUserId")) ? null : json.getString("doctorUserId"); if (StringUtil.isEmpty(userId)) { return ApiUtil.getRespJSON(-10000, "userId不能为空").toString(); } if (StringUtil.isEmpty(handlerId)) { return ApiUtil.getRespJSON(-10000, "handlerId不能为空").toString(); } if (StringUtil.isEmpty(content)) { return ApiUtil.getRespJSON(-10000, "content不能为空").toString(); } if (StringUtil.isEmpty(doctorUserId)) { return ApiUtil.getRespJSON(-10000, "doctorUserId不能为空").toString(); } IHealthService api = new IHealthService(); String resp = api.push(userId,handlerId,content,detailUrl,doctorUserId); JSONObject re = JSONObject.fromObject(resp); if (re.getInt("status") != 10000) { return ApiUtil.getRespJSON(-10000, "微信推送失败:" + re).toString(); } JSONObject obj = ApiUtil.getRespJSON(10000, "成功"); return obj.toString(); } catch (JSONException e) { return ApiUtil.getRespJSON(-10000, "非标准json:[" + msg.getParam() + "]").toString(); } catch (Exception e) { e.printStackTrace(); return ApiUtil.getRespJSON(-14444, "加载异常!" + StringUtil.getException(e)).toString(); } } /** * 短信发送 * @param msg * @return */ public String send(InterfaceMessage msg) { try { JSONObject json = JSONObject.fromObject(msg.getParam()); String mobile = StringUtil.isEmpty(json.get("mobile")) ? null : json.getString("mobile"); String content = StringUtil.isEmpty(json.get("content")) ? null : json.getString("content"); String doctorUserId = StringUtil.isEmpty(json.get("doctorUserId")) ? null : json.getString("doctorUserId"); if (StringUtil.isEmpty(mobile)) { return ApiUtil.getRespJSON(-10000, "mobile不能为空").toString(); } if (StringUtil.isEmpty(content)) { return ApiUtil.getRespJSON(-10000, "content不能为空").toString(); } if (StringUtil.isEmpty(doctorUserId)) { return ApiUtil.getRespJSON(-10000, "doctorUserId不能为空").toString(); } IHealthService api = new IHealthService(); String resp = api.send(mobile,content,doctorUserId); JSONObject re = JSONObject.fromObject(resp); if (re.getInt("status") != 10000) { return ApiUtil.getRespJSON(-10000, "短信失败:" + re).toString(); } JSONObject obj = ApiUtil.getRespJSON(10000, "成功"); return obj.toString(); } catch (JSONException e) { return ApiUtil.getRespJSON(-10000, "非标准json:[" + msg.getParam() + "]").toString(); } catch (Exception e) { e.printStackTrace(); return ApiUtil.getRespJSON(-14444, "加载异常!" + StringUtil.getException(e)).toString(); } } }