影馳迴應“顯示卡金手指異常投訴”:系全新原裝正品顯示卡
阿新 • • 發佈:2022-05-06
1.先申請騰訊雲簡訊服務:https://console.cloud.tencent.com/smsv2
2.簡訊服務程式碼
package com.atguigu.msmservice.controller; import com.atguigu.commonutils.R; import com.atguigu.msmservice.service.MsmService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate;import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import java.util.concurrent.TimeUnit; @RestController @RequestMapping("/edumsm/msm") @CrossOrigin public class MsmController { @Autowired private MsmService msmService; @Autowired privateRedisTemplate<String,String> redisTemplate; //傳送簡訊的方法 @GetMapping("send/{phone}") @CrossOrigin public R sendMsm(@PathVariable String phone) { //1 從redis獲取驗證碼,如果獲取到直接返回 String code = redisTemplate.opsForValue().get(phone); if(!StringUtils.isEmpty(code)) {return R.ok(); } //2 如果redis獲取 不到,進行阿里雲傳送 //呼叫service傳送簡訊的方法 boolean isSend = msmService.send(phone); if(isSend) { //傳送成功,把傳送成功驗證碼放到redis裡面 //設定有效時間 redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES); return R.ok(); } else { return R.error().message("簡訊傳送失敗"); } } }
簡訊服務實現類
package com.atguigu.msmservice.service.impl; import com.atguigu.msmservice.service.MsmService; import com.atguigu.msmservice.utils.RandomUtil; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.sms.v20210111.SmsClient; import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @Service public class MsmServiceImpl implements MsmService { /** * 傳送簡訊的方法 */ @Override public boolean send(String phone) { if(StringUtils.isEmpty(phone)) { return false; } try { //這裡是例項化一個Credential,也就是認證物件,引數是金鑰對;你要使用肯定要進行認證 Credential credential = new Credential("你的secretId", "你的secretKey"); //HttpProfile這是http的配置檔案操作,比如設定請求型別(post,get)或者設定超時時間了、還有指定域名了 //最簡單的就是例項化該物件即可,它的構造方法已經幫我們設定了一些預設的值 HttpProfile httpProfile = new HttpProfile(); //這個setEndpoint可以省略的 httpProfile.setEndpoint("sms.tencentcloudapi.com"); //例項化一個客戶端配置物件,這個配置可以進行簽名(使用私鑰進行加密的過程),對方可以利用公鑰進行解密 ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); //例項化要請求產品(以sms為例)的client物件 SmsClient smsClient = new SmsClient(credential, "ap-beijing", clientProfile); //例項化request封裝請求資訊 SendSmsRequest request = new SendSmsRequest(); String[] phoneNumber = {phone}; //手機號 request.setPhoneNumberSet(phoneNumber); //SDK AppID request.setSmsSdkAppId("你的SDK AppID"); //簽名內容 request.setSignName("LilLazy公眾號"); //模板id request.setTemplateId("你的模板id"); //生成隨機驗證碼 String verificationCode = RandomUtil.getSixBitRandom();
//模板內容的引數有幾個就要設定幾個,我這裡有兩個 String[] templateParamSet = {verificationCode,"5"}; request.setTemplateParamSet(templateParamSet); //傳送簡訊 SendSmsResponse response = smsClient.SendSms(request); return true; } catch (Exception e) { return false; } } }
簡訊服務常見問題和解決辦法:https://cloud.tencent.com/document/product/382/9558