smsTemplate.vue 18.9 KB
Newer Older
sunhongwei's avatar
sunhongwei committed
1 2 3 4 5
<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="150px">
Marcus's avatar
Marcus committed
6 7
      <el-form-item :label="$t('短信类型')" prop="type">
        <el-select v-model="queryParams.type" :placeholder="$t('请选择短信类型')" clearable>
sunhongwei's avatar
sunhongwei committed
8 9 10 11
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_SMS_TEMPLATE_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
Marcus's avatar
Marcus committed
12 13
      <el-form-item :label="$t('开启状态')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('请选择开启状态')" clearable>
sunhongwei's avatar
sunhongwei committed
14 15 16 17
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
Marcus's avatar
Marcus committed
18 19
      <el-form-item :label="$t('模板编码')" prop="code">
        <el-input v-model="queryParams.code" :placeholder="$t('请输入模板编码')" clearable @keyup.enter.native="handleQuery"/>
sunhongwei's avatar
sunhongwei committed
20 21 22 23
      </el-form-item>
      <el-form-item label="短信 API 的模板编号" prop="apiTemplateId">
        <el-input v-model="queryParams.apiTemplateId" placeholder="请输入短信 API 的模板编号" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
24 25 26
      <el-form-item label="短信 API 的模板编号" prop="apiTemplateIdEn">
        <el-input v-model="queryParams.apiTemplateIdEn" placeholder="请输入英文短信 API 的模板编号" clearable size="small" @keyup.enter.native="handleQuery"/>
      </el-form-item>
Marcus's avatar
Marcus committed
27 28
      <el-form-item :label="$t('短信渠道')" prop="channelId">
        <el-select v-model="queryParams.channelId" :placeholder="$t('请选择短信渠道')" clearable>
sunhongwei's avatar
sunhongwei committed
29 30 31 32 33
          <el-option v-for="channel in channelOptions"
                     :key="channel.id" :value="channel.id"
                     :label="channel.signature + '【' + getDictDataLabel(DICT_TYPE.SYSTEM_SMS_CHANNEL_CODE, channel.code) + '】'" />
        </el-select>
      </el-form-item>
Marcus's avatar
Marcus committed
34
      <el-form-item :label="$t('创建时间')">
sunhongwei's avatar
sunhongwei committed
35 36 37 38
        <el-date-picker v-model="dateRangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item>
Marcus's avatar
Marcus committed
39 40
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('重置') }}</el-button>
sunhongwei's avatar
sunhongwei committed
41 42 43 44 45 46 47
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
Marcus's avatar
Marcus committed
48
                   v-hasPermi="['system:sms-template:create']">{{ $t('新增') }}</el-button>
sunhongwei's avatar
sunhongwei committed
49 50 51
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
Marcus's avatar
Marcus committed
52
                   v-hasPermi="['system:sms-template:export']">{{ $t('导出') }}</el-button>
sunhongwei's avatar
sunhongwei committed
53 54 55 56 57 58
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
59 60
      <el-table-column :label="$t('模板编号')" align="center" prop="id" />
      <el-table-column :label="$t('模板编码')" align="center" prop="code" />
Marcus's avatar
Marcus committed
61 62
      <el-table-column :label="$t('模板编码')" align="center" prop="code" />
      <el-table-column :label="$t('模板名称')" align="center" prop="name" />
houjn@hikoon.cn's avatar
houjn@hikoon.cn committed
63
      <el-table-column label="中文API模板编号" align="center" prop="apiTemplateId" width="120" />
Marcus's avatar
Marcus committed
64 65
      <el-table-column :label="$t('中文模板内容')" align="center" prop="content" width="200" />
      <el-table-column :label="$t('短信类型')" align="center" prop="type">
sunhongwei's avatar
sunhongwei committed
66 67 68 69
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_SMS_TEMPLATE_TYPE" :value="scope.row.type"/>
        </template>
      </el-table-column>
Marcus's avatar
Marcus committed
70
      <el-table-column :label="$t('开启状态')" align="center" prop="status">
sunhongwei's avatar
sunhongwei committed
71 72 73 74
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status"/>
        </template>
      </el-table-column>
houjn@hikoon.cn's avatar
houjn@hikoon.cn committed
75
      <el-table-column label="英文API模板编号" align="center" prop="apiTemplateIdEn" width="120" />
Marcus's avatar
Marcus committed
76 77
      <el-table-column :label="$t('英文模板内容')" align="center" prop="contentEn" width="100" />
      <el-table-column :label="$t('短信渠道')" align="center" width="120">
sunhongwei's avatar
sunhongwei committed
78 79 80 81 82
        <template slot-scope="scope">
          <div>{{ formatChannelSignature(scope.row.channelId) }}</div>
          <dict-tag :type="DICT_TYPE.SYSTEM_SMS_CHANNEL_CODE" :value="scope.row.channelCode"/>
        </template>
      </el-table-column>
Marcus's avatar
Marcus committed
83
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime" width="180">
sunhongwei's avatar
sunhongwei committed
84 85 86 87
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
Marcus's avatar
Marcus committed
88
      <el-table-column :label="$t('备注')" align="center" prop="remark" />
89

Marcus's avatar
Marcus committed
90
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="150">
sunhongwei's avatar
sunhongwei committed
91 92
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-share" @click="handleSendSms(scope.row)"
Marcus's avatar
Marcus committed
93
                     v-hasPermi="['system:sms-template:send-sms']">{{ $t('测试') }}</el-button>
sunhongwei's avatar
sunhongwei committed
94
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
Marcus's avatar
Marcus committed
95
                     v-hasPermi="['system:sms-template:update']">{{ $t('修改') }}</el-button>
sunhongwei's avatar
sunhongwei committed
96
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
Marcus's avatar
Marcus committed
97
                     v-hasPermi="['system:sms-template:delete']">{{ $t('删除') }}</el-button>
sunhongwei's avatar
sunhongwei committed
98 99 100 101 102 103 104 105 106 107
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
Marcus's avatar
Marcus committed
108 109
        <el-form-item :label="$t('短信渠道编号')" prop="channelId">
          <el-select v-model="form.channelId" :placeholder="$t('请选择短信渠道编号')">
sunhongwei's avatar
sunhongwei committed
110 111 112 113 114
            <el-option v-for="channel in channelOptions"
                       :key="channel.id" :value="channel.id"
                       :label="channel.signature + '【' + getDictDataLabel(DICT_TYPE.SYSTEM_SMS_CHANNEL_CODE, channel.code) + '】'" />
          </el-select>
        </el-form-item>
Marcus's avatar
Marcus committed
115 116
        <el-form-item :label="$t('短信类型')" prop="type">
          <el-select v-model="form.type" :placeholder="$t('请选择短信类型')">
sunhongwei's avatar
sunhongwei committed
117 118 119 120
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_SMS_TEMPLATE_TYPE)"
                       :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
Marcus's avatar
Marcus committed
121 122
        <el-form-item :label="$t('模板编号')" prop="code">
          <el-input v-model="form.code" :placeholder="$t('请输入模板编号')" />
sunhongwei's avatar
sunhongwei committed
123
        </el-form-item>
Marcus's avatar
Marcus committed
124 125
        <el-form-item :label="$t('模板名称')" prop="name">
          <el-input v-model="form.name" :placeholder="$t('请输入模板名称')" />
sunhongwei's avatar
sunhongwei committed
126
        </el-form-item>
127 128


Marcus's avatar
Marcus committed
129
        <el-form-item :label="$t('开启状态')" prop="status">
sunhongwei's avatar
sunhongwei committed
130 131 132 133 134
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                      :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
135
        <el-form-item label="中文短信API模板编号" prop="apiTemplateId">
sunhongwei's avatar
sunhongwei committed
136 137
          <el-input v-model="form.apiTemplateId" placeholder="请输入短信 API 的模板编号" />
        </el-form-item>
Marcus's avatar
Marcus committed
138 139
        <el-form-item :label="$t('模板内容')" prop="content">
          <el-input type="textarea" v-model="form.content" :placeholder="$t('请输入模板内容')" />
140 141 142 143
        </el-form-item>
        <el-form-item label="英文短信API模板编号" prop="apiTemplateIdEn">
          <el-input v-model="form.apiTemplateIdEn" placeholder="请输入英文短信API模板编号" />
        </el-form-item>
Marcus's avatar
Marcus committed
144 145
        <el-form-item :label="$t('英文模板内容')" prop="contentEn">
          <el-input type="textarea" v-model="form.contentEn" :placeholder="$t('请输入英文模板内容')" />
huyufeng's avatar
huyufeng committed
146
        </el-form-item>
Marcus's avatar
Marcus committed
147 148
        <el-form-item :label="$t('备注')" prop="remark">
          <el-input v-model="form.remark" :placeholder="$t('请输入备注')" />
sunhongwei's avatar
sunhongwei committed
149 150 151
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
Marcus's avatar
Marcus committed
152 153
        <el-button type="primary" @click="submitForm">{{ $t('确 定') }}</el-button>
        <el-button @click="cancel">{{ $t('取 消') }}</el-button>
sunhongwei's avatar
sunhongwei committed
154 155 156 157
      </div>
    </el-dialog>

    <!-- 对话框(发送短信) -->
Marcus's avatar
Marcus committed
158
    <el-dialog :title="$t('测试发送短信')" :visible.sync="sendSmsOpen" width="500px" append-to-body>
sunhongwei's avatar
sunhongwei committed
159
      <el-form ref="sendSmsForm" :model="sendSmsForm" :rules="sendSmsRules" label-width="140px">
Marcus's avatar
Marcus committed
160 161
        <el-form-item :label="$t('模板内容')" prop="content">
          <el-input v-model="sendSmsForm.content" type="textarea" :placeholder="$t('请输入模板内容')" readonly />
sunhongwei's avatar
sunhongwei committed
162
        </el-form-item>
Marcus's avatar
Marcus committed
163 164
        <el-form-item :label="$t('英文模板内容')" prop="contentEn">
          <el-input v-model="sendSmsForm.contentEn" type="textarea" :placeholder="$t('请输入英文模板内容')" readonly />
165 166 167
        </el-form-item>
<!--        <el-table-column-->
<!--          prop="areaCode"-->
Marcus's avatar
Marcus committed
168
<!--          :label="$t('区号')">-->
169 170 171 172
<!--          <template v-slot:header>-->
<!--            区号 <span style="color: #ff0000">*</span>-->
<!--          </template>-->
<!--&lt;!&ndash;          <template v-slot="{row}">&ndash;&gt;-->
Marcus's avatar
Marcus committed
173
<!--            <el-select v-model="areaCode" :placeholder="$t('请选择区号')" filterable>-->
174 175 176 177 178
<!--              <el-option v-for="(item, index) in countryList"-->
<!--                         :key="index" :label="item.nameShort + item.nameZh + '(' + item.tel + ')'" :value="item.tel" />-->
<!--            </el-select>-->
<!--&lt;!&ndash;          </template>&ndash;&gt;-->
<!--        </el-table-column>-->
179
        <el-form-item :label="$t('区号')" prop="areaCode">
180 181
          <el-input v-model="sendSmsForm.areaCode" :placeholder="$t('请输入区号')" />
        </el-form-item>
Marcus's avatar
Marcus committed
182 183
        <el-form-item :label="$t('手机号')" prop="mobile">
          <el-input v-model="sendSmsForm.mobile" :placeholder="$t('请输入手机号')" />
sunhongwei's avatar
sunhongwei committed
184 185 186 187 188 189
        </el-form-item>
        <el-form-item v-for="param in sendSmsForm.params" :label="'参数 {' + param + '}'" :prop="'templateParams.' + param">
          <el-input v-model="sendSmsForm.templateParams[param]" :placeholder="'请输入 ' + param + ' 参数'" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
Marcus's avatar
Marcus committed
190 191
        <el-button type="primary" @click="submitSendSmsForm">{{ $t('确 定') }}</el-button>
        <el-button @click="cancelSendSms">{{ $t('取 消') }}</el-button>
sunhongwei's avatar
sunhongwei committed
192 193 194 195 196 197 198 199 200 201
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { createSmsTemplate, updateSmsTemplate, deleteSmsTemplate, getSmsTemplate, getSmsTemplatePage,
  exportSmsTemplateExcel, sendSms } from "@/api/system/sms/smsTemplate";
import {  getSimpleSmsChannels } from "@/api/system/sms/smsChannel";
202
import {getCountryListAll} from "@/api/ecw/country";
sunhongwei's avatar
sunhongwei committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

export default {
  name: "SmsTemplate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 短信模板列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        type: null,
        status: null,
        code: null,
        content: null,
        apiTemplateId: null,
232
        apiTemplateIdEn: null,
sunhongwei's avatar
sunhongwei committed
233 234 235 236 237 238
        channelId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
Marcus's avatar
Marcus committed
239 240 241 242 243 244
        type: [{ required: true, message: this.$t("短信类型不能为空"), trigger: "change" }],
        status: [{ required: true, message: this.$t("开启状态不能为空"), trigger: "blur" }],
        code: [{ required: true, message: this.$t("模板编码不能为空"), trigger: "blur" }],
        name: [{ required: true, message: this.$t("模板名称不能为空"), trigger: "blur" }],
        content: [{ required: true, message: this.$t("模板内容不能为空"), trigger: "blur" }],
        contentEn: [{ required: false, message: this.$t("英文模板内容不能为空"), trigger: "blur" }],
245
        apiTemplateId: [{ required: true, message: "中文短信API的模板编号不能为空", trigger: "blur" }],
246
        apiTemplateIdEn: [{ required: false, message: "英文短信API的模板编号不能为空", trigger: "blur" }],
Marcus's avatar
Marcus committed
247
        channelId: [{ required: true, message: this.$t("短信渠道编号不能为空"), trigger: "change" }],
sunhongwei's avatar
sunhongwei committed
248 249 250
      },
      // 短信渠道
      channelOptions: [],
251 252
      countryList: [],
      areaCode:"",
sunhongwei's avatar
sunhongwei committed
253 254 255 256 257 258
      // 发送短信
      sendSmsOpen: false,
      sendSmsForm: {
        params: [], // 模板的参数列表
      },
      sendSmsRules: {
Marcus's avatar
Marcus committed
259 260
        mobile: [{ required: true, message: this.$t("手机不能为空"), trigger: "blur" }],
        templateCode: [{ required: true, message: this.$t("手机不能为空"), trigger: "blur" }],
sunhongwei's avatar
sunhongwei committed
261 262 263 264 265 266 267 268 269 270
        templateParams: { }
      }
    };
  },
  created() {
    this.getList();
    // 获得短信渠道
    getSimpleSmsChannels().then(response => {
      this.channelOptions = response.data;
    })
271 272 273 274
      getCountryListAll().then(r => {
        this.countryList = r.data
      })

sunhongwei's avatar
sunhongwei committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getSmsTemplatePage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        status: undefined,
        code: undefined,
        name: undefined,
        content: undefined,
        remark: undefined,
        apiTemplateId: undefined,
        channelId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
Marcus's avatar
Marcus committed
325
      this.title = this.$t("添加短信模板");
sunhongwei's avatar
sunhongwei committed
326 327 328 329 330 331 332 333
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getSmsTemplate(id).then(response => {
        this.form = response.data;
        this.open = true;
Marcus's avatar
Marcus committed
334
        this.title = this.$t("修改短信模板");
sunhongwei's avatar
sunhongwei committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateSmsTemplate(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createSmsTemplate(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除短信模板编号为"' + id + '"的数据项?').then(function() {
        return deleteSmsTemplate(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有短信模板数据项?', "警告").then(() => {
        this.exportLoading = true;
        return exportSmsTemplateExcel(params);
      }).then(response => {
        this.$download.excel(response, '短信模板.xls');
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 发送短息按钮 */
    handleSendSms(row) {
      this.resetSendSms(row);
      // 设置参数
      this.sendSmsForm.content = row.content;
391
      this.sendSmsForm.contentEn = row.contentEn;
sunhongwei's avatar
sunhongwei committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
      this.sendSmsForm.params = row.params;
      this.sendSmsForm.templateCode = row.code;
      this.sendSmsForm.templateParams = row.params.reduce(function(obj, item) {
        obj[item] = undefined;
        return obj;
      }, {});
      // 根据 row 重置 rules
      this.sendSmsRules.templateParams = row.params.reduce(function(obj, item) {
        obj[item] = { required: true, message: '参数 ' + item + " 不能为空", trigger: "change" };
        return obj;
      }, {});
      // 设置打开
      this.sendSmsOpen = true;
    },
    /** 重置发送短信的表单 */
    resetSendSms() {
      // 根据 row 重置表单
      this.sendSmsForm = {
        content: undefined,
        params: undefined,
        mobile: undefined,
        templateCode: undefined,
        templateParams: {}
      };
      this.resetForm("sendSmsForm");
    },
    /** 取消发送短信 */
    cancelSendSms() {
      this.sendSmsOpen = false;
      this.resetSendSms();
    },
    /** 提交按钮 */
    submitSendSmsForm() {
      this.$refs["sendSmsForm"].validate(valid => {
        if (!valid) {
          return;
        }
        // 添加的提交
        sendSms(this.sendSmsForm).then(response => {
          this.$modal.msgSuccess("提交发送成功!发送结果,见发送日志编号:" + response.data);
          this.sendSmsOpen = false;
        });
      });
    },
    /** 格式化短信渠道 */
    formatChannelSignature(channelId) {
      for (const channel of this.channelOptions) {
        if (channel.id === channelId) {
          return channel.signature;
        }
      }
Marcus's avatar
Marcus committed
443
      return this.$t('找不到签名:') + channelId;
sunhongwei's avatar
sunhongwei committed
444 445 446 447
    }
  }
};
</script>