Jelajahi Sumber

feat: 重构登录方式

shc 1 tahun lalu
induk
melakukan
15af57c746

+ 20 - 9
hnqz-auth/src/main/java/com/qunzhixinxi/hnqz/auth/service/HnqzUserDetailsServiceImpl.java

@@ -45,20 +45,31 @@ public class HnqzUserDetailsServiceImpl implements HnqzUserDetailsService {
      * 用户密码登录
      *
      * @param username 用户名
-     * @return
-     * @throws UsernameNotFoundException
+     * @return UserDetails
+     * @throws UsernameNotFoundException 异常
      */
     @Override
     @SneakyThrows
     public UserDetails loadUserByUsername(String username) {
-        String u1 = username.endsWith("@mp") ? username.split("@")[0] : username;
+
+        String rawUsername;
+        boolean isApp;
+
+        if (username.endsWith("@mp")) {
+            isApp = true;
+            rawUsername = username.split("@")[0];
+        } else {
+            isApp = false;
+            rawUsername = username;
+        }
+
 
         Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
-        if (cache != null && cache.get(u1) != null) {
-            return (HnqzUser) cache.get(u1).get();
+        if (cache != null && cache.get(rawUsername) != null) {
+            return (HnqzUser) cache.get(rawUsername).get();
         }
 
-        R<UserInfo> result = remoteUserService.info(username, SecurityConstants.FROM_IN);
+        R<UserInfo> result = remoteUserService.info1(rawUsername, isApp, SecurityConstants.FROM_IN);
         UserDetails userDetails = getUserDetails(result);
         cache.put(username, userDetails);
         return userDetails;
@@ -69,7 +80,7 @@ public class HnqzUserDetailsServiceImpl implements HnqzUserDetailsService {
      *
      * @param inStr TYPE@CODE
      * @return UserDetails
-     * @throws UsernameNotFoundException
+     * @throws UsernameNotFoundException 异常
      */
     @Override
     @SneakyThrows
@@ -78,10 +89,10 @@ public class HnqzUserDetailsServiceImpl implements HnqzUserDetailsService {
     }
 
     /**
-     * 构建userdetails
+     * 构建 UserDetails
      *
      * @param result 用户信息
-     * @return
+     * @return UserDetails
      */
     private UserDetails getUserDetails(R<UserInfo> result) {
         if (result == null || result.getData() == null) {

+ 42 - 26
hnqz-upms/hnqz-upms-api/src/main/java/com/qunzhixinxi/hnqz/admin/api/feign/RemoteUserService.java

@@ -1,4 +1,3 @@
-
 package com.qunzhixinxi.hnqz.admin.api.feign;
 
 import com.qunzhixinxi.hnqz.admin.api.dto.UserInfo;
@@ -10,6 +9,7 @@ import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestParam;
 
 import java.util.List;
 
@@ -20,30 +20,46 @@ import java.util.List;
 @FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.UPMS_SERVICE)
 public interface RemoteUserService {
 
-	/**
-	 * 通过用户名查询用户、角色信息
-	 * @param username 用户名
-	 * @param from 调用标志
-	 * @return R
-	 */
-	@GetMapping("/user/info/{username}")
-	R<UserInfo> info(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM) String from);
-
-	/**
-	 * 通过社交账号或手机号查询用户、角色信息
-	 * @param inStr appid@code
-	 * @param from 调用标志
-	 * @return
-	 */
-	@GetMapping("/social/info/{inStr}")
-	R<UserInfo> social(@PathVariable("inStr") String inStr, @RequestHeader(SecurityConstants.FROM) String from);
-
-	/**
-	 * 查询上级部门的用户信息
-	 * @param username 用户名
-	 * @return R
-	 */
-	@GetMapping("/user/ancestor/{username}")
-	R<List<SysUser>> ancestorUsers(@PathVariable("username") String username);
+    /**
+     * 通过用户名查询用户、角色信息
+     *
+     * @param username 用户名
+     * @param from     调用标志
+     * @return R
+     */
+    @GetMapping("/user/info/{username}")
+    R<UserInfo> info(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM) String from);
+
+    /**
+     * 通过用户名查询用户、角色信息
+     *
+     * @param username 用户名
+     * @param from     调用标志
+     * @return R
+     */
+    @GetMapping("/user/info/inner")
+    R<UserInfo> info1(@RequestParam("username") String username,
+                     @RequestParam("appLogin") Boolean isApp,
+                     @RequestHeader(SecurityConstants.FROM) String from);
+
+
+    /**
+     * 通过社交账号或手机号查询用户、角色信息
+     *
+     * @param inStr appid@code
+     * @param from  调用标志
+     * @return R<UserInfo>
+     */
+    @GetMapping("/social/info/{inStr}")
+    R<UserInfo> social(@PathVariable("inStr") String inStr, @RequestHeader(SecurityConstants.FROM) String from);
+
+    /**
+     * 查询上级部门的用户信息
+     *
+     * @param username 用户名
+     * @return R
+     */
+    @GetMapping("/user/ancestor/{username}")
+    R<List<SysUser>> ancestorUsers(@PathVariable("username") String username);
 
 }

+ 68 - 1
hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/controller/SysUserController.java

@@ -37,6 +37,7 @@ import com.qunzhixinxi.hnqz.admin.service.SysUserAreaService;
 import com.qunzhixinxi.hnqz.admin.service.SysUserService;
 import com.qunzhixinxi.hnqz.admin.service.WmPlatformQuizTestResultService;
 import com.qunzhixinxi.hnqz.common.core.constant.CommonConstants;
+import com.qunzhixinxi.hnqz.common.core.constant.SecurityConstants;
 import com.qunzhixinxi.hnqz.common.core.entity.BaseEntity;
 import com.qunzhixinxi.hnqz.common.core.util.R;
 import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
@@ -56,6 +57,7 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
@@ -198,6 +200,71 @@ public class SysUserController {
         return R.failed(null, String.format("用户信息为空 %s", username));
     }
 
+    /**
+     * 获取指定用户全部信息
+     *
+     * @return 用户信息
+     */
+    @Inner
+    @GetMapping("/info/inner")
+    R<UserInfo> info1(@RequestParam("username") String username,
+                      @RequestParam("appLogin") Boolean isApp) {
+
+        // 根据用户名查询用户时,过滤掉已删除和已禁用的用户
+        List<SysUser> users = userService.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username)
+                .eq(SysUser::getDelFlag, 0).eq(SysUser::getLockFlag, 0));
+
+        if (CollUtil.isEmpty(users)) {
+            return R.failed(null, String.format("用户信息为空 %s", username));
+        }
+
+        // 移动端登录
+        if (isApp) {
+            return appUserInfo(users);
+        }
+        // pc端登录
+        else {
+            return webUserInfo(username, users);
+        }
+    }
+
+    // pc端登录
+    private R<UserInfo> webUserInfo(String username, List<SysUser> users) {
+        // 单一账号情况
+        if (users.size() == 1) {
+            SysUser sysUser = users.get(0);
+            UserInfo userInfo = userService.findUserInfo(sysUser);
+            // 校验用户角色是否包含众包用户和医药代表用户, 兼职和全职不能登录pc
+            return isZbOrDbUsers(userInfo.getRoles()) ? R.ok(userInfo) : R.failed(null, String.format("用户信息错误 %s", username));
+        }
+        // 多账号情况
+        else {
+            String password = (String) redisTemplate.opsForValue().get(username);
+            if (StrUtil.isNotBlank(password)) {
+                for (SysUser sysUser : users) {
+                    UserInfo userInfo = userService.findUserInfo(sysUser);
+                    if (Md5Utils.getMD5(password.getBytes()).equals(sysUser.getW1())) {
+                        if (isZbOrDbUsers(userInfo.getRoles())) {
+                            redisTemplate.delete(username);
+                            return R.ok(userInfo);
+                        }
+                    }
+                }
+            }
+        }
+        return R.failed(null, String.format("用户信息为空 %s", username));
+
+    }
+
+    // 移动端登录:管理员和业务员均可登录
+    private R<UserInfo> appUserInfo(List<SysUser> users) {
+
+        SysUser sysUser = users.get(0);
+        UserInfo userInfo = userService.findUserInfo(sysUser);
+        return R.ok(userInfo);
+
+    }
+
     /**
      * 通过ID查询用户信息
      *
@@ -1157,7 +1224,7 @@ public class SysUserController {
             userAreaTree = areaEntityService.selectTree(new SysAreaEntity());
         }
         //  药企管理员(事业部) || 区域管理员(一级cso管理员) || etc,.
-        else if (roles.contains(3) || roles.contains(4) || roles.contains(39)|| roles.contains(40)|| roles.contains(41)|| roles.contains(42) || roles.contains(43)) {
+        else if (roles.contains(3) || roles.contains(4) || roles.contains(39) || roles.contains(40) || roles.contains(41) || roles.contains(42) || roles.contains(43)) {
             userAreaTree = userService.getUserAreaTree(Long.valueOf(user.getId()));
         }
         // 服务商管理员(二级cso管理员)