|
@@ -0,0 +1,76 @@
|
|
|
+package cn.com.ty.lift.common.aliservice.aliyunsms;
|
|
|
+
|
|
|
+import cn.com.ty.lift.common.aliservice.constants.AliConstants;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.aliyuncs.CommonRequest;
|
|
|
+import com.aliyuncs.CommonResponse;
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
+import com.aliyuncs.exceptions.ServerException;
|
|
|
+import com.aliyuncs.http.MethodType;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
+
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author huangyuan
|
|
|
+ * @date 2019-12-05
|
|
|
+ * @description 阿里云短信接口
|
|
|
+ */
|
|
|
+public class AliyunSmsUtil {
|
|
|
+
|
|
|
+
|
|
|
+ public static String sendSmsCode(String phoneNumbers, String templateCode){
|
|
|
+ String smsCode = GenerateSMSCode();
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile(AliConstants.REGION_ID, AliConstants.SmsConstants.ACCESS_KEY_ID,
|
|
|
+ AliConstants.SmsConstants.ACCESS_SECRET);
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+ CommonRequest request = new CommonRequest();
|
|
|
+ request.setMethod(MethodType.POST);
|
|
|
+ request.setDomain(AliConstants.SmsConstants.DOMAIN);
|
|
|
+ request.setVersion(AliConstants.SmsConstants.VERSION);
|
|
|
+ request.setAction(AliConstants.SmsConstants.ACTION_SEND_SMS);
|
|
|
+ //设置区域
|
|
|
+ request.putQueryParameter("RegionId", AliConstants.REGION_ID);
|
|
|
+ //设置手机号
|
|
|
+ request.putQueryParameter("PhoneNumbers", phoneNumbers);
|
|
|
+ //设置模板签名
|
|
|
+ request.putQueryParameter("SignName", AliConstants.SmsConstants.SIGN_NAME);
|
|
|
+ //设置模板编码
|
|
|
+ request.putQueryParameter("TemplateCode", templateCode);
|
|
|
+ //设置验证码
|
|
|
+ String templateParam = "{\"code\":\"" + smsCode + "\"}";
|
|
|
+ request.putQueryParameter("TemplateParam", templateParam);
|
|
|
+
|
|
|
+ try {
|
|
|
+ CommonResponse response = client.getCommonResponse(request);
|
|
|
+ String returnMessage = response.getData();
|
|
|
+ SmsResponse smsResponse = JSONUtil.toBean(returnMessage, SmsResponse.class);
|
|
|
+ if(!AliConstants.STATUS_CODE_OK.equals(smsResponse.getCode())){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ } catch (ServerException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return smsCode;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成6位随机码
|
|
|
+ *@author huangy
|
|
|
+ *@return String
|
|
|
+ *@throws
|
|
|
+ */
|
|
|
+ public static String GenerateSMSCode() {
|
|
|
+ String smsVerifyCode = "";
|
|
|
+ Random random = new Random();
|
|
|
+ for(int i = 0; i < 6; i++) {
|
|
|
+ smsVerifyCode = smsVerifyCode + random.nextInt(10);//生成0-10(不包括10)之间的随机数
|
|
|
+ }
|
|
|
+ return smsVerifyCode;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|