123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- import moment from "moment";
- import { post, get } from "@/utils/request";
- import Cookies from "js-cookie";
- import html2canvas from "html2canvas";
- import JsPDF from "jspdf";
- import * as toEnglish from "./ACapital.js";
- //根据value值回显字典label值
- export function dictDataEcho(value, arr) {
- if (value && arr) {
- value = value + "";
- const current = arr.find((x) => x.dictKey === value);
- if (current != undefined && current.dictValue) {
- return current.dictValue;
- }
- return "";
- }
- return "";
- }
- //根据value值回显字典label值
- export function dictValueLabel(value, arr) {
- if ((value || value === 0) && arr) {
- const current = arr.filter((x) => x.value == value);
- if (current && current.length > 0) {
- return current[0].label;
- }
- return "";
- }
- return "";
- }
- export function getDict(key) {
- return new Promise((resolve, reject) => {
- let dictObj = {};
- let arr = {};
- let num = 0;
- if (!sessionStorage.getItem("dict")) {
- sessionStorage.setItem("dict", JSON.stringify(dictObj));
- } else {
- dictObj = JSON.parse(sessionStorage.getItem("dict"));
- }
- for (let i = 0; i < key.length; i++) {
- const element = key[i];
- if (dictObj[element]) {
- arr[element] = dictObj[element];
- num++;
- if (num === key.length) {
- resolve(arr);
- }
- } else {
- post("/dictTenantData/page", {
- pageNum: 1,
- pageSize: 999,
- tenantId: Cookies.get("tenantId"),
- dictCode: element,
- }).then((res) => {
- dictObj[element] = res.rows;
- arr[element] = res.rows;
- sessionStorage.setItem("dict", JSON.stringify(dictObj));
- num++;
- console.log(num);
- if (num === key.length) {
- resolve(arr);
- }
- });
- }
- }
- });
- }
- // 金额千分符
- export function moneyFormat(s, n) {
- if (s) {
- s = s + "";
- let str = s.slice(0, 1);
- if (str === "-") {
- s = s.slice(1, s.length);
- }
- n = n > 0 && n <= 20 ? n : 2;
- s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
- var l = s.split(".")[0].split("").reverse(),
- r = s.split(".")[1];
- var t = "";
- for (let i = 0; i < l.length; i++) {
- t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? "," : "");
- }
- let result = t.split("").reverse().join("") + "." + r;
- if (str === "-") {
- return "-" + result;
- } else {
- return result;
- }
- } else {
- return "0.00";
- }
- }
- // 计算该日期是星期几
- export function calculationWeek(val, format) {
- let weekArrayList = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
- let index = new Date(moment(val, format).format("yyyy-MM-DD")).getDay();
- let week = weekArrayList[index];
- return week;
- }
- export function getPdf(title) {
- // pdfDom 这个就是你vue页面中定义的ID 比如<div id="pdfDom"> 这个也要避下雷
- const element = document.getElementById("pdfDom");
- window.pageYoffset = 0;
- document.documentElement.scrollTop = 0;
- document.body.scrollTop = 0;
- setTimeout(() => {
- // const nowDate = new Date();
- // const date = {
- // year: nowDate.getFullYear(),
- // month: nowDate.getMonth() + 1,
- // date: nowDate.getDate(),
- // hours: nowDate.getHours(),
- // minutes: nowDate.getMinutes(),
- // seconds: nowDate.getSeconds(),
- // };
- // const newMonth = date.month > 10 ? date.month : "0" + date.month;
- // const newDay = date.date > 10 ? date.date : "0" + date.date;
- // const newMinutes = date.minutes < 10 ? "0" + date.minutes : date.minutes;
- // const newSeconds = date.seconds < 10 ? "0" + date.seconds : date.seconds;
- // const value = date.year + newMonth + newDay + date.hours + newMinutes + newSeconds;
- let id = "";
- //创建一个画布 --- 增加导出的pdf水印 !!
- let can = document.createElement("canvas");
- //设置画布的长宽
- can.width = 400;
- can.height = 500;
- let cans = can.getContext("2d");
- //旋转角度
- cans.rotate((-15 * Math.PI) / 180);
- cans.font = "18px Vedana";
- //设置填充绘画的颜色、渐变或者模式
- cans.fillStyle = "rgba(200, 200, 200, 0.40)";
- //设置文本内容的当前对齐方式
- cans.textAlign = "left";
- //设置在绘制文本时使用的当前文本基线
- cans.textBaseline = "Middle";
- //在画布上绘制填色的文本(输出的文本,开始绘制文本的X坐标位置,开始绘制文本的Y坐标位置)
- //cans.fillText(value, can.width / 8, can.height / 2)
- let div = document.createElement("div");
- div.id = id;
- div.style.pointerEvents = "none";
- div.style.top = "2000px";
- div.style.left = "-2000px";
- div.style.position = "fixed";
- div.style.zIndex = "100000";
- div.style.width = document.getElementById("pdfDom").scrollHeight + "px";
- div.style.height = document.getElementById("pdfDom").scrollHeight + "px";
- div.style.background = "url(" + can.toDataURL("image/png") + ") left top repeat";
- document.getElementById("pdfDom").appendChild(div); // 到页面中
- html2canvas(element, {
- allowTaint: true,
- useCORS: true, // 需要注意,element的 高度 宽度一定要在这里定义一下,不然会存在只下载了当前你能看到的页面 避雷避雷!!!
- scale: 2, // 提升画面质量,但是会增加文件大小
- height: document.getElementById("pdfDom").scrollHeight,
- windowHeight: document.getElementById("pdfDom").scrollHeight,
- }).then(function (canvas) {
- var contentWidth = canvas.width;
- var contentHeight = canvas.height;
- // 一页pdf显示html页面生成的canvas高度;
- var pageHeight = (contentWidth * 841.89) / 592.28;
- // 未生成pdf的html页面高度
- var leftHeight = contentHeight;
- // 页面偏移
- var position = 0;
- // a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
- var imgWidth = 595.28;
- var imgHeight = (592.28 / contentWidth) * contentHeight;
- var pageData = canvas.toDataURL("image/jpeg", 1.0);
- var pdf = new JsPDF("", "pt", "a4");
- // 有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
- // 当内容未超过pdf一页显示的范围,无需分页
- if (leftHeight < pageHeight) {
- pdf.addImage(pageData, "JPEG", 0, 0, imgWidth, imgHeight);
- } else {
- while (leftHeight > 0) {
- pdf.addImage(pageData, "JPEG", 0, position, imgWidth, imgHeight);
- leftHeight -= pageHeight;
- position -= 841.89;
- // 避免添加空白页
- if (leftHeight > 0) {
- pdf.addPage();
- }
- }
- }
- pdf.save(title + ".pdf");
- });
- }, 1000);
- }
- export function currencyPrefix(key) {
- if (["¥", "¥", "1"].includes(key)) {
- return "SAY RMB ";
- } else if (["$", "2"].includes(key)) {
- return "SAY US DOLLARS ";
- } else if (["€", "3"].includes(key)) {
- return "SAY EURO ";
- } else {
- return "SAY RMB ";
- }
- }
- export function translateIntoEnglish(money, currencyType) {
- let text = "";
- if (money) {
- text = currencyPrefix(currencyType);
- if (!/^\d+$/.test(Number(money))) {
- text = text + parseFloat(money).toFixed(2).toEnglish().toUpperCase();
- } else {
- text = text + parseFloat(money).toFixed(2).toEnglish().toUpperCase() + " ONLY";
- }
- }
- text = text.split(" POINT ZERO ZERO ").join("");
- return text;
- }
|