3
0

8 Revīzijas e03a581e5a ... 46bb611eac

Autors SHA1 Ziņojums Datums
  huodongdong 46bb611eac fix 6 gadi atpakaļ
  huodongdong a7c33c52fe fix feign pathvariable error 6 gadi atpakaļ
  huodongdong 7450e47d4f fix 6 gadi atpakaļ
  huodongdong c9ecf201a9 add merchantId of white user 6 gadi atpakaļ
  huodongdong 8bb7d1122a add merchant name of white user 6 gadi atpakaļ
  huodongdong 50a2dd42d0 add api binder cms white user 6 gadi atpakaļ
  huodongdong ac39c034af add white user model 6 gadi atpakaļ
  huodongdong 2798495bf0 add white user table 6 gadi atpakaļ

+ 37 - 1
rankin-api-web/src/main/java/cn/rankin/apiweb/service/auth/AuthService.java

@@ -1,22 +1,58 @@
 package cn.rankin.apiweb.service.auth;
 
+import cn.rankin.apiweb.service.user.UserClient;
 import cn.rankin.common.utils.api.model.APIResult;
+import cn.rankin.common.utils.api.model.BaseCode;
 import cn.rankin.data.api.auth.dto.InnerAuthDTO;
 import cn.rankin.data.api.auth.vo.AuthResult;
+import cn.rankin.data.api.user.vo.WhiteUserVo;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.alibaba.fastjson.JSON;
 
+import java.util.Date;
+
 @Service
 @Slf4j
 public class AuthService{
 
     @Autowired
+    private UserClient userClient;
+
+    @Autowired
     private AuthClient authClient;
 
+    public APIResult<AuthResult> authWhiteUser(String userId, String productId) {
+        APIResult<WhiteUserVo> result = userClient.findWhiteUserById(userId);
+        if (!result.getSuccess()) {
+            log.error("user white user auth error, code={}, msg={}", result.getCode(), result.getMessage());
+            return APIResult.error(new BaseCode(result.getCode(), result.getMessage()));
+        }
+        WhiteUserVo whiteUserVo = result.getData();
+
+        AuthResult authResult = new AuthResult();
+        authResult.setUserId(userId);
+        authResult.setPid(productId);
+        Date startTime = whiteUserVo.getStartTime();
+        authResult.setStartTime(startTime.getTime());
+        Date endTime = whiteUserVo.getEndTime();
+        if (endTime != null) {
+            authResult.setEndTime(endTime.getTime());
+        }
+        return APIResult.ok(authResult);
+
+    }
+
     public APIResult<AuthResult> auth(String userId, String merchantId, String itemId) {
-        log.info("auth vo: {}, item: {}", userId, itemId);
+        log.info("auth start: userId={}, itemId={}", userId, itemId);
+
+        // 如果在白名单里就不再查询鉴权信息
+        APIResult<AuthResult> whiteUserAuthApiResult = this.authWhiteUser(userId, itemId);
+        if (whiteUserAuthApiResult.getSuccess()) {
+            return whiteUserAuthApiResult;
+        }
+
         InnerAuthDTO authDTO = new InnerAuthDTO();
         authDTO.setItemId(itemId);
         authDTO.setUserId(userId);

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

@@ -5,6 +5,7 @@ import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.data.api.user.dto.TerminalDeviceDTO;
 import cn.rankin.data.api.user.vo.TerminalDeviceVo;
 import cn.rankin.data.api.user.vo.TerminalUserVo;
+import cn.rankin.data.api.user.vo.WhiteUserVo;
 import org.springframework.cloud.netflix.feign.FeignClient;
 import org.springframework.stereotype.Component;
 import org.springframework.web.bind.annotation.*;
@@ -30,6 +31,9 @@ public interface UserClient {
     @RequestMapping(value = "/device/deviceCode/{deviceCode}", method = RequestMethod.GET)
     APIResult<TerminalDeviceVo> findByDeviceCode(@PathVariable("deviceCode") String deviceCode);
 
+    @RequestMapping(value = "/white/user/{userId}", method = RequestMethod.GET)
+    APIResult<WhiteUserVo> findWhiteUserById(@PathVariable("userId") String userId);
+
     @Component
     class UserServiceHystrix implements UserClient {
 
@@ -62,5 +66,10 @@ public interface UserClient {
         public APIResult findByDeviceCode(String deviceCode) {
             return APIResult.error(ApiWebCode.SERVER_ERROR);
         }
+
+        @Override
+        public APIResult<WhiteUserVo> findWhiteUserById(String userId) {
+            return APIResult.error(ApiWebCode.SERVER_ERROR);
+        }
     }
 }

+ 68 - 0
rankin-cms-web/src/main/java/cn/rankin/cmsweb/controller/user/WhiteUserController.java

@@ -0,0 +1,68 @@
+package cn.rankin.cmsweb.controller.user;
+
+import cn.rankin.cmsweb.assist.resolver.NeedUser;
+import cn.rankin.cmsweb.code.CmsWebAPICode;
+import cn.rankin.cmsweb.entity.UserDetails;
+import cn.rankin.cmsweb.service.user.WhiteUserService;
+import cn.rankin.common.utils.api.model.APIResult;
+import cn.rankin.common.utils.api.page.Page;
+import cn.rankin.data.api.user.dto.WhiteUserDTO;
+import cn.rankin.data.api.user.dto.WhiteUserSearchDTO;
+import cn.rankin.data.api.user.vo.WhiteUserVo;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.propertyeditors.CustomDateEditor;
+import org.springframework.web.bind.ServletRequestDataBinder;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletRequest;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+
+@RestController
+@RequestMapping(value = "/white/user")
+public class WhiteUserController {
+
+    @InitBinder
+    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
+        binder.registerCustomEditor(Date.class,
+                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
+    }
+
+    @Autowired
+    private WhiteUserService whiteUserService;
+
+    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
+    public APIResult<WhiteUserVo> getWhiteUser(@PathVariable String userId) {
+        return whiteUserService.findUserByUserId(userId);
+    }
+
+    @RequestMapping(method = RequestMethod.POST)
+    public APIResult<WhiteUserVo> create(@NeedUser UserDetails user, @RequestBody WhiteUserDTO whiteUserDTO) {
+        if (!user.isPlatForm()) {
+            return APIResult.error(CmsWebAPICode.ACCESS_DENIED);
+        }
+        return whiteUserService.addWhiteUser(whiteUserDTO);
+    }
+
+    @RequestMapping(method = RequestMethod.PUT)
+    public APIResult<WhiteUserVo> update(@NeedUser UserDetails user, @RequestBody WhiteUserDTO whiteUserDTO) {
+        if (!user.isPlatForm()) {
+            return APIResult.error(CmsWebAPICode.ACCESS_DENIED);
+        }
+        return whiteUserService.updateWhiteUser(whiteUserDTO);
+    }
+
+    @RequestMapping(method = RequestMethod.DELETE)
+    public APIResult<Boolean> delete(@NeedUser UserDetails user, @RequestBody WhiteUserDTO whiteUserDTO) {
+        if (!user.isPlatForm()) {
+            return APIResult.error(CmsWebAPICode.ACCESS_DENIED);
+        }
+        return whiteUserService.deleteWhiteUser(whiteUserDTO);
+    }
+
+    @RequestMapping(value = "/list", method = RequestMethod.GET)
+    public APIResult<Page<WhiteUserVo>> search(WhiteUserSearchDTO searchDTO) {
+        return whiteUserService.search(searchDTO);
+    }
+}

+ 29 - 0
rankin-cms-web/src/main/java/cn/rankin/cmsweb/service/user/WhiteUserService.java

@@ -0,0 +1,29 @@
+package cn.rankin.cmsweb.service.user;
+
+import cn.rankin.common.utils.api.model.APIResult;
+import cn.rankin.common.utils.api.page.Page;
+import cn.rankin.data.api.user.dto.WhiteUserDTO;
+import cn.rankin.data.api.user.dto.WhiteUserSearchDTO;
+import cn.rankin.data.api.user.vo.WhiteUserVo;
+import org.springframework.cloud.netflix.feign.FeignClient;
+import org.springframework.web.bind.annotation.*;
+
+@FeignClient(name = "${service.user.name}")
+public interface WhiteUserService {
+
+    @RequestMapping(value = "/white/user/{userId}", method = RequestMethod.GET)
+    APIResult<WhiteUserVo> findUserByUserId(@PathVariable("userId") String userId);
+
+    @RequestMapping(value = "/white/user", method = RequestMethod.POST)
+    APIResult<WhiteUserVo> addWhiteUser(@RequestBody WhiteUserDTO whiteUserDTO);
+
+    @RequestMapping(value = "/white/user", method = RequestMethod.PUT)
+    APIResult<WhiteUserVo> updateWhiteUser(@RequestBody WhiteUserDTO whiteUserDTO);
+
+    @RequestMapping(value = "/white/user", method = RequestMethod.DELETE)
+    APIResult<Boolean> deleteWhiteUser(@RequestBody WhiteUserDTO whiteUserDTO);
+
+    @RequestMapping(value = "/white/user/list", method = RequestMethod.POST)
+    APIResult<Page<WhiteUserVo>> search(@RequestBody WhiteUserSearchDTO searchDTO);
+
+}

+ 1 - 1
rankin-data-api/src/main/java/cn/rankin/data/api/auth/vo/AuthResult.java

@@ -20,7 +20,7 @@ public class AuthResult implements Serializable {
 
     private Long endTime;
 
-    public Long getValidTime() {
+    private Long getValidTime() {
         if (startTime == null || endTime == null) {
             return 0L;
         }

+ 2 - 0
rankin-data-api/src/main/java/cn/rankin/data/api/user/dto/WhiteUserDTO.java

@@ -15,6 +15,8 @@ public class WhiteUserDTO implements Serializable{
 
     private String userId;
 
+    private String merchantId;
+
     private Date startTime;
 
     private Date endTime;

+ 2 - 1
rankin-data-api/src/main/java/cn/rankin/data/api/user/dto/WhiteUserSearchDTO.java

@@ -1,6 +1,5 @@
 package cn.rankin.data.api.user.dto;
 
-import cn.rankin.common.utils.enums.BaseStatusEnum;
 import lombok.Data;
 import lombok.ToString;
 
@@ -10,6 +9,8 @@ import java.io.Serializable;
 @ToString
 public class WhiteUserSearchDTO implements Serializable {
 
+    private String merchantId;
+
     private String code;
 
     private Integer pageNo = 1;

+ 6 - 0
rankin-data-api/src/main/java/cn/rankin/data/api/user/entity/WhiteUser.java

@@ -24,6 +24,9 @@ public class WhiteUser implements Serializable {
     @Column(name = "user_id", nullable = false, unique = true)
     private String userId;
 
+    @Column(name = "merchant_id", nullable = false)
+    private String merchantId;
+
     @Column(name = "start_time", nullable = false, columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP")
     private Date startTime;
 
@@ -45,4 +48,7 @@ public class WhiteUser implements Serializable {
     @Transient
     private String code;
 
+    @Transient
+    private String merchantName;
+
 }

+ 4 - 0
rankin-data-api/src/main/java/cn/rankin/data/api/user/vo/WhiteUserVo.java

@@ -15,6 +15,8 @@ public class WhiteUserVo implements Serializable {
 
     private String userId;
 
+    private String merchantId;
+
     private Date startTime;
 
     private Date endTime;
@@ -26,4 +28,6 @@ public class WhiteUserVo implements Serializable {
     private Date gmtModified;
 
     private String code;
+
+    private String merchantName;
 }

+ 78 - 2
rankin-user-service/src/main/java/cn/rankin/userservice/controller/WhiteUserController.java

@@ -3,22 +3,34 @@ package cn.rankin.userservice.controller;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.common.utils.enums.BaseOrderEnum;
+import cn.rankin.common.utils.util.ListUtil;
 import cn.rankin.data.api.user.dto.WhiteUserDTO;
 import cn.rankin.data.api.user.dto.WhiteUserSearchDTO;
+import cn.rankin.data.api.user.entity.Merchant;
+import cn.rankin.data.api.user.entity.TerminalUser;
 import cn.rankin.data.api.user.entity.WhiteUser;
 import cn.rankin.userservice.code.UserServiceAPICode;
+import cn.rankin.userservice.service.MerchantService;
+import cn.rankin.userservice.service.TerminalUserService;
 import cn.rankin.userservice.service.WhiteUserService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.LinkedHashMap;
+import java.util.*;
 
 @RestController
 @RequestMapping(value = "/white/user")
 public class WhiteUserController {
 
     @Autowired
+    private MerchantService merchantService;
+
+    @Autowired
+    private TerminalUserService terminalUserService;
+
+    @Autowired
     private WhiteUserService whiteUserService;
 
     @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
@@ -27,15 +39,24 @@ public class WhiteUserController {
         if (whiteUser == null) {
             return APIResult.error(UserServiceAPICode.NOT_EXISTS);
         }
+        setUserInfo(whiteUser);
         return APIResult.ok(whiteUser);
     }
 
     @RequestMapping(method = RequestMethod.POST)
     public APIResult<WhiteUser> create(@RequestBody WhiteUserDTO whiteUserDTO) {
+        String userId = whiteUserDTO.getUserId();
+        TerminalUser terminalUser = terminalUserService.find(userId);
+        if (terminalUser == null) {
+            return APIResult.error(UserServiceAPICode.NOT_EXISTS);
+        }
+        whiteUserDTO.setMerchantId(terminalUser.getMerchantId());
+
         WhiteUser whiteUser = whiteUserService.addUser(whiteUserDTO);
         if (whiteUser != null) {
             return APIResult.ok(whiteUser);
         }
+        setUserInfo(whiteUser);
         return APIResult.error(UserServiceAPICode.error("创建失败"));
     }
 
@@ -45,6 +66,7 @@ public class WhiteUserController {
         if (whiteUser != null) {
             return APIResult.ok(whiteUser);
         }
+        setUserInfo(whiteUser);
         return APIResult.error(UserServiceAPICode.error("更新失败"));
     }
 
@@ -58,18 +80,72 @@ public class WhiteUserController {
         return APIResult.error(UserServiceAPICode.NOT_EXISTS);
     }
 
-    @RequestMapping(value = "/list", method = RequestMethod.GET)
+    @RequestMapping(value = "/list", method = RequestMethod.POST)
     public APIResult<Page<WhiteUser>> search(WhiteUserSearchDTO searchDTO) {
         WhiteUser sample = new WhiteUser();
         String code = searchDTO.getCode();
         if (!StringUtils.isEmpty(code)) {
             sample.setCode("%" + code + "%");
         }
+        String merchantId = searchDTO.getMerchantId();
+        if (!StringUtils.isEmpty(merchantId)) {
+            sample.setMerchantId(merchantId);
+        }
         Integer pageNo = searchDTO.getPageNo();
         Integer pageSize = searchDTO.getPageSize();
         LinkedHashMap<String, BaseOrderEnum> sort = new LinkedHashMap<>();
         sort.put("gmtModified", BaseOrderEnum.DESC);
         Page<WhiteUser> page = whiteUserService.search(sample, pageNo, pageSize, sort);
+        List<WhiteUser> whiteUserList = page.getList();
+        setUserInfo(whiteUserList);
         return APIResult.ok(page);
     }
+
+    public void setUserInfo(WhiteUser whiteUser) {
+        if (whiteUser == null) {
+            return;
+        }
+        String userId = whiteUser.getUserId();
+        TerminalUser terminalUser = terminalUserService.find(userId);
+        Merchant merchant = merchantService.findOne(whiteUser.getMerchantId());
+        if (terminalUser != null) {
+            whiteUser.setCode(terminalUser.getCode());
+        }
+        if (merchant != null) {
+            whiteUser.setMerchantName(merchant.getName());
+        }
+    }
+
+    public void setUserInfo(List<WhiteUser> whiteUserList) {
+        if (CollectionUtils.isEmpty(whiteUserList)) {
+            return;
+        }
+
+        List<String> userIdList = new ArrayList<>();
+        List<String> merchantIdList = new ArrayList<>();
+        for (WhiteUser whiteUser : whiteUserList) {
+            userIdList.add(whiteUser.getUserId());
+            String merchantId = whiteUser.getMerchantId();
+            if (!merchantIdList.contains(merchantId)) {
+                merchantIdList.add(merchantId);
+            }
+        }
+
+        List<TerminalUser> terminalUserList = terminalUserService.findByIds(userIdList);
+        Map<String, TerminalUser> terminalUserMap = ListUtil.convert(terminalUserList, "id", TerminalUser.class);
+        Map<String, Merchant> merchantMap = merchantService.getMerchantMap(merchantIdList);
+
+        for (WhiteUser whiteUser : whiteUserList) {
+            String userId = whiteUser.getUserId();
+            TerminalUser terminalUser = terminalUserMap.get(userId);
+            if (terminalUser != null) {
+                whiteUser.setCode(terminalUser.getCode());
+            }
+            String merchantId = whiteUser.getMerchantId();
+            Merchant merchant = merchantMap.get(merchantId);
+            if (merchant != null) {
+                whiteUser.setMerchantName(merchant.getName());
+            }
+        }
+    }
 }

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

@@ -16,7 +16,6 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
 import javax.transaction.Transactional;
-import java.math.BigDecimal;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;

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

@@ -1,6 +1,5 @@
 package cn.rankin.userservice.service;
 
-import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.common.utils.enums.BaseOrderEnum;
 import cn.rankin.common.utils.enums.BaseStatusEnum;