【sgCreateQrcode】自定义组件:模仿草料二维码做了一个简单的二维码制作组件

sgCreateQrcode

html 复制代码
<template>
  <div :class="$options.name" v-if="visible">
    <!-- 如果不加v-if="visible"弹窗中使用el-tabs组件就会死循环卡死,这个是elementUI的bug -->
    <el-dialog
      :append-to-body="true"
      :close-on-click-modal="true"
      :close-on-press-escape="true"
      :custom-class="`${$options.name}-el-dialog`"
      :destroy-on-close="true"
      :fullscreen="false"
      :show-close="true"
      :title="`生成二维码`"
      :width="`800px`"
      :visible.sync="visible"
      style="animation: none"
      center
    >
      <div
        style="
          margin: -20px 0 0px;
          max-height: 600px;
          overflow-y: hidden;
          display: flex;
          justify-content: flex-start;
        "
        v-loading="loading"
        :element-loading-text="elementLoadingText"
        radius-loading
      >
        <!-- 弹窗内容 -->

        <div class="col" style="width: 300px">
          <sgQrcode
            ref="sgQrcode"
            :data="{
              text: text,
              title: title,
              logoSrc: logoSrc,
              type: `image`,
              size: 300,
              titleStyle: { fontSize: `24px`, paddingBottom: title ? `10px` : 0 },
            }"
          />
        </div>
        <div class="col" style="margin-left: 10px">
          <el-form @submit.native.prevent :readonly="disabled" label-position="right">
            <el-form-item label="标题" :label-width="labelWidth">
              <el-input
                v-model="title"
                :placeholder="`输入内容`"
                :maxlength="20"
                clearable
              />
            </el-form-item>
            <el-form-item label="Logo" :label-width="labelWidth">
              <el-button type="" @click="(uploadBtn || {}).click()"
                >本地上传...</el-button
              >
            </el-form-item>
          </el-form>
          <div class="foot" style="margin-left: 10px; margin-top: 30px">
            <div>
              <el-button @click="cancel">取消</el-button>
              <el-button
                type="primary"
                @click="downloadQrcode"
                v-if="!disabled"
                :loading="loading"
                >下载</el-button
              >
              <el-button
                @click="go2CLEWM"
                style="background-color: #00a13b; color: white; border-color: #00a13b"
                type="default"
                >复制链接制作更好看的二维码</el-button
              >
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
    <el-upload
      ref="upload"
      :accept="`*`"
      :action="`#`"
      :auto-upload="false"
      :multiple="true"
      :on-change="getUploadFiles"
      :show-file-list="false"
      :dragenter="isDragenter"
      :drag="false"
    />
    <!-- 如果drag=true,accept必须为具体的格式,否则无法监听on-change -->
  </div>
</template>
<script>
import sgQrcode from "@/vue/components/admin/sgQrcode";
export default {
  name: `sgCreateQrcode`,
  components: { sgQrcode },
  data() {
    return {
      loading: false, //加载状态
      elementLoadingText: ``, //加载提示文字

      visible: false,
      form: {},
      disabled: false, //是否只读
      labelWidth: `60px`,
      title: `微信扫一扫`,
      logoSrc: `static/favicon.png`,
      uploadBtn: null, //上传触发按钮
      isDragenter: false, //是否拖拽进入
    };
  },
  props: [`value`, `data`],

  watch: {
    loading(newValue, oldValue) {
      newValue || (this.elementLoadingText = ``);
    },

    value: {
      handler(d) {
        this.visible = d;
      },
      deep: true,
      immediate: true,
    },
    visible(d) {
      this.$emit(`input`, d);
    },

    data: {
      handler(newValue, oldValue) {
        //console.log(`深度监听${this.$options.name}:`, newValue, oldValue);
        if (Object.keys(newValue || {}).length) {
          this.form = this.$g.deepCopy(newValue);
          this.$g.cF2CP(`disabled`, this);
          this.$g.cF2CP(`text`, this);
        }
      },
      deep: true, //深度监听
      immediate: true, //立即执行
    },
  },
  created() {},
  mounted() {
    this.uploadBtn = this.$refs.upload.$children[0].$refs.input;
    //this.uploadBtn.webkitdirectory = true;//让el-upload支持上传文件夹
  },

  beforeDestroy() {},
  methods: {
    // 获取上传文件----------------------------------------
    getUploadFiles(file) {
      file = file.raw;
      this.logoSrc = URL.createObjectURL(file);
      // console.log(`获取上传文件:`, file);
    },
    go2CLEWM(d) {
      this.$g.copy(this.text);
      this.$g.go2RouterPath(
        { path: `http://cli.im/url`, query: {}, target: `_blank` },
        this
      );
    },
    valid(d) {
      if (!this.form.NAME) return this.$message.error(this.$refs.NAME.$attrs.placeholder);
    },

    downloadQrcode(d) {
      this.$g.downloadImg({
        dom: this.$refs.sgQrcode.$refs[`vue-qr-container`],
        fileName: `分享二维码.png`,
      });
    },
    cancel() {
      this.visible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.sgCreateQrcode {
}

>>> .sgCreateQrcode-el-dialog {
  .form-body {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    overflow-y: hidden;
  }
}
</style>

demo

html 复制代码
<template>
  <div :class="$options.name">
    <el-button
      type=""
      @click="
        rw_createQrcode({
          d: { text: `${$g.getWebURLBeforeHash()}/static/html/chooseSeat/` },
        })
      "
      >生成二维码</el-button
    >

    <sgCreateQrcode
      :data="data_createQrcode"
      v-model="show_createQrcode"
      v-if="show_createQrcode"
      @s="g()"
    />
  </div>
</template>
<script>
import sgCreateQrcode from "@/vue/components/admin/sgCreateQrcode";
export default {
  components: { sgCreateQrcode },
  data() {
    return {
      data_createQrcode: {},
      show_createQrcode: false,
    };
  },

  methods: {
    rw_createQrcode({ w = true, d = {} } = {}) {
      this.data_createQrcode = this.$g.deepCopy(d);
      this.data_createQrcode.disabled = !w;
      this.show_createQrcode = true;
    },
  },
};
</script>
相关推荐
M ? A42 分钟前
你的 Vue 3 响应式状态,VuReact 如何生成 React Hooks 依赖数组?
前端·javascript·vue.js·经验分享·react.js·面试·vureact
competes1 小时前
React.js JavaScript前端技术脚本运行框架。程序员进行研发组项目现场工作落地的一瞬之间适应性恒强说明可塑性强度达到应用架构师的考核标准
前端·javascript·人工智能·react.js·java-ee·ecmascript
jiayong231 小时前
第 25 课:给学习笔记页加上搜索、标签筛选和 URL 同步
开发语言·前端·javascript·vue.js·学习
jiayong231 小时前
第 12 课:`watch` 和防抖到底该怎么用
前端·javascript·vue.js
im_AMBER2 小时前
Leetcode 158 数组中的第K个最大元素 | 查找和最小的 K 对数字
javascript·数据结构·算法·leetcode·
bug修复工程师2 小时前
Vue 项目高德地图性能优化实战:从卡死到丝滑的完整过程
vue.js
qq_12084093712 小时前
Three.js 场景性能优化实战:首屏、帧率与内存的工程化治理
开发语言·javascript·性能优化·three.js
Ruihong2 小时前
Vue v-if 转 React:VuReact 怎么处理?
vue.js·react.js·面试
竹林8182 小时前
Solana前端开发:从连接钱包到发送交易,我如何用@solana/web3.js搞定第一个DApp
前端·javascript
军军君012 小时前
数字孪生监控大屏实战模板:商圈大数据监控
前端·javascript·vue.js·typescript·前端框架·echarts·three