| | |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.sinata.common.constant.Constants; |
| | | import com.sinata.common.core.domain.AjaxResult; |
| | | import com.sinata.common.core.domain.R; |
| | |
| | | import com.sinata.common.core.domain.entity.SysUser; |
| | | import com.sinata.common.core.domain.model.LoginBody; |
| | | import com.sinata.common.core.domain.model.LoginUser; |
| | | import com.sinata.common.core.redis.RedisCache; |
| | | import com.sinata.common.utils.SecurityUtils; |
| | | import com.sinata.common.utils.StringUtils; |
| | | import com.sinata.framework.web.service.SysLoginService; |
| | | import com.sinata.framework.web.service.SysPermissionService; |
| | | import com.sinata.framework.web.service.TokenService; |
| | | import com.sinata.system.domain.SysDepartment; |
| | | import com.sinata.system.domain.SysUserRole; |
| | | import com.sinata.system.domain.dto.UserInfoDTO; |
| | | import com.sinata.system.mapper.SysUserRoleMapper; |
| | | import com.sinata.system.service.ISysMenuService; |
| | | import com.sinata.system.service.ISysRoleService; |
| | | import com.sinata.system.service.ISysUserService; |
| | | import com.sinata.system.service.SysDepartmentService; |
| | | import com.sinata.system.service.biz.AliSmsService; |
| | | import com.sinata.web.controller.tool.weChat.WXCore; |
| | |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | 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.time.LocalDateTime; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Random; |
| | |
| | | private SysDepartmentService departmentService; |
| | | @Resource |
| | | private AliSmsService aliSmsService; |
| | | @Resource |
| | | private ISysUserService userService; |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | | |
| | | |
| | | @PostMapping("/getCode") |
| | |
| | | public AjaxResult getCode(String phone) |
| | | { |
| | | Random random = new Random(); |
| | | |
| | | List<SysUser> list = userService.lambdaQuery().eq(SysUser::getUserName, phone).list(); |
| | | if (list.isEmpty()){ |
| | | return AjaxResult.error("当前手机号没有对应用户,请联系管理员"); |
| | | } |
| | | int i = 100000 + random.nextInt(900000); |
| | | aliSmsService.sendLoginCode(phone, String.valueOf(i)); |
| | | redisCache.setCacheObject(String.valueOf(phone), String.valueOf(i)); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | | @PostMapping("/set/getCode") |
| | | @ApiOperation("获取验证码") |
| | | public AjaxResult setgetCode(String phone) |
| | | { |
| | | Random random = new Random(); |
| | | int i = 100000 + random.nextInt(900000); |
| | | aliSmsService.sendLoginCode(phone, String.valueOf(i)); |
| | | redisCache.setCacheObject(String.valueOf(phone), String.valueOf(i)); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | | @Resource |
| | | private WeChatUtil weChatUtil; |
| | | |
| | |
| | | String openid = map.get("openid").toString(); |
| | | String sessionKey = map.get("sessionKey").toString(); |
| | | |
| | | |
| | | String decrypt = WXCore.decrypt(EncryptedData_phone, sessionKey, Iv_phone); |
| | | if (StringUtils.isEmpty(decrypt)) { |
| | | return R.fail("获取手机信息失败"); |
| | | } |
| | | JSONObject phone = JSON.parseObject(decrypt); |
| | | String purePhoneNumber = phone.getString("purePhoneNumber"); |
| | | return R.ok(purePhoneNumber); |
| | | |
| | | // 查询手机号对应的用户 |
| | | SysUser user = userService.lambdaQuery() |
| | | .eq(SysUser::getUserName, purePhoneNumber) |
| | | .one(); |
| | | if(user == null) { |
| | | return R.fail("该手机号未注册"); |
| | | } |
| | | |
| | | // 创建LoginUser对象 |
| | | LoginUser loginUser = new LoginUser(); |
| | | loginUser.setUserId(user.getUserId()); |
| | | loginUser.setUser(user); |
| | | |
| | | // 生成token |
| | | String token = tokenService.createToken(loginUser); |
| | | |
| | | JSONObject result = new JSONObject(); |
| | | result.put("phone", purePhoneNumber); |
| | | result.put(Constants.TOKEN, token); |
| | | |
| | | return R.ok(result); |
| | | } |
| | | /** |
| | | * 登录方法 |
| | |
| | | * @param loginBody 登录信息 |
| | | * @return 结果 |
| | | */ |
| | | // @PostMapping("/login") |
| | | // @ApiOperation("验证码登录") |
| | | // public AjaxResult login(@RequestBody LoginBody loginBody) |
| | | // { |
| | | // AjaxResult ajax = AjaxResult.success(); |
| | | // // 生成令牌 |
| | | // LoginUser loginUser = loginService.login1(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), |
| | | // loginBody.getUuid()); |
| | | // ajax.put(Constants.TOKEN, tokenService.createToken(loginUser)); |
| | | // // List<SysRole> roles = loginUser.getUser().getRoles(); |
| | | // // if (CollectionUtils.isEmpty(roles)) { |
| | | // // return AjaxResult.error("请关联角色!"); |
| | | // // } |
| | | |
| | | // List<SysMenu> menus = roleService.roleInfoFromUserId(loginUser.getUserId()); |
| | | // String string = redisCache.getCacheObject(loginBody.getUsername()).toString(); |
| | | // if (StringUtils.isBlank(string)) { |
| | | // return AjaxResult.error("验证码已失效"); |
| | | // } |
| | | // if (!loginBody.getCode().equals(string)){ |
| | | // return AjaxResult.error("验证码错误,请重试"); |
| | | // } |
| | | // // ajax.put("menus", menus); |
| | | // ajax.put("userInfo", loginUser); |
| | | // return ajax; |
| | | // } |
| | | |
| | | @PostMapping("/login") |
| | | @ApiOperation("验证码登录") |
| | | @ApiOperation("管理后台登录") |
| | | public AjaxResult login(@RequestBody LoginBody loginBody) |
| | | { |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | // 生成令牌 |
| | | LoginUser loginUser = loginService.login1(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), |
| | | LoginUser loginUser = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), |
| | | loginBody.getUuid()); |
| | | ajax.put(Constants.TOKEN, tokenService.createToken(loginUser)); |
| | | // List<SysRole> roles = loginUser.getUser().getRoles(); |
| | | // if (CollectionUtils.isEmpty(roles)) { |
| | | // return AjaxResult.error("请关联角色!"); |
| | | // } |
| | | |
| | | List<SysRole> roles = loginUser.getUser().getRoles(); |
| | | if (CollectionUtils.isEmpty(roles)) { |
| | | return AjaxResult.error("请关联角色!"); |
| | | } |
| | | List<SysMenu> menus = roleService.roleInfoFromUserId(loginUser.getUserId()); |
| | | |
| | | // ajax.put("menus", menus); |
| | | Set<Long> permissions = roleService.getMenuIdSets(loginUser.getUserId()); |
| | | ajax.put("menus", menus); |
| | | ajax.put("userInfo", loginUser); |
| | | ajax.put("permissions", permissions); |
| | | return ajax; |
| | | } |
| | | |
| | | |
| | | @PostMapping("/get/department") |
| | | @ApiOperation("登录后获取部门") |
| | |
| | | SysDepartment byId = departmentService.getById(user.getDepartmentId()); |
| | | return R.ok(byId); |
| | | } |
| | | @Resource |
| | | private SysUserRoleMapper sysUserRoleMapper; |
| | | @PostMapping("/get/user") |
| | | @ApiOperation("获取个人信息") |
| | | public R<UserInfoDTO> user() |
| | | { |
| | | SysUser user = SecurityUtils.getLoginUser().getUser(); |
| | | SysUser user1 = SecurityUtils.getLoginUser().getUser(); |
| | | SysUser user = userService.getById(user1.getUserId()); |
| | | Set<String> roles = permissionService.getRolePermission(user); |
| | | SysDepartment byId2 = departmentService.getById(user.getDepartmentId()); |
| | | UserInfoDTO userInfoDTO = new UserInfoDTO(); |
| | | userInfoDTO.setNickName(user.getNickName()); |
| | | userInfoDTO.setDepartment(byId2); |
| | | userInfoDTO.setRoles(roles); |
| | | SysUserRole sysUserRole = sysUserRoleMapper.selectOne(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getUserId, user.getUserId())); |
| | | if (sysUserRole != null) { |
| | | SysRole byId = roleService.getById(sysUserRole.getRoleId()); |
| | | userInfoDTO.setRole(byId); |
| | | } |
| | | return R.ok(userInfoDTO); |
| | | } |
| | | |