用户登录
前端提交登录表单,返回数据到redis,如果该用户近期登录过,直接登录,否则核对用户名密码登录
前端Vue
<el-form ref="form" :model="loginForm" :rules="loginRules">
<el-form-item prop="id">
<el-input v-model="loginForm.id" placeholder="请输入用户ID" />
</el-form-item>
<el-form-item prop="password">
<el-input v-model="loginForm.password" show-password placeholder="请输入密码" />
</el-form-item>
<el-form-item prop="isAgree" >
<el-checkbox v-model="loginForm.isAgree">
用户平台使用协议
</el-checkbox>
</el-form-item>
<el-form-item>
<el-button style="width:350px" type="primary" @click="login">登录</el-button>
</el-form-item>
</el-form>
后端spring boot
@PostMapping("/redisLogin")
public Result<String> redisLogin(@RequestBody UserDto userDto, HttpSession codeInSession) {
//键值对,id和令牌
String token1 = (String) redisTemplate.opsForValue().get(userDto.getId());
if(codeInSession==null){
return Result.error("连接错误");
}
//没有令牌,用户名密码登录
else if(token1 == null){
Integer id = userDto.getId();
String password = userDto.getPassword();
System.out.println(userDto.getId());
User user = userService.getById(id);
if (user == null || !user.getPassword().equals(password)||codeInSession == null) {
return Result.error("用户名或密码错误");
} else {
String token2="123";//默认令牌为123
redisTemplate.opsForValue().set(userDto.getId(), token2, 5, TimeUnit.MINUTES);
return Result.success("密码登陆成功,欢迎~");
}
}
发送信息
send() {
this.$refs.form.validate(async (isOK) => {
if (isOK) {
await this.$store.dispatch('user/send',this.sendForm);
this.sendForm.text = '';
alert("Are you there");
}
});
}
@PostMapping("/send")//发送消息
public Result<String> sendMessage(@RequestBody MessageDto messageDto) throws JMSException, IOException {
//从哪个用户发到哪一个用户,消息内容
System.out.println("发送者"+messageDto.getFromUser()+"接收者"+messageDto.getToUser());
sendService.sendMessage(messageDto);
sendService.save(messageDto);
return Result.success("发送消息成功");
}
接收信息
通过数据库接收历史消息并展示在前端表单
page(){
this.$refs.form.validate(async (isOK) => {
if (isOK) {
// await this.$store.dispatch('user/receive');
await this.$store.dispatch('user/page');
jsonObject = JSON.parse(JSON.stringify(pageData));
// 提取data.records数组中的每个元素的text字段
texts = jsonObject.data.records.map(record => record.text);
fromUser= jsonObject.data.records.map(record => record.fromUser);
this.getTableData();
}
});
},
getTableData() {
t.length=0;
for (let i = 0; i < fromUser.length; i++) {
var obj1={fromUser:fromUser[i]};
// console.log(obj1)
var obj2={message:texts[i]};
// console.log(obj2)
var obj3 = Object.assign(obj1,obj2);
// console.log(obj3)
t.push(obj3);
}
},
<el-table
:data="getTableData1"
style="width: 100%"
height="250">
<!-- <el-table-column
fixed
prop="time"
label="时间"
width="150">
</el-table-column> -->
<el-table-column
prop="fromUser"
label="用户"
width="120">
</el-table-column>
<el-table-column
prop="message"
label="消息内容"
width="1200">
</el-table-column>
</el-table>
getTableData1() {
return this.tableData1;
}
tableData1: t,
@GetMapping("/page")//数据库里面的历史消息
public Result<Page> page(@RequestBody int user){
//分页构造器,Page(第几页, 查几条)
Page pageInfo = new Page(1,10);
//查询构造器
LambdaQueryWrapper<IMessage> lambdaQueryWrapper = new LambdaQueryWrapper();
//过滤条件.like(什么条件下启用模糊查询,模糊查询字段,被模糊插叙的名称)
lambdaQueryWrapper.like(IMessage::getToUser,user);
// lambdaQueryWrapper
lambdaQueryWrapper.or(q -> {
q.like(IMessage::getFromUser,user);
});
//添加排序
lambdaQueryWrapper.orderByDesc(IMessage::getDate);
//查询分页、自动更新
receiveService.page(pageInfo,lambdaQueryWrapper);
//返回查询结果
return Result.success(pageInfo);
}