mail-component.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <template>
  2. <el-dialog title="发送邮件" :visible.sync="dialogVisible" append-to-body width="50%" :close-on-click-modal="false"
  3. custom-class="email-dialog">
  4. <el-form ref="emailForm" :model="formData" :rules="formRules" label-width="80px" label-position="right">
  5. <el-form-item label="收件人" prop="to">
  6. <el-input v-model="formData.to" placeholder="多个收件人用分号(;)分隔" clearable></el-input>
  7. </el-form-item>
  8. <el-form-item label="抄送" prop="cc">
  9. <el-input v-model="formData.cc" placeholder="多个抄送人用分号(;)分隔" clearable></el-input>
  10. </el-form-item>
  11. <el-form-item label="密送" prop="bcc">
  12. <el-input v-model="formData.bcc" placeholder="多个密送人用分号(;)分隔" clearable></el-input>
  13. </el-form-item>
  14. <el-form-item label="主题" prop="subject">
  15. <el-input v-model="formData.subject" placeholder="请输入邮件主题" clearable></el-input>
  16. </el-form-item>
  17. <el-form-item label="正文" prop="content">
  18. <avue-ueditor v-model="formData.content" :options="options"></avue-ueditor>
  19. </el-form-item>
  20. <el-form-item label="附件">
  21. <el-input v-model="formData.attachments" placeholder="请输入文件名" clearable></el-input>
  22. </el-form-item>
  23. </el-form>
  24. <span slot="footer" class="dialog-footer">
  25. <el-button @click="dialogVisible = false" size="medium">取 消</el-button>
  26. <el-button type="primary" @click="submitForm" size="medium" :loading="sending">发 送</el-button>
  27. </span>
  28. </el-dialog>
  29. </template>
  30. <script>
  31. import { getToken } from "@/util/auth";
  32. import { generateMailFile } from "@/api/iosBasicData/reports";
  33. export default {
  34. name: 'EmailDialog',
  35. props: {
  36. visible: {
  37. type: Boolean,
  38. default: false
  39. }
  40. },
  41. data() {
  42. return {
  43. dialogVisible: this.visible,
  44. sending: false,
  45. options: {
  46. //普通上传地址
  47. action: "/api/blade-resource/oss/endpoint/put-file",
  48. customConfig: {
  49. menus: [
  50. 'head',
  51. 'bold',
  52. 'fontSize',
  53. 'fontName',
  54. 'italic',
  55. 'underline',
  56. 'strikeThrough',
  57. 'foreColor',
  58. 'backColor',
  59. 'link',
  60. 'list',
  61. 'justify',
  62. 'quote',
  63. 'emoticon',
  64. 'image',
  65. 'table',
  66. 'undo',
  67. 'redo'
  68. ],
  69. },
  70. headers: { "Blade-Auth": "Bearer " + getToken() },
  71. props: {
  72. res: "data",
  73. url: 'link'
  74. }
  75. },
  76. formData: {
  77. to: '',
  78. cc: '',
  79. bcc: '',
  80. subject: '',
  81. content: '',
  82. attachments: '',
  83. fileType: '',
  84. fileContent: ''
  85. },
  86. formRules: {
  87. to: [
  88. { required: true, message: '请输入收件人邮箱', trigger: 'blur' },
  89. {
  90. validator: this.validateEmails,
  91. message: '邮箱格式不正确',
  92. trigger: 'blur'
  93. }
  94. ],
  95. subject: [
  96. { required: true, message: '请输入邮件主题', trigger: 'blur' },
  97. { max: 100, message: '主题长度不能超过100个字符', trigger: 'blur' }
  98. ],
  99. content: [
  100. { required: true, message: '请输入邮件正文', trigger: 'blur' }
  101. ],
  102. attachments: [
  103. { required: true, message: '请输入附件名', trigger: 'blur' }
  104. ]
  105. },
  106. fileTypeList: [
  107. {
  108. key: 'Pdf',
  109. value: 'pdf'
  110. },
  111. {
  112. key: 'ImageSvg',
  113. value: 'svg'
  114. },
  115. {
  116. key: 'Excel2007',
  117. value: 'xls'
  118. },
  119. {
  120. key: 'Text',
  121. value: 'txt'
  122. },
  123. {
  124. key: 'Html',
  125. value: 'html'
  126. },
  127. {
  128. key: 'Word2007',
  129. value: 'doc'
  130. },
  131. {
  132. key: 'Csv',
  133. value: 'csv'
  134. },
  135. {
  136. key: 'Csv',
  137. value: 'csv'
  138. }
  139. ]
  140. }
  141. },
  142. watch: {
  143. visible(newVal) {
  144. this.dialogVisible = newVal
  145. },
  146. dialogVisible(newVal) {
  147. this.$emit('update:visible', newVal)
  148. if (!newVal) {
  149. this.resetForm()
  150. }
  151. }
  152. },
  153. methods: {
  154. validateEmails(rule, value, callback) {
  155. if (!value) return callback()
  156. const emails = value.split(';').filter(e => e.trim())
  157. const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  158. for (const email of emails) {
  159. if (!emailRegex.test(email.trim())) {
  160. return callback(new Error(rule.message))
  161. }
  162. }
  163. callback()
  164. },
  165. submitForm() {
  166. this.$refs.emailForm.validate(valid => {
  167. if (valid) {
  168. this.sendEmail()
  169. }
  170. })
  171. },
  172. sendEmail() {
  173. this.sending = true
  174. let fileContentStrList = ["ImageSvg", "Html"]
  175. let nowFile = this.fileTypeList.find(f => f.key === this.formData.fileType)
  176. let param = {
  177. fileName: this.formData.attachments,
  178. fileType: nowFile.value,
  179. sendTo: this.formData.to,
  180. mailTitle: this.formData.subject,
  181. mailContent: this.formData.content,
  182. fileContent: this.formData.fileContent,
  183. fileContentStr: ''
  184. }
  185. if (fileContentStrList.indexOf(this.formData.fileType) !== -1) {
  186. param.fileContent = null
  187. param.fileContentStr = this.formData.fileContent
  188. }
  189. generateMailFile(param).then(res => {
  190. console.info(res)
  191. })
  192. // 模拟发送请求
  193. setTimeout(() => {
  194. this.$message.success('邮件发送成功')
  195. this.sending = false
  196. this.dialogVisible = false
  197. }, 1500)
  198. },
  199. resetForm() {
  200. this.$refs.emailForm.resetFields()
  201. this.formData.attachments = ''
  202. }
  203. }
  204. }
  205. </script>
  206. <style lang="scss">
  207. .email-dialog {
  208. border-radius: 8px;
  209. box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  210. .el-dialog__header {
  211. border-bottom: 1px solid #f0f0f0;
  212. padding: 15px 20px;
  213. .el-dialog__title {
  214. font-size: 18px;
  215. font-weight: 600;
  216. color: #333;
  217. }
  218. }
  219. .el-dialog__body {
  220. padding: 20px 30px;
  221. .el-form-item {
  222. margin-bottom: 22px;
  223. .el-form-item__label {
  224. color: #666;
  225. font-weight: normal;
  226. }
  227. .el-input__inner,
  228. .el-textarea__inner {
  229. border-radius: 4px;
  230. transition: border-color 0.3s;
  231. &:focus {
  232. border-color: #409eff;
  233. box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  234. }
  235. }
  236. }
  237. }
  238. .el-dialog__footer {
  239. border-top: 1px solid #f0f0f0;
  240. padding: 15px 20px;
  241. text-align: right;
  242. .el-button {
  243. min-width: 80px;
  244. border-radius: 4px;
  245. }
  246. }
  247. }
  248. </style>