1. 程式人生 > 其它 >SpringBoot簡訊通知

SpringBoot簡訊通知

技術標籤:SpringBootjava

SpringBoot簡訊通知

1.pom檔案匯入依賴

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.41</version>
        </dependency>
        <dependency>
            <
groupId>
org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <
/dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.1.0</version> </dependency>

2.建立SmsService

package ydj.plugins.pojo;
import org.apache.ibatis.
annotations.Param; public interface SmsService { boolean sendSms(@Param("phoneNumber") String phoneNumber, @Param("accessKeyId")String accessKeyId, @Param("accessSecret")String accessSecret, @Param("signName")String signName, @Param("templateCode")String templateCode); }

3.建立SmsServiceImpl實現類去實現SmsService

package ydj.plugins.pojo;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService{
//    private String accessKeyId ="LTAI4GG8qdFXA8WqS1ya3mJb";
//    private String accessSecret = "z4fqvdtGYssrVh3dMJNVeZqdbGsr6M";
//    private String signName = "商城"; //todo 簽名名稱
//    private String templateCode = "SMS_141460134"; //todo 模板

    @Override
    public boolean sendSms(@Param("phoneNumber") String phoneNumber, @Param("accessKeyId")String accessKeyId,
                           @Param("accessSecret")String accessSecret, @Param("signName")String signName,
                           @Param("templateCode")String templateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-xian", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-xian");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        JSONObject object=new JSONObject();
        String randCode=getRandCode(6);
        log.info("驗證碼為:{}",randCode);
        System.out.println("驗證碼為:"+randCode);
        object.put("code",randCode);
        request.putQueryParameter("TemplateParam", object.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info(response.getData());
            return true;
        } catch (Exception e) {
            log.error("{}",e);
        }
        return false;
    }

    //todo 生成隨機驗證碼
    public static String getRandCode(int digits) {
        StringBuilder sBuilder = new StringBuilder();
        Random rd = new Random((new Date()).getTime());
        for(int i = 0; i < digits; ++i) {
            sBuilder.append((rd.nextInt(9)));
        }
        return sBuilder.toString();
    }
}

4.建立SmsController控制器類

package ydj.plugins.pojo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ydj.commons.pojo.CommonResultPojo;
import javax.annotation.Resource;

@RestController
@RequestMapping("/smsController")
public class SmsController {

    @Resource
    private SmsService smsService;

    @GetMapping("/sendSms")
    public CommonResultPojo sendSms(@Param("phoneNumber") String phoneNumber,@Param("accessKeyId")String accessKeyId,
                                    @Param("accessSecret")String accessSecret,@Param("signName")String signName,
                                    @Param("templateCode")String templateCode){
        boolean i=smsService.sendSms(phoneNumber,accessKeyId,accessSecret,signName,templateCode);
        if(i == true){
            return new CommonResultPojo(200,"傳送成功",1);
        }else {
            return new CommonResultPojo(444,"傳送失敗",0);
        }
    }
}

5.postman測試:

{
    "code": 200,
    "message": "傳送成功",
    "data": 1
}

在這裡插入圖片描述