Browse Source

权限 未完成

shiqian 3 years ago
parent
commit
90e6f470ba

+ 1 - 1
boman-api/boman-api-system/src/main/java/com/boman/system/api/RemoteUserService.java

@@ -68,6 +68,6 @@ public interface RemoteUserService
     @GetMapping("user/getByPhone/{phone}")
     SysUser getByPhone(@PathVariable("phone") String phone);
 
-    @GetMapping("user/packInfo")
+    @PostMapping("user/packInfo")
     LoginUser packInfo(@RequestBody SysUser sysUser);
 }

+ 15 - 1
boman-web-core/src/main/java/com/boman/web/core/service/vaccineInfo/impl/VaccineInfoServiceImpl.java

@@ -13,13 +13,16 @@ import com.alibaba.fastjson.JSONObject;
 import com.boman.common.core.utils.DateUtils;
 import com.boman.common.core.utils.SecurityUtils;
 import com.boman.common.core.utils.StringUtils;
+import com.boman.domain.SysDept;
 import com.boman.domain.SysDictData;
+import com.boman.system.api.RemoteDeptService;
 import com.boman.web.core.domain.VaccineInfoOperation;
 import com.boman.web.core.domain.VaccineInfoUser;
 import com.boman.web.core.mapper.VaccineInfoOperationMapper;
 import com.boman.web.core.mapper.VaccineInfoUserMapper;
 import com.boman.web.core.service.vaccineInfo.IVaccineInfoService;
 
+import com.boman.web.core.utils.AuthUtils;
 import io.swagger.models.auth.In;
 import org.springframework.stereotype.Service;
 import com.boman.web.core.mapper.VaccineInfoMapper;
@@ -47,6 +50,8 @@ public class VaccineInfoServiceImpl implements IVaccineInfoService {
     private VaccineInfoOperationMapper vaccineInfoOperationMapper;
     @Resource
     private VaccineInfoUserMapper vaccineInfoUserMapper;
+    @Resource
+    private RemoteDeptService remoteDeptService;
 
     /**
      * 查询疫苗信息
@@ -109,7 +114,8 @@ public class VaccineInfoServiceImpl implements IVaccineInfoService {
 
         //如果查询条件查询疫苗表且没数据,就不要查询主表
         List<VaccineInfoOperation> vaccineInfoOperations = new ArrayList<>();
-        if (flag) {
+        if (flag){
+            setAddrCondition(vaccineInfoOperation);
             vaccineInfoOperations = vaccineInfoMapper.selectVaccineInfoList(vaccineInfoOperation);
         }
 
@@ -161,6 +167,14 @@ public class VaccineInfoServiceImpl implements IVaccineInfoService {
         return vaccineInfoOperations;
     }
 
+    private void setAddrCondition(VaccineInfoOperation vaccineInfoOperation) {
+        // 当前登陆人所在部门,有可能是县,有可能是镇,有可能是村
+        Long deptId = AuthUtils.getLoginUser().getSysUser().getDeptId();
+        SysDept dept = remoteDeptService.getById(deptId);
+        String deptName = dept.getDeptName();
+        vaccineInfoOperation.setVillageTowns(deptName);
+    }
+
     private Date vaccinationTimeNext(Date vaccinationTime, String vaccineNameLast) {
         Date date = new Date();
         if ("北京科兴中维".equals(vaccineNameLast)) {

+ 4 - 1
boman-web-core/src/main/resources/mapper/VaccineInfoMapper.xml

@@ -79,7 +79,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         vi.is_del,vi.code,vi.birthday,vi.work_unit,vi.crowd_classification,vi.manufacturer,vi.now_in,vi.url from vaccine_info vi
         <where>
             vi.is_del = 'N'
-            <if test="villageTowns != null  and villageTowns != ''"> and vi.village_towns = #{villageTowns}</if>
+            <if test="villageTowns != null  and villageTowns != ''">
+                and (vi.village_towns = #{villageTowns} or vi.city = #{villageTowns} or vi.region = #{villageTowns})
+            </if>
+
             <if test="village != null  and village != ''"> and vi.village = #{village}</if>
             <if test="villagerGroup != null  and villagerGroup != ''"> and vi.villager_group = #{villagerGroup}</if>
             <if test="houseType != null  and houseType != ''"> and vi.house_type = #{houseType}</if>

+ 108 - 3
boman-wechat/src/main/java/com/boman/wechat/controller/AppletLoginController.java

@@ -1,17 +1,38 @@
 package com.boman.wechat.controller;
 
-import com.boman.common.security.service.TokenService;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.boman.common.core.enums.UserStatus;
+import com.boman.common.core.exception.BaseException;
+import com.boman.common.core.utils.IdUtils;
+import com.boman.common.core.utils.ServletUtils;
+import com.boman.common.core.utils.ip.IpUtils;
+import com.boman.common.core.utils.obj.ObjectUtils;
+import com.boman.common.redis.service.RedisService;
 import com.boman.domain.AppletLoginForm;
+import com.boman.domain.SysDept;
+import com.boman.domain.SysUser;
+import com.boman.domain.constant.CacheConstants;
+import com.boman.domain.constant.Constants;
+import com.boman.domain.constant.UserEnvConstant;
 import com.boman.domain.dto.AppletSessionDTO;
 import com.boman.domain.dto.R;
+import com.boman.system.api.RemoteDeptService;
+import com.boman.system.api.RemoteLogService;
+import com.boman.system.api.RemoteUserService;
+import com.boman.system.api.model.LoginUser;
 import com.boman.wechat.utils.WxCodeSessionUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
+import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author shiqian
@@ -21,15 +42,99 @@ import java.util.Map;
 @RequestMapping("applet")
 public class AppletLoginController {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(AppletLoginController.class);
+
+    private final static long EXPIRE_TIME = Constants.TOKEN_EXPIRE * 60;
+
+    private final static long EXPIRE_TIME_QR = 3 * 60;
+
+    private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY;
+
+    protected static final long MILLIS_SECOND = 1000;
+
     @Resource
     private WxCodeSessionUtil codeUtil;
     @Resource
-    private TokenService tokenService;
+    private RedisService redisService;
+    @Resource
+    private RemoteUserService remoteUserService;
+    @Resource
+    private RemoteLogService remoteLogService;
+    @Resource
+    private RemoteDeptService remoteDeptService;
 
     @PostMapping("/login")
     public R<Map<String, Object>> getPhone(@RequestBody AppletLoginForm form) {
         AppletSessionDTO dto = codeUtil.jscode2Session(form);
-        return tokenService.appletLogin(dto.getPhoneNumber());
+        SysUser user = remoteUserService.getByPhone(dto.getPhoneNumber());
+        String userName = user.getUserName();
+        if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
+            remoteLogService.saveLogininfor(userName, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
+            throw new BaseException("对不起,您的账号:" + userName + " 已被删除");
+        }
+
+        if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
+            remoteLogService.saveLogininfor(userName, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
+            throw new BaseException("对不起,您的账号:" + userName + " 已停用");
+        }
+
+        remoteLogService.saveLogininfor(userName, Constants.LOGIN_SUCCESS, "登录成功");
+        LOGGER.info("appletLogin, remoteUserService: {}", remoteUserService);
+        LoginUser loginUser = remoteUserService.packInfo(user);
+        LOGGER.info("appletLogin, loginUser: {}", JSON.toJSONString(loginUser));
+        return R.ok(createToken(loginUser));
+    }
+
+    public Map<String, Object> createToken(LoginUser loginUser) {
+        // 生成token
+        String token = IdUtils.fastUUID();
+        loginUser.setToken(token);
+        loginUser.setUserid(loginUser.getSysUser().getId());
+        loginUser.setUsername(loginUser.getSysUser().getUserName());
+        loginUser.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
+
+        JSONObject userEnv = packUserEnv(loginUser);
+        loginUser.setUserEnv(userEnv);
+
+        refreshToken(loginUser);
+
+        // 保存或更新用户token
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("access_token", token);
+        map.put("expires_in", EXPIRE_TIME);
+        redisService.setCacheObject(ACCESS_TOKEN + token, loginUser, EXPIRE_TIME, TimeUnit.SECONDS);
+        return map;
+    }
+
+    public void refreshToken(LoginUser loginUser) {
+        loginUser.setLoginTime(System.currentTimeMillis());
+        loginUser.setExpireTime(loginUser.getLoginTime() + EXPIRE_TIME * MILLIS_SECOND);
+        // 根据uuid将loginUser缓存
+        String userKey = getTokenKey(loginUser.getToken());
+        redisService.setCacheObject(userKey, loginUser, EXPIRE_TIME, TimeUnit.SECONDS);
+    }
+
+    private String getTokenKey(String token) {
+        return ACCESS_TOKEN + token;
+    }
+
+    private JSONObject packUserEnv(LoginUser loginUser) {
+        JSONObject userEnv = new JSONObject();
+        userEnv.put(UserEnvConstant.USER_ID, loginUser.getUserid());
+        userEnv.put(UserEnvConstant.USERNAME, loginUser.getUsername());
+
+        SysDept dept = loginUser.getSysUser().getDept();
+        userEnv.put(UserEnvConstant.USER_DEPT_ID, loginUser.getSysUser().getDeptId());
+        userEnv.put(UserEnvConstant.USER_DEPT_NAME, dept.getDeptName());
+
+        Long parentId = dept.getParentId();
+        SysDept parentDept = remoteDeptService.getById(parentId);
+        if (ObjectUtils.isNotEmpty(parentDept)) {
+            userEnv.put(UserEnvConstant.USER_PARENT_DEPT_ID, parentDept.getId());
+            userEnv.put(UserEnvConstant.USER_PARENT_DEPT_NAME, parentDept.getDeptName());
+        }
+
+        return userEnv;
     }
 
 }