|
@@ -0,0 +1,261 @@
|
|
|
+package com.fdkankan.user.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fdkankan.common.constant.ConstantFilePath;
|
|
|
+import com.fdkankan.common.constant.ConstantRegex;
|
|
|
+import com.fdkankan.common.exception.BusinessException;
|
|
|
+import com.fdkankan.common.util.*;
|
|
|
+import com.fdkankan.redis.constant.RedisKey;
|
|
|
+import com.fdkankan.redis.util.RedisUtil;
|
|
|
+import com.fdkankan.sms.SendMailAcceUtils;
|
|
|
+import com.fdkankan.sms.SmsService;
|
|
|
+import com.fdkankan.user.common.RedisKeyUtil;
|
|
|
+import com.fdkankan.user.constant.LoginConstant;
|
|
|
+import com.fdkankan.user.entity.Camera;
|
|
|
+import com.fdkankan.user.entity.CameraDetail;
|
|
|
+import com.fdkankan.user.entity.User;
|
|
|
+import com.fdkankan.user.service.ICameraDetailService;
|
|
|
+import com.fdkankan.user.service.ICameraService;
|
|
|
+import com.fdkankan.user.service.IUserService;
|
|
|
+import com.fdkankan.user.vo.request.LoginParam;
|
|
|
+import com.fdkankan.user.vo.request.RegisterParam;
|
|
|
+import com.fdkankan.user.vo.response.LoginVo;
|
|
|
+import com.fdkankan.user.vo.response.UserVo;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.security.GeneralSecurityException;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class LoginService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IUserService userService;
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+ @Autowired
|
|
|
+ private SmsService smsService;
|
|
|
+ @Autowired
|
|
|
+ private ICameraService cameraService;
|
|
|
+ @Autowired
|
|
|
+ private ICameraDetailService cameraDetailService;
|
|
|
+ @Value("${phone.code.cn}")
|
|
|
+ private String cnCode;
|
|
|
+ @Value("${main.url}")
|
|
|
+ private String mainUrl;
|
|
|
+
|
|
|
+ public LoginVo login(LoginParam param) {
|
|
|
+ if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum())){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ String password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
|
|
|
+ String passwordCode = SecurityUtil.MD5(password);
|
|
|
+ User user = userService.getByUserName(param.getPhoneNum());
|
|
|
+ if(user == null){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
|
|
|
+ }
|
|
|
+ if(!user.getPassword().equals(passwordCode)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
|
|
|
+ }
|
|
|
+ String token = this.redisLogin(user.getUserName(),JSONObject.toJSONString(user));
|
|
|
+
|
|
|
+ UserVo userVo = new UserVo();
|
|
|
+ BeanUtils.copyProperties(user,userVo);
|
|
|
+ LoginVo vo = new LoginVo();
|
|
|
+ vo.setToken(token);
|
|
|
+ vo.setUser(userVo);
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void logout(String token) {
|
|
|
+ String redisKey = String.format(RedisKey.TOKEN_V3,token);
|
|
|
+ redisUtil.del(redisKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkUser(String phoneNum) {
|
|
|
+ if(StringUtils.isNotBlank(phoneNum)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ User user = userService.getByUserName(phoneNum);
|
|
|
+ if(user == null){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3014, LoginConstant.FAILURE_MSG_3014);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void getMsgAuthCode(String areaNum, String phoneNum) {
|
|
|
+ String redisKeyTime = RedisKeyUtil.PREFIX_MSG_NOT_CODE + phoneNum; //重发验证
|
|
|
+ String redisKeyMsg = RedisKeyUtil.PREFIX_MSG_AUTH_CODE + phoneNum; //验证码code
|
|
|
+
|
|
|
+ String value = redisUtil.get(redisKeyTime);
|
|
|
+ if(value !=null){
|
|
|
+ long times = new Date().getTime() - Long.parseLong(value) ;
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3033, String.valueOf(60-(times/1000)));
|
|
|
+ }
|
|
|
+ String code = String.valueOf((int)((Math.random()*9+1)*100000));
|
|
|
+ if ("86".equals(areaNum)){
|
|
|
+ String sendCode = null;
|
|
|
+ try {
|
|
|
+ sendCode = smsService.sendSms(phoneNum, "{\"code\":\"" + code + "\"}", cnCode);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if("isv.BUSINESS_LIMIT_CONTROL".equals(sendCode)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3023, LoginConstant.FAILURE_MSG_3023);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ try{
|
|
|
+ smsService.sendSMSMessage(areaNum + phoneNum, code);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3013, LoginConstant.FAILURE_MSG_3013);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(redisUtil.hasKey(redisKeyMsg)){
|
|
|
+ redisUtil.del(redisKeyMsg);
|
|
|
+ }
|
|
|
+ redisUtil.set(redisKeyMsg,code,300);
|
|
|
+ redisUtil.set(redisKeyTime,String.valueOf(new Date().getTime()),60);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void register(RegisterParam param) {
|
|
|
+ if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum()) ||
|
|
|
+ StringUtils.isEmpty(param.getMsgAuthCode()) || StringUtils.isEmpty(param.getCountry()) || StringUtils.isEmpty(param.getConfirmPwd())){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ if(!param.getPassword().matches(ConstantRegex.PASSWORD_REGEX)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3011, LoginConstant.FAILURE_MSG_3011);
|
|
|
+ }
|
|
|
+ if (!param.getConfirmPwd().equals(param.getPassword())){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3009, LoginConstant.FAILURE_MSG_3009);
|
|
|
+ }
|
|
|
+ checkSms(param.getMsgAuthCode(),param.getPhoneNum(),true);
|
|
|
+ User user = userService.getByUserName(param.getPhoneNum());
|
|
|
+ if(user != null){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3008, LoginConstant.FAILURE_MSG_3008);
|
|
|
+ }
|
|
|
+ userService.register(param);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject createLoginQrCode() throws Exception {
|
|
|
+ String uuid = NumberUtils.getUUID();
|
|
|
+ String filePath = ConstantFilePath.LOGIN_QR_CODE_PATH + uuid + ".png";
|
|
|
+ String path =this.getClass().getResource("/static/img/logo.jpg").getPath();
|
|
|
+ MatrixToImageWriterUtil.createQRCode(mainUrl + "app/index.html?m="+uuid, filePath,true,path);
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("url", filePath.replace(ConstantFilePath.BASE_PATH, ""));
|
|
|
+ json.put("uuid", uuid);
|
|
|
+ return json;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject sendUserInfo(String uuid) {
|
|
|
+ if (StringUtils.isEmpty(uuid)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ if(!redisUtil.hasKey(uuid)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
|
|
|
+ }
|
|
|
+ String childName = redisUtil.get(uuid);
|
|
|
+ Camera camera = cameraService.getBySnCode(childName);
|
|
|
+ if(camera == null){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
|
|
|
+ }
|
|
|
+ CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
|
|
|
+ if(cameraDetail == null){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
|
|
|
+ }
|
|
|
+ UserVo userVo = new UserVo();
|
|
|
+ userVo.setUserName(childName);
|
|
|
+ userVo.setId(cameraDetail.getUserId());
|
|
|
+ userVo.setCameraId(camera.getId());
|
|
|
+ userVo.setCameraLogin(1);
|
|
|
+ String token = this.redisLogin(childName,JSONObject.toJSONString(userVo));
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("token",token);
|
|
|
+ obj.put("childName",childName);
|
|
|
+ obj.put("to",1);
|
|
|
+ redisUtil.del(uuid);
|
|
|
+ FileUtils.deleteFile(ConstantFilePath.LOGIN_QR_CODE_PATH +uuid +".png");
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void getEmailAuthCode(String email, String country) throws Exception {
|
|
|
+ if(StringUtils.isEmpty(email) || StringUtils.isEmpty(country)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ String code = String.valueOf((int)((Math.random()*9+1)*100000));
|
|
|
+
|
|
|
+ if("1".equals(country)){
|
|
|
+ SendMailAcceUtils.sendMail(email, SendMailAcceUtils.EN_CODE_SUBJECT, SendMailAcceUtils.EN_CODE_MSG.replace("${code}", code), null);
|
|
|
+ }if("2".equals(country)){
|
|
|
+ SendMailAcceUtils.sendMailUsa(email, SendMailAcceUtils.EN_CODE_SUBJECT_USA, SendMailAcceUtils.EN_CODE_MSG_USA.replace("${code}", code), null);
|
|
|
+ }else {
|
|
|
+ SendMailAcceUtils.sendMail(email, SendMailAcceUtils.CN_CODE_SUBJECT, SendMailAcceUtils.CN_CODE_MSG.replace("${code}", code), null);
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除缓存
|
|
|
+ if (redisUtil.hasKey(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + email)){
|
|
|
+ redisUtil.del(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + email);
|
|
|
+ }
|
|
|
+ //短信验证码,5分钟有效
|
|
|
+ redisUtil.set(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + email, code, 300);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void changePassword(RegisterParam param) {
|
|
|
+ if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getConfirmPwd()) ||
|
|
|
+ StringUtils.isEmpty(param.getMsgAuthCode())
|
|
|
+ || StringUtils.isEmpty(param.getPhoneNum())){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ if (!param.getPassword().equals(param.getConfirmPwd())){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3009, LoginConstant.FAILURE_MSG_3009);
|
|
|
+ }
|
|
|
+ //对前端传的密码解密
|
|
|
+ String password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
|
|
|
+ //正则判断密码是否符合规则(8位以上并且数字英文组合)
|
|
|
+ if(!password.matches(ConstantRegex.PASSWORD_REGEX)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3011, LoginConstant.FAILURE_MSG_3011);
|
|
|
+ }
|
|
|
+ User user = userService.getByUserName(param.getPhoneNum());
|
|
|
+ if(user == null){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
|
|
|
+ }
|
|
|
+ checkSms(param.getMsgAuthCode(),param.getPhoneNum(),true);
|
|
|
+ String pwdMd5 = SecurityUtil.MD5(password);
|
|
|
+ userService.updatePassword(param.getPhoneNum(), pwdMd5);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String redisLogin(String userName,String value){
|
|
|
+ String token = JwtUtil.createJWT(-1,userName,"user");
|
|
|
+ String redisKey = String.format(RedisKey.TOKEN_V3,token);
|
|
|
+ redisUtil.set(redisKey, value);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void loginCheck(String token) {
|
|
|
+ String redisKey = String.format(RedisKey.TOKEN_V3,token);
|
|
|
+ if(!redisUtil.hasKey(redisKey)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3004, LoginConstant.FAILURE_MSG_3004);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkSms(String msgAuthCode, String userName,boolean del) {
|
|
|
+ //验证码校验
|
|
|
+ String codeValue = redisUtil.get(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + userName);
|
|
|
+ if (StringUtils.isEmpty(codeValue)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
|
|
|
+ }
|
|
|
+ if (!codeValue.equals(msgAuthCode)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
|
|
|
+ }
|
|
|
+ if(del){
|
|
|
+ redisUtil.del(RedisKeyUtil.PREFIX_MSG_AUTH_CODE + userName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|