|
|
@@ -0,0 +1,265 @@
|
|
|
+package com.fdkankan.app.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fdkankan.app.common.RedisKeyUtil;
|
|
|
+import com.fdkankan.app.constant.LoginConstant;
|
|
|
+import com.fdkankan.app.entity.Camera;
|
|
|
+import com.fdkankan.app.entity.CameraDetail;
|
|
|
+import com.fdkankan.app.entity.User;
|
|
|
+import com.fdkankan.app.service.*;
|
|
|
+import com.fdkankan.app.vo.request.AppLoginParam;
|
|
|
+import com.fdkankan.app.vo.request.LoginParam;
|
|
|
+import com.fdkankan.app.vo.request.RegisterParam;
|
|
|
+import com.fdkankan.app.vo.response.LoginVo;
|
|
|
+import com.fdkankan.app.vo.response.UserVo;
|
|
|
+import com.fdkankan.common.constant.AppConstant;
|
|
|
+import com.fdkankan.common.constant.CameraConstant;
|
|
|
+import com.fdkankan.common.constant.ConstantRegex;
|
|
|
+import com.fdkankan.common.exception.BusinessException;
|
|
|
+import com.fdkankan.common.util.Base64Converter;
|
|
|
+import com.fdkankan.common.util.JwtUtil;
|
|
|
+import com.fdkankan.common.util.RandomUtil;
|
|
|
+import com.fdkankan.common.util.SecurityUtil;
|
|
|
+import com.fdkankan.redis.util.RedisUtil;
|
|
|
+import com.fdkankan.sms.SmsService;
|
|
|
+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.util.Date;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class LoginService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IUserService userService;
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+ @Autowired
|
|
|
+ private ICameraDetailService cameraDetailService;
|
|
|
+ @Autowired
|
|
|
+ ISceneProService sceneProService;
|
|
|
+ @Autowired
|
|
|
+ IScenePlusService scenePlusService;
|
|
|
+ @Autowired
|
|
|
+ ICameraService cameraService;
|
|
|
+ @Autowired
|
|
|
+ private SmsService smsService;
|
|
|
+
|
|
|
+ @Value("${phone.code.cn}")
|
|
|
+ private String cnCode;
|
|
|
+
|
|
|
+
|
|
|
+ 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 passwordCode = SecurityUtil.MD5(param.getPassword());
|
|
|
+ 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(), 21800L);
|
|
|
+
|
|
|
+ return commonLogin(user,param,token);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private LoginVo commonLogin(User user, LoginParam param, String token){
|
|
|
+ Long count = cameraDetailService.getCountByUserId(user.getId());
|
|
|
+ if(param.getCameraType() == null){
|
|
|
+ param.setCameraType( 4);
|
|
|
+ }
|
|
|
+ Long sceneProCount = sceneProService.getCountByUserId(user.getId(),param.getCameraType());
|
|
|
+ Long scenePlusCount = scenePlusService.getCountByUserId(user.getId(),param.getCameraType());
|
|
|
+ UserVo userVo = new UserVo();
|
|
|
+ userVo.setCameraCount(count);
|
|
|
+ userVo.setSceneCount(sceneProCount + scenePlusCount);
|
|
|
+ BeanUtils.copyProperties(user,userVo);
|
|
|
+ LoginVo vo = new LoginVo();
|
|
|
+ vo.setToken(token);
|
|
|
+ vo.setUser(userVo);
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String redisLogin(String userName,Long time){
|
|
|
+ String token = JwtUtil.createJWT(-1,userName,"app");
|
|
|
+ String redisKey = RedisKeyUtil.PREFIX_CACHE_CAMERA+ userName;
|
|
|
+ redisUtil.set(redisKey, token,time);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void appLogin(AppLoginParam param) {
|
|
|
+ if(StringUtils.isEmpty(param.getAppUserName()) || StringUtils.isEmpty(param.getAppPassword())
|
|
|
+ || StringUtils.isEmpty(param.getUuid())){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ commonCheckCamera(param);
|
|
|
+ redisUtil.set(param.getUuid(),param.getAppUserName(),60 * 5);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void commonCheckCamera(AppLoginParam param){
|
|
|
+ Camera camera = cameraService.getBySnCodeAndPassword(param.getAppUserName(),param.getAppPassword());
|
|
|
+ if(camera == null){
|
|
|
+ throw new BusinessException(CameraConstant.FAILURE_6003);
|
|
|
+ }
|
|
|
+ CameraDetail detail = cameraDetailService.getByCameraId(camera.getId());
|
|
|
+ if (detail == null ){
|
|
|
+ throw new BusinessException(AppConstant.FAILURE_CODE_4012, AppConstant.FAILURE_MSG_4012);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject login2(AppLoginParam param) {
|
|
|
+ if(StringUtils.isEmpty(param.getAppUserName()) || StringUtils.isEmpty(param.getAppPassword())){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ commonCheckCamera(param);
|
|
|
+ String token = redisLogin(param.getAppUserName(), 604800L);
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("token", token);
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ public LoginVo quickLogin(LoginParam param) {
|
|
|
+ if(StringUtils.isEmpty(param.getPhoneNum()) || StringUtils.isEmpty(param.getMsgAuthCode())){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ this.checkSms(param.getMsgAuthCode(),param.getPhoneNum(),true);
|
|
|
+
|
|
|
+ User user = userService.getByUserName(param.getPhoneNum());
|
|
|
+ if(user == null){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
|
|
|
+ }
|
|
|
+ String token = redisLogin(param.getPhoneNum(), 21600L);
|
|
|
+ return commonLogin(user,param,token);
|
|
|
+ }
|
|
|
+
|
|
|
+ private 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void logout(String token) {
|
|
|
+ String username = JwtUtil.getUsername(token);
|
|
|
+ String redisKey = RedisKeyUtil.PREFIX_CACHE_CAMERA+ username;
|
|
|
+ if(redisUtil.hasKey(redisKey)){
|
|
|
+ redisUtil.del(redisKey);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject getNickName() {
|
|
|
+ String nickName = null;
|
|
|
+ Long count = 0L;
|
|
|
+ do {
|
|
|
+ nickName = "mob" + RandomUtil.generateShortUuid();
|
|
|
+ count = userService.getCountByNickName(nickName);
|
|
|
+ } while (count > 0);
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("nickName",nickName);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 void checkUser(String phoneNum, boolean flg) {
|
|
|
+ if(StringUtils.isNotBlank(phoneNum)){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
|
|
|
+ }
|
|
|
+ User user = userService.getByUserName(phoneNum);
|
|
|
+ if(user == null && flg){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3015, LoginConstant.FAILURE_MSG_3015);
|
|
|
+ }
|
|
|
+ if(user != null && !flg){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3008, LoginConstant.FAILURE_MSG_3008);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 void getMsgAuthCode(String areaNum, String phoneNum) {
|
|
|
+ String redisKeyTime = RedisKeyUtil.PREFIX_MSG_NOT_CODE + phoneNum; //重发验证
|
|
|
+ String redisKeyMsg = RedisKeyUtil.PREFIX_MSG_AUTH_CODE + phoneNum; //验证码code
|
|
|
+
|
|
|
+ long value = redisUtil.getExpire(redisKeyTime);
|
|
|
+ if(value != -2){
|
|
|
+ throw new BusinessException(LoginConstant.FAILURE_CODE_3033, String.valueOf(value));
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|