当前位置:网站首页>云片网案例

云片网案例

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);
    }
原网站

版权声明
本文为[昊天h1]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45741895/article/details/126067114