@cmstops/pro-compo
Version:
[物料平台文档中心](https://arco.design/docs/material/guide)
141 lines (140 loc) • 4.26 kB
JavaScript
import { ref, nextTick } from "vue";
import { Message } from "@arco-design/web-vue";
import QRCode from "qrcodejs2-fix";
import request from "../../utils/request.js";
import { copyContent } from "../../utils/index.js";
function generateDocPreviewLink(BASE_API, params) {
return request(BASE_API, {
url: "/poplar/v3/preview/document/generate",
method: "post",
data: params
});
}
function getTimeToNow(second) {
second = parseInt((second / 1e3).toFixed(0), 10);
function need(value, label) {
if (!value)
return "";
const tow = (n) => {
return n >= 0 && n < 10 ? `0${n}` : `${n}`;
};
return tow(value) + label;
}
const day = Math.floor(second / 86400);
second %= 86400;
const hour = Math.floor(second / 3600);
second %= 3600;
const minute = Math.floor(second / 60);
second %= 60;
return need(day, "\u5929") + need(hour, "\u5C0F\u65F6") + need(minute, "\u5206\u949F") + need(second, "\u79D2");
}
const EXPIRE_TIME_OPTIONS = [
{ label: "1\u5C0F\u65F6", value: 60 * 60 },
{ label: "6\u5C0F\u65F6", value: 60 * 60 * 6 },
{ label: "12\u5C0F\u65F6", value: 60 * 60 * 12 },
{ label: "24\u5C0F\u65F6", value: 60 * 60 * 24 },
{ label: "3\u5929", value: 60 * 60 * 24 * 3 },
{ label: "7\u5929", value: 60 * 60 * 24 * 7 },
{ label: "15\u5929", value: 60 * 60 * 24 * 15 },
{ label: "30\u5929", value: 60 * 60 * 24 * 30 }
];
function useDocPreview(BASE_API, hashid) {
const isPublish = ref(false);
const innerUrl = ref("");
const shareData = ref(null);
const expireTime = ref(60 * 60 * 24);
const loading = ref(false);
const qrCode = ref("");
const intervalTime = ref("");
let interval = null;
function startInterval() {
var _a;
if (!((_a = shareData.value) == null ? void 0 : _a.expiredAt)) {
clearInterval(interval);
interval = null;
return;
}
const now = new Date().getTime();
const expire = new Date(shareData.value.expiredAt).getTime();
intervalTime.value = getTimeToNow(expire - now);
}
function genQrCode(shareUrl, selector) {
innerUrl.value = shareUrl;
isPublish.value = true;
nextTick(() => {
const qr = new QRCode(selector, {
width: 200,
height: 200
});
qr.makeCode(shareUrl);
console.log(qr, " kdkd");
});
}
async function generate(cancel2) {
loading.value = true;
const params = { force: true, hash_id: hashid.value };
if (!cancel2)
params.expiration = expireTime.value;
else
params.expiration = 0;
try {
const { code, message } = await generateDocPreviewLink(BASE_API, params);
if (code !== 0)
return false;
const { expired_at, share_url, inner_url, qr_code } = message;
if (cancel2) {
shareData.value = null;
clearInterval(interval);
interval = null;
intervalTime.value = "";
qrCode.value = "";
} else {
const base64 = `data:image/png;base64,${qr_code}`;
shareData.value = {
qrCode: base64,
shareUrl: share_url,
expiredAt: expired_at
};
qrCode.value = base64;
innerUrl.value = `${inner_url}?reqTime=${+new Date()}`;
setInterval(startInterval, 1e3);
}
return true;
} catch (e) {
console.log("\u751F\u6210\u9884\u89C8\u94FE\u63A5\u5931\u8D25", e);
return false;
} finally {
loading.value = false;
}
}
async function cancel(update) {
const flag = await generate(true);
if (!flag)
return;
const text = update ? "\u5DF2\u53D6\u6D88\u9884\u89C8\uFF0C\u8BF7\u66F4\u65B0\u6709\u6548\u671F\u91CD\u65B0\u751F\u6210\u9884\u89C8\u94FE\u63A5" : "\u5DF2\u53D6\u6D88\u9884\u89C8\u94FE\u63A5";
Message.warning(text);
}
async function copy() {
var _a;
if (!((_a = shareData.value) == null ? void 0 : _a.shareUrl))
return;
const res = await copyContent(shareData.value.shareUrl);
if (res) {
Message.success("\u590D\u5236\u6210\u529F");
}
}
return {
genQrCode,
generate,
cancel,
copy,
qrCode,
isPublish,
loading,
expireTime,
innerUrl,
shareData,
intervalTime
};
}
export { EXPIRE_TIME_OPTIONS, generateDocPreviewLink, useDocPreview };