Bläddra i källkod

短信发送模板

zhaopeiqing 3 år sedan
förälder
incheckning
4aa36d098d

+ 49 - 0
src/main/java/com/ssm/controller/business/SendMessageContorller.java

@@ -207,6 +207,55 @@ public class SendMessageContorller {
 		Util.json(response, json.toString());
 	}
 
+	/**
+	 * 接口发送短信
+	 * @param phones 手机号码
+	 * @param Contents 发送内容
+	 * @param md5 加密
+	 * @return
+	 */
+	@WithoutUserLogin
+	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
+	@Transactional(value = "txManager")
+	@RequestMapping(value = "sendSmsBySignId")
+	public void sendSms(HttpServletRequest request, HttpServletResponse response) {
+		JSONObject json = new JSONObject();
+		try {
+			request.setCharacterEncoding("utf-8");
+			String smsUserPhoens = request.getParameter("phones");
+			String content = request.getParameter("contents");
+			String signId = request.getParameter("signId");
+			String res = SmsUtil.sendNewSmsPostBySignId(smsUserPhoens, content, signId);
+			Map mapObj = JSON.parseObject(res, Map.class);
+			String msgNo = mapObj.get("msg_no").toString();
+			Integer code = Integer.valueOf(mapObj.get("code").toString());
+			//将短信记录保存进数据库
+			SmsRecord smsRecord = new SmsRecord();
+			smsRecord.setContent(content);
+			smsRecord.setCreateDate(new Date());
+			smsRecord.setPhoens(smsUserPhoens);
+			smsRecord.setMsgid(msgNo);
+			smsRecordService.insertSelective(smsRecord);
+			if (code == 0) {// 提交供应商成功,查询发送是否成功
+				SmsSendState sss = new SmsSendState();
+				sss.setMsgid(msgNo);
+				sss.setCreateDate(new Date());
+				sss.setContents(res);
+				smsSendStateService.insertSelective(sss);
+				json.element("status", "0");
+				json.element("message", "发送成功");
+			} else {
+				json.element("status", "3");
+				json.element("message", "校验失败");
+			}
+		} catch (Exception e) {
+			json.element("status", SmsConst.ERROR);
+			json.element("message", "发送异常");
+			e.printStackTrace();
+		}
+		Util.json(response, json.toString());
+	}
+
 	/**
 	 * 添加用户
 	 */

+ 26 - 0
src/main/java/com/ssm/util/sms/SmsUtil.java

@@ -115,6 +115,32 @@ public class SmsUtil {
 		return res;
 	}
 
+	/**
+	 * post请求发送短信(新)
+	 *
+	 * @param phones  手机号码
+	 * @param content 发送的内容
+	 * @param signId 模板Id
+	 * @return res    json返回值
+	 */
+	public static String sendNewSmsPostBySignId(String phones, String content, String signId) throws IOException {
+		HttpClient httpClient = HttpClients.createDefault();
+		HttpPost httpPost = new HttpPost("https://api.4321.sh/sms/send");
+		httpPost.addHeader("Content-Type", "application/json");
+		Map<String, Object> map = new HashMap<>();
+		map.put("apikey", "N11727b332");// api接口账号
+		map.put("secret", "1172799e3242ebe1");// api接口秘钥
+		map.put("sign_id", signId);// 签名id
+		map.put("mobile", phones);// 手机号
+		map.put("content", content);// 短信内容
+		String json = JSON.toJSONString(map);
+		httpPost.setEntity(new StringEntity(json, "UTF-8"));
+		HttpResponse response = httpClient.execute(httpPost);
+		HttpEntity entity = response.getEntity();
+		String res = EntityUtils.toString(entity);
+		return res;
+	}
+
 	/**
 	   * get请求发送的状态(根据发送信息时返回成功的编号查询)
 	   * @param msgid 编号