@fle-ui/plus-im-record
Version:
197 lines (196 loc) • 7.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.concatMsgs = concatMsgs;
exports.copyText = copyText;
exports.default = void 0;
exports.downloadByUrl = downloadByUrl;
exports.getDateDiff = getDateDiff;
exports.getSessionIdFromMsg = getSessionIdFromMsg;
exports.getSimplifyMsg = getSimplifyMsg;
exports.matchs = void 0;
exports.paserAttach = paserAttach;
var _const = require("../const");
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
/* eslint-disable radix */
function getSessionIdFromMsg(msg, myAccount) {
// 在群中,不管 from 是不是自己,sessionId 都要取 to
var from = msg.from,
to = msg.to,
scene = msg.scene;
var target = '';
if (scene === 'p2p') {
if (from === myAccount) {
target = to;
} else {
target = from;
}
} else {
target = to;
}
var sessionId = "".concat(scene, "-").concat(target);
return sessionId;
}
function paserAttach() {
var attach = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if (attach.content && typeof attach.content === 'string' && ![_const.imMsgTypes.tip, _const.imMsgTypes.text].includes(attach.type)) {
var defaultObj = [_const.imMsgTypes.faq].includes(attach.type) ? [] : {};
try {
attach.content = JSON.parse(attach.content);
} catch (error) {
console.log('paser', error);
attach.content = defaultObj;
}
}
return attach;
}
function getSimplifyMsg() {
var attach = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var type = attach.type,
content = attach.content;
if ([_const.imMsgTypes.text, _const.imMsgTypes.tip].includes(type)) return content;
if (type === _const.imMsgTypes.faqLink) return content.title;
if (type === _const.imMsgTypes.event) return content.tips;
if (type === _const.imMsgTypes.audio) return '[音频]...';
if (type === _const.imMsgTypes.faq) return '[猜你想问]...';
if (type === _const.imMsgTypes.file) return '[文件]...';
if (type === _const.imMsgTypes.formCustomer) return '[表单]...';
if (type === _const.imMsgTypes.image) return '[图片]...';
if (type === _const.imMsgTypes.video) return '[视频]...';
if (type === _const.imMsgTypes.link) return '[链接]...';
if (type === _const.imMsgTypes.order) return '[订单卡片]...';
if (type === _const.imMsgTypes.product) return '[商品卡片]...';
if (type === _const.imMsgTypes.chatrecord) return '[转接记录]...';
}
function concatMsgs() {
var arr1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var arr2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var idMap = {};
var result = [];
[].concat(_toConsumableArray(arr1), _toConsumableArray(arr2)).forEach(function () {
var item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if (!idMap[item.idServer]) {
result.push(item);
idMap[item.idServer] = true;
}
});
result.sort(function (a, b) {
return a.time - b.time;
});
return result;
}
// 时间戳转多少分钟之前
function getDateDiff(dateTimeStamp) {
var timestamp = dateTimeStamp || new Date().getTime();
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var month = day * 30;
var year = day * 365;
var now = new Date().getTime();
var diffValue = now - timestamp;
var result;
if (diffValue < 0) {
return '刚刚';
}
var yearC = Math.floor(diffValue / year);
var monthC = Math.floor(diffValue / month);
var weekC = Math.floor(diffValue / (7 * day));
var dayC = Math.floor(diffValue / day);
var hourC = Math.floor(diffValue / hour);
var minC = Math.floor(diffValue / minute);
if (yearC >= 1) {
result = "".concat(yearC, "\u5E74\u524D");
} else if (monthC >= 1) {
result = "".concat(monthC, "\u6708\u524D");
} else if (weekC >= 1) {
result = "".concat(weekC, "\u5468\u524D");
} else if (dayC >= 1) {
result = "".concat(dayC, "\u5929\u524D");
} else if (hourC >= 1) {
result = "".concat(hourC, "\u5C0F\u65F6\u524D");
} else if (minC >= 1) {
result = "".concat(minC, "\u5206\u949F\u524D");
} else result = '刚刚';
return result;
}
function downloadByUrl(src) {
fetch(src).then(function (response) {
return response.blob();
}).then(function (blob) {
var url = URL.createObjectURL(new Blob([blob]));
var link = document.createElement('a');
link.href = url;
link.download = 'image.png';
document.body.appendChild(link);
link.click();
URL.revokeObjectURL(url);
link.remove();
});
}
/**
* im-record可能运行在wujie子应用中
* 子应用的复制行为会被劫持
* 需要调用主应用提供的能力
*/
function getClipboardAPI() {
// @ts-ignore
if (window.__POWERED_BY_WUJIE__ || window.$wujie) {
var _window$parent$naviga, _window$parent$window;
return ((_window$parent$naviga = window.parent.navigator) === null || _window$parent$naviga === void 0 ? void 0 : _window$parent$naviga.clipboard) || ((_window$parent$window = window.parent.window.navigator) === null || _window$parent$window === void 0 ? void 0 : _window$parent$window.clipboard);
}
return navigator.clipboard;
}
/**
* 将文字写入剪切板
* @param {string} text
* @returns {Promise} 返回promise对象
*/
function copyText() {
var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var clipboard = getClipboardAPI();
if (clipboard === null || clipboard === void 0 ? void 0 : clipboard.writeText) {
return clipboard.writeText(text);
} else {
// 传统降级方案
try {
var t = document.createElement('textarea');
t.nodeValue = text;
t.value = text;
document.body.appendChild(t);
t.select();
document.execCommand('copy');
document.body.removeChild(t);
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
}
}
var matchs = exports.matchs = {
paserProductFromUrl: function paserProductFromUrl(str) {
if (!str) return null;
var res = str.match(/^https*(.+?)(product\/product_info|productDetail|goodsDetail)\/(\d+)$/);
return res === null || res === void 0 ? void 0 : res[3];
},
paserOrderFromUrl: function paserOrderFromUrl(str) {
if (!str) return null;
var res = str.match(/^https*(.+?)(order\/order_detail.+?order_id=)(\d+)$/);
return res === null || res === void 0 ? void 0 : res[3];
}
};
var _default = exports.default = {
getSessionIdFromMsg: getSessionIdFromMsg,
paserAttach: paserAttach,
getDateDiff: getDateDiff,
downloadByUrl: downloadByUrl,
copyText: copyText,
getSimplifyMsg: getSimplifyMsg,
matchs: matchs
};