wechatChannelForm.vue 10.8 KB
Newer Older
sunhongwei's avatar
sunhongwei committed
1 2 3 4 5
<template>
  <div>
    <el-dialog :visible.sync="transferParam.wechatOpen" :title="title" @close="close" append-to-body width="800px">
      <el-form ref="wechatJsApiForm" :model="form" :rules="rules" size="medium" label-width="100px"
               v-loading="transferParam.loading">
Marcus's avatar
Marcus committed
6 7
        <el-form-item label-width="180px" :label="$t('渠道费率')" prop="feeRate">
          <el-input v-model="form.feeRate" :placeholder="$t('请输入渠道费率')" clearable :style="{width: '100%'}">
sunhongwei's avatar
sunhongwei committed
8 9 10 11 12 13 14
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label-width="180px" label="公众号APPID" prop="weChatConfig.appId">
          <el-input v-model="form.weChatConfig.appId" placeholder="请输入公众号APPID" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
Marcus's avatar
Marcus committed
15
        <el-form-item label-width="180px" :label="$t('商户号')" prop="weChatConfig.mchId">
sunhongwei's avatar
sunhongwei committed
16 17
          <el-input v-model="form.weChatConfig.mchId" :style="{width: '100%'}"></el-input>
        </el-form-item>
Marcus's avatar
Marcus committed
18
        <el-form-item label-width="180px" :label="$t('渠道状态')" prop="status">
sunhongwei's avatar
sunhongwei committed
19 20 21 22 23 24 25 26 27 28 29 30 31
          <el-radio-group v-model="form.status" size="medium">
            <el-radio v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="parseInt(dict.value)">
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label-width="180px" label="API 版本" prop="weChatConfig.apiVersion">
          <el-radio-group v-model="form.weChatConfig.apiVersion" size="medium">
            <el-radio v-for="dict in versionDictDatas" :key="dict.value" :label="dict.value">
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
Marcus's avatar
Marcus committed
32
        <el-form-item label-width="180px" :label="$t('商户秘钥')" prop="weChatConfig.mchKey"
sunhongwei's avatar
sunhongwei committed
33
                      v-if="form.weChatConfig.apiVersion === 'v2'">
Marcus's avatar
Marcus committed
34
          <el-input v-model="form.weChatConfig.mchKey" :placeholder="$t('请输入商户秘钥')" clearable
sunhongwei's avatar
sunhongwei committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
                    :style="{width: '100%'}" type="textarea" :autosize="{minRows: 8, maxRows: 8}"></el-input>
        </el-form-item>
        <div v-if="form.weChatConfig.apiVersion === 'v3'">
          <el-form-item label-width="180px" label="API V3秘钥" prop="weChatConfig.apiV3Key">
            <el-input v-model="form.weChatConfig.apiV3Key" placeholder="请输入API V3秘钥" clearable
                      :style="{width: '100%'}" type="textarea" :autosize="{minRows: 8, maxRows: 8}"></el-input>
          </el-form-item>
          <el-form-item label-width="180px" label="apiclient_key.perm证书" prop="weChatConfig.privateKeyContent">
            <el-input v-model="form.weChatConfig.privateKeyContent" type="textarea"
                      placeholder="请上传apiclient_key.perm证书"
                      readonly :autosize="{minRows: 8, maxRows: 8}" :style="{width: '100%'}"></el-input>
          </el-form-item>
          <el-form-item label-width="180px" label="" prop="privateKeyContentFile">
            <el-upload ref="privateKeyContentFile"
                       :limit="1"
                       :accept="fileAccept"
                       :headers="header"
                       action=""
                       :before-upload="pemFileBeforeUpload"
                       :http-request="privateKeyUpload"
            >
Marcus's avatar
Marcus committed
56
              <el-button size="small" type="primary" icon="el-icon-upload">{{ $t('点击上传') }}</el-button>
sunhongwei's avatar
sunhongwei committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
            </el-upload>
          </el-form-item>
          <el-form-item label-width="180px" label="apiclient_cert.perm证书" prop="weChatConfig.privateCertContent">
            <el-input v-model="form.weChatConfig.privateCertContent" type="textarea"
                      placeholder="请上传apiclient_cert.perm证书"
                      readonly :autosize="{minRows: 8, maxRows: 8}" :style="{width: '100%'}"></el-input>
          </el-form-item>
          <el-form-item label-width="180px" label="" prop="privateCertContentFile">
            <el-upload ref="privateCertContentFile"
                       :limit="1"
                       :accept="fileAccept"
                       :headers="header"
                       action=""
                       :before-upload="pemFileBeforeUpload"
                       :http-request="privateCertUpload"
            >
Marcus's avatar
Marcus committed
73
              <el-button size="small" type="primary" icon="el-icon-upload">{{ $t('点击上传') }}</el-button>
sunhongwei's avatar
sunhongwei committed
74 75 76
            </el-upload>
          </el-form-item>
        </div>
Marcus's avatar
Marcus committed
77
        <el-form-item label-width="180px" :label="$t('备注')" prop="remark">
sunhongwei's avatar
sunhongwei committed
78 79 80 81
          <el-input v-model="form.remark" :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
Marcus's avatar
Marcus committed
82 83
        <el-button @click="close">{{ $t('取消') }}</el-button>
        <el-button type="primary" @click="handleConfirm">{{ $t('确定') }}</el-button>
sunhongwei's avatar
sunhongwei committed
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
import {createChannel, getChannel, updateChannel} from "@/api/pay/channel";

const defaultForm = {
  code: '',
  status: null,
  remark: '',
  feeRate: null,
  appId: '',
  merchantId: null,
  weChatConfig: {
    appId: '',
    mchId: '',
    apiVersion: '',
    mchKey: '',
    privateKeyContent: '',
    privateCertContent: '',
    apiV3Key:'',
  }
}

export default {
  name: "wechatChannelForm",
  components: {},
  props: {
    // 传输的参数
    transferParam: {
      // 加载动画
      "loading": false,
      // 是否修改
      "edit": false,
      // 是否显示
      "wechatOpen": false,
      // 应用ID
      "appId": null,
      // 渠道编码
      "payCode": null,
      // 商户对象
      "payMerchant": {
        // 编号
        "id": null,
        // 名称
        "name": null
      },
    }
  },
  data() {
    return {
      title:'',
      form: JSON.parse(JSON.stringify(defaultForm)),
      rules: {
        feeRate: [{
          required: true,
Marcus's avatar
Marcus committed
142
          message: this.$t('请输入渠道费率'),
sunhongwei's avatar
sunhongwei committed
143 144 145 146
          trigger: 'blur'
        }],
        'weChatConfig.mchId': [{
          required: true,
Marcus's avatar
Marcus committed
147
          message: this.$t('请传入商户号'),
sunhongwei's avatar
sunhongwei committed
148 149 150 151 152 153 154 155 156
          trigger: 'blur'
        }],
        'weChatConfig.appId': [{
          required: true,
          message: '请输入公众号APPID',
          trigger: 'blur'
        }],
        status: [{
          required: true,
Marcus's avatar
Marcus committed
157
          message: this.$t('渠道状态不能为空'),
sunhongwei's avatar
sunhongwei committed
158 159 160 161 162 163 164 165 166
          trigger: 'blur'
        }],
        'weChatConfig.apiVersion': [{
          required: true,
          message: 'API版本不能为空',
          trigger: 'blur'
        }],
        'weChatConfig.mchKey': [{
          required: true,
Marcus's avatar
Marcus committed
167
          message: this.$t('请输入商户秘钥'),
sunhongwei's avatar
sunhongwei committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
          trigger: 'blur'
        }],
        'weChatConfig.privateKeyContent': [{
          required: true,
          message: '请上传apiclient_key.perm证书',
          trigger: 'blur'
        }],
        'weChatConfig.privateCertContent': [{
          required: true,
          message: '请上传apiclient_cert.perm证书',
          trigger: 'blur'
        }],
        'weChatConfig.apiV3Key': [{
          required: true,
          message: '请上传apiV3秘钥值',
          trigger: 'blur'
        }],
      },
      // 文件上传的header
      header: {
        "Authorization": null
      },
      fileAccept: ".pem",
      // 渠道状态 数据字典
      statusDictDatas: getDictDatas(DICT_TYPE.COMMON_STATUS),
      versionDictDatas: getDictDatas(DICT_TYPE.PAY_CHANNEL_WECHAT_VERSION),
    }
  },
  watch: {
    transferParam: {
      deep: true,  // 深度监听
      handler(newVal) {
        if (newVal.wechatOpen) {
          this.form.code = newVal.payCode;
          this.form.appId = newVal.appId;
          this.form.merchantId = newVal.payMerchant.id;
          // 只有在初次进来为编辑 并且为加载中的时候才回去请求数据
          if (newVal.edit && newVal.loading) {
Marcus's avatar
Marcus committed
206
            this.title = this.$t("编辑支付渠道");
sunhongwei's avatar
sunhongwei committed
207 208
            this.init();
          } else {
Marcus's avatar
Marcus committed
209
            this.title = this.$t("创建支付渠道");
sunhongwei's avatar
sunhongwei committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 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
          }
        }
      }
    }
  },
  methods: {
    init() {
      getChannel(this.transferParam.payMerchant.id, this.transferParam.appId, this.transferParam.payCode)
        .then(response => {
          this.form.id = response.data.id;
          this.form.feeRate = response.data.feeRate;
          this.form.appId = response.data.appId;
          this.form.status = response.data.status;
          this.form.remark = response.data.remark;

          let config = JSON.parse(response.data.config);
          this.form.weChatConfig.appId = config.appId;
          this.form.weChatConfig.apiVersion = config.apiVersion;
          this.form.weChatConfig.mchId = config.mchId;
          this.form.weChatConfig.mchKey = config.mchKey;
          this.form.weChatConfig.privateKeyContent = config.privateKeyContent;
          this.form.weChatConfig.privateCertContent = config.privateCertContent;
          this.form.weChatConfig.apiV3Key = config.apiV3Key;
          this.transferParam.loading = false;
        })
    },
    close() {
      this.transferParam.wechatOpen = false;
      this.form = JSON.parse(JSON.stringify(defaultForm));
    },
    handleConfirm() {
      this.$refs['wechatJsApiForm'].validate(valid => {
        if (!valid) {
          return
        }
        let data = this.form;
        data.config = JSON.stringify(this.form.weChatConfig);
        if (this.transferParam.edit) {
          updateChannel(data).then(response => {
            if (response.code === 0) {
              this.$modal.msgSuccess("修改成功");
              this.close();
            }

          })
        } else {

          createChannel(data).then(response => {
            if (response.code === 0) {
              this.$modal.msgSuccess("新增成功");
              this.$parent.refreshTable();
              this.close();
            }
          });
        }
      });
    },
    pemFileBeforeUpload(file) {
      let format = '.' + file.name.split(".")[1];
      if (format !== this.fileAccept) {
        this.$message.error('请上传指定格式"' + this.fileAccept + '"文件');
        return false;
      }
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
    privateKeyUpload(event) {
      const readFile = new FileReader()
      readFile.onload = (e) => {
        this.form.weChatConfig.privateKeyContent = e.target.result
      }
      readFile.readAsText(event.file);
    },
    privateCertUpload(event) {
      const readFile = new FileReader()
      readFile.onload = (e) => {
        this.form.weChatConfig.privateCertContent = e.target.result
      }
      readFile.readAsText(event.file);
    }
  }
}

</script>
<style scoped>

</style>