Email.vue 6.1 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 59 60 61 62 63 64 65 66 67 68
  </el-drawer>
</template>
<script>
//
import axios from "axios";
export default {
  props: {
    drawObj: Object,
  },
  data() {
    return {
      msgDrawTitle: "~邮件沟通~",
      direction: "rtl",
      msgDraw: false,
yma16's avatar
yma16 已提交
69
      baseUrl: "/api/",
yma16's avatar
yma16 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
      basePath: "send-email/",
      loading: false,
      emailForm: {
        title: "",
        email: "",
        content: "",
        rules: {
          title: [
            {
              type: "string",
              required: true,
              message: "标题不能为空",
              trigger: "blur",
            },
          ],
          email: [
            {
              type: "string",
              required: true,
yma16's avatar
yma16 已提交
89
              message: "邮箱不能为空",
yma16's avatar
yma16 已提交
90 91 92 93 94 95 96
              trigger: "blur",
            },
            {
              validator: (callback, value) => {
                if (value) {
                  //  验证邮箱
                  let patter =
97
                    /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
yma16's avatar
yma16 已提交
98 99
                  let testEmail = patter.test(value);
                  if (!testEmail) {
yma16's avatar
yma16 已提交
100
                    return Promise.reject("邮箱格式有误");
yma16's avatar
yma16 已提交
101 102
                  }
                }
yma16's avatar
yma16 已提交
103
                return Promise.resolve("");
yma16's avatar
yma16 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
              },
              trigger: "blur",
            },
          ],
          content: [
            {
              type: "string",
              required: true,
              message: "内容不能为空",
              trigger: "blur",
            },
          ],
        },
      },
    };
  },
  methods: {
    //关闭抽屉
yma16's avatar
yma16 已提交
122
    handleDrawClose() {
yma16's avatar
yma16 已提交
123 124
      const that = this;
      that.drawObj.show = false;
125 126 127
      that.emailForm.title = "";
      that.emailForm.email = "";
      that.emailForm.content = "";
yma16's avatar
yma16 已提交
128 129 130 131
    },
    //  发送消息
    sendEmail(formName) {
      //  发送email内容
yma16's avatar
yma16 已提交
132 133 134 135 136 137 138 139 140 141 142
      try {
        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 {
yma16's avatar
yma16 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156
              axios
                .post(that.baseUrl + that.basePath, params)
                .then((res) => {
                  if (
                    res &&
                    res.data &&
                    res.data.code &&
                    res.data.code === 20000
                  ) {
                    that.loading = false;
                    that.$message({
                      message: "邮件发送成功!",
                      type: "success",
                    });
yma16's avatar
yma16 已提交
157
                    that.handleDrawClose()
yma16's avatar
yma16 已提交
158 159 160 161 162 163 164 165 166
                  } else {
                    that.loading = false;
                    that.$message({
                      message: "邮件发送失败!",
                      type: "warning",
                    });
                  }
                })
                .catch((r) => {
yma16's avatar
yma16 已提交
167 168 169 170 171
                  that.loading = false;
                  that.$message({
                    message: "邮件发送失败!",
                    type: "warning",
                  });
yma16's avatar
yma16 已提交
172 173
                  throw Error(r);
                });
yma16's avatar
yma16 已提交
174 175
            } catch (r) {
              that.loading = false;
yma16's avatar
yma16 已提交
176 177 178 179
              that.$message({
                message: "邮件发送失败!",
                type: "warning",
              });
yma16's avatar
yma16 已提交
180 181 182
              throw Error(r);
            }
          } else {
yma16's avatar
yma16 已提交
183 184
            that.loading = false;
          }
yma16's avatar
yma16 已提交
185 186 187 188 189
        });
      } catch (r) {
        that.loading = false;
        throw Error(r);
      }
yma16's avatar
yma16 已提交
190 191 192 193 194
    },
  },
};
</script>
<style>
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
.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 已提交
212
.email-container {
yma16's avatar
yma16 已提交
213
  padding: 10px;
yma16's avatar
yma16 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}
.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>