整合进行短信发送
編寫發送短信接口
在service-msm的pom中引入依賴
<dependencies><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId></dependency></dependencies>編寫controller,根據手機號發送短信
@RestController @RequestMapping("/api/msm") @CrossOrigin //跨域 public class MsmApiController {@Autowiredprivate MsmService msmService;@Autowiredprivate RedisTemplate<String, String> redisTemplate;@GetMapping(value = "/send/{phone}")public R code(@PathVariable String phone) {String code = redisTemplate.opsForValue().get(phone);if(!StringUtils.isEmpty(code)) return R.ok();code = RandomUtil.getFourBitRandom();Map<String,Object> param = new HashMap<>();param.put("code", code);boolean isSend = msmService.send(phone, "SMS_180051135", param);if(isSend) {redisTemplate.opsForValue().set(phone, code,5,TimeUnit.MINUTES);return R.ok();} else {return R.error().message("發送短信失敗");}} }編寫service
@Service public class MsmServiceImpl implements MsmService {/*** 發送短信*/public boolean send(String PhoneNumbers, String templateCode, Map<String,Object> param) {if(StringUtils.isEmpty(PhoneNumbers)) return false;DefaultProfile profile =DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF");IAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();//request.setProtocol(ProtocolType.HTTPS);request.setMethod(MethodType.POST);request.setDomain("dysmsapi.aliyuncs.com");request.setVersion("2017-05-25");request.setAction("SendSms");request.putQueryParameter("PhoneNumbers", PhoneNumbers);request.putQueryParameter("SignName", "我的在線教育網站");request.putQueryParameter("TemplateCode", templateCode);request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());return response.getHttpResponse().isSuccess();} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return false;} } package com.leon.msmservice.util;import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Random;/*** 獲取隨機數**/ public class RandomUtil {private static final Random random = new Random();private static final DecimalFormat fourdf = new DecimalFormat("0000");private static final DecimalFormat sixdf = new DecimalFormat("000000");public static String getFourBitRandom() {return fourdf.format(random.nextInt(10000));}public static String getSixBitRandom() {return sixdf.format(random.nextInt(1000000));}/*** 給定數組,抽取n個數據* @param list* @param n* @return*/public static ArrayList getRandom(List list, int n) {Random random = new Random();HashMap<Object, Object> hashMap = new HashMap<Object, Object>();// 生成隨機數字并存入HashMapfor (int i = 0; i < list.size(); i++) {int number = random.nextInt(100) + 1;hashMap.put(number, i);}// 從HashMap導入數組Object[] robjs = hashMap.values().toArray();ArrayList r = new ArrayList();// 遍歷數組并打印數據for (int i = 0; i < n; i++) {r.add(list.get((int) robjs[i]));System.out.print(list.get((int) robjs[i]) + "\t");}System.out.print("\n");return r;} }?
總結
- 上一篇: 项目添加JWT工具类
- 下一篇: 用户登录注册接口