Browse Source

api login/authentication add merchant

guozhaoshun 6 years ago
parent
commit
a5483beb57

+ 2 - 1
rankin-api-web/src/main/java/cn/rankin/apiweb/controller/LoginController.java

@@ -35,7 +35,8 @@ public class LoginController {
     @RequestMapping(method = RequestMethod.PUT)
     public APIResult<UserInfoVo> refresh(@RequestBody DeviceLoginDTO deviceLoginDTO) {
         String deviceCode = deviceLoginDTO.getDeviceCode();
-        return userService.refresh(deviceCode, Boolean.TRUE);
+        String merchant = deviceLoginDTO.getMerchant();
+        return userService.refresh(deviceCode, merchant, Boolean.TRUE);
     }
 
     @RequestMapping(method = RequestMethod.DELETE)

+ 2 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/entity/GlobalHeader.java

@@ -11,6 +11,8 @@ public class GlobalHeader implements Serializable {
 
     private String uid;
 
+    private String merchant;
+
     private String eid;
 
     private String sign;

+ 9 - 1
rankin-api-web/src/main/java/cn/rankin/apiweb/service/user/UserClient.java

@@ -33,7 +33,7 @@ public interface UserClient {
     APIResult<TerminalDeviceVo> findDeviceByUid(@PathVariable("userId") String uid);
 
     @RequestMapping(value = "/device/deviceCode/{deviceCode}", method = RequestMethod.GET)
-    APIResult<TerminalDeviceVo> findByDeviceCode(@PathVariable("deviceCode") String deviceCode);
+    APIResult<List<TerminalDeviceVo>> findByDeviceCode(@PathVariable("deviceCode") String deviceCode);
 
     @RequestMapping(value = "/white/user/{userId}", method = RequestMethod.GET)
     APIResult<WhiteUserVo> findWhiteUserById(@PathVariable("userId") String userId);
@@ -44,6 +44,9 @@ public interface UserClient {
     @RequestMapping(value = "/userTag/code/{code}", method = RequestMethod.GET)
     List<UserTag> findUserTagByCodeUid(@PathVariable("code") String code, @RequestParam("uid") String uid);
 
+    @RequestMapping(value = "/device/updatebind", method = RequestMethod.PUT)
+    APIResult<TerminalDeviceVo> updateDeviceBind(TerminalDeviceDTO deviceDTO);
+
 
     @Component
     class UserServiceHystrix implements UserClient {
@@ -93,5 +96,10 @@ public interface UserClient {
             return null;
         }
 
+        @Override
+        public APIResult<TerminalDeviceVo> updateDeviceBind(TerminalDeviceDTO deviceDTO) {
+            return APIResult.error(ApiWebCode.SERVER_ERROR);
+        }
+
     }
 }

+ 51 - 7
rankin-api-web/src/main/java/cn/rankin/apiweb/service/user/UserService.java

@@ -18,11 +18,13 @@ import cn.rankin.data.api.user.vo.TerminalDeviceVo;
 import cn.rankin.data.api.user.vo.TerminalUserVo;
 import com.alibaba.fastjson.JSON;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.time.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
 import java.util.Date;
 import java.util.List;
@@ -52,6 +54,7 @@ public class UserService {
 
     public APIResult<UserInfoVo> login(LoginInfoDTO loginInfoDTO) {
         String deviceCode = loginInfoDTO.getDeviceCode();
+        String merchant = loginInfoDTO.getMerchant();
         String eid = loginInfoDTO.getEid();
         String loginPassword = loginInfoDTO.getPassword();
         log.info("user login start, user={}", JSON.toJSONString(loginInfoDTO));
@@ -63,12 +66,20 @@ public class UserService {
             return APIResult.error(ApiWebCode.LOGIN_ERROR);
         }
 
+
+
         String storePassword = userVo.getPassword();
         if (!SecurityManager.validate(loginPassword, storePassword)) {
             log.error("密码校验错误, loginPassword={}, storePassword={}", loginPassword, storePassword);
             return APIResult.error(ApiWebCode.PASSWORD_ERROR);
         }
 
+
+        if(StringUtils.isNotBlank(merchant) && !merchant.equals(userVo.getMerchantSimple())){
+            log.error("渠道校验错误, requestMerchant={}, userMerchant={}", merchant, userVo.getMerchantSimple());
+            return APIResult.error(ApiWebCode.LOGIN_ERROR);
+        }
+
         String userId = userVo.getId();
         String ip = loginInfoDTO.getIp();
         String terminal = loginInfoDTO.getTerminal();
@@ -90,7 +101,7 @@ public class UserService {
             }
         }
 
-        TerminalDeviceVo deviceVo = this.bind(userId, deviceCode, ip, terminal);
+        TerminalDeviceVo deviceVo = this.bind(userId, deviceCode, merchant, ip, terminal);
         if (deviceVo == null) {
             return APIResult.error(ApiWebCode.DEVICE_BOUND_ERROR);
         }
@@ -118,16 +129,17 @@ public class UserService {
         return APIResult.ok();
     }
 
-    public TerminalDeviceVo bind(String userId, String deviceCode, String ip, String terminal) {
+    public TerminalDeviceVo bind(String userId, String deviceCode, String merchant, String ip, String terminal) {
         TerminalDeviceDTO deviceDTO = new TerminalDeviceDTO();
         deviceDTO.setUserId(userId);
         deviceDTO.setDeviceCode(deviceCode);
+        deviceDTO.setMerchant(merchant);
         deviceDTO.setIp(ip);
         deviceDTO.setTerminal(terminal);
         APIResult<TerminalDeviceVo> apiResult = userClient.deviceBind(deviceDTO);
         if (!apiResult.getSuccess()) {
             log.error("bind user api error");
-            return null;
+            throw new RuntimeException(apiResult.getMessage());
         }
         return apiResult.getData();
     }
@@ -168,19 +180,40 @@ public class UserService {
         return toDeviceUserVo(userVo, deviceVo);
     }
 
-    public APIResult<UserInfoVo> refresh(String deviceCode, Boolean force) {
-        APIResult<TerminalDeviceVo> deviceAPIResult = userClient.findByDeviceCode(deviceCode);
+    public APIResult<UserInfoVo> refresh(String deviceCode, String merchant, Boolean force) {
+
+        APIResult<List<TerminalDeviceVo>> deviceAPIResult = userClient.findByDeviceCode(deviceCode);
         if (!deviceAPIResult.getSuccess()) {
             log.error("load device api error, {}", deviceAPIResult.getMessage());
             return APIResult.error(ApiWebCode.NOT_FOUND);
         }
 
-        TerminalDeviceVo deviceVo = deviceAPIResult.getData();
-        if (deviceVo == null) {
+        List<TerminalDeviceVo> deviceVoList = deviceAPIResult.getData();
+        if (CollectionUtils.isEmpty(deviceVoList)) {
             log.error("device code not bind any eid! deviceCode={}", deviceCode);
             return APIResult.error(ApiWebCode.NOT_EXISTS);
         }
 
+        TerminalDeviceVo deviceVo = null;
+
+        for (TerminalDeviceVo dv : deviceVoList) {
+            //判断当前鉴权账号
+            if(StringUtils.isBlank(dv.getMerchant())){
+                //如果设备渠道code为空,则设置为当前渠道code
+                dv.setMerchant(merchant);
+                //更新数据库
+                updateDeviceBind(dv);
+                deviceVo = dv;
+            }else if(StringUtils.isNotBlank(merchant) && merchant.equals(dv.getMerchant())) {
+                deviceVo = dv;
+            }
+        }
+
+        if(null == deviceVo){
+            log.error("by device code not find this merchant! deviceCode={},merchant={}", deviceCode, merchant);
+            return APIResult.error(ApiWebCode.INVALID_TOKEN);
+        }
+
         String uid = deviceVo.getUserId();
 
         DeviceUserVo deviceUserVo;
@@ -209,6 +242,17 @@ public class UserService {
         return APIResult.ok(userInfoVo);
     }
 
+    private void updateDeviceBind(TerminalDeviceVo terminalDeviceVo) {
+        TerminalDeviceDTO deviceDTO = new TerminalDeviceDTO();
+        deviceDTO.setId(terminalDeviceVo.getId());
+        deviceDTO.setMerchant(terminalDeviceVo.getMerchant());
+        APIResult<TerminalDeviceVo> apiResult = userClient.updateDeviceBind(deviceDTO);
+        if (!apiResult.getSuccess()) {
+            log.error("update bind user api error");
+            throw new RuntimeException("update bind user api error");
+        }
+    }
+
     // cache user and device info
     public void save(DeviceUserVo deviceUserVo) {
         String key = getUserFormatKey(deviceUserVo.getUid());

+ 7 - 1
rankin-user-service/src/main/java/cn/rankin/userservice/controller/TerminalDeviceController.java

@@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.validation.Valid;
+import java.util.List;
 
 @RestController
 @RequestMapping(value = "/device")
@@ -22,6 +23,11 @@ public class TerminalDeviceController {
         return terminalDeviceService.bind(terminalDeviceDTO);
     }
 
+    @RequestMapping(value = "/updatebind", method = RequestMethod.PUT)
+    public APIResult<TerminalDevice> updatebind(@Valid @RequestBody TerminalDeviceDTO terminalDeviceDTO) {
+        return terminalDeviceService.updatebind(terminalDeviceDTO);
+    }
+
     @RequestMapping(value = "/unbind", method = RequestMethod.DELETE)
     public APIResult<Boolean> unbind(@RequestParam("userId") String userId) {
         return terminalDeviceService.unbind(userId);
@@ -38,7 +44,7 @@ public class TerminalDeviceController {
     }
 
     @RequestMapping(value = "/deviceCode/{deviceCode}", method = RequestMethod.GET)
-    public APIResult<TerminalDevice> findByDeviceCode(@PathVariable("deviceCode") String deviceCode) {
+    public APIResult<List<TerminalDevice>> findByDeviceCode(@PathVariable("deviceCode") String deviceCode) {
         return terminalDeviceService.findByDeviceCode(deviceCode);
     }
 }

+ 1 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/controller/TerminalUserController.java

@@ -285,6 +285,7 @@ public class TerminalUserController {
 
         Merchant merchant = merchantService.findOne(campus.getMerchantId());
         if (merchant != null) {
+            terminalUser.setMerchantSimple(merchant.getSimple());
             terminalUser.setMerchantName(merchant.getName());
             terminalUser.setMerchantContactMobile(merchant.getMobile());
             terminalUser.setMerchantContactName(merchant.getContactName());

+ 6 - 1
rankin-user-service/src/main/java/cn/rankin/userservice/repository/TerminalDeviceRepository.java

@@ -5,11 +5,13 @@ import cn.rankin.data.api.user.entity.TerminalDevice;
 import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 
 @Repository
 public interface TerminalDeviceRepository extends BasicJpaRepository<TerminalDevice, String> {
 
-    TerminalDevice findByDeviceCode(String code);
+    List<TerminalDevice> findByDeviceCode(String code);
 
     TerminalDevice findByUserId(String userId);
 
@@ -18,4 +20,7 @@ public interface TerminalDeviceRepository extends BasicJpaRepository<TerminalDev
     @Modifying
     Integer deleteByUserId(String userId);
 
+    @Modifying
+    Integer deleteByDeviceCodeAndMerchant(String code, String merchant);
+
 }

+ 2 - 1
rankin-user-service/src/main/java/cn/rankin/userservice/service/TerminalDeviceBindLogService.java

@@ -14,9 +14,10 @@ public class TerminalDeviceBindLogService {
     private TerminalDeviceBindLogRepository terminalDeviceBindLogRepository;
 
     @Transactional
-    public TerminalDeviceBindLog insert(String deviceCode, String userId, String brand, String ip, String modelNo, String terminal) {
+    public TerminalDeviceBindLog insert(String deviceCode, String merchant, String userId, String brand, String ip, String modelNo, String terminal) {
         TerminalDeviceBindLog deviceBindLog = new TerminalDeviceBindLog();
         deviceBindLog.setDeviceCode(deviceCode);
+        deviceBindLog.setMerchant(merchant);
         deviceBindLog.setUserId(userId);
         deviceBindLog.setBrand(brand);
         deviceBindLog.setIp(ip);

+ 37 - 9
rankin-user-service/src/main/java/cn/rankin/userservice/service/TerminalDeviceService.java

@@ -5,10 +5,12 @@ import cn.rankin.data.api.user.dto.TerminalDeviceDTO;
 import cn.rankin.data.api.user.entity.TerminalDevice;
 import cn.rankin.userservice.code.UserServiceAPICode;
 import cn.rankin.userservice.repository.TerminalDeviceRepository;
+import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.transaction.Transactional;
+import java.util.List;
 
 @Service
 public class TerminalDeviceService {
@@ -22,35 +24,45 @@ public class TerminalDeviceService {
     @Transactional
     public APIResult<TerminalDevice> bind(TerminalDeviceDTO terminalDeviceDTO) {
         String deviceCode = terminalDeviceDTO.getDeviceCode();
+        String merchant = terminalDeviceDTO.getMerchant();
         String userId = terminalDeviceDTO.getUserId();
         String brand = terminalDeviceDTO.getBrand();
         String ip = terminalDeviceDTO.getIp();
         String modelNo = terminalDeviceDTO.getModelNo();
         String terminal = terminalDeviceDTO.getTerminal();
 
-        TerminalDevice terminalDevice = terminalDeviceRepository.findByDeviceCodeOrUserId(deviceCode, userId);
+        TerminalDevice terminalDevice = terminalDeviceRepository.findByUserId(userId);
         if (terminalDevice != null) {
-            if (userId.equals(terminalDevice.getUserId())) {
+            if(!deviceCode.equals(terminalDevice.getDeviceCode())){
+                //存在设备号不一致,账号被其他设备使用中
                 return APIResult.error(UserServiceAPICode.USER_IS_BOUND);
-            }else {
-                return APIResult.error(UserServiceAPICode.DEVICE_IS_BOUND);
+            }else{
+                //根据设备号,merchant,删除设备绑定记录,后面会重新添加绑定关系
+                deleteTerminalDeviceByDeviceCodeMerchant(deviceCode, merchant);
+                terminalDeviceBindLogService.insert(deviceCode, merchant, userId, brand, ip, modelNo, terminal+"_del");
             }
         }
 
         terminalDevice = new TerminalDevice();
         terminalDevice.setUserId(userId);
         terminalDevice.setDeviceCode(deviceCode);
+        terminalDevice.setMerchant(merchant);
         terminalDevice.setBrand(brand);
         terminalDevice.setModelNo(modelNo);
         terminalDevice.setIp(ip);
         terminalDevice.setTerminal(terminal);
 
         TerminalDevice deviceBind = terminalDeviceRepository.save(terminalDevice);
-        terminalDeviceBindLogService.insert(deviceCode, userId, brand, ip, modelNo, terminal);
+        terminalDeviceBindLogService.insert(deviceCode, merchant, userId, brand, ip, modelNo, terminal);
 
         return APIResult.ok(deviceBind);
     }
 
+    private void deleteTerminalDeviceByDeviceCodeMerchant(String deviceCode, String merchant) {
+        terminalDeviceRepository.deleteByDeviceCodeAndMerchant(deviceCode, merchant);
+        terminalDeviceRepository.deleteByDeviceCodeAndMerchant(deviceCode, null);
+    }
+
     @Transactional
     public APIResult<Boolean> unbind(String userId) {
         TerminalDevice terminalDevice = terminalDeviceRepository.findByUserId(userId);
@@ -73,11 +85,27 @@ public class TerminalDeviceService {
         return terminalDeviceRepository.findByUserId(userId);
     }
 
-    public APIResult<TerminalDevice> findByDeviceCode(String deviceCode) {
-        TerminalDevice terminalDevice = terminalDeviceRepository.findByDeviceCode(deviceCode);
-        if (terminalDevice == null) {
+    public APIResult<List<TerminalDevice>> findByDeviceCode(String deviceCode) {
+        List<TerminalDevice> terminalDeviceList = terminalDeviceRepository.findByDeviceCode(deviceCode);
+        /*if (terminalDevice == null) {
             return APIResult.error(UserServiceAPICode.NOT_EXISTS);
+        }*/
+        return APIResult.ok(terminalDeviceList);
+    }
+
+    public APIResult<TerminalDevice> updatebind(TerminalDeviceDTO terminalDeviceDTO) {
+        if(StringUtils.isBlank(terminalDeviceDTO.getId())){
+            return APIResult.error(UserServiceAPICode.PARAMETER_ERROR);
         }
-        return APIResult.ok(terminalDevice);
+
+        TerminalDevice terminalDevice = terminalDeviceRepository.getOne(terminalDeviceDTO.getId());
+        if(null == terminalDevice){
+            return APIResult.error(UserServiceAPICode.PARAMETER_ERROR);
+        }
+        terminalDevice.setMerchant(terminalDeviceDTO.getMerchant());
+
+        TerminalDevice device = terminalDeviceRepository.update(terminalDevice);
+
+        return APIResult.ok(device);
     }
 }