sms-test-send-dialog.vue 5.34 KB
<template>
  <el-dialog
    title="测试发送"
    :visible.sync="visible"
    width="640px"
    append-to-body
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="90px" size="small">
      <el-form-item label="场景">
        <el-select v-model="form.sceneCode" style="width: 100%" @change="onSceneChange">
          <el-option label="生日 Birthday" value="Birthday" />
          <el-option label="登录 Login" value="Login" />
          <el-option label="注册 Register" value="Register" />
          <el-option label="验证码 VerifyCode" value="VerifyCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.mobile" clearable placeholder="测试手机号" maxlength="11" />
      </el-form-item>
      <el-form-item label="姓名变量">
        <el-input v-model="form.name" clearable placeholder="{name}" />
      </el-form-item>
      <el-form-item v-if="form.sceneCode !== 'Birthday'" label="验证码">
        <el-input v-model="form.code" clearable placeholder="{code}" maxlength="8" />
      </el-form-item>
    </el-form>

    <div class="sms-test-actions">
      <el-button type="primary" size="small" :loading="testSending" @click="doTestSend">测试发送</el-button>
      <el-button size="small" :loading="previewLoading" @click="doPreviewBirthday">预览今日生日</el-button>
      <el-button type="warning" size="small" :loading="birthdaySending" @click="doSendBirthday(true)">试跑(不发)</el-button>
      <el-button type="danger" size="small" :loading="birthdaySending" @click="confirmSendBirthday">发送今日生日短信</el-button>
    </div>

    <p v-if="resultText" class="sms-test-result">{{ resultText }}</p>

    <div slot="footer">
      <el-button size="small" @click="visible = false">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  sendSmsByScene,
  previewBirthdayMembers,
  sendBirthdaySms
} from '@/api/extend/lqSms'

const emptyForm = () => ({
  mobile: '',
  name: '测试会员',
  code: '123456',
  sceneCode: 'Birthday'
})

export default {
  name: 'SmsTestSendDialog',
  data() {
    return {
      visible: false,
      testSending: false,
      previewLoading: false,
      birthdaySending: false,
      resultText: '',
      form: emptyForm()
    }
  },
  methods: {
    open() {
      this.form = emptyForm()
      this.resultText = ''
      this.visible = true
    },
    handleClose() {
      this.form = emptyForm()
      this.resultText = ''
    },
    onSceneChange() {
      if (this.form.sceneCode !== 'Birthday' && !this.form.code) {
        this.form.code = '123456'
      }
    },
    doTestSend() {
      if (!this.form.mobile) {
        this.$message.warning('请填写测试手机号')
        return
      }
      this.testSending = true
      const variables = { name: this.form.name || '测试会员' }
      if (this.form.sceneCode !== 'Birthday') {
        variables.code = this.form.code || '123456'
      }
      sendSmsByScene({
        sceneCode: this.form.sceneCode,
        mobile: this.form.mobile,
        variables,
        memberName: this.form.name,
        isTest: true,
        skipDuplicateCheck: true
      }).then(res => {
        const data = (res && res.data) || {}
        const tip = `发送完成:${data.statusName || ''} ${data.providerResult || ''}`
        this.resultText = tip
        this.$message.success(tip)
        this.$emit('success')
      }).finally(() => {
        this.testSending = false
      })
    },
    doPreviewBirthday() {
      this.previewLoading = true
      previewBirthdayMembers({}).then(res => {
        const data = (res && res.data) || {}
        this.resultText = `${data.bizDate || ''} 共 ${data.total || 0} 位生日会员(有手机号方可发送)`
      }).finally(() => {
        this.previewLoading = false
      })
    },
    doSendBirthday(dryRun) {
      this.birthdaySending = true
      sendBirthdaySms({ dryRun: !!dryRun }).then(res => {
        const data = (res && res.data) || {}
        if (dryRun) {
          this.resultText = `试跑:候选 ${data.total || 0} 人,有手机号 ${data.withMobile || 0} 人`
          this.$message.success('试跑完成(未实际发送)')
        } else {
          this.resultText = `发送完成:成功 ${data.success || 0} / 失败 ${data.failed || 0} / 跳过 ${data.skipped || 0}`
          this.$message.success('生日短信发送完成')
          this.$emit('success')
        }
      }).finally(() => {
        this.birthdaySending = false
      })
    },
    confirmSendBirthday() {
      this.$confirm('确认向今日生日会员发送短信?将产生阿里云短信费用。', '确认发送', {
        type: 'warning'
      }).then(() => {
        this.doSendBirthday(false)
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.sms-test-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  gap: 8px;
  margin: 4px 0 12px;
}

.sms-test-result {
  margin: 0;
  padding: 10px 12px;
  border-radius: 4px;
  background: #f4f4f5;
  color: #606266;
  font-size: 13px;
  line-height: 1.6;
}
</style>

<style lang="scss">
/* append-to-body:暗黑需挂 body / #app */
body.ncc-content-dark .sms-test-result,
#app .ncc-content-dark .sms-test-result {
  background: rgba(255, 255, 255, 0.08);
  color: rgba(255, 255, 255, 0.75);
}
</style>