Bladeren bron

添加统一验证

JensionDzero 1 jaar geleden
bovenliggende
commit
414726a0b6

+ 11 - 11
pom.xml

@@ -77,17 +77,17 @@
                 <activeByDefault>true</activeByDefault>
             </activation>
         </profile>
-        <profile>
-            <id>prod</id>
-            <properties>
-                <profiles.active>prod</profiles.active>
-                <logging.level>warn</logging.level>
-            </properties>
-            <activation>
-                <!-- 默认环境 -->
-                <activeByDefault>false</activeByDefault>
-            </activation>
-        </profile>
+<!--        <profile>-->
+<!--            <id>prod</id>-->
+<!--            <properties>-->
+<!--                <profiles.active>prod</profiles.active>-->
+<!--                <logging.level>warn</logging.level>-->
+<!--            </properties>-->
+<!--            <activation>-->
+<!--                &lt;!&ndash; 默认环境 &ndash;&gt;-->
+<!--                <activeByDefault>false</activeByDefault>-->
+<!--            </activation>-->
+<!--        </profile>-->
     </profiles>
 
     <!-- 依赖声明 -->

+ 74 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/benyun/IUserinfoController.java

@@ -0,0 +1,74 @@
+package com.ruoyi.web.controller.benyun;
+
+import cn.dev33.satoken.annotation.SaIgnore;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.system.domain.vo.OpenAccountVO;
+import com.ruoyi.system.service.AccountService;
+import com.ruoyi.system.service.IUserinfoService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.ArrayList;
+
+@RestController
+@RequiredArgsConstructor
+public class IUserinfoController {
+    @Autowired
+    private IUserinfoService iUserinfoService;
+
+    @Autowired
+    private AccountService accountService;
+
+
+    /**
+     * 创建管理员
+     * @param openAccountVO
+     * @return
+     */
+    @PostMapping("/createAdmin")
+    public R createAdmin(@Validated @RequestBody OpenAccountVO openAccountVO) {
+        ArrayList<Integer> integers = new ArrayList<>();
+        integers.add(1); //管理员
+        openAccountVO.setRoles(integers);
+        R admin = iUserinfoService.createAdmin(openAccountVO);
+        return admin;
+    }
+
+    /**
+     * 创建用户
+     * @param openAccountVO
+     * @return
+     */
+    @PostMapping("/createUser")
+    public R createUser(@Validated @RequestBody OpenAccountVO openAccountVO) {
+        ArrayList<Integer> integers = new ArrayList<>();
+        integers.add(2); //管理员
+        openAccountVO.setRoles(integers);
+        R user = iUserinfoService.createUser(openAccountVO);
+        return user;
+    }
+
+
+    @PostMapping("/disableUser")
+    public R disableUser(@Validated @RequestBody OpenAccountVO openAccountVO) {
+        iUserinfoService.disableUser(openAccountVO);
+        return R.ok();
+    }
+
+
+    /**
+     * uId免密登录
+     * @Author Yyz
+     * @Date 2020/12/1 13:58
+     */
+    @SaIgnore
+    @PostMapping("/loginByUId")
+    public R login(String uId)  {
+        return R.ok(accountService.login(uId));
+    }
+
+}

+ 0 - 18
ruoyi-admin/src/main/java/com/ruoyi/web/controller/benyun/TestController.java

@@ -1,18 +0,0 @@
-package com.ruoyi.web.controller.benyun;
-
-
-import com.ruoyi.common.core.domain.R;
-import lombok.RequiredArgsConstructor;
-import org.springframework.web.bind.annotation.RequestMapping;
-
-@RequiredArgsConstructor
-@RequestMapping("/test")
-public class TestController {
-
-
-    @RequestMapping("/test2")
-    public R test2(){
-        return R.ok("test2");
-    }
-
-}

+ 191 - 0
ruoyi-system/src/main/java/com/ruoyi/system/domain/OpenAccount.java

@@ -0,0 +1,191 @@
+package com.ruoyi.system.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.ruoyi.common.annotation.Sensitive;
+import com.ruoyi.common.constant.UserConstants;
+import com.ruoyi.common.core.domain.entity.SysDept;
+import com.ruoyi.common.core.domain.entity.SysRole;
+import com.ruoyi.common.enums.SensitiveStrategy;
+import com.ruoyi.system.domain.vo.OpenAccountVO;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.validation.constraints.Email;
+import javax.validation.constraints.Size;
+import java.util.Date;
+import java.util.List;
+
+@Data
+@NoArgsConstructor
+@TableName("sys_user")
+public class OpenAccount {
+    /**
+     * 统一用户id
+     */
+    @TableField("u_id")
+    private Long uId;
+
+    /**
+     * 用户账号
+     */
+    @TableField("user_name")
+    private String userName;
+
+    /**
+     * 用户Id
+     */
+    @TableId(value = "user_id",type = IdType.ASSIGN_ID)
+    private Long userId;
+
+    /**
+     * 用户昵称
+     */
+    @TableField("nick_name")
+    private String nickName;
+
+    /**
+     *
+     */
+
+    /**
+     * 手机号码
+     */
+
+    @TableField("phonenumber")
+    private String phonenumber;
+
+    /**
+     * 帐号状态(0正常 1停用)
+     */
+    @TableField("status")
+    private String status;
+    /**
+     * 删除标志(0代表存在 2代表删除)
+     */
+    @TableField("del_flag")
+    private String delFlag;
+
+
+    /**
+     * 创建人uId
+     */
+    @TableField("create_by")
+    private String createBy;
+
+    /**
+     * 密码
+     */
+    @TableField("password")
+    private String password;
+
+
+    /**
+     * 部门ID
+     */
+    private Long deptId;
+
+
+
+    /**
+     * 用户类型(sys_user系统用户)
+     */
+    private String userType;
+
+    /**
+     * 用户邮箱
+     */
+    @Sensitive(strategy = SensitiveStrategy.EMAIL)
+    @Email(message = "邮箱格式不正确")
+    @Size(min = 0, max = 50, message = "邮箱长度不能超过{max}个字符")
+    private String email;
+
+
+    /**
+     * 用户性别
+     */
+    private String sex;
+
+    /**
+     * 用户头像
+     */
+    private String avatar;
+
+
+
+    @JsonIgnore
+    @JsonProperty
+    public String getPassword() {
+        return password;
+    }
+
+
+
+    /**
+     * 最后登录IP
+     */
+    private String loginIp;
+
+    /**
+     * 最后登录时间
+     */
+    private Date loginDate;
+
+    /**
+     * 备注
+     */
+    private String remark;
+
+    /**
+     * 部门对象
+     */
+    @TableField(exist = false)
+    private SysDept dept;
+
+    /**
+     * 角色对象
+     */
+    @TableField(exist = false)
+    private List<SysRole> roles;
+
+    /**
+     * 角色组
+     */
+    @TableField(exist = false)
+    private Long[] roleIds;
+
+    /**
+     * 岗位组
+     */
+    @TableField(exist = false)
+    private Long[] postIds;
+
+    /**
+     * 数据权限 当前角色ID
+     */
+    @TableField(exist = false)
+    private Long roleId;
+
+    public OpenAccount(Long userId) {
+        this.userId = userId;
+    }
+
+    public boolean isAdmin() {
+        return UserConstants.ADMIN_ID.equals(this.userId);
+    }
+
+    public OpenAccount(OpenAccountVO openAccountVO) {
+        this.uId = openAccountVO.getU_id();
+        this.userName = openAccountVO.getUserName();
+        this.nickName = openAccountVO.getNickName();
+        this.phonenumber = openAccountVO.getPhonenumber();
+        this.status = openAccountVO.getStatus();
+        this.delFlag = openAccountVO.getDelFlag();
+        this.createBy = openAccountVO.getCreateBy();
+        this.password = openAccountVO.getPassword();
+    }
+}

+ 67 - 0
ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/OpenAccountVO.java

@@ -0,0 +1,67 @@
+package com.ruoyi.system.domain.vo;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+import java.io.Serializable;
+import java.util.List;
+
+@Data
+public class OpenAccountVO implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 统一用户id(uId不懂为什么用不了)
+     */
+//    @NotBlank(message = "uId不能为空")
+//    private Long uId;
+
+    @NotNull(message = "u_id不能为空")
+    private Long u_id;
+
+    /**
+     * 用户账号
+     */
+    @NotBlank(message = "用户账号不能为空")
+    @Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符")
+    private String userName;
+
+    /**
+     * 用户昵称
+     */
+    @NotBlank(message = "用户昵称不能为空")
+    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
+    private String nickName;
+
+    /**
+     * 手机号码
+     */
+    private String phonenumber;
+
+    /**
+     * 帐号状态(0正常 1停用)
+     */
+    private String status;
+    /**
+     * 删除标志(0代表存在 2代表删除)
+     */
+    private String delFlag;
+
+    /**
+     * 创建人uId
+     */
+    private String createBy;
+
+    /**
+     * 密码
+     */
+    @NotBlank(message = "密码不能为空")
+    private String password;
+    /**
+     * 角色列表
+     */
+    private List<Integer> roles;
+}

+ 9 - 0
ruoyi-system/src/main/java/com/ruoyi/system/mapper/IUserinfoMapper.java

@@ -0,0 +1,9 @@
+package com.ruoyi.system.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.ruoyi.system.domain.OpenAccount;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface IUserinfoMapper extends BaseMapper<OpenAccount> {
+}

+ 5 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/AccountService.java

@@ -0,0 +1,5 @@
+package com.ruoyi.system.service;
+
+public interface AccountService {
+    String login(String uId);
+}

+ 10 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/IUserinfoService.java

@@ -0,0 +1,10 @@
+package com.ruoyi.system.service;
+
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.system.domain.vo.OpenAccountVO;
+
+public interface IUserinfoService {
+    R createAdmin(OpenAccountVO openAccountVO);
+    R createUser(OpenAccountVO openAccountVO);
+    void disableUser(OpenAccountVO openAccountVO);
+}

+ 13 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/SysLoginService.java

@@ -331,4 +331,17 @@ public class SysLoginService {
         // 登录成功 清空错误次数
         RedisUtils.deleteObject(errorKey);
     }
+
+    public String UIdLogin(String username) {
+        // 框架登录不限制从什么表查询 只要最终构建出 LoginUser 即可
+        SysUser user = loadUserByUsername(username);
+        // 此处可根据登录用户的数据不同 自行创建 loginUser 属性不够用继承扩展就行了
+        LoginUser loginUser = buildLoginUser(user);
+        // 生成token
+        LoginHelper.loginByDevice(loginUser, DeviceType.PC);
+
+        recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
+        recordLoginInfo(user.getUserId(), username);
+        return StpUtil.getTokenValue();
+    }
 }

+ 38 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AccountServiceImpl.java

@@ -0,0 +1,38 @@
+package com.ruoyi.system.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.ruoyi.system.domain.OpenAccount;
+import com.ruoyi.system.mapper.IUserinfoMapper;
+import com.ruoyi.system.mapper.SysUserMapper;
+import com.ruoyi.system.service.AccountService;
+import com.ruoyi.system.service.SysLoginService;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@RequiredArgsConstructor
+@Service
+@Slf4j
+public class AccountServiceImpl implements AccountService {
+
+    @Autowired
+    IUserinfoMapper iUserinfoMapper;
+
+
+    private final SysLoginService sysLoginService;
+
+    @Override
+    public String login(String uId) {
+        QueryWrapper<OpenAccount> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("u_id",uId);
+        OpenAccount openAccount = iUserinfoMapper.selectOne(queryWrapper);
+
+        if (openAccount != null){
+            //登录
+            String token = sysLoginService.UIdLogin(openAccount.getUserName());
+            return token;
+        }
+        return null;
+    }
+}

+ 97 - 0
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/IUserinfoServiceImpl.java

@@ -0,0 +1,97 @@
+package com.ruoyi.system.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.system.domain.OpenAccount;
+import com.ruoyi.system.domain.SysUserRole;
+import com.ruoyi.system.domain.vo.OpenAccountVO;
+import com.ruoyi.system.mapper.IUserinfoMapper;
+import com.ruoyi.system.mapper.SysUserRoleMapper;
+import com.ruoyi.system.service.IUserinfoService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+
+@Service
+@RequiredArgsConstructor
+@Transactional
+public class IUserinfoServiceImpl implements IUserinfoService {
+    @Autowired
+    IUserinfoMapper iUserinfoMapper;
+    private final SysUserRoleMapper userRoleMapper;
+
+
+    @Override
+    public R createAdmin(OpenAccountVO openAccountVO) {
+        //检查用户名称、Uid是否重复
+        String s = checkUIdAndUserName(openAccountVO);
+        if (s == null){
+            //注册用户
+            OpenAccount openAccount = new OpenAccount(openAccountVO);
+            iUserinfoMapper.insert(openAccount);
+            //设置角色
+            for (Integer role : openAccountVO.getRoles()) {
+                SysUserRole sysUserRole = new SysUserRole();
+                sysUserRole.setUserId(openAccount.getUserId());
+                sysUserRole.setRoleId(Long.valueOf(role));
+                userRoleMapper.insert(sysUserRole);
+            }
+            return R.ok();
+        }
+        return R.fail(s);
+    }
+
+    @Override
+    public R createUser(OpenAccountVO openAccountVO) {
+        //检查用户名称、Uid是否重复
+        String s = checkUIdAndUserName(openAccountVO);
+        if (s == null){
+            //注册用户
+            OpenAccount openAccount = new OpenAccount(openAccountVO);
+            iUserinfoMapper.insert(openAccount);
+            //设置角色
+            for (Integer role : openAccountVO.getRoles()) {
+                SysUserRole sysUserRole = new SysUserRole();
+                sysUserRole.setUserId(openAccount.getUserId());
+                sysUserRole.setRoleId(Long.valueOf(role));
+                userRoleMapper.insert(sysUserRole);
+            }
+            return R.ok();
+        }
+        return R.fail(s);
+    }
+
+    @Override
+    public void disableUser(OpenAccountVO openAccountVO) {
+        UpdateWrapper<OpenAccount> updateWrapper = new UpdateWrapper<>();
+        updateWrapper.eq("u_id",openAccountVO.getU_id());
+        updateWrapper.set("delFlag",2);
+        iUserinfoMapper.update(null,updateWrapper);
+    }
+
+    /**
+     * 判断用户是否存在
+     * */
+    public String checkUIdAndUserName(OpenAccountVO openAccountVO){
+        //判断u_id 和 用户名称是否存在
+        QueryWrapper<OpenAccount> queryWrapper = new QueryWrapper<>();
+        queryWrapper.select("u_id","user_name");
+        queryWrapper.eq("u_id",openAccountVO.getU_id());
+        queryWrapper.or();
+        queryWrapper.eq("user_name",openAccountVO.getUserName());
+        List<OpenAccount> openAccounts = iUserinfoMapper.selectList(queryWrapper);
+        if (!openAccounts.isEmpty()){
+            if (openAccounts.size() == 2)
+                return "u_id、userName已存在";
+            else if (openAccounts.get(0).getUId() == openAccountVO.getU_id())
+                return "u_id已存在";
+            else
+                return "userName已存在";
+        }
+        return null;
+    }
+}