1. 快速登录

2. 解绑设备
This commit is contained in:
邱贞招 2024-04-18 13:38:51 +08:00
parent b2cfaf1e0c
commit 82e881ce1b
11 changed files with 136 additions and 44 deletions

View File

@ -132,10 +132,23 @@ public class SysLoginController
* 微信登录 * 微信登录
*/ */
@PostMapping("/wxlogin") @PostMapping("/wxlogin")
public AjaxResult wxlogin(String mobileCode) { public AjaxResult wxlogin(String mobileCode,String openid) {
AjaxResult ajax = AjaxResult.success(); 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); ajax.put(Constants.TOKEN, token);
return ajax; return ajax;
} }

View File

@ -14,7 +14,7 @@ import java.util.Date;
/** /**
* 用户对象 as_user * 用户对象 as_user
* *
* @author qiuzhenzhao * @author qiuzhenzhao
*/ */
@ToString @ToString
@ -81,6 +81,17 @@ public class AsUser extends BaseEntity
/** 展示当前设备id */ /** 展示当前设备id */
private Long deviceId; private Long deviceId;
/** 微信openid */
private String wxopenid;
public String getWxopenid() {
return wxopenid;
}
public void setWxopenid(String wxopenid) {
this.wxopenid = wxopenid;
}
public AsUser() public AsUser()
{ {

View File

@ -113,7 +113,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// 过滤请求 // 过滤请求
.authorizeRequests() .authorizeRequests()
// 对于登录login 注册register 验证码captchaImage 允许匿名访问 // 对于登录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(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()

View File

@ -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 url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
String phoneNumber = null; String phoneNumber = null;
AsUser user = null; AsUser user = null;
@ -286,6 +286,7 @@ public class SysLoginService
asUser.setPhonenumber(phoneNumber); asUser.setPhonenumber(phoneNumber);
asUser.setLoginIp(IpUtils.getIpAddr()); asUser.setLoginIp(IpUtils.getIpAddr());
asUser.setLoginDate(DateUtils.getNowDate()); asUser.setLoginDate(DateUtils.getNowDate());
asUser.setWxopenid(openid);
asUser.setCreateTime(DateUtils.getNowDate()); asUser.setCreateTime(DateUtils.getNowDate());
int i = asUserService.insertUser(asUser); int i = asUserService.insertUser(asUser);
user = asUser; user = asUser;
@ -314,5 +315,37 @@ public class SysLoginService
return tokenService.createToken(loginUser); 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);
}
} }

View File

@ -157,15 +157,16 @@ public class AppController extends BaseController
} }
/** /**
* 删除设备列表 逻辑删除 * 解绑设备
*/ */
@Log(title = "删除设备", businessType = BusinessType.DELETE) @Log(title = "解绑设备", businessType = BusinessType.DELETE)
@DeleteMapping("/device/{deviceId}") @DeleteMapping("/device/unbind/{deviceId}")
public AjaxResult remove(@PathVariable Long deviceId) public AjaxResult unbind(@PathVariable Long deviceId)
{ {
AsDevice device = new AsDevice(); AsDevice device = new AsDevice();
device.setDeviceId(deviceId); device.setDeviceId(deviceId);
device.setStatus("1"); device.setUserId(0L);
device.setIsNetwork("0");
return toAjax(asDeviceService.logicallyDelete(device)); return toAjax(asDeviceService.logicallyDelete(device));
} }

View File

@ -7,14 +7,14 @@ import java.util.List;
/** /**
* 用户表 数据层 * 用户表 数据层
* *
* @author ruoyi * @author ruoyi
*/ */
public interface AsUserMapper public interface AsUserMapper
{ {
/** /**
* 根据条件分页查询用户列表 * 根据条件分页查询用户列表
* *
* @param sysUser 用户信息 * @param sysUser 用户信息
* @return 用户信息集合信息 * @return 用户信息集合信息
*/ */
@ -22,7 +22,7 @@ public interface AsUserMapper
/** /**
* 根据条件分页查询已配用户角色列表 * 根据条件分页查询已配用户角色列表
* *
* @param user 用户信息 * @param user 用户信息
* @return 用户信息集合信息 * @return 用户信息集合信息
*/ */
@ -30,7 +30,7 @@ public interface AsUserMapper
/** /**
* 根据条件分页查询未分配用户角色列表 * 根据条件分页查询未分配用户角色列表
* *
* @param user 用户信息 * @param user 用户信息
* @return 用户信息集合信息 * @return 用户信息集合信息
*/ */
@ -38,7 +38,7 @@ public interface AsUserMapper
/** /**
* 通过用户名查询用户 * 通过用户名查询用户
* *
* @param userName 用户名 * @param userName 用户名
* @return 用户对象信息 * @return 用户对象信息
*/ */
@ -46,7 +46,7 @@ public interface AsUserMapper
/** /**
* 通过用户ID查询用户 * 通过用户ID查询用户
* *
* @param userId 用户ID * @param userId 用户ID
* @return 用户对象信息 * @return 用户对象信息
*/ */
@ -54,7 +54,7 @@ public interface AsUserMapper
/** /**
* 新增用户信息 * 新增用户信息
* *
* @param user 用户信息 * @param user 用户信息
* @return 结果 * @return 结果
*/ */
@ -62,7 +62,7 @@ public interface AsUserMapper
/** /**
* 修改用户信息 * 修改用户信息
* *
* @param user 用户信息 * @param user 用户信息
* @return 结果 * @return 结果
*/ */
@ -70,7 +70,7 @@ public interface AsUserMapper
/** /**
* 修改用户头像 * 修改用户头像
* *
* @param userName 用户名 * @param userName 用户名
* @param avatar 头像地址 * @param avatar 头像地址
* @return 结果 * @return 结果
@ -79,7 +79,7 @@ public interface AsUserMapper
/** /**
* 重置用户密码 * 重置用户密码
* *
* @param userName 用户名 * @param userName 用户名
* @param password 密码 * @param password 密码
* @return 结果 * @return 结果
@ -88,7 +88,7 @@ public interface AsUserMapper
/** /**
* 通过用户ID删除用户 * 通过用户ID删除用户
* *
* @param userId 用户ID * @param userId 用户ID
* @return 结果 * @return 结果
*/ */
@ -96,7 +96,7 @@ public interface AsUserMapper
/** /**
* 批量删除用户信息 * 批量删除用户信息
* *
* @param userIds 需要删除的用户ID * @param userIds 需要删除的用户ID
* @return 结果 * @return 结果
*/ */
@ -104,7 +104,7 @@ public interface AsUserMapper
/** /**
* 校验用户名称是否唯一 * 校验用户名称是否唯一
* *
* @param userName 用户名称 * @param userName 用户名称
* @return 结果 * @return 结果
*/ */
@ -133,4 +133,6 @@ public interface AsUserMapper
* @return 用户对象信息 * @return 用户对象信息
*/ */
public AsUser selectUserByPhone(String phone); public AsUser selectUserByPhone(String phone);
AsUser selectUserByWxopenid(String openid);
} }

View File

@ -188,4 +188,12 @@ public interface IAsUserService
* 计算下次定时时间 * 计算下次定时时间
*/ */
public void setNextDs(AsDevice asDevice1, DatapointValue.nextDs next_ds); public void setNextDs(AsDevice asDevice1, DatapointValue.nextDs next_ds);
/**
* 通过openid查询用户
*
* @param openid
* @return 用户对象信息
*/
AsUser selectUserByWxopenid(String openid);
} }

View File

@ -123,7 +123,15 @@ public class AsModelServiceImpl extends ServiceImpl<AsModelMapper, AsModel> impl
AsDeviceClassify deviceClassify = classifyMapper.selectAsDeviceClassifyByClassifyId(asModel.getClassifyId()); AsDeviceClassify deviceClassify = classifyMapper.selectAsDeviceClassifyByClassifyId(asModel.getClassifyId());
String classifyName = deviceClassify.getClassifyName(); String classifyName = deviceClassify.getClassifyName();
asModel.setClassifyName(classifyName); asModel.setClassifyName(classifyName);
return asModelMapper.updateAsModel(asModel); int i = asModelMapper.updateAsModel(asModel);
AsDevice device = new AsDevice();
device.setModelId(asModel.getModelId());
List<AsDevice> asDevices = deviceMapper.selectAsDeviceList(device);
for(AsDevice device1: asDevices){
device1.setModel(asModel.getModel());
deviceMapper.updateAsDevice(device1);
}
return i;
} }
/** /**

View File

@ -418,6 +418,17 @@ public class AsUserServiceImpl implements IAsUserService
return asUserMapper.selectUserByPhone(phone); return asUserMapper.selectUserByPhone(phone);
} }
/**
* 通过openid查询用户
*
* @param openid 手机号
* @return 用户对象信息
*/
@Override
public AsUser selectUserByWxopenid(String openid) {
return asUserMapper.selectUserByWxopenid(openid);
}
/** /**
* 根据登录用户获取绑定设备 * 根据登录用户获取绑定设备
* 根据登录用户获取是否有绑定设备没有则显示添加设备页面如果有则从列表中取一条记录展示 * 根据登录用户获取是否有绑定设备没有则显示添加设备页面如果有则从列表中取一条记录展示

View File

@ -54,7 +54,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="onlineStatus != null and onlineStatus != ''"> and online_status = #{onlineStatus}</if> <if test="onlineStatus != null and onlineStatus != ''"> and online_status = #{onlineStatus}</if>
<if test="nickName != null and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if> <if test="nickName != null and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
<if test="userId != null "> and user_id = #{userId}</if> <if test="userId != null "> and user_id = #{userId}</if>
<if test="deviceId != null "> and device_id != #{deviceId}</if> <if test="modelId != null "> and model_id = #{modelId}</if>
</where> </where>
</select> </select>
@ -70,7 +70,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="isBandByMac" resultType="java.lang.Integer"> <select id="isBandByMac" resultType="java.lang.Integer">
select count(1) from as_device select count(1) from as_device
where mac = #{mac} and user_id IS NOT NULL and is_network = 1 where mac = #{mac} and user_id IS NOT NULL and is_network = 1 and user_id != 0
</select> </select>
<insert id="insertAsDevice" parameterType="AsDevice" useGeneratedKeys="true" keyProperty="deviceId"> <insert id="insertAsDevice" parameterType="AsDevice" useGeneratedKeys="true" keyProperty="deviceId">

View File

@ -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 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 from as_user u
</sql> </sql>
<select id="selectUserList" parameterType="AsUser" resultMap="AsUserResult"> <select id="selectUserList" parameterType="AsUser" resultMap="AsUserResult">
select u.user_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, 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 select u.user_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, 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
where u.del_flag = '0' where u.del_flag = '0'
@ -53,7 +53,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d') AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
</if> </if>
</select> </select>
<select id="selectAllocatedList" parameterType="AsUser" resultMap="AsUserResult"> <select id="selectAllocatedList" parameterType="AsUser" resultMap="AsUserResult">
select distinct u.user_id, u.user_name, u.nick_name, u.email, u.phonenumber, u.status, u.create_time select distinct u.user_id, u.user_name, u.nick_name, u.email, u.phonenumber, u.status, u.create_time
from as_user u from as_user u
@ -65,7 +65,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND u.phonenumber like concat('%', #{phonenumber}, '%') AND u.phonenumber like concat('%', #{phonenumber}, '%')
</if> </if>
</select> </select>
<select id="selectUnallocatedList" parameterType="AsUser" resultMap="AsUserResult"> <select id="selectUnallocatedList" parameterType="AsUser" resultMap="AsUserResult">
select distinct u.user_id, u.user_name, u.nick_name, u.email, u.phonenumber, u.status, u.create_time select distinct u.user_id, u.user_name, u.nick_name, u.email, u.phonenumber, u.status, u.create_time
from as_user u from as_user u
@ -77,25 +77,25 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
AND u.phonenumber like concat('%', #{phonenumber}, '%') AND u.phonenumber like concat('%', #{phonenumber}, '%')
</if> </if>
</select> </select>
<select id="selectUserByUserName" parameterType="String" resultMap="AsUserResult"> <select id="selectUserByUserName" parameterType="String" resultMap="AsUserResult">
<include refid="selectUserVo"/> <include refid="selectUserVo"/>
where u.user_name = #{userName} and u.del_flag = '0' where u.user_name = #{userName} and u.del_flag = '0'
</select> </select>
<select id="selectUserById" parameterType="Long" resultMap="AsUserResult"> <select id="selectUserById" parameterType="Long" resultMap="AsUserResult">
<include refid="selectUserVo"/> <include refid="selectUserVo"/>
where u.user_id = #{userId} where u.user_id = #{userId}
</select> </select>
<select id="checkUserNameUnique" parameterType="String" resultMap="AsUserResult"> <select id="checkUserNameUnique" parameterType="String" resultMap="AsUserResult">
select user_id, user_name from as_user where user_name = #{userName} and del_flag = '0' limit 1 select user_id, user_name from as_user where user_name = #{userName} and del_flag = '0' limit 1
</select> </select>
<select id="checkPhoneUnique" parameterType="String" resultMap="AsUserResult"> <select id="checkPhoneUnique" parameterType="String" resultMap="AsUserResult">
select user_id, phonenumber from as_user where phonenumber = #{phonenumber} and del_flag = '0' limit 1 select user_id, phonenumber from as_user where phonenumber = #{phonenumber} and del_flag = '0' limit 1
</select> </select>
<select id="checkEmailUnique" parameterType="String" resultMap="AsUserResult"> <select id="checkEmailUnique" parameterType="String" resultMap="AsUserResult">
select user_id, email from as_user where email = #{email} and del_flag = '0' limit 1 select user_id, email from as_user where email = #{email} and del_flag = '0' limit 1
</select> </select>
@ -104,7 +104,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<include refid="selectUserVo"/> <include refid="selectUserVo"/>
where u.phonenumber = #{phonenumber} where u.phonenumber = #{phonenumber}
</select> </select>
<select id="selectUserByWxopenid" parameterType="String" resultMap="AsUserResult">
<include refid="selectUserVo"/>
where u.wxopenid = #{openid}
</select>
<insert id="insertUser" parameterType="AsUser" useGeneratedKeys="true" keyProperty="userId"> <insert id="insertUser" parameterType="AsUser" useGeneratedKeys="true" keyProperty="userId">
insert into as_user( insert into as_user(
<if test="userId != null and userId != 0">user_id,</if> <if test="userId != null and userId != 0">user_id,</if>
@ -134,7 +139,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
sysdate() sysdate()
) )
</insert> </insert>
<update id="updateUser" parameterType="AsUser"> <update id="updateUser" parameterType="AsUser">
update as_user update as_user
<set> <set>
@ -154,28 +159,28 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</set> </set>
where user_id = #{userId} where user_id = #{userId}
</update> </update>
<update id="updateUserStatus" parameterType="AsUser"> <update id="updateUserStatus" parameterType="AsUser">
update as_user set status = #{status} where user_id = #{userId} update as_user set status = #{status} where user_id = #{userId}
</update> </update>
<update id="updateUserAvatar" parameterType="AsUser"> <update id="updateUserAvatar" parameterType="AsUser">
update as_user set avatar = #{avatar} where user_name = #{userName} update as_user set avatar = #{avatar} where user_name = #{userName}
</update> </update>
<update id="resetUserPwd" parameterType="AsUser"> <update id="resetUserPwd" parameterType="AsUser">
update as_user set password = #{password} where user_name = #{userName} update as_user set password = #{password} where user_name = #{userName}
</update> </update>
<delete id="deleteUserById" parameterType="Long"> <delete id="deleteUserById" parameterType="Long">
update as_user set del_flag = '2' where user_id = #{userId} update as_user set del_flag = '2' where user_id = #{userId}
</delete> </delete>
<delete id="deleteUserByIds" parameterType="Long"> <delete id="deleteUserByIds" parameterType="Long">
update as_user set del_flag = '2' where user_id in update as_user set del_flag = '2' where user_id in
<foreach collection="array" item="userId" open="(" separator="," close=")"> <foreach collection="array" item="userId" open="(" separator="," close=")">
#{userId} #{userId}
</foreach> </foreach>
</delete> </delete>
</mapper> </mapper>