send.vue
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<template>
<div>
<el-form ref="form" :model="form" :rules="rules" style="margin-top: 6px;" size="small" label-width="100px">
<el-form-item label="邮件标题" prop="subject">
<el-input v-model="form.subject" style="width: 646px" />
</el-form-item>
<el-form-item
v-for="(domain, index) in tos"
:key="domain.key"
:label="'收件邮箱' + (index === 0 ? '': index)"
>
<el-input v-model="domain.value" style="width: 550px" />
<el-button icon="el-icon-plus" @click="addDomain" />
<el-button style="margin-left:0;" icon="el-icon-minus" @click.prevent="removeDomain(domain)" />
</el-form-item>
<div ref="editor" class="editor" />
<el-button :loading="loading" style="margin-left:1.6%;" size="medium" type="primary" @click="doSubmit">发送邮件</el-button>
</el-form>
</div>
</template>
<script>
import { send } from '@/api/tools/email'
import { upload } from '@/utils/upload'
import { validEmail } from '@/utils/validate'
import { mapGetters } from 'vuex'
import E from 'wangeditor'
export default {
name: 'Index',
data() {
return {
loading: false, form: { subject: '', tos: [], content: '' },
tos: [{
value: ''
}],
rules: {
subject: [
{ required: true, message: '标题不能为空', trigger: 'blur' }
]
}
}
},
computed: {
...mapGetters([
'imagesUploadApi'
])
},
mounted() {
const _this = this
var editor = new E(this.$refs.editor)
// 自定义菜单配置
editor.customConfig.zIndex = 10
// 文件上传
editor.customConfig.customUploadImg = function(files, insert) {
// files 是 input 中选中的文件列表
// insert 是获取图片 url 后,插入到编辑器的方法
files.forEach(image => {
files.forEach(image => {
upload(_this.imagesUploadApi, image).then(data => {
insert(data.data.url)
})
})
})
}
editor.customConfig.onchange = (html) => {
this.form.content = html
}
editor.create()
},
methods: {
removeDomain(item) {
var index = this.tos.indexOf(item)
if (index !== -1 && this.tos.length !== 1) {
this.tos.splice(index, 1)
} else {
this.$message({
message: '请至少保留一位联系人',
type: 'warning'
})
}
},
addDomain() {
this.tos.push({
value: '',
key: Date.now()
})
},
doSubmit() {
const _this = this
this.$refs['form'].validate((valid) => {
this.form.tos = []
if (valid) {
let sub = false
this.tos.forEach(function(data, index) {
if (data.value === '') {
_this.$message({
message: '收件邮箱不能为空',
type: 'warning'
})
sub = true
} else if (validEmail(data.value)) {
_this.form.tos.push(data.value)
} else {
_this.$message({
message: '收件邮箱格式错误',
type: 'warning'
})
sub = true
}
})
if (sub) { return false }
this.loading = true
send(this.form).then(res => {
this.$notify({
title: '发送成功',
type: 'success',
duration: 2500
})
this.loading = false
}).catch(err => {
this.loading = false
console.log(err.response.data.message)
})
} else {
return false
}
})
}
}
}
</script>
<style scoped>
.editor{
text-align:left;
margin: 20px;
width: 730px;
}
::v-deep .w-e-text-container {
height: 360px !important;
}
</style>