cube-ui组件upload在form中使用的动态action解决方案

直接上代码

<cube-form-item :field="fields[2]" class="cube-form-none-border">
  <div class="upload-box">
    <p>上传营业执照照片</p>
    <cube-upload
      ref="businessLicenseUrl"
      v-model="model.businessLicenseUrl"
      :action="businessLicenseAction"
      @files-added="addedHandler('businessLicenseUrl')"
      @file-error="errHandler"
    >
      <div class="clear-fix">
        <cube-upload-file
          v-for="(file, i) in model.businessLicenseUrl"
          :file="file"
          :key="i"
        ></cube-upload-file>

        <cube-upload-btn :multiple="false">
          <div>
            <i></i>
            <p>上传营业执照照片</p>
          </div>
        </cube-upload-btn>
      </div>
    </cube-upload>
  </div>
</cube-form-item>

JS:

  data() {
    return {
      action: "http://xuan.zhaoshang800.com/place/appointment/upload",
      hasData: true,
      title: "在线预约",
      uploadSuc: false,
      isIPhoneX: Global.config.isIPhoneX(),
      isIOS: !!Global.config.ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
      showCustomTitle: true,
      toastText: "",
      showToast: false,
      validity: {},
      valid: undefined,
      organizationCode: "",
      businessLicenseAction: {
        target: () => {
          return this.action + "?type=3&code=" + this.model.organizationCode;
        },
        checkSuccess: (res, file) => {
          console.log("res", res);
          if (res.code == 179) {
            this.createToastFun();
          } else if (res.code == 200) {
            return true;
          }
        }
      },
       fields: [
        fields.input(
          "entName",
          { placeholder: "企业名称", maxlength: 50 },
          /^[a-zA-Z\u4e00-\u9fa5]+$/
        ),
        fields.input(
          "organizationCode",
          { placeholder: "统一社会信用代码", maxlength: 18 },
          creditReg
        ),
        {
          type: "upload",
          modelKey: "businessLicenseUrl",
          props: {
            multiple: false,
            max: 1
          },
          events: {
            "file-removed": (...args) => {
              console.log("file removed", args);
            }
          },
          rules: {
            required: true,
            uploaded: (val, config) => {
              return Promise.all(
                val.length &&
                  val.map((file, i) => {
                    return new Promise((resolve, reject) => {
                      if (file.uploadedUrl) {
                        return resolve(val);
                      }
                      // fake request
                      setTimeout(() => {
                        if (i % 2) {
                          reject(new Error());
                        } else {
                          file.uploadedUrl = "uploaded/url";
                          resolve(val);
                        }
                      }, 1500);
                    });
                  })
              ).then(res => {
                return true;
              });
            }
          },
          messages: {
            uploaded: "上传失败"
          }
        },
      ]
    }

 

 

 

 

 

 

 

 

 

 

 

发布了153 篇原创文章 · 获赞 52 · 访问量 23万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览