当前位置:网站首页>云片网案例
云片网案例
2022-08-02 14:01:00 【昊天h1】
一, 导入依赖
<!--云片网-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
二,导入工具类
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** * @auther Teao * @description 手机短信API **/
public class JavaSmsApi {
//智能匹配模版发送接口的http地址
private static String URI_SEND_SMS = "地址";
//编码格式。发送编码格式统一用UTF-8
private static String ENCODING = "UTF-8";
// private static String apikey = "apikey";
public static String sendSms(String mobile,String code) throws IOException {
mobile=URLEncoder.encode(mobile,ENCODING);
String text = "短信模板信息";
Map<String, String> params = new HashMap<String, String>();
params.put("apikey", apikey);
params.put("text", text);
params.put("mobile", mobile);
// 这里返回的是Json字符串
return post(URI_SEND_SMS, params);
}
/** * 基于HttpClient 4.3的通用POST方法 * * @param url 提交的URL * @param paramsMap 提交<参数,值>Map * @return 提交响应 */
public static String post(String url, Map<String, String> paramsMap) {
CloseableHttpClient client = HttpClients.createDefault();
String responseText = "";
CloseableHttpResponse response = null;
try {
HttpPost method = new HttpPost(url);
if (paramsMap != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
paramList.add(pair);
}
method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity != null) {
responseText = EntityUtils.toString(entity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(responseText);
return responseText;
}
}
三,使用
1.发送验证码
/** * @description:发送验证码 * @author: Teao * @date: 2022/7/16 14:31 * @param: [phoneNumber] * @return: com.teao.springboot003.entity.R **/
@GetMapping("/sendCode")
@ApiOperation("发送验证码")
public R sendCode(String phoneNumber) throws IOException {
// 创建一个4位数的随机整数作为验证码
String code = RandomUtil.randomNumbers(4);
// 调用工具类发送验证码,用sms接收JSON字符串
String sms = JavaSmsApi.sendSms(phoneNumber, code);
// JSON字符串转成map方便取值
JSONObject map = JSONUtil.parseObj(sms);
// 取返回的code
String responseCode = map.get("code").toString();
// 判断返回的状态码是不是0 是发送成功,不是发送失败
if (responseCode.equals("0")){
// 发送成功后将 key为手机号,value为验证码,推送到redis存储,有效期1分钟
redisTemplate.opsForValue().set(phoneNumber,code,120,TimeUnit.SECONDS);
return new R(1000,"验证码发送成功", code);
}
return new R(-1000,"验证码发送失败", code);
}
2.注册或者登录
/** * @description:用户手机注册 * @author: Teao * @date: 2022/7/16 14:32 * @param: [phoneNumber, code] * @return: com.teao.springboot003.entity.R **/
@GetMapping("/reg")
@ApiOperation("用户手机注册")
public R reg(String phoneNumber,String code) throws IOException{
// redisTemplate.hasKey(phoneNumber) 判断验证码是否失效 ()
if (!redisTemplate.hasKey(phoneNumber)){
return new R(-1000,"验证码已过期", null);
}
// 验证码没过期时 获取到第三方服务存储在redis上的验证码
String c = redisTemplate.opsForValue().get(phoneNumber).toString();
// 将第三方的与自己输入的进行比较
if (!c.equals(code)){
return new R(-1000,"验证码错误", null);
}
return new R(1000,"注册成功", null);
}
边栏推荐
猜你喜欢

The future of financial services will never stop, and the bull market will continue 2021-05-28

About the development forecast of the market outlook?2021-05-23

网络安全第二次作业

shell脚本“画画”

政策利空对行情没有长期影响,牛市仍将继续 2021-05-19

二极管及其应用

监管再次重拳出击,后市如何?2021-05-22

RHCE第一天作业

【Tensorflow】AttributeError: '_TfDeviceCaptureOp' object has no attribute '_set_device_from_string'

ZABBIX配置邮件报警和微信报警
随机推荐
RKMPP库快速上手--(一)RKMPP功能及使用详解
第二届中国Rust开发者大会(RustChinaConf 2021~2022)线上大会正式开启报名
mysql的case when如何用
电脑死机,Word忘了保存怎么办?怎么恢复?(编辑器是WPS)
线程安全问题及关键字synchronized,volatile
els 长条方块变形条件、边界碰撞判定
Diodes and their applications
无序数组排序并得到最大间隔
机器学习——交叉验证法
How to solve 1045 cannot log in to mysql server
Audio processing: floating point data stream to PCM file
微信小程序-最近动态滚动实现
Large and comprehensive pom file example
[C language] Explicit array solution (1)
ZABBIX配置邮件报警和微信报警
一维卷积神经网络_卷积神经网络的基础知识「建议收藏」
【Tensorflow】AttributeError: module ‘keras.backend‘ has no attribute ‘tf‘
拯救流浪猫 | 「喵先锋」系列数字版权盲盒明日开抢
web测试和app测试的区别?
RKMPP 在FFmpeg上实现硬编解码