| @@ -23,8 +23,8 @@ import java.util.*; | |||||
| @Service | @Service | ||||
| public class WxMsgServiceImpl implements WxMsgService { | public class WxMsgServiceImpl implements WxMsgService { | ||||
| @Autowired | |||||
| @Autowired | |||||
| WxMsgMapper wxMsgMapper; | WxMsgMapper wxMsgMapper; | ||||
| @Autowired | @Autowired | ||||
| @@ -41,89 +41,132 @@ public class WxMsgServiceImpl implements WxMsgService { | |||||
| } | } | ||||
| @Override | @Override | ||||
| public ResultData saveOrUpdate(WxMsg record) { | |||||
| public ResultData saveOrUpdate(WxMsg wxMsg) { | |||||
| if (wxMsg.getId() == null) { | |||||
| //草稿 | |||||
| if (wxMsg.getSendstatus() == 2) { | |||||
| IdWorker idWorker = new IdWorker(0, 0); | |||||
| wxMsg.setId(idWorker.nextId()); | |||||
| wxMsgMapper.insertSelective(wxMsg); | |||||
| return new ResultData(Result.SUCCESS, "已保存到草稿箱"); | |||||
| } | |||||
| //是否立即发送 | |||||
| if (wxMsg.getIsright() == 0) { | |||||
| wxMsg.setStatus(1); | |||||
| sendmsg(wxMsg); | |||||
| } else { | |||||
| wxMsg.setStatus(0); | |||||
| IdWorker idWorker = new IdWorker(0, 0); | |||||
| wxMsg.setId(idWorker.nextId()); | |||||
| wxMsgMapper.insertSelective(wxMsg); | |||||
| return new ResultData(Result.SUCCESS, "短信会在预设时间发送"); | |||||
| } | |||||
| } else { | |||||
| //草稿 | |||||
| if (wxMsg.getSendstatus() == 2) { | |||||
| wxMsgMapper.updateByPrimaryKeySelective(wxMsg); | |||||
| return new ResultData(Result.SUCCESS, "已保存到草稿箱"); | |||||
| } | |||||
| //是否立即发送 | |||||
| if (wxMsg.getIsright() == 0) { | |||||
| wxMsg.setStatus(1); | |||||
| sendmsg(wxMsg); | |||||
| } else { | |||||
| //定时 | |||||
| wxMsg.setStatus(0); | |||||
| wxMsgMapper.updateByPrimaryKeySelective(wxMsg); | |||||
| return new ResultData(Result.SUCCESS, "短信会在预设时间发送"); | |||||
| } | |||||
| } | |||||
| return new ResultData(); | |||||
| } | |||||
| @Override | |||||
| public void deleteById(Long id) { | |||||
| wxMsgMapper.deleteByPrimaryKey(id); | |||||
| } | |||||
| public ResultData sendmsg(WxMsg wxMsg) { | |||||
| //从短信配置中查询密钥 bid 等信息 | //从短信配置中查询密钥 bid 等信息 | ||||
| WxMsgConfig wxMsgConfig = new WxMsgConfig(); | WxMsgConfig wxMsgConfig = new WxMsgConfig(); | ||||
| wxMsgConfig.setTenantId("1"); | wxMsgConfig.setTenantId("1"); | ||||
| List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig); | List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig); | ||||
| if(wxMsgConfigs.size()==0)return new ResultData(Result.SUCCESS, "您还未接入短信运营商,请联系平台管理员"); | |||||
| if (wxMsgConfigs.size() == 0) return new ResultData(Result.SUCCESS, "您还未接入短信运营商,请联系平台管理员"); | |||||
| wxMsgConfig = wxMsgConfigs.get(0); | wxMsgConfig = wxMsgConfigs.get(0); | ||||
| String secret = wxMsgConfig.getSecret(); | String secret = wxMsgConfig.getSecret(); | ||||
| String bid = wxMsgConfig.getBid(); | String bid = wxMsgConfig.getBid(); | ||||
| String publickey = wxMsgConfig.getPublickey(); | String publickey = wxMsgConfig.getPublickey(); | ||||
| String phone="13810135185"; | |||||
| String signature="123"; | |||||
| String msg="测试"; | |||||
| String notifyUrl="http://8d8fe61b.ngrok.io/wxMsgCallback/receivemsg/"+bid; | |||||
| String phone = wxMsg.getPhones(); | |||||
| String signature = wxMsg.getSignature(); | |||||
| String msg = wxMsg.getMsg(); | |||||
| String notifyUrl = wxMsgConfig.getNotifyurl(); | |||||
| TreeMap<String, String> message = new TreeMap<>(); | TreeMap<String, String> message = new TreeMap<>(); | ||||
| message.put("bid",bid); | |||||
| message.put("phone",phone); | |||||
| message.put("signature",signature); | |||||
| message.put("msg",msg); | |||||
| message.put("notify_url",notifyUrl); | |||||
| message.put("bid", bid); | |||||
| message.put("phone", phone); | |||||
| message.put("signature", signature); | |||||
| message.put("msg", msg); | |||||
| message.put("notify_url", notifyUrl); | |||||
| StringBuilder sb=new StringBuilder(); | |||||
| StringBuilder sb = new StringBuilder(); | |||||
| Set<Map.Entry<String, String>> entries = message.entrySet(); | Set<Map.Entry<String, String>> entries = message.entrySet(); | ||||
| for(Map.Entry<String, String> entry:entries){ | |||||
| for (Map.Entry<String, String> entry : entries) { | |||||
| sb.append(entry.getKey()).append("=").append(entry.getValue()); | sb.append(entry.getKey()).append("=").append(entry.getValue()); | ||||
| } | } | ||||
| sb.append("&secret=").append(secret); | sb.append("&secret=").append(secret); | ||||
| String sign = HMACSHA256.sha256_HMAC(sb.toString(), secret); | String sign = HMACSHA256.sha256_HMAC(sb.toString(), secret); | ||||
| message.put("sign",sign.toUpperCase()); | |||||
| message.put("sign", sign.toUpperCase()); | |||||
| String str32="198b02e8fd704e96198b02e8fd704e96"; | |||||
| String iv="198b02e8fd704e96"; | |||||
| String str32 = "198b02e8fd704e96198b02e8fd704e96"; | |||||
| String iv = "198b02e8fd704e96"; | |||||
| Map<String,String> params=new HashMap<>(); | |||||
| params.put("iv",iv); | |||||
| params.put("bid",bid); | |||||
| Map<String, String> params = new HashMap<>(); | |||||
| params.put("iv", iv); | |||||
| params.put("bid", bid); | |||||
| try { | try { | ||||
| String data = AesUtil.AESEncode(str32, JSONObject.toJSONString(message), iv); | String data = AesUtil.AESEncode(str32, JSONObject.toJSONString(message), iv); | ||||
| String sc = RsaUtil.RSAEncode(str32.getBytes(),publickey); | |||||
| params.put("data",data); | |||||
| params.put("sc",sc); | |||||
| String sc = RsaUtil.RSAEncode(str32.getBytes(), publickey); | |||||
| params.put("data", data); | |||||
| params.put("sc", sc); | |||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| e.printStackTrace(); | e.printStackTrace(); | ||||
| } | } | ||||
| String requestUrl="https://webapp.wiwide.com/apisms/send"; | |||||
| String requestUrl = "https://webapp.wiwide.com/apisms/send"; | |||||
| String result = HttpUtil.doPost(requestUrl, params); | String result = HttpUtil.doPost(requestUrl, params); | ||||
| JSONObject jsonObjectResult = JSONObject.parseObject(result); | JSONObject jsonObjectResult = JSONObject.parseObject(result); | ||||
| String ret=jsonObjectResult.get("ret").toString(); | |||||
| if(ret.equals("1")){ | |||||
| System.out.println("发送成功"); | |||||
| }else{ | |||||
| System.out.println("发送失败"); | |||||
| } | |||||
| String ret = jsonObjectResult.get("ret").toString(); | |||||
| /* if (record.getId() == null) { | |||||
| //record.setId(UUID.randomUUID().toString().replaceAll("-", "")); | |||||
| IdWorker idWorker = new IdWorker(0, 0); | |||||
| record.setId(idWorker.nextId()); | |||||
| wxMsgMapper.insertSelective(record); | |||||
| if (ret.equals("1")) { | |||||
| wxMsg.setSendstatus(1); | |||||
| } else { | } else { | ||||
| wxMsgMapper.updateByPrimaryKeySelective(record); | |||||
| }*/ | |||||
| return new ResultData(Result.SUCCESS, "短信发送中,您可在短信明细中查看发送状态"); | |||||
| } | |||||
| wxMsg.setSendstatus(0); | |||||
| } | |||||
| @Override | |||||
| public void deleteById(Long id) { | |||||
| wxMsgMapper.deleteByPrimaryKey(id); | |||||
| } | |||||
| if(wxMsg.getId()==null){ | |||||
| IdWorker idWorker = new IdWorker(0, 0); | |||||
| wxMsg.setId(idWorker.nextId()); | |||||
| wxMsgMapper.insertSelective(wxMsg); | |||||
| }else{ | |||||
| wxMsgMapper.updateByPrimaryKeySelective(wxMsg); | |||||
| } | |||||
| @Override | |||||
| public ResultData sendmsgbyexcel(MultipartFile file, WxMsg wxMsg) { | |||||
| return null; | |||||
| } | |||||
| if (ret.equals("1")) { | |||||
| return new ResultData(Result.SUCCESS, "短信发送中,您可在短信明细中查看发送状态"); | |||||
| } else { | |||||
| return new ResultData(Result.SUCCESS, "短信发送发败"); | |||||
| } | |||||
| @Override | |||||
| public ResultData sendmsgbylabel(WxMsg wxMsg) { | |||||
| return null; | |||||
| } | } | ||||