Email.vue 7.5 KB
Newer Older
yma16's avatar
yma16 已提交
1 2 3 4 5 6 7 8 9 10 11
<template>
  <!--  测试-->
  <!--      抽屉-->
  <el-drawer
    :title="msgDrawTitle"
    :visible.sync="drawObj.show"
    :direction="direction"
    v-loading="loading"
    element-loading-text="正在发送邮件..."
    element-loading-spinner="el-icon-loading"
    :before-close="handleDrawClose"
12 13
    :wrapperClosable="false"
    class="email-drawer"
yma16's avatar
yma16 已提交
14
  >
yma16's avatar
yma16 已提交
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
    <template v-if="drawObj.show">
      <div class="email-container">
        <div class="email-content">
          <el-form
            ref="emailForm"
            :model="emailForm"
            :rules="emailForm.rules"
            label-width="60px"
          >
            <el-form-item label="标题" prop="title">
              <el-input placeholder="标题" v-model="emailForm.title" clearable>
              </el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input placeholder="邮箱" v-model="emailForm.email" clearable>
              </el-input>
            </el-form-item>
            <el-form-item label="内容" prop="content">
              <el-input
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }"
                placeholder="请输入邮件内容"
                v-model="emailForm.content"
                clearable
              >
              </el-input>
            </el-form-item>
            <div class="email-submit">
              <el-button type="primary" @click="sendEmail('emailForm')"
                >发送</el-button
              >
            </div>
          </el-form>
        </div>
        <div class="email-footer">
          <p>联系邮箱</p>
          <p>1432448610@qq.com</p>
        </div>
yma16's avatar
yma16 已提交
53
      </div>
54
    </template>
yma16's avatar
yma16 已提交
55 56 57 58
  </el-drawer>
</template>
<script>
export default {
yma16's avatar
yma16 已提交
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
    props: {
        drawObj: Object
    },
    data () {
        return {
            msgDrawTitle: '~邮件沟通~',
            direction: 'rtl',
            msgDraw: false,
            baseUrl: '/api/',
            basePath: 'send-email/',
            loading: false,
            emailForm: {
                title: '',
                email: '',
                content: '',
                rules: {
                    title: [
                        {
                            type: 'string',
                            required: true,
                            message: '标题不能为空',
                            trigger: 'blur'
                        }
                    ],
                    email: [
                        {
                            type: 'string',
                            required: true,
                            message: '邮箱不能为空',
                            trigger: 'blur'
                        },
                        {
                            validator: (callback, value) => {
                                if (value) {
                                    //  验证邮箱
                                    let patter =
                    /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
                                    let testEmail = patter.test(value)
                                    if (!testEmail) {
                                        return Promise.reject('邮箱格式有误')
                                    }
                                }
                                return Promise.resolve('')
                            },
                            trigger: 'blur'
                        }
                    ],
                    content: [
                        {
                            type: 'string',
                            required: true,
                            message: '内容不能为空',
                            trigger: 'blur'
                        }
                    ]
yma16's avatar
yma16 已提交
114
                }
yma16's avatar
yma16 已提交
115 116
            }
        }
yma16's avatar
yma16 已提交
117
    },
yma16's avatar
yma16 已提交
118 119 120 121 122 123 124 125 126 127 128 129
    methods: {
    // 关闭抽屉
        handleDrawClose () {
            const that = this
            that.drawObj.show = false
            that.emailForm.title = ''
            that.emailForm.email = ''
            that.emailForm.content = ''
        },
        //  发送消息
        sendEmail (formName) {
            //  发送email内容
yma16's avatar
yma16 已提交
130
            try {
yma16's avatar
yma16 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
                const that = this
                that.loading = true
                that.$refs[formName].validate((valid) => {
                    if (valid) {
                        const params = {
                            title: that.emailForm.title,
                            email: that.emailForm.email,
                            content: that.emailForm.content
                        }
                        try {
                            this.$axios
                                .post(that.baseUrl + that.basePath, params)
                                .then((res) => {
                                    if (
                                        res &&
yma16's avatar
yma16 已提交
146 147 148
                    res.data &&
                    res.data.code &&
                    res.data.code === 20000
yma16's avatar
yma16 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
                                    ) {
                                        that.loading = false
                                        that.$message({
                                            message: '邮件发送成功!',
                                            type: 'success'
                                        })
                                        that.handleDrawClose()
                                    } else {
                                        that.loading = false
                                        that.$message({
                                            message: '邮件发送失败!',
                                            type: 'warning'
                                        })
                                    }
                                })
                                .catch((r) => {
                                    that.loading = false
                                    that.$message({
                                        message: '邮件发送失败!',
                                        type: 'warning'
                                    })
                                    throw Error(r)
                                })
                        } catch (r) {
                            that.loading = false
                            that.$message({
                                message: '邮件发送失败!',
                                type: 'warning'
                            })
                            throw Error(r)
                        }
                    } else {
                        that.loading = false
                    }
yma16's avatar
yma16 已提交
183
                })
yma16's avatar
yma16 已提交
184
            } catch (r) {
yma16's avatar
yma16 已提交
185 186
                that.loading = false
                throw Error(r)
yma16's avatar
yma16 已提交
187
            }
yma16's avatar
yma16 已提交
188 189 190
        }
    }
}
yma16's avatar
yma16 已提交
191 192
</script>
<style>
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
.el-drawer__open .el-drawer.rtl {
  background: rgba(212, 190, 180, 1); /* fallback for old browsers */
  background: -webkit-linear-gradient(
    to left,
    rgba(212, 190, 180, 1),
    rgba(255, 255, 255, 1)
  ); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(
    to left,
    rgba(212, 190, 180, 1),
    rgba(255, 255, 255, 1)
  ); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  /*overflow: hidden;*/
}
.el-drawer__body {
  overflow: hidden;
}
yma16's avatar
yma16 已提交
210
.email-container {
yma16's avatar
yma16 已提交
211
  padding: 10px;
yma16's avatar
yma16 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
}
.email-content {
  margin: 10px;
}
.email-submit {
  float: right;
}
.email-title,
.email-email {
  width: 50%;
  margin: 10px;
  display: block;
}
.email-footer {
  float: left;
  width: 100%;
  margin-left: 30px;
  text-align: left;
  font-size: 12px;
}
</style>