package com.eco.plugin.wink.hrzdmsg.controller; import com.eco.plugin.wink.hrzdmsg.config.PluginSimpleConfig; import com.eco.plugin.wink.hrzdmsg.utils.FRUtils; import com.eco.plugin.wink.hrzdmsg.utils.HttpUtils; import com.eco.plugin.wink.hrzdmsg.utils.ResponseUtils; import com.eco.plugin.wink.hrzdmsg.utils.Utils; import com.fr.decision.webservice.annotation.LoginStatusChecker; import com.fr.json.JSONObject; import com.fr.plugin.context.PluginContexts; import com.fr.stable.fun.Authorize; import com.fr.third.springframework.stereotype.Controller; import com.fr.third.springframework.web.bind.annotation.PostMapping; import com.fr.third.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; @Controller @LoginStatusChecker(required = false) @Authorize(callSignKey = "com.fr.plugin.wink.hrzdmsg") public class ControllerSelf { @PostMapping(value = "/sendMsg") @ResponseBody public void sendMsg(HttpServletRequest req,HttpServletResponse res){ if(!PluginContexts.currentContext().isAvailable()){ ResponseUtils.failedResponse(res,"插件授权过期,请联系管理员!"); return ; } JSONObject param = Utils.getRequestBody(req); FRUtils.FRLogInfo("param1:"+param.toString()); JSONObject content = new JSONObject(param.getString("content")); param.put("content",content); FRUtils.FRLogInfo("param2:"+param.toString()); JSONObject msg = sendMsg(param,PluginSimpleConfig.getInstance()); ResponseUtils.response(res,msg); } /** * 发送短信 * @param param * @param psc * @return */ public static JSONObject sendMsg(JSONObject param,PluginSimpleConfig psc){ JSONObject result = new JSONObject(); result.put("code",200); String token = getToken(psc); if(Utils.isNullStr(token)){ result.put("code",-1); result.put("msg","获取token失败!"); return result; } Map header = new HashMap(); header.put("Authorization","Bearer "+token); String url = psc.getSendMsgUrl(); String resultStr = HttpUtils.HttpPostJson(url,param.toString(),header); if(Utils.isNullStr(resultStr)){ result.put("code",-1); result.put("msg","发送短信失败!"); return result; } result.put("data",resultStr); return result; } /** * 获取token,用于接口鉴权 * @param psc * @return */ private static String getToken(PluginSimpleConfig psc){ String url = psc.getTokenUrl(); String username = psc.getLoginName(); String psd = psc.getPassword(); JSONObject param = new JSONObject(); param.put("loginName",username); param.put("password",psd); String result = HttpUtils.HttpPostJson(url,param.toString(),null); if(Utils.isNullStr(result)){ return ""; } return new JSONObject(result).getString("accessToken"); } }