首页 > 其他分享 >图片生成加下载

图片生成加下载

时间:2022-11-11 17:12:04浏览次数:38  
标签:flex canvas img infoObj 生成 let display 下载 图片

 

 

<template>
  <div>
    <!-- 表具报停 -->
    <Drawer
      :title="type == 2 ? '表具二维码' : '表具报停'"
      :closable="false"
      v-model="DrawerShow"
      width="50%"
    >
      <div>
        <div class="t-head">
          <div>
            <span>表具编号:{{ infoObj.meterNo }}</span>
            <span>表具类型:{{ METERTYPE[infoObj.meterType] }}</span>
            <span>表具形式:{{ RECORDWAY[infoObj.recordWay] }}</span>
          </div>
          <div>
            <span>表具初始读数:{{ infoObj.degree }}</span>
            <span>安装日期:{{ infoObj.createTime }}</span>
            <span
              >预警类型:{{ infoObj.warnType ? infoObj.warnType : "/" }}</span
            >
          </div>
        </div>
        <!-- 报停 -->
        <div style="display: flex; margin-top: 20px" v-if="type == 1">
          <Form
            ref="stopFormRef"
            :model="stopForm"
            :rules="stopFormRule"
            :label-width="120"
            :label-colon="true"
            style="width: 100%"
          >
            <FormItem label="报停人" prop="reportStop">
              <Input
                style="'width:380px"
                type="text"
                maxlength="32"
                v-model="stopForm.reportStop"
                placeholder="请输入报停人"
              />
            </FormItem>
            <FormItem label="报停说明" prop="remark">
              <Input
                style="'width:380px"
                type="textarea"
                maxlength="200"
                :rows="6"
                v-model="stopForm.remark"
                placeholder="请输入报停说明"
              />
            </FormItem>
            <FormItem>
              <Button
                type="primary"
                :disabled="btnType"
                @click="handleSubmit('stopFormRef')"
                >提交
              </Button>
              <Button @click="goBack()" style="margin-left: 8px">返回</Button>
            </FormItem>
          </Form>
        </div>
        <!-- 二维码 -->
        <div class="cont-w" v-if="type == 2">
          <div class="qrcode-img">
            <img :src="imgUrl" alt="" />
            <Button type="primary" :disabled="btnType" @click="imgdown()"
              >导出
            </Button>
            <p>注:二维码导出后,可添加到设备的表面,便于扫码查询</p>
          </div>
        </div>
        <div class="btn-box" v-if="type == 2">
          <Button @click="goBack()" style="margin-left: 8px">返回</Button>
        </div>
      </div>
    </Drawer>
  </div>
</template>
<script>
import { addReportStop, getMeterQRCode } from "@/api/Instrumentmanagement.js";
import { formatDate } from "@/libs";
import _GLOBAL from "@/libs/config";
export default {
  components: {},
  props: {
    infoObj: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      METERTYPE: _GLOBAL.meterType,
      RECORDWAY: _GLOBAL.recordWay,
      DrawerShow: false,
      stopForm: {
        reportStop: "",
        remark: "",
      },
      stopFormRule: {
        reportStop: [
          {
            required: true,
            message: "请输入报停人",
            trigger: "blur",
          },
        ],
        remark: [
          {
            required: true,
            message: "请输入报停说明",
            trigger: "blur",
          },
        ],
      },
      btnType: false,
      // meterId: "",
      type: "",
      id_: "",
      imgUrl: "",
    };
  },
  watch: {},
  filters: {
    chengearmTime(armTime) {
      return formatDate(Number(armTime), "yyyy-MM-dd hh:mm:ss");
    },
  },
  mounted() {},
  created() {},
  methods: {
    show(row, type) {
      this.DrawerShow = true;
      this.handleReset();
      this.type = type;
      this.id_ = row.id;
      this.imgUrl =
        this.$store.state.app.getMeterQRCode +
        "?meterId=" +
        row.id +
        "&author-token-key=" +
        localStorage.getItem("token");
    },
    imgdown() {
      let src = this.imgUrl;
      let canvas = document.createElement("canvas");
      let img = document.createElement("img");
      let meterNo = this.infoObj.meterNo;
      // // 解决跨域 Canvas 污染问题
      img.setAttribute("crossOrigin", "Anonymous");
      img.onload = function (e) {
        canvas.width = img.width;
        canvas.height = img.height;
        let context = canvas.getContext("2d");
        //绘制图片
        context.drawImage(img, 0, 0, img.width, img.height);
        canvas.getContext("2d").drawImage(img, 0, 0, img.width, img.height);
        //将canvas转base64码,然后创建一个a连接自动下载图片
        canvas.toBlob((blob) => {
          let link = document.createElement("a");
          link.href = window.URL.createObjectURL(blob);
          link.download = meterNo + "二维码";
          link.click();
        }, "image/jpeg");
      };
      //将资源链接赋值过去,才能触发img.onload事件
      img.src = src;
    },
    //提交
    handleSubmit(name) {
      this.btnType = true;
      setTimeout(() => {
        this.btnType = false;
      }, 2000);
      this.$refs[name].validate((valid) => {
        if (valid) {
          const params = Object.assign({}, this.stopForm);
          params.meterId = this.infoObj.id;
          addReportStop(params).then((res) => {
            if (res.data.status == 200) {
              this.$Message.success("提交成功");
              //回到列表
              this.DrawerShow = false;
              this.$emit("update");
            } else {
              this.$Modal.error({
                title: "提示",
                content: res.data.desc,
              });
            }
          });
        }
      });
    },
    //返回
    goBack() {
      this.DrawerShow = false;
      this.handleReset();
    },
    //重置
    handleReset() {
      Object.keys(this.stopForm).forEach((key) => {
        this.stopForm[key] = "";
      });
    },
  },
  destroyed() {},
};
</script>
<style lang="less" scoped>
.t-head {
  display: flex;
  justify-content: space-between;
  margin-left: 12px;
  flex-direction: column;
  div {
    display: flex;
    margin: 5px 0;
    span {
      display: flex;
      flex: 1;
    }
  }
}
.cont-w {
  display: flex;
  margin-top: 20px;
  justify-content: center;
  align-items: center;
  .qrcode-img {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    img {
      width: 220px;
      height: 220px;
      overflow: hidden;
    }
    button {
      margin: 25px 0;
    }
    p {
      margin: 25px 0;
    }
  }
}
</style>

 

标签:flex,canvas,img,infoObj,生成,let,display,下载,图片
From: https://www.cnblogs.com/Byme/p/16881095.html

相关文章

  • 个人学习笔记附Markdown格式下载
    B/S方向Java与生活「MySQL」从零到删库步入Linux的现代方法面向对象大胆向前JavaAPI实战1、笔记由Yeats_Liao与Shea_Qiu共同完成,​​​​​2、以上所有内容来自​​Micro......
  • python代码批量压缩图片
    python代码批量压缩图片以下代码用于批量压缩png/jpg格式的图片文件,遇到报错就使用pip大法安装一下对应的类库就可以了dynamic_quality.pyimportPIL.Imagefrommathi......
  • 开源一段 Mac 批量压缩图片的脚本
    近日处理了一批照片,现在分享一下如何在mac平台进行图片批量处理。0、安装xCode命令行工具,需要确定电脑上已经安装了xCode,如果没有,自己去AppStore里面搜索就看到了。......
  • java通过cglib动态生成实体bean的操作
    转载自:https://www.jb51.net/article/205882.htm maven依赖:12345678910<dependency>      <groupId>commons-beanutils</groupId>   ......
  • 【杭州专场】蚂蚁单测自动生成产品体验活动招募开启
    「单元测试产品体验样板间」是由蚂蚁集团技术风险团队&企业级应用设计团队联合举办,针对于单元测试产品发起的线下用户体验活动,旨在邀请相关领域的技术同学亲临现场互动交流......
  • RGBA 图片格式转换 RGB 无损
    拿到的图片是webp格式的,然后系统无法处理。只能使用googlecode的dwebp工具,把图片转成png或者jpg的。这一步是ok了。但是转成的图片是RGBA的,系统只能处理RGB。。。报了下面......
  • java使用mybatis-generator实现自动生成model、dao、xml
    pom中添加插件<plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifa......
  • UEditor的word图片转存
    百度ueditor新增的将word内容导入到富文本编辑框的功能怎么没有啊,...ueditor实现word文档的导入和下载功能的方法:1、UEditor没有提供word的导入功能,只能说是粘贴复制。2......
  • 【实操日记】使用 PyQt5 设计下载远程服务器日志文件程序
    最近通过PyQt5设计了一个下载服务器指定日期日志文件的程序,里面有些有意思的技术点,现在做一些分享。PyQt5是一套Python绑定DigiaQt5应用的框架,是最强大的GUI库......
  • OS rpm包下载地址
    在做操作系统适配的时候提示缺对应的rpm包银河麒麟OSrpm包下载地址https://update.cs2c.com.cn/NS/V10/V10SP1.1/os/adv/lic/base/aarch64/Packages/ openeuler的rpm......