Files
Avideo/dist/core/utils/index.utils.js
2024-09-27 01:25:59 +08:00

207 lines
9.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
var OptionParams_model_1 = require("../model/OptionParams.model");
var index_config_1 = require("../config/index.config");
var Ioc_annotation_1 = require("../annotation/Ioc.annotation");
var Hls = require("hls.js");
var VideoMimeType_config_1 = require("../config/VideoMimeType.config");
var Dom_annotation_1 = require("../annotation/Dom.annotation");
var Utils = /** @class */ (function () {
function Utils() {
}
Utils.prototype.$ = function (el) {
return document.querySelector(el);
};
/**
* 自动调用类中的事件绑定方法
* @param params 类数组
*/
Utils.prototype.callMethods = function (params) {
setTimeout(function () {
return params.forEach(function (val) {
return Object.getOwnPropertyNames(val.prototype).splice(1).filter(function (f) {
return f.includes("BindEvent");
}).forEach(function (v) {
return (new val())[v]();
});
});
}, 400);
};
/**
* 修改元素类名
* @param dom
* @param className
*/
Utils.prototype.addClassName = function (dom, className) {
dom.classList.toggle(className);
};
/**
* 视同用户填入的模板传入配置渲染输出HTML
*/
Utils.prototype.getLayout = function () {
var render = require("../../assets/tpl/" + OptionParams_model_1.default.OptionParams.layout + ".pug");
return render({ params: Object.assign(OptionParams_model_1.default.OptionParams, index_config_1.Config.IconConfig) });
};
/**
* 将 string 转换为 dom 元素
* @param arg
* @constructor
*/
Utils.prototype.ParseDom = function (arg) {
var objE = document.createElement("div");
objE.innerHTML = arg;
return objE.childNodes[0];
};
Utils.prototype.Render = function () {
// // 获取dom树中最后一个child也就是 script
// let oldScript = father.lastChild;
// // 从dom树中删除 script 代码因为不管是innerHTML还是appendChild dom中的script都不执行
// // 所以需要特殊处理
// father.removeChild(father.childNodes[father.childNodes.length-1])
// // 将去除 script 后的dom树插入到网页中
// el.appendChild(father);
//
// // 生成 script写入 js插入 到网页中这时候js可以执行
// let newScript = document.createElement('script');
// newScript.type = 'text/javascript';
// newScript.innerHTML = (oldScript as HTMLElement).innerHTML;
// el.appendChild(newScript);
};
/**
* 使用fetch 获取视频地址的编码类型,
* 然后采用不同的播放方式
* @constructor
*/
Utils.prototype.CheckVideoType = function (URL) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fetch("http://localhost:9000/check/url?url=" + URL)
.then(function (res) {
return res.json();
})
.then(function (res) {
switch (res.result) {
// HLS 播放支持
case VideoMimeType_config_1.VideoMimeType.HLS:
_this.PlayHls(_this.Video, URL);
break;
// MP4播放支持
case VideoMimeType_config_1.VideoMimeType.MP4:
_this.Video.src = URL;
break;
// MP4不同编码的播放支持
case VideoMimeType_config_1.VideoMimeType.STREA:
_this.Video.src = URL;
break;
default:
throw new Error("不支持播放的视频地址");
}
})];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
/**
* 下载文件
* @param href 文件地址
* @param fileType 文件类型
*/
Utils.prototype.downLoadFile = function (href, fileType) {
if (fileType === void 0) { fileType = 'image/png'; }
var dlLink = document.createElement('a');
dlLink.download = "AVideo_" + (new Date()).valueOf().toString();
dlLink.href = href;
dlLink.dataset.downloadurl = [fileType, dlLink.download, dlLink.href].join(':');
document.body.appendChild(dlLink);
dlLink.click();
document.body.removeChild(dlLink);
};
/**
* 格式化时间
* @param t
*/
Utils.prototype.formatTime = function (t) {
if (isNaN(t)) {
return "00:00:00";
}
var h = parseInt(String(t / 3600));
h = h < 10 ? '0' + h : h;
var m = parseInt(String(t % 3600 / 60));
m = m < 10 ? '0' + m : m;
var s = parseInt(String(t % 60));
s = s < 10 ? '0' + s : s;
return h + ':' + m + ':' + s;
};
/**
* 使用插件 hls.js 播放视频
* @param Video
* @param url
* @constructor
*/
Utils.prototype.PlayHls = function (Video, url) {
var hls = new Hls();
hls.loadSource(url);
hls.attachMedia(Video);
};
__decorate([
Dom_annotation_1.Dom(),
__metadata("design:type", HTMLVideoElement)
], Utils.prototype, "Video", void 0);
Utils = __decorate([
Ioc_annotation_1.Injectable()
], Utils);
return Utils;
}());
exports.Utils = Utils;