|
|
@@ -1,10 +1,17 @@
|
|
|
package com.izouma.awesomeadmin.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+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 com.github.kevinsawicki.http.HttpRequest;
|
|
|
import com.izouma.awesomeadmin.dao.SmsRecordMapper;
|
|
|
import com.izouma.awesomeadmin.model.SmsRecord;
|
|
|
import com.izouma.awesomeadmin.service.SmsService;
|
|
|
-import net.sf.json.JSONObject;
|
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -16,26 +23,37 @@ import java.util.Map;
|
|
|
public class SmsServiceImpl implements SmsService {
|
|
|
@Autowired
|
|
|
private SmsRecordMapper smsRecordMapper;
|
|
|
- private String url = "http://smssh1.253.com/msg/send/json";
|
|
|
- private String account = "N2542530";
|
|
|
- private String password = "5uZjhd3OG28175";
|
|
|
|
|
|
@Override
|
|
|
public String send(String phone) throws SmsException {
|
|
|
String code = RandomStringUtils.randomNumeric(4);
|
|
|
String sessionId = RandomStringUtils.randomAlphabetic(16);
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("account", account);
|
|
|
- params.put("password", password);
|
|
|
- params.put("phone", phone);
|
|
|
- params.put("msg", "你的短信验证码是" + code + ",请在15分钟内输入使用。超时请重新申请。欢迎使用绿洲电竞链。");
|
|
|
- System.out.println(JSONObject.fromObject(params).toString());
|
|
|
- String body = HttpRequest.post(url).header("Content-type", "application/json")
|
|
|
- .send(JSONObject.fromObject(params).toString()).body();
|
|
|
- JSONObject jsonObject = JSONObject.fromObject(body);
|
|
|
- if (!jsonObject.getString("code").equals("0")) {
|
|
|
- throw new SmsException("发送失败,请稍后再试");
|
|
|
+
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tPoBCiEMSDaS1Q4HKr9", "F8ZNiqdH35T7gikBkn6Fq8tgbvdY88");
|
|
|
+ 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("PhoneNumbers", phone);
|
|
|
+ request.putQueryParameter("SignName", "绿洲数字藏品中心");
|
|
|
+ request.putQueryParameter("TemplateCode", "SMS_228870098");
|
|
|
+ request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
|
|
|
+ try {
|
|
|
+ CommonResponse response = client.getCommonResponse(request);
|
|
|
+ if (response.getHttpStatus() != 200) {
|
|
|
+ throw new SmsException("发送失败,请稍后再试");
|
|
|
+ }
|
|
|
+ JSONObject jsonObject = JSON.parseObject(response.getData());
|
|
|
+ if (!"ok".equalsIgnoreCase(jsonObject.getString("Code"))) {
|
|
|
+ throw new SmsException("发送失败,请稍后再试");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new SmsException("发送失败");
|
|
|
}
|
|
|
+
|
|
|
SmsRecord smsRecord = new SmsRecord();
|
|
|
smsRecord.setPhone(phone);
|
|
|
smsRecord.setCode(code);
|