上个小节我们写的JS代码有很多地方等待实现,这节课咱们来实现其中的入会签到功能。只要用户连接上视频会议室,就自动发送签到请求,由后端程序做判断,如果没签到就自动签到,要是签过到了,就不必重复签到。
一、编写持久层代码
在TbMeetingDao.xml
文件中,定义SQL语句。
<select id="searchCanCheckinMeeting" parameterType="HashMap" resultType="long">
SELECT COUNT( * )
FROM tb_meeting
WHERE id = #{meetingId}
AND status IN( 3,4 )
AND NOW() BETWEEN DATE_SUB(CONCAT(date," ",`start`),INTERVAL 15 MINUTE) AND DATE_ADD(CONCAT(date," ",`start`),INTERVAL 5 MINUTE)
AND JSON_CONTAINS ( IFNULL(members,JSON_ARRAY()), CAST( #{userId} AS CHAR ) )
AND NOT JSON_CONTAINS ( IFNULL(present,JSON_ARRAY()), CAST( #{userId} AS CHAR ) )
</select>
<update id="updateMeetingPresent" parameterType="HashMap">
UPDATE tb_meeting
SET present = JSON_ARRAY_APPEND ( IFNULL( present, JSON_ARRAY ( ) ), '$', #{userId} )
WHERE id = #{meetingId}
AND NOW() >= DATE_SUB(CONCAT(date," ",`start`), INTERVAL 15 MINUTE)
AND NOW() < DATE_SUB(CONCAT(date," ",`end`),INTERVAL 15 MINUTE)
AND JSON_CONTAINS ( IFNULL( members, JSON_ARRAY ( ) ), CAST( #{userId} AS CHAR ) )
AND NOT JSON_CONTAINS ( IFNULL( present, JSON_ARRAY ( ) ), CAST( #{userId} AS CHAR ) )
</update>
在TbMeetingDao.java
接口中,声明DAO方法。
public interface TbMeetingDao {
……
public long searchCanCheckinMeeting(HashMap param);
public int updateMeetingPresent(HashMap param);
}
二、编写业务层代码
在MeetingService.java
接口中,定义Web方法。
public interface MeetingService {
……
public boolean searchCanCheckinMeeting(HashMap param);
public int updateMeetingPresent(HashMap param);
}
在MeetingServiceImpl.java
类中,实现抽象方法。
public class MeetingServiceImpl implements MeetingService {
……
@Override
public boolean searchCanCheckinMeeting(HashMap param) {
long count = meetingDao.searchCanCheckinMeeting(param);
return count == 1 ? true : false;
}
@Override
public int updateMeetingPresent(HashMap param) {
int rows = meetingDao.updateMeetingPresent(param);
return rows;
}
}
三、编写Web层代码
创建UpdateMeetingPresentForm.java
类,封装Ajax提交的数据。
@Data
@Schema(description = "会议签到表单")
public class UpdateMeetingPresentForm {
@NotNull(message = "meetingId不能为空")
@Min(value = 1, message = "meetingId不能小于1")
private Integer meetingId;
}
在MeetingController.java
类中,声明Web方法。
public class MeetingController {
……
@PostMapping("/updateMeetingPresent")
@Operation(summary = "执行会议签到")
@SaCheckLogin
public R updateMeetingPresent(@Valid @RequestBody UpdateMeetingPresentForm form) {
HashMap param = new HashMap() {{
put("meetingId", form.getMeetingId());
put("userId", StpUtil.getLoginIdAsInt());
}};
boolean bool = meetingService.searchCanCheckinMeeting(param);
if (bool) {
int rows = meetingService.updateMeetingPresent(param);
return R.ok().put("rows", rows);
}
return R.ok().put("rows", 0);
}
}
四、前端页面发起签到请求
在meeting_video.vue
页面中编写JS代码,发起签到请求。
//执行签到
that.$http('meeting/updateMeetingPresent','POST', { meetingId: that.meetingId }, true,function(resp) {
let rows = resp.rows;
if (rows == 1) {
that.$message({
message: '签到成功',
type: 'success',
duration: 1200
});
}
});