总览:(总体功能与注册发送短信功能相似)
在web模块service.impl包下,创建SmsCodeLoginImpl,实现的还是SmsService接口
package com.bjpowernode.front.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.bjpowernode.common.constants.RedisKey;
import com.bjpowernode.front.config.JdwxSmsConfig;
import com.bjpowernode.front.service.SmsService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* 登录发送短信验证码
*/
@Service(value = "smsCodeLoginImpl")
public class SmsCodeLoginImpl implements SmsService {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource
private JdwxSmsConfig smsConfig;
@Override
public boolean sendSms(String phone) {
boolean send = false;
// 设置短信内容
String random = RandomStringUtils.randomNumeric(4);
System.out.println("登录验证码的随机数 random="+random);
//更新content中的 %s 【大富科技】登录验证码是:%s,3分钟内有效,请勿泄露给他人
String content = String.format(smsConfig.getLoginText(), random);
//使用HttpClient发送 get 请求给第三方。
CloseableHttpClient client = HttpClients.createDefault();
//https://way.jd.com/chuangxin/dxjk?mobile=13568813957&content=
//【创信】你的验证码是:5873,3分钟内有效!&appkey=您申请的APPKEY
String url = smsConfig.getUrl()+"?mobile="+phone
+"&content=" + content
+"&appkey="+smsConfig.getAppkey();
HttpGet get = new HttpGet(url);
try{
//CloseableHttpResponse response = client.execute(get);
// if( response.getStatusLine().getStatusCode() == HttpStatus.SC_OK ){
//得到返回的数据,json
//String text = EntityUtils.toString(response.getEntity());
String text="{\n" +
" \"code\": \"10000\",\n" +
" \"charge\": false,\n" +
" \"remain\": 1305,\n" +
" \"msg\": \"查询成功\",\n" +
" \"result\": {\n" +
" \"ReturnStatus\": \"Success\",\n" +
" \"Message\": \"ok\",\n" +
" \"RemainPoint\": 420842,\n" +
" \"TaskID\": 18424321,\n" +
" \"SuccessCounts\": 1\n" +
" }\n" +
"}";
//解析json
if(StringUtils.isNotBlank(text)){
// fastjson
JSONObject jsonObject = JSONObject.parseObject(text);
if("10000".equals(jsonObject.getString("code"))){ //第三方接口调用成功
//读取result中的key:ReturnStatus
if("Success".equalsIgnoreCase(
jsonObject.getJSONObject("result").getString("ReturnStatus"))){
//短信发送成功
send = true;
//把短信验证码,存到redis
String key = RedisKey.KEY_SMS_CODE_LOGIN + phone;
stringRedisTemplate.boundValueOps(key).set(random,3 , TimeUnit.MINUTES);
}
}
}
// }
}catch (Exception e){
e.printStackTrace();
}
return send;
}
@Override
public boolean checkSmsCode(String phone, String code) {
String key = RedisKey.KEY_SMS_CODE_LOGIN + phone;
if( stringRedisTemplate.hasKey(key)){
String querySmsCode = stringRedisTemplate.boundValueOps(key).get();
if( code.equals(querySmsCode)){
return true;
}
}
return false;
}
}
其中:
验证码存入redis时更新添加相应的RedisKey(在common模块constants包下的RedisKey类):
/*登录时,短信验证码 SMS:LOGIN:手机号*/
public static final String KEY_SMS_CODE_LOGIN = "SMS:LOGIN:";
在web模块下补充对应的SmsController类:
package com.bjpowernode.front.controller;
import com.bjpowernode.common.constants.RedisKey;
import com.bjpowernode.common.enums.RCode;
import com.bjpowernode.common.util.CommonUtil;
import com.bjpowernode.front.service.SmsService;
import com.bjpowernode.front.view.RespResult;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@Api(tags = "短信业务")
@RestController
@RequestMapping("/v1/sms")
public class SmsController extends BaseController {
@Resource(name = "smsCodeRegisterImpl")
private SmsService smsService;
@Resource(name = "smsCodeLoginImpl")
private SmsService loginSmsService;
/**发送注册验证码短信*/
@GetMapping("/code/register")
public RespResult sendCodeRegister(@RequestParam String phone){
RespResult result = RespResult.fail();
if(CommonUtil.checkPhone(phone)){
//判断redis中是否有这个手机号的验证码
String key = RedisKey.KEY_SMS_CODE_REG + phone;
if(stringRedisTemplate.hasKey(key)){
result = RespResult.ok();
result.setRCode(RCode.SMS_CODE_CAN_USE);
} else {
boolean isSuccess = smsService.sendSms(phone);
if( isSuccess ){
result = RespResult.ok();
}
}
} else {
result.setRCode(RCode.PHONE_FORMAT_ERR);
}
return result;
}
/**发送登录验证码短信*/
@GetMapping("/code/login")
public RespResult sendCodeLogin(@RequestParam String phone){
RespResult result = RespResult.fail();
if(CommonUtil.checkPhone(phone)){
//判断redis中是否有这个手机号的验证码
String key = RedisKey.KEY_SMS_CODE_LOGIN + phone;
if(stringRedisTemplate.hasKey(key)){
result = RespResult.ok();
result.setRCode(RCode.SMS_CODE_CAN_USE);
} else {
boolean isSuccess = loginSmsService.sendSms(phone);
if( isSuccess ){
result = RespResult.ok();
}
}
} else {
result.setRCode(RCode.PHONE_FORMAT_ERR);
}
return result;
}
}