1、邮件日志功能实现

pull/254/head
xjs 3 years ago
parent 51c7fe7358
commit dfc3db10e9

@ -3,6 +3,7 @@ package com.xjs.business.log;
import com.ruoyi.common.core.constant.ServiceNameConstants;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.log.domain.ApiLog;
import com.xjs.business.log.domain.MailLog;
import com.xjs.business.log.domain.TaskLog;
import com.xjs.business.log.domain.WebmagicLog;
import com.xjs.business.log.factory.RemoteLogFactory;
@ -37,4 +38,7 @@ public interface RemoteLogFeign {
@PostMapping("taskLog/saveForPRC")
R<Object> saveTaskLog(@RequestBody TaskLog taskLog);
@PostMapping("maillog/saveForRPC")
R<Object> saveMailLog(@RequestBody MailLog mailLog);
}

@ -0,0 +1,41 @@
package com.xjs.business.log.domain;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
*
* @author xiejs
* @since 2022-04-13
*/
@Data
public class MailLog implements Serializable {
private static final long serialVersionUID = 1L;
/** 主键id */
private Long id;
/** 邮件标题 */
private String title;
/** 邮件内容 */
private String content;
/** 收件人 */
private String recipient;
/**
*
*/
private String mailType;
/** 请求时间 */
private Long requestTime;
/**
*
*/
private Date createTime;
}

@ -3,6 +3,7 @@ package com.xjs.business.log.factory;
import com.ruoyi.common.core.domain.R;
import com.xjs.business.log.RemoteLogFeign;
import com.xjs.business.log.domain.ApiLog;
import com.xjs.business.log.domain.MailLog;
import com.xjs.business.log.domain.TaskLog;
import com.xjs.business.log.domain.WebmagicLog;
import org.slf4j.Logger;
@ -48,6 +49,12 @@ public class RemoteLogFactory implements FallbackFactory<RemoteLogFeign> {
log.error("日志模块任务日志服务添加调用失败");
return R.fail("日志模块任务日志服务添加调用失败" + cause.getMessage());
}
@Override
public R<Object> saveMailLog(MailLog mailLog) {
log.error("日志模块邮件日志服务添加调用失败");
return R.fail("日志模块邮件日志服务添加调用失败" + cause.getMessage());
}
};
}
}

@ -0,0 +1,27 @@
import request from '@/utils/request'
// 查询邮件日志列表
export function listMaillog(query) {
return request({
url: '/log/maillog/list',
method: 'get',
params: query
})
}
// 查询邮件日志详细
export function getMaillog(id) {
return request({
url: '/log/maillog/' + id,
method: 'get'
})
}
// 删除邮件日志
export function delMaillog(id) {
return request({
url: '/log/maillog/' + id,
method: 'delete'
})
}

@ -0,0 +1,253 @@
<template>
<div class="app-container">
<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<el-form-item label="邮件主题" prop="title">
<el-input
v-model="queryParams.title"
placeholder="请输入邮件主题"
clearable
maxlength="20"
size="small"
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item label="邮件内容" prop="content">
<el-input
v-model="queryParams.content"
placeholder="请输入邮件内容"
clearable
maxlength="20"
size="small"
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item label="收件人" prop="recipient">
<el-input
v-model="queryParams.recipient"
placeholder="请输入收件人"
clearable
maxlength="20"
size="small"
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item label="创建时间">
<el-date-picker
v-model="daterangeCreateTime"
size="small"
:picker-options="pickerOptions"
style="width: 240px"
value-format="yyyy-MM-dd"
type="daterange"
@change="dateQuery('queryForm')"
range-separator="-"
start-placeholder="开始日期"
end-placeholder="结束日期"
></el-date-picker>
</el-form-item>
<el-form-item>
<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"></el-button>
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery"></el-button>
</el-form-item>
</el-form>
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button
type="danger"
plain
icon="el-icon-delete"
size="mini"
:disabled="multiple"
@click="handleDelete"
v-hasPermi="['log:maillog:remove']"
>删除
</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="warning"
plain
icon="el-icon-download"
size="mini"
@click="handleExport"
v-hasPermi="['log:maillog:export']"
>导出
</el-button>
</el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
<el-table v-loading="loading" :data="maillogList" @selection-change="handleSelectionChange" border>
<el-table-column type="selection" width="55" align="center"/>
<el-table-column label="邮件主题" align="center" prop="title" :show-overflow-tooltip="true"/>
<el-table-column label="邮件内容" align="center" prop="content" :show-overflow-tooltip="true"/>
<el-table-column label="收件人" align="center" prop="recipient" :show-overflow-tooltip="true"/>
<el-table-column label="邮件类型" align="center" prop="mailType" :show-overflow-tooltip="true"/>
<el-table-column label="请求时间" align="center" prop="requestTime">
<template slot-scope="scope">
<span>{{
scope.row.requestTime < 1000
?
scope.row.requestTime + '毫秒'
:
Math.round(scope.row.requestTime / 1000) + '秒'
}}</span>
</template>
</el-table-column>
<el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true"/>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
<el-button
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDelete(scope.row)"
v-hasPermi="['log:maillog:remove']"
>删除
</el-button>
</template>
</el-table-column>
</el-table>
<pagination
v-show="total>0"
:total="total"
:page.sync="queryParams.pageNum"
:limit.sync="queryParams.pageSize"
@pagination="getList"
/>
</div>
</template>
<script>
import {listMaillog, delMaillog,} from "@/api/business/log/maillog";
import {pickerOptions} from "@/layout/mixin/PickerOptions";
export default {
mixins: [pickerOptions],
name: "Maillog",
data() {
return {
//
loading: true,
//
ids: [],
//
single: true,
//
multiple: true,
//
showSearch: true,
//
total: 0,
//
maillogList: [],
//
daterangeCreateTime: [],
//
title: "",
//
open: false,
//
queryParams: {
pageNum: 1,
pageSize: 10,
title: null,
content: null,
recipient: null,
},
//
form: {},
//
rules: {}
};
},
created() {
this.getList();
},
methods: {
/** 查询邮件日志列表 */
getList() {
this.loading = true;
if (null != this.daterangeCreateTime && '' !== this.daterangeCreateTime) {
this.queryParams.createTime = this.daterangeCreateTime[0];
this.queryParams.endCreateTime = this.daterangeCreateTime[1];
}
listMaillog(this.queryParams).then(response => {
this.maillogList = response.rows;
this.total = response.total;
this.loading = false;
});
},
//
cancel() {
this.open = false;
this.reset();
},
dateQuery(formName) {
//
this.queryParams.createTime=null
this.queryParams.endCreateTime=null
this.handleQuery(formName);
},
//
reset() {
this.form = {
id: null,
title: null,
content: null,
recipient: null,
mailType: null,
requestTime: null,
createTime: null
};
this.resetForm("form");
},
/** 搜索按钮操作 */
handleQuery() {
this.queryParams.pageNum = 1;
this.getList();
},
/** 重置按钮操作 */
resetQuery() {
this.resetForm("queryForm");
this.handleQuery();
},
//
handleSelectionChange(selection) {
this.ids = selection.map(item => item.id)
this.single = selection.length !== 1
this.multiple = !selection.length
},
/** 删除按钮操作 */
handleDelete(row) {
const ids = row.id || this.ids;
this.$modal.confirm('是否确认删除邮件日志编号为"' + ids + '"的数据项?').then(function () {
return delMaillog(ids);
}).then(() => {
this.getList();
this.$modal.msgSuccess("删除成功");
}).catch(() => {
});
},
/** 导出按钮操作 */
handleExport() {
this.download('log/maillog/export', {
...this.queryParams
}, `maillog_${new Date().getTime()}.xlsx`)
}
}
};
</script>

@ -210,7 +210,7 @@ export default {
/** 查询爬虫日志列表 */
getList() {
this.loading = true;
if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
if (null != this.daterangeCreateTime && '' !== this.daterangeCreateTime) {
this.queryParams.createTime = this.daterangeCreateTime[0];
this.queryParams.endCreateTime = this.daterangeCreateTime[1];
}

@ -6,7 +6,7 @@ import java.lang.annotation.*;
/**
* api
* @author xiejs
* @create 2021-12-26
* @since 2021-12-26
*/
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)

@ -0,0 +1,14 @@
package com.xjs.annotation;
import java.lang.annotation.*;
/**
*
* @author xiejs
* @since 2022-04-13
*/
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MailLog {
}

@ -0,0 +1,97 @@
package com.xjs.maillog.controller;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.xjs.maillog.domain.MailLog;
import com.xjs.maillog.service.MailLogService;
import com.xjs.validation.group.SelectGroup;
import com.xjs.web.MyBaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* Controller
*
* @author xiejs
* @since 2022-04-14
*/
@RestController
@RequestMapping("/maillog")
@Api(tags = "业务模块-邮件日志")
@Transactional
public class MailLogController extends MyBaseController<MailLog> {
@Autowired
private MailLogService mailLogService;
//---------------------------远程调用-----------------------------------
@PostMapping("saveForRPC")
@ApiOperation("保存邮件日志ForRPC")
public R<Object> saveMailLog(@RequestBody MailLog mailLog) {
boolean save = mailLogService.save(mailLog);
return save ? R.ok() : R.fail();
}
//---------------------------代码生成-----------------------------------
/**
*
*/
@RequiresPermissions("log:maillog:list")
@GetMapping("/list")
@ApiOperation("查询邮件日志列表")
public TableDataInfo list(@Validated({SelectGroup.class}) MailLog mailLog) {
startPage();
List<MailLog> list = mailLogService.selectMailLogList(mailLog);
return getDataTable(list);
}
/**
*
*/
@RequiresPermissions("log:maillog:export")
@Log(title = "邮件日志", businessType = BusinessType.EXPORT)
@PostMapping("/export")
@ApiOperation("导出邮件日志列表")
public void export(HttpServletResponse response, MailLog mailLog) {
List<MailLog> list = mailLogService.selectMailLogList(mailLog);
ExcelUtil<MailLog> util = new ExcelUtil<MailLog>(MailLog.class);
util.exportExcel(response, list, "邮件日志数据");
}
/**
*
*/
@RequiresPermissions("log:maillog:query")
@GetMapping(value = "/{id}")
@ApiOperation("获取邮件日志详细信息")
public AjaxResult getInfo(@PathVariable("id") Long id) {
return AjaxResult.success(mailLogService.selectMailLogById(id));
}
/**
*
*/
@RequiresPermissions("log:maillog:remove")
@Log(title = "邮件日志", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
@ApiOperation("删除邮件日志")
public AjaxResult remove(@PathVariable Long[] ids) {
return toAjax(mailLogService.deleteMailLogByIds(ids));
}
}

@ -0,0 +1,66 @@
package com.xjs.maillog.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.ruoyi.common.core.annotation.Excel;
import com.xjs.validation.group.SelectGroup;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
/**
*
*
* @author xiejs
* @since 2022-04-14
*/
@Data
public class MailLog implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
/**
*
*/
@Excel(name = "邮件主题")
@Size(max = 20, message = "请控制邮件主题长度在20字符", groups = { SelectGroup.class})
private String title;
/**
*
*/
@Excel(name = "邮件内容")
@Size(max = 20, message = "请控制邮件内容长度在20字符", groups = { SelectGroup.class})
private String content;
/**
*
*/
@Excel(name = "收件人")
@Size(max = 20, message = "请控制收件人长度在20字符", groups = { SelectGroup.class})
private String recipient;
/**
*
*/
@Excel(name = "邮件类型")
private String mailType;
/**
*
*/
@Excel(name = "请求时间")
private Long requestTime;
@Excel(name = "创建时间", dateFormat = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(exist = false)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date endCreateTime;
}

@ -0,0 +1,50 @@
package com.xjs.maillog.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjs.maillog.domain.MailLog;
import java.util.List;
/**
* Mapper
*
* @author xiejs
* @since 2022-04-14
*/
public interface MailLogMapper extends BaseMapper<MailLog> {
//---------------------------代码生成-----------------------------------
/**
*
*
* @param id
* @return
*/
public MailLog selectMailLogById(Long id);
/**
*
*
* @param mailLog
* @return
*/
public List<MailLog> selectMailLogList(MailLog mailLog);
/**
*
*
* @param id
* @return
*/
public int deleteMailLogById(Long id);
/**
*
*
* @param ids
* @return
*/
public int deleteMailLogByIds(Long[] ids);
}

@ -0,0 +1,50 @@
package com.xjs.maillog.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xjs.maillog.domain.MailLog;
import java.util.List;
/**
* Service
*
* @author xiejs
* @since 2022-04-14
*/
public interface MailLogService extends IService<MailLog> {
//---------------------------代码生成-----------------------------------
/**
*
*
* @param id
* @return
*/
MailLog selectMailLogById(Long id);
/**
*
*
* @param mailLog
* @return
*/
List<MailLog> selectMailLogList(MailLog mailLog);
/**
*
*
* @param ids
* @return
*/
int deleteMailLogByIds(Long[] ids);
/**
*
*
* @param id
* @return
*/
int deleteMailLogById(Long id);
}

@ -0,0 +1,68 @@
package com.xjs.maillog.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjs.maillog.domain.MailLog;
import com.xjs.maillog.mapper.MailLogMapper;
import com.xjs.maillog.service.MailLogService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* Service
*
* @author xiejs
* @since 2022-04-14
*/
@Service
public class MailLogServiceImpl extends ServiceImpl<MailLogMapper,MailLog> implements MailLogService {
@Resource
private MailLogMapper mailLogMapper;
//---------------------------代码生成-----------------------------------
/**
*
*
* @param id
* @return
*/
@Override
public MailLog selectMailLogById(Long id) {
return mailLogMapper.selectMailLogById(id);
}
/**
*
*
* @param mailLog
* @return
*/
@Override
public List<MailLog> selectMailLogList(MailLog mailLog) {
return mailLogMapper.selectMailLogList(mailLog);
}
/**
*
*
* @param ids
* @return
*/
@Override
public int deleteMailLogByIds(Long[] ids) {
return mailLogMapper.deleteMailLogByIds(ids);
}
/**
*
*
* @param id
* @return
*/
@Override
public int deleteMailLogById(Long id) {
return mailLogMapper.deleteMailLogById(id);
}
}

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xjs.maillog.mapper.MailLogMapper">
<resultMap type="com.xjs.maillog.domain.MailLog" id="MailLogResult">
<result property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
<result property="recipient" column="recipient" />
<result property="mailType" column="mail_type" />
<result property="requestTime" column="request_time" />
<result property="createTime" column="create_time" />
</resultMap>
<sql id="selectMailLogVo">
select id, title, content, recipient, mail_type, request_time, create_time from mail_log
</sql>
<select id="selectMailLogList" parameterType="com.xjs.maillog.domain.MailLog" resultMap="MailLogResult">
<include refid="selectMailLogVo"/>
<where>
<if test="title != null and title != ''"> and title like concat('%', #{title}, '%')</if>
<if test="content != null and content != ''"> and content like concat('%', #{content}, '%')</if>
<if test="recipient != null and recipient != ''"> and recipient like concat('%', #{recipient}, '%')</if>
<if test="createTime != null and endCreateTime != null"> and create_time between #{createTime} and #{endCreateTime}</if>
</where>
</select>
<select id="selectMailLogById" parameterType="Long" resultMap="MailLogResult">
<include refid="selectMailLogVo"/>
where id = #{id}
</select>
<delete id="deleteMailLogById" parameterType="Long">
delete from mail_log where id = #{id}
</delete>
<delete id="deleteMailLogByIds" parameterType="String">
delete from mail_log where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>

@ -0,0 +1,69 @@
package com.xjs.aop;
import cn.hutool.core.date.DateUtil;
import com.xjs.business.log.RemoteLogFeign;
import com.xjs.business.log.domain.MailLog;
import com.xjs.domain.mall.MailBean;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
*
* @author xiejs
* @since 2022-04-13
*/
@Component
@Aspect
@Log4j2
public class MailLogAspect {
@Autowired
private RemoteLogFeign remoteLogFeign;
/**
* AOP
*/
@Pointcut("@annotation(com.xjs.annotation.MailLog)")
public void pointcut() {
}
@Around("pointcut()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
//记录时间
LocalDateTime localDateTime1 = DateUtil.date().toLocalDateTime();
Object obj = joinPoint.proceed();
LocalDateTime localDateTime2 = DateUtil.date().toLocalDateTime();
long between = ChronoUnit.MILLIS.between(localDateTime1, localDateTime2);
//获取形参
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
if (arg instanceof MailBean) {
MailBean mailBean = (MailBean) arg;
//构建对象
MailLog mailLog = new MailLog();
mailLog.setMailType(mailBean.getMailType().getMsg());
mailLog.setContent(mailBean.getContent());
mailLog.setTitle(mailBean.getSubject());
mailLog.setRecipient(mailBean.getRecipient());
mailLog.setRequestTime(between);
remoteLogFeign.saveMailLog(mailLog);
}
}
return obj;
}
}

@ -1,6 +1,7 @@
package com.xjs.server;
import com.ruoyi.common.redis.service.RedisService;
import com.xjs.annotation.MailLog;
import com.xjs.domain.mall.MailBean;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
@ -50,6 +51,7 @@ public class MailServer {
*
* @param mailBean
*/
@MailLog
public Boolean sendMail(MailBean mailBean) {
if (redisService.hasKey(MAIL_SENDER)) {

@ -1,7 +1,10 @@
package com.xjs.service.impl;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.model.LoginUser;
import com.xjs.business.api.RemoteWeatherFeign;
import com.xjs.business.api.domain.NowWeather;
import com.xjs.domain.mall.MailBean;
@ -14,6 +17,7 @@ import javax.annotation.Resource;
/**
* service
*
* @author xiejs
* @since 2022-04-13
*/
@ -24,6 +28,10 @@ public class MailServiceImpl implements MailService {
private RemoteWeatherFeign remoteWeatherFeign;
@Autowired
private MailServer mailServer;
@Resource
private RemoteUserService remoteUserService;
public static final String EMAIL = "1294405880@qq.com";
@Override
public Boolean sendWeatherMail() {
@ -35,10 +43,19 @@ public class MailServiceImpl implements MailService {
MailBean mailBean = new MailBean();
mailBean.setUserName("用户");
mailBean.setSubject("天气播报");
mailBean.setRecipient("1294405880@qq.com");
//获取管理员邮件
R<LoginUser> admin = remoteUserService.getUserInfo("admin", SecurityConstants.INNER);
if (admin.getCode() == HttpStatus.SUCCESS) {
String email = admin.getData().getSysUser().getEmail();
mailBean.setRecipient(email);
} else {
mailBean.setRecipient(EMAIL);
}
mailBean.setMailType(MailBean.MailType.HTML);
mailBean.setContent("<h3>当前"+nowWeather.getCity()+"的天气:"+nowWeather.getTemperature()+"℃</h3>");
mailBean.setContent("<h3>当前" + nowWeather.getCity() + "的天气:" + nowWeather.getTemperature() + "℃,天气状况:" + nowWeather.getWeather() + "</h3>");
return mailServer.sendMail(mailBean);
}

Loading…
Cancel
Save