# Conflicts: # user-service/src/main/java/com/hp/user/service/utils/HttpResult.javadev
@@ -163,7 +163,11 @@ | |||||
<artifactId>jedis</artifactId> | <artifactId>jedis</artifactId> | ||||
<version>2.9.0</version> | <version>2.9.0</version> | ||||
</dependency> | </dependency> | ||||
<dependency> | |||||
<groupId>com.aliyun</groupId> | |||||
<artifactId>aliyun-java-sdk-core</artifactId> | |||||
<version>4.5.3</version> | |||||
</dependency> | |||||
</dependencies> | </dependencies> | ||||
@@ -0,0 +1,14 @@ | |||||
package com.hp.user.service.constants; | |||||
public class UserConstants { | |||||
/** | |||||
* 验证码redis前缀 | |||||
*/ | |||||
public static String VERIFICATION_CODE = "verificationCode@"; | |||||
/** | |||||
* rediskey失效时间 | |||||
*/ | |||||
public static int EXPIRE_SECONDS = 900; | |||||
} |
@@ -1,10 +1,12 @@ | |||||
package com.hp.user.service.controller; | package com.hp.user.service.controller; | ||||
import com.hp.user.client.entity.AreaVO; | import com.hp.user.client.entity.AreaVO; | ||||
import com.hp.user.utils.HttpResult; | |||||
import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
import com.hp.user.client.service.UserAreaService; | import com.hp.user.client.service.UserAreaService; | ||||
import com.hp.user.service.utils.HttpResult; | |||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
import org.springframework.web.bind.annotation.GetMapping; | import org.springframework.web.bind.annotation.GetMapping; | ||||
import org.springframework.web.bind.annotation.RequestMapping; | import org.springframework.web.bind.annotation.RequestMapping; | ||||
@@ -3,8 +3,9 @@ package com.hp.user.service.controller; | |||||
import com.hp.user.client.entity.*; | import com.hp.user.client.entity.*; | ||||
import com.hp.user.client.service.AuditService; | import com.hp.user.client.service.AuditService; | ||||
import com.hp.user.client.service.CustomerCompanyService; | import com.hp.user.client.service.CustomerCompanyService; | ||||
import com.hp.user.util.IdWorker; | |||||
import com.hp.user.utils.HttpResult; | |||||
import com.hp.user.service.utils.HttpResult; | |||||
import com.hp.user.service.utils.IdWorker; | |||||
import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
@@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.RequestMapping; | |||||
import org.springframework.web.bind.annotation.RestController; | import org.springframework.web.bind.annotation.RestController; | ||||
import com.hp.user.client.service.MessageService; | import com.hp.user.client.service.MessageService; | ||||
import com.hp.user.service.utils.SendSmsUtil; | |||||
import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
@@ -21,6 +22,7 @@ public class MessageController { | |||||
@Autowired | @Autowired | ||||
private MessageService messageService; | private MessageService messageService; | ||||
/** | /** | ||||
* 发送验证码 | * 发送验证码 | ||||
* @param phoneNumber | * @param phoneNumber | ||||
@@ -3,8 +3,9 @@ package com.hp.user.service.controller; | |||||
import com.hp.user.client.entity.StationPage; | import com.hp.user.client.entity.StationPage; | ||||
import com.hp.user.client.entity.StationVO; | import com.hp.user.client.entity.StationVO; | ||||
import com.hp.user.client.service.StationService; | import com.hp.user.client.service.StationService; | ||||
import com.hp.user.util.IdWorker; | |||||
import com.hp.user.utils.HttpResult; | |||||
import com.hp.user.service.utils.HttpResult; | |||||
import com.hp.user.service.utils.IdWorker; | |||||
import io.swagger.annotations.Api; | import io.swagger.annotations.Api; | ||||
import io.swagger.annotations.ApiOperation; | import io.swagger.annotations.ApiOperation; | ||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
@@ -1,8 +1,17 @@ | |||||
package com.hp.user.service.impl; | package com.hp.user.service.impl; | ||||
import java.util.Map; | |||||
import java.util.Random; | |||||
import org.springframework.beans.factory.annotation.Autowired; | |||||
import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
import com.hp.user.client.service.MessageService; | import com.hp.user.client.service.MessageService; | ||||
import com.hp.user.service.constants.UserConstants; | |||||
import com.hp.user.service.redis.RedisOperation; | |||||
import com.hp.user.service.utils.SendSmsUtil; | |||||
import redis.clients.jedis.Jedis; | |||||
/** | /** | ||||
* 信息服务实现类 | * 信息服务实现类 | ||||
@@ -12,11 +21,37 @@ import com.hp.user.client.service.MessageService; | |||||
*/ | */ | ||||
@Service | @Service | ||||
public class MessageServiceImpl implements MessageService { | public class MessageServiceImpl implements MessageService { | ||||
@Autowired | |||||
private SendSmsUtil sendSmsUtil; | |||||
@Autowired | |||||
private RedisOperation redisOperation; | |||||
@Override | @Override | ||||
public void sendVerificationCode(String phone) { | public void sendVerificationCode(String phone) { | ||||
// TODO Auto-generated method stub | |||||
try { | |||||
Random rd = new Random(); | |||||
StringBuilder code = new StringBuilder(); | |||||
for(int i=0;i<6;i++) { | |||||
code.append(rd.nextInt(10)); | |||||
} | |||||
String codeStr = code.toString(); | |||||
Map map = sendSmsUtil.sendVerificationCode(phone, codeStr); | |||||
if("OK".equals(map.get("Code"))) { | |||||
//调用阿里云接口成功 | |||||
//调用成功后需要将code缓存到redis中 | |||||
Jedis jedis = redisOperation.getRedis(); | |||||
jedis.setex(UserConstants.VERIFICATION_CODE+phone, UserConstants.EXPIRE_SECONDS, codeStr); | |||||
jedis.close(); | |||||
}else { | |||||
//调用失败阿里云接口成功 | |||||
} | |||||
}catch(Exception e) { | |||||
} | |||||
} | } | ||||
@Override | @Override | ||||
@@ -10,7 +10,6 @@ import com.hp.user.client.service.StationService; | |||||
import com.hp.user.service.dao.StationMapper; | import com.hp.user.service.dao.StationMapper; | ||||
import com.hp.user.service.entity.CustomerCompany; | import com.hp.user.service.entity.CustomerCompany; | ||||
import com.hp.user.service.entity.Station; | import com.hp.user.service.entity.Station; | ||||
import com.hp.user.util.IdWorker; | |||||
import org.springframework.beans.BeanUtils; | import org.springframework.beans.BeanUtils; | ||||
import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
@@ -1,11 +1,5 @@ | |||||
package com.hp.user.service.redis; | package com.hp.user.service.redis; | ||||
import java.io.IOException; | |||||
import java.util.HashSet; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
import java.util.Set; | |||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; | import org.apache.commons.pool2.impl.GenericObjectPoolConfig; | ||||
import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
import org.springframework.boot.ApplicationArguments; | import org.springframework.boot.ApplicationArguments; | ||||
@@ -0,0 +1,62 @@ | |||||
package com.hp.user.utils; | |||||
import java.io.Serializable; | |||||
public class HttpResult implements Serializable { | |||||
private int code; // 状态码 | |||||
private String message; | |||||
private Object data; | |||||
public HttpResult() { | |||||
} | |||||
public HttpResult(int code, String message, Object data) { | |||||
this.code = code; | |||||
this.message = message; | |||||
this.data = data; | |||||
} | |||||
public static HttpResult success() { | |||||
return new HttpResult(0, "操作成功", ""); | |||||
} | |||||
public static HttpResult success(Object data) { | |||||
return new HttpResult(0, "操作成功", data); | |||||
} | |||||
public static HttpResult success(String message, Object data) { | |||||
return new HttpResult(0, message, data); | |||||
} | |||||
public static HttpResult fail() { | |||||
return new HttpResult(-1, "操作失败", ""); | |||||
} | |||||
public static HttpResult fail(String message) { | |||||
return new HttpResult(-1, message, ""); | |||||
} | |||||
public int getCode() { | |||||
return code; | |||||
} | |||||
public void setCode(int code) { | |||||
this.code = code; | |||||
} | |||||
public String getMessage() { | |||||
return message; | |||||
} | |||||
public void setMessage(String message) { | |||||
this.message = message; | |||||
} | |||||
public Object getData() { | |||||
return data; | |||||
} | |||||
public void setData(Object data) { | |||||
this.data = data; | |||||
} | |||||
} |
@@ -1,4 +1,4 @@ | |||||
package com.hp.user.util; | |||||
package com.hp.user.service.utils; | |||||
/** | /** | ||||
* Id生成器 | * Id生成器 |
@@ -0,0 +1,78 @@ | |||||
package com.hp.user.service.utils; | |||||
import java.util.Map; | |||||
import org.springframework.beans.factory.annotation.Value; | |||||
import org.springframework.boot.ApplicationArguments; | |||||
import org.springframework.boot.ApplicationRunner; | |||||
import org.springframework.stereotype.Component; | |||||
import com.alibaba.fastjson.JSONObject; | |||||
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.profile.DefaultProfile; | |||||
import com.google.gson.Gson; | |||||
@Component | |||||
public class SendSmsUtil implements ApplicationRunner{ | |||||
@Value("${aliyun.regionId}") | |||||
private String regionId; | |||||
@Value("${aliyun.accessKeyId}") | |||||
private String accessKeyId; | |||||
@Value("${aliyun.secret}") | |||||
private String secret; | |||||
@Value("${aliyun.signName}") | |||||
private String signName; | |||||
@Value("${aliyun.templateCode}") | |||||
private String templateCode; | |||||
private DefaultProfile profile = null; | |||||
@Override | |||||
public void run(ApplicationArguments args) throws Exception { | |||||
if(profile == null) { | |||||
profile = DefaultProfile.getProfile(regionId, accessKeyId, secret); | |||||
} | |||||
} | |||||
/** | |||||
* 验证码发送 | |||||
* @param phoneNumbers 手机号码 | |||||
* @param code 6位随机验证码 | |||||
* @return | |||||
* @throws ServerException | |||||
* @throws ClientException | |||||
*/ | |||||
public Map sendVerificationCode(String phoneNumbers,String code) throws ServerException, ClientException { | |||||
IAcsClient client = new DefaultAcsClient(profile); | |||||
CommonRequest request = new CommonRequest(); | |||||
request.setSysDomain("dysmsapi.aliyuncs.com"); | |||||
request.setSysVersion("2017-05-25"); | |||||
request.setSysAction("SendSms"); | |||||
// 接收短信的手机号码 | |||||
request.putQueryParameter("PhoneNumbers", phoneNumbers); | |||||
// 短信签名名称。请在控制台签名管理页面签名名称一列查看(必须是已添加、并通过审核的短信签名)。 | |||||
request.putQueryParameter("SignName", signName); | |||||
// 短信模板ID | |||||
request.putQueryParameter("TemplateCode", templateCode); | |||||
// 短信模板变量对应的实际值,JSON格式。 | |||||
JSONObject codeJson = new JSONObject(); | |||||
codeJson.put("code", code); | |||||
request.putQueryParameter("TemplateParam", codeJson.toJSONString()); | |||||
CommonResponse commonResponse = client.getCommonResponse(request); | |||||
String data = commonResponse.getData(); | |||||
String sData = data.replaceAll("'\'", ""); | |||||
// log_print("sendSms", sData); | |||||
Gson gson = new Gson(); | |||||
Map map = gson.fromJson(sData, Map.class); | |||||
return map; | |||||
} | |||||
} |