From 82e881ce1bfcecf803e5590f3a0c7f09484d3bf1 Mon Sep 17 00:00:00 2001 From: 18650502300 <18650502300@163.com> Date: Thu, 18 Apr 2024 13:38:51 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=BF=AB=E9=80=9F=E7=99=BB=E5=BD=95=202.?= =?UTF-8?q?=20=E8=A7=A3=E7=BB=91=E8=AE=BE=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/SysLoginController.java | 17 +++++++- .../common/core/domain/entity/AsUser.java | 13 +++++- .../framework/config/SecurityConfig.java | 2 +- .../web/service/SysLoginService.java | 35 +++++++++++++++- .../com/ruoyi/device/app/AppController.java | 11 ++--- .../com/ruoyi/device/mapper/AsUserMapper.java | 28 +++++++------ .../ruoyi/device/service/IAsUserService.java | 8 ++++ .../service/impl/AsModelServiceImpl.java | 10 ++++- .../service/impl/AsUserServiceImpl.java | 11 +++++ .../mapper/device/AsDeviceMapper.xml | 4 +- .../resources/mapper/user/AsUserMapper.xml | 41 +++++++++++-------- 11 files changed, 136 insertions(+), 44 deletions(-) diff --git a/AutoSprout-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java b/AutoSprout-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java index 5abe88d..c772655 100644 --- a/AutoSprout-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java +++ b/AutoSprout-admin/src/main/java/com/ruoyi/web/controller/system/SysLoginController.java @@ -132,10 +132,23 @@ public class SysLoginController * 微信登录 */ @PostMapping("/wxlogin") - public AjaxResult wxlogin(String mobileCode) { + public AjaxResult wxlogin(String mobileCode,String openid) { AjaxResult ajax = AjaxResult.success(); // 生成令牌 - String token = loginService.wxloing(mobileCode); + String token = loginService.wxloing(mobileCode,openid); + ajax.put(Constants.TOKEN, token); + return ajax; + } + + + /** + * 根据openid静默登录 + */ + @PostMapping("/loginByopenid") + public AjaxResult loginByopenid(String openid) { + AjaxResult ajax = AjaxResult.success(); + // 生成令牌 + String token = loginService.loginByopenid(openid); ajax.put(Constants.TOKEN, token); return ajax; } diff --git a/AutoSprout-common/src/main/java/com/ruoyi/common/core/domain/entity/AsUser.java b/AutoSprout-common/src/main/java/com/ruoyi/common/core/domain/entity/AsUser.java index 582c867..811f54d 100644 --- a/AutoSprout-common/src/main/java/com/ruoyi/common/core/domain/entity/AsUser.java +++ b/AutoSprout-common/src/main/java/com/ruoyi/common/core/domain/entity/AsUser.java @@ -14,7 +14,7 @@ import java.util.Date; /** * 用户对象 as_user - * + * * @author qiuzhenzhao */ @ToString @@ -81,6 +81,17 @@ public class AsUser extends BaseEntity /** 展示当前设备id */ private Long deviceId; + /** 微信openid */ + private String wxopenid; + + public String getWxopenid() { + return wxopenid; + } + + public void setWxopenid(String wxopenid) { + this.wxopenid = wxopenid; + } + public AsUser() { diff --git a/AutoSprout-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/AutoSprout-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index 0d7a216..a88712e 100644 --- a/AutoSprout-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/AutoSprout-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -113,7 +113,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter // 过滤请求 .authorizeRequests() // 对于登录login 注册register 验证码captchaImage 允许匿名访问 - .antMatchers("/login","/wxlogin", "/register", "/captchaImage","/common/receive","/appCaptcha","/appCodeLogin","/app/**","/common/upload").permitAll() + .antMatchers("/login","/wxlogin", "/register", "/captchaImage","/common/receive","/appCaptcha","/appCodeLogin","/app/**","/common/upload","/loginByopenid").permitAll() // 静态资源,可匿名访问 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() diff --git a/AutoSprout-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/AutoSprout-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index 92b586c..b50b25e 100644 --- a/AutoSprout-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/AutoSprout-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -259,7 +259,7 @@ public class SysLoginService /** * 微信登录 */ - public String wxloing(String mobileCode) { + public String wxloing(String mobileCode,String openid) { String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="; String phoneNumber = null; AsUser user = null; @@ -286,6 +286,7 @@ public class SysLoginService asUser.setPhonenumber(phoneNumber); asUser.setLoginIp(IpUtils.getIpAddr()); asUser.setLoginDate(DateUtils.getNowDate()); + asUser.setWxopenid(openid); asUser.setCreateTime(DateUtils.getNowDate()); int i = asUserService.insertUser(asUser); user = asUser; @@ -314,5 +315,37 @@ public class SysLoginService return tokenService.createToken(loginUser); } + /** + * 根据openid静默登录 + */ + public String loginByopenid(String openid) { + AsUser user = asUserService.selectUserByWxopenid(openid); + if(ObjectUtils.isEmpty(user)){ + throw new ServiceException("未查询到用户信息"); + } + Authentication authentication = null; // 用户验证 + try { + UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(), Constants.CUSTOM_LOGIN_WX); + // 用户名和密码等信息保存在一个上下文中,只要是同一线程等会就能拿到用户名和密码,也就是能在loadUserByUsername(String username)方法中进行密码验证等 + AuthenticationContextHolder.setContext(authenticationToken); + // 把用户类型放在上下文中的details属性中,在UserDetailsServiceImpl.loadUserByUsername中获取 + authenticationToken.setDetails(Constants.USER_TYPE_APP); + // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername + authentication = authenticationManager.authenticate(authenticationToken); + } + catch (Exception e) { + if (e instanceof BadCredentialsException) { + throw new UserPasswordNotMatchException(); //抛出账号或者密码错误的异常 + } else { + throw new ServiceException(e.getMessage()); //抛出其他异常 + } + } finally { + AuthenticationContextHolder.clearContext(); + } + LoginUser loginUser = (LoginUser) authentication.getPrincipal(); + recordAppLoginInfo(loginUser.getUserId()); + return tokenService.createToken(loginUser); + } + } diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/app/AppController.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/app/AppController.java index 74edb1b..4459444 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/app/AppController.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/app/AppController.java @@ -157,15 +157,16 @@ public class AppController extends BaseController } /** - * 删除设备列表 逻辑删除 + * 解绑设备 */ - @Log(title = "删除设备", businessType = BusinessType.DELETE) - @DeleteMapping("/device/{deviceId}") - public AjaxResult remove(@PathVariable Long deviceId) + @Log(title = "解绑设备", businessType = BusinessType.DELETE) + @DeleteMapping("/device/unbind/{deviceId}") + public AjaxResult unbind(@PathVariable Long deviceId) { AsDevice device = new AsDevice(); device.setDeviceId(deviceId); - device.setStatus("1"); + device.setUserId(0L); + device.setIsNetwork("0"); return toAjax(asDeviceService.logicallyDelete(device)); } diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/mapper/AsUserMapper.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/mapper/AsUserMapper.java index 83f173b..9c1e7c5 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/mapper/AsUserMapper.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/mapper/AsUserMapper.java @@ -7,14 +7,14 @@ import java.util.List; /** * 用户表 数据层 - * + * * @author ruoyi */ public interface AsUserMapper { /** * 根据条件分页查询用户列表 - * + * * @param sysUser 用户信息 * @return 用户信息集合信息 */ @@ -22,7 +22,7 @@ public interface AsUserMapper /** * 根据条件分页查询已配用户角色列表 - * + * * @param user 用户信息 * @return 用户信息集合信息 */ @@ -30,7 +30,7 @@ public interface AsUserMapper /** * 根据条件分页查询未分配用户角色列表 - * + * * @param user 用户信息 * @return 用户信息集合信息 */ @@ -38,7 +38,7 @@ public interface AsUserMapper /** * 通过用户名查询用户 - * + * * @param userName 用户名 * @return 用户对象信息 */ @@ -46,7 +46,7 @@ public interface AsUserMapper /** * 通过用户ID查询用户 - * + * * @param userId 用户ID * @return 用户对象信息 */ @@ -54,7 +54,7 @@ public interface AsUserMapper /** * 新增用户信息 - * + * * @param user 用户信息 * @return 结果 */ @@ -62,7 +62,7 @@ public interface AsUserMapper /** * 修改用户信息 - * + * * @param user 用户信息 * @return 结果 */ @@ -70,7 +70,7 @@ public interface AsUserMapper /** * 修改用户头像 - * + * * @param userName 用户名 * @param avatar 头像地址 * @return 结果 @@ -79,7 +79,7 @@ public interface AsUserMapper /** * 重置用户密码 - * + * * @param userName 用户名 * @param password 密码 * @return 结果 @@ -88,7 +88,7 @@ public interface AsUserMapper /** * 通过用户ID删除用户 - * + * * @param userId 用户ID * @return 结果 */ @@ -96,7 +96,7 @@ public interface AsUserMapper /** * 批量删除用户信息 - * + * * @param userIds 需要删除的用户ID * @return 结果 */ @@ -104,7 +104,7 @@ public interface AsUserMapper /** * 校验用户名称是否唯一 - * + * * @param userName 用户名称 * @return 结果 */ @@ -133,4 +133,6 @@ public interface AsUserMapper * @return 用户对象信息 */ public AsUser selectUserByPhone(String phone); + + AsUser selectUserByWxopenid(String openid); } diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/IAsUserService.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/IAsUserService.java index 027a61c..3e4c8b4 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/IAsUserService.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/IAsUserService.java @@ -188,4 +188,12 @@ public interface IAsUserService * 计算下次定时时间 */ public void setNextDs(AsDevice asDevice1, DatapointValue.nextDs next_ds); + + /** + * 通过openid查询用户 + * + * @param openid + * @return 用户对象信息 + */ + AsUser selectUserByWxopenid(String openid); } diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsModelServiceImpl.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsModelServiceImpl.java index 909bb1a..e792c71 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsModelServiceImpl.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsModelServiceImpl.java @@ -123,7 +123,15 @@ public class AsModelServiceImpl extends ServiceImpl impl AsDeviceClassify deviceClassify = classifyMapper.selectAsDeviceClassifyByClassifyId(asModel.getClassifyId()); String classifyName = deviceClassify.getClassifyName(); asModel.setClassifyName(classifyName); - return asModelMapper.updateAsModel(asModel); + int i = asModelMapper.updateAsModel(asModel); + AsDevice device = new AsDevice(); + device.setModelId(asModel.getModelId()); + List asDevices = deviceMapper.selectAsDeviceList(device); + for(AsDevice device1: asDevices){ + device1.setModel(asModel.getModel()); + deviceMapper.updateAsDevice(device1); + } + return i; } /** diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsUserServiceImpl.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsUserServiceImpl.java index 71dbcf6..2fc39c2 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsUserServiceImpl.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsUserServiceImpl.java @@ -418,6 +418,17 @@ public class AsUserServiceImpl implements IAsUserService return asUserMapper.selectUserByPhone(phone); } + /** + * 通过openid查询用户 + * + * @param openid 手机号 + * @return 用户对象信息 + */ + @Override + public AsUser selectUserByWxopenid(String openid) { + return asUserMapper.selectUserByWxopenid(openid); + } + /** * 根据登录用户获取绑定设备 * 根据登录用户获取是否有绑定设备,没有则显示添加设备页面,如果有则从列表中取一条记录展示 diff --git a/AutoSprout-watering/src/main/resources/mapper/device/AsDeviceMapper.xml b/AutoSprout-watering/src/main/resources/mapper/device/AsDeviceMapper.xml index e3da874..a9b293c 100644 --- a/AutoSprout-watering/src/main/resources/mapper/device/AsDeviceMapper.xml +++ b/AutoSprout-watering/src/main/resources/mapper/device/AsDeviceMapper.xml @@ -54,7 +54,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and online_status = #{onlineStatus} and nick_name like concat('%', #{nickName}, '%') and user_id = #{userId} - and device_id != #{deviceId} + and model_id = #{modelId} @@ -70,7 +70,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" diff --git a/AutoSprout-watering/src/main/resources/mapper/user/AsUserMapper.xml b/AutoSprout-watering/src/main/resources/mapper/user/AsUserMapper.xml index 055f594..234ccba 100644 --- a/AutoSprout-watering/src/main/resources/mapper/user/AsUserMapper.xml +++ b/AutoSprout-watering/src/main/resources/mapper/user/AsUserMapper.xml @@ -30,7 +30,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" select u.user_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber,u.birthday, u.password, u.pay_password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark from as_user u - + - + - + - + - + - + - + - + @@ -104,7 +104,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where u.phonenumber = #{phonenumber} - + + + insert into as_user( user_id, @@ -134,7 +139,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" sysdate() ) - + update as_user @@ -154,28 +159,28 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where user_id = #{userId} - + update as_user set status = #{status} where user_id = #{userId} - + update as_user set avatar = #{avatar} where user_name = #{userName} - + update as_user set password = #{password} where user_name = #{userName} - + update as_user set del_flag = '2' where user_id = #{userId} - + update as_user set del_flag = '2' where user_id in #{userId} - + - - \ No newline at end of file + +