@bytedance/mona-client-web
Version:
web for mona
681 lines • 31.6 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["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 (g && (g = 0, op[0] && (_ = 0)), _) 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 };
}
};
// @ts-nocheck 临时发包
import './abortcontroller-polfill';
import formatPath from '@bytedance/mona-shared/dist/formatPath';
// import clipboard from 'clipboardy';
import { showPreviewImage } from './components/';
import { APP_ID, MyFetch, getAppId, getLightHeaders } from './light';
export var LIGHT_APP_GET_TOEKN = '__MONA_LIGHT_APP_GET_TOEKN';
// @ts-ignore ignore
export function webRequest(data) {
return __awaiter(this, void 0, void 0, function () {
var isLightApp, defaultHeader, controller, init, lightHeaders, appId, url, promise, timeoutId;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (typeof data.url === 'undefined' && typeof data.fn === 'undefined') {
return [2 /*return*/, Promise.reject(new Error('url and funcName must be specified'))];
}
isLightApp = data.fn;
if (data.fn && window.__LIGHT_ISV_REQ) {
return [2 /*return*/, window.__LIGHT_ISV_REQ(data)];
}
defaultHeader = {
'Content-Type': 'application/json',
};
controller = new AbortController();
init = {
headers: data.header ? data.header : defaultHeader,
method: data.method || 'GET',
signal: controller.signal,
};
if (!isLightApp) return [3 /*break*/, 2];
init.credentials = 'include';
init.method = 'POST';
return [4 /*yield*/, getLightHeaders()];
case 1:
lightHeaders = _a.sent();
init.headers = __assign(__assign({}, init.headers), lightHeaders);
appId = APP_ID || getAppId();
data.data = {
appId: appId,
method: data.fn,
param: JSON.stringify(data.data),
};
_a.label = 2;
case 2:
if (data.credentials)
init.credentials = data.credentials;
// if app not mirco app ,but set fn params, prompt waring
if (data.fn && !isLightApp) {
console.error("\u5FC5\u987B\u5728\u4E3B\u7AEF\u8C03\u7528".concat(data.fn));
}
url = isLightApp ? "https://".concat(window.__MONA_LIGNT_APP_DOMAIN_NAME || 'lgw.jinritemai.com', "/invoke") : data.url;
if (init.method.toUpperCase() === 'POST') {
init.body = data.body ? data.body : data.data ? JSON.stringify(data.data) : '';
}
promise = MyFetch(url, init);
promise
.then(function (r) { return r.json(); })
.then(function (r) {
var _a, _b;
var lightAppData;
if (isLightApp) {
if ((_a = r === null || r === void 0 ? void 0 : r.BizError) === null || _a === void 0 ? void 0 : _a.message) {
var message = r.BizError.message;
throw new Error(message);
// lightAppData = { code, data: '', message };
}
else {
var parseData = void 0;
try {
parseData = JSON.parse(r.data);
}
catch (e) {
parseData = r.data;
}
lightAppData = parseData;
}
}
(_b = data.success) === null || _b === void 0 ? void 0 : _b.call(data, {
statusCode: r.status,
header: r.headers,
data: isLightApp ? lightAppData : r,
// @ts-ignore ignore
profile: '',
});
})
.catch(function (err) {
var _a;
(_a = data.fail) === null || _a === void 0 ? void 0 : _a.call(data, {
errMsg: (typeof err === 'string' ? err : err === null || err === void 0 ? void 0 : err.message) || '未知错误',
errNo: '',
// @ts-ignore 忽略报错
toString: function () {
return (typeof err === 'string' ? err : err === null || err === void 0 ? void 0 : err.message) || '未知错误';
},
});
})
.finally();
if (data.timeout) {
controller.signal.addEventListener('abort', function () { return controller.abort(); });
}
timeoutId = setTimeout(function () { return controller.abort; }, data.timeout);
promise.finally(function () { return clearTimeout(timeoutId); });
return [2 /*return*/, {
abort: controller.abort,
}];
}
});
});
}
export var webChooseImage = function (options) {
var _a, _b;
if (options === void 0) { options = {}; }
try {
var input_1 = document.createElement('input');
input_1.type = 'file';
input_1.multiple = true;
input_1.id = 'file';
input_1.accept = 'image/*';
var event_1 = new MouseEvent('click', {
bubbles: false,
cancelable: true,
view: window,
});
input_1.dispatchEvent(event_1);
input_1.onchange = function () {
var _a;
(_a = options === null || options === void 0 ? void 0 : options.success) === null || _a === void 0 ? void 0 : _a.call(options, input_1.files);
};
}
catch (e) {
(_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: 'chooseImage:fail' });
}
(_b = options === null || options === void 0 ? void 0 : options.complete) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: 'chooseImage:complete' });
};
export var webPreviewImage = function (options) { return showPreviewImage(options); };
export var webGetImageInfo = function (options) {
var img = document.createElement('img');
img.src = options.src;
img.onload = function () {
var _a, _b;
var ret = {
errMsg: 'getImageInfo:ok',
height: img.height,
width: img.width,
type: 'image',
path: options.src,
orientation: 'up',
};
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, ret);
(_b = options.complete) === null || _b === void 0 ? void 0 : _b.call(options, ret);
};
img.onerror = function () {
var _a, _b;
var errMsg = 'getImageInfo:fail';
(_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
(_b = options.complete) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
};
};
export var webChooseVideo = function (options) {
var _a, _b;
if (options === void 0) { options = {}; }
try {
var input_2 = document.createElement('input');
input_2.type = 'file';
input_2.id = 'file';
input_2.accept = 'video/*';
var event_2 = new MouseEvent('click', {
bubbles: false,
cancelable: true,
view: window,
});
input_2.dispatchEvent(event_2);
input_2.onchange = function () {
var _a;
var content = (_a = input_2.files) === null || _a === void 0 ? void 0 : _a[0];
if (content) {
var video_1 = document.createElement('video');
video_1.preload = 'metadata';
video_1.src = URL.createObjectURL(content);
video_1.onloadedmetadata = function () {
var _a;
(_a = options === null || options === void 0 ? void 0 : options.success) === null || _a === void 0 ? void 0 : _a.call(options, {
width: video_1.videoWidth,
height: video_1.videoHeight,
errMsg: 'chooseVideo: ok',
// @ts-ignore
duration: video_1.duration,
size: content === null || content === void 0 ? void 0 : content.size,
tempFilePath: video_1.src,
});
};
}
};
}
catch (e) {
(_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: 'chooseImage:fail' });
}
(_b = options === null || options === void 0 ? void 0 : options.complete) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: 'complete' });
};
export var webCreateVideoContext = function (element) {
var video = document.getElementById(element);
return {
play: function () {
video.play();
},
pause: function () {
video.pause();
},
exitFullScreen: function () {
// @ts-ignore ignore
video.exitFullscreen();
},
requestFullScreen: function () {
video.requestFullscreen;
},
seek: function (time) {
video.currentTime = time;
},
stop: function () {
video.pause();
video.currentTime = 0;
},
};
};
export var webGetFileInfo = function (options) {
var xhr = new XMLHttpRequest();
xhr.open('GET', options.filePath, true);
var errMsg = 'getFileInfo:fail';
xhr.addEventListener('load', function (ret) {
var _a, _b;
var result = {
errMsg: 'getFileInfo.ok',
size: ret.loaded,
};
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, result);
(_b = options.complete) === null || _b === void 0 ? void 0 : _b.call(options, result);
});
xhr.addEventListener('error', function () {
var _a, _b;
(_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg, errNo: 21101 });
(_b = options.complete) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg, errNo: 21101 });
});
xhr.send();
};
export var webGetStorage = function (options) {
var _a, _b, _c;
try {
var data = webGetStorageSync(options.key);
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { data: data });
}
catch (e) {
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, {
errMsg: 'getStorage:fail',
});
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, 'complete');
};
export var webGetStorageSync = function (key) { return window.localStorage.getItem(key); };
export var webSetStorage = function (options) {
var _a, _b, _c;
var errMsg = 'setStorage:fail';
try {
errMsg = 'setStorage:ok';
webSetStorageSync(options.key, options.data);
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
}
catch (e) {
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webSetStorageSync = function (key, value) {
window.localStorage.setItem(key, value);
};
export var webRemoveStorage = function (options) {
var _a, _b, _c;
var errMsg = 'removeStorage:fail';
try {
var errMsg_1 = 'removeStorage:ok';
webRemoveStorageSync(options.key);
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg_1 });
}
catch (e) {
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webRemoveStorageSync = function (key) {
window.localStorage.removeItem(key);
};
export var webClearStorage = function (options) {
var _a, _b, _c;
if (options === void 0) { options = {}; }
var errMsg = 'clearStorage:fail';
try {
errMsg = 'clearStorage:ok';
webClearStorageSync();
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
}
catch (e) {
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webClearStorageSync = function () {
window.localStorage.clear();
};
export var webGetStorageInfo = function (options) {
var _a, _b, _c;
var errMsg = 'clearStorage:fail';
try {
errMsg = 'clearStorage:ok';
var data = webGetStorageInfoSync();
(_a = options === null || options === void 0 ? void 0 : options.success) === null || _a === void 0 ? void 0 : _a.call(options, __assign(__assign({}, data), { errMsg: errMsg }));
}
catch (e) {
(_b = options === null || options === void 0 ? void 0 : options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options === null || options === void 0 ? void 0 : options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webGetStorageInfoSync = function () { return ({
keys: Object.keys(window.localStorage),
limitSize: 10240,
currentSize: 1,
}); };
export var webGetLocation = function (options) {
var _a;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var _a;
(_a = options === null || options === void 0 ? void 0 : options.success) === null || _a === void 0 ? void 0 : _a.call(options, position.coords);
}, function (err) {
var _a;
(_a = options === null || options === void 0 ? void 0 : options.fail) === null || _a === void 0 ? void 0 : _a.call(options, {
errMsg: err.message,
});
});
}
else {
(_a = options === null || options === void 0 ? void 0 : options.fail) === null || _a === void 0 ? void 0 : _a.call(options, {
errMsg: 'The browser does not support geolocation',
});
}
};
export var webGetNetworkType = function (options) {
var _a, _b, _c;
if (options === void 0) { options = {}; }
var errMsg;
if (navigator.connection) {
// @ts-ignore ignore
errMsg = navigator.connection.effectiveType;
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { networkType: errMsg });
}
else {
errMsg = 'getNetworkType call faild';
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webMakePhoneCall = function (options) {
var _a, _b, _c;
var errMsg = 'makePhoneCall:fail';
try {
window.open("tel:".concat(options.phoneNumber));
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
}
catch (e) {
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webPageScrollTo = function (options) {
var _a, _b;
var errMsg;
try {
errMsg = 'pageScrollTo:ok';
setTimeout(function () {
var _a;
window.scrollTo({
top: options.scrollTop,
behavior: 'smooth',
});
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
}, options.duration || 200);
}
catch (err) {
errMsg = "pageScrollTo:fail".concat(err);
(_a = options.fail) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
}
(_b = options.complete) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
};
export var webNavigateTo = function (options) {
var _a, _b, _c;
var errMsg;
try {
errMsg = 'navigateTo:ok';
var monaHistory = window.__mona_history;
var currentPath = monaHistory.location.pathname;
var targetPath = formatPath(options.url || '', currentPath);
monaHistory.push(targetPath);
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
}
catch (err) {
errMsg = "navigateTo:fail".concat(err);
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webRedirectTo = function (options) {
var _a, _b, _c;
var errMsg;
try {
errMsg = 'redirectTo:ok';
var monaHistory = window.__mona_history;
var currentPath = monaHistory.location.pathname;
var targetPath = formatPath(options.url, currentPath);
monaHistory.replace(targetPath);
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
}
catch (err) {
errMsg = "redirectTo:fail".concat(err);
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webSwitchTab = function (_a) {
var url = _a.url, success = _a.success, fail = _a.fail, complete = _a.complete;
webRedirectTo({ url: url, success: success, fail: fail, complete: complete });
};
export var webNavigateBack = function (options) {
var _a, _b, _c;
if (options === void 0) { options = {}; }
var errMsg;
try {
var monaHistory = window.__mona_history;
errMsg = 'navigateBack:ok';
var delta = options.delta || 1;
monaHistory.go(-delta);
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
}
catch (err) {
errMsg = "navigateBack:fail".concat(err);
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webReLaunch = function (options) {
var _a, _b, _c;
var errMsg;
try {
errMsg = 'reLaunch:ok';
// clear stack
if (window.history._stack && window.history._stack.length > 0) {
window.history._stack = [];
window.history._pos = -1;
webNavigateTo(options);
}
else {
window.location.href = options.url;
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
}
}
catch (err) {
errMsg = "reLaunch:fail".concat(err);
(_b = options.fail) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
(_c = options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
};
export var webGetClipboardData = function (options) {
var _a, _b, _c, _d;
if (options === void 0) { options = {}; }
var errMsg = 'setClipboardData:fail';
try {
// TODO
// const data = clipboard.readSync();
var ret = { data: {}, errMsg: 'setClipboardData:ok' };
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, ret);
(_b = options.complete) === null || _b === void 0 ? void 0 : _b.call(options, ret);
}
catch (e) {
(_c = options.fail) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
(_d = options.complete) === null || _d === void 0 ? void 0 : _d.call(options, { errMsg: errMsg });
}
};
export var webSetClipboardData = function (options) {
var _a, _b, _c, _d;
var errMsg = 'setClipboardData:fail';
try {
errMsg = 'setClipboardData:ok';
// TODO
// clipboard.writeSync(options.data);
(_a = options.success) === null || _a === void 0 ? void 0 : _a.call(options, { errMsg: errMsg });
(_b = options.complete) === null || _b === void 0 ? void 0 : _b.call(options, { errMsg: errMsg });
}
catch (e) {
(_c = options.fail) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: errMsg });
(_d = options.complete) === null || _d === void 0 ? void 0 : _d.call(options, { errMsg: errMsg });
}
};
export var webGetSystemInfo = function (options) {
var _a, _b, _c, _d;
try {
var systemInfo = webGetSystemInfoSync();
(_a = options === null || options === void 0 ? void 0 : options.success) === null || _a === void 0 ? void 0 : _a.call(options, systemInfo);
(_b = options === null || options === void 0 ? void 0 : options.complete) === null || _b === void 0 ? void 0 : _b.call(options, systemInfo);
}
catch (e) {
console.log('e', e);
(_c = options === null || options === void 0 ? void 0 : options.fail) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: 'getSystemInfo:fail' });
(_d = options === null || options === void 0 ? void 0 : options.complete) === null || _d === void 0 ? void 0 : _d.call(options, { errMsg: 'getSystemInfo:fail' });
}
};
export var webGetSystemInfoSync = function () {
var _a;
var appVersion = navigator.appVersion, userAgent = navigator.userAgent, appName = navigator.appName;
var systemInfo = {
system: userAgent,
// @ts-ignore ignore
platform: ((_a = navigator === null || navigator === void 0 ? void 0 : navigator.userAgentData) === null || _a === void 0 ? void 0 : _a.platform) || (navigator === null || navigator === void 0 ? void 0 : navigator.platform),
brand: '',
model: '',
version: appVersion,
SDKVersion: '',
screenWidth: document.body.clientWidth,
screenHeight: document.body.clientHeight,
appName: appName,
windowWidth: window.screen.width,
windowHeight: window.screen.height,
pixelRatio: window.devicePixelRatio,
statusBarHeight: 0,
safeArea: {
left: 0,
right: 0,
top: 0,
bottom: 0,
width: 0,
height: 0,
},
};
return systemInfo;
};
var USER_AUTHORIZATION_CACHE = '__USER_AUTHORIZATION_CACHE__';
var MONA_APPID = '__MONA_APPID__';
var MONA_JSAPI_LIST = '__MONA_JSAPI_LIST__';
var MONA_SHOW_AUTHORIZE_MODAL = '__MONA_SHOW_AUTHORIZE_MODAL__';
export var webGetSetting = function (options) {
var _a, _b, _c, _d, _e, _f, _g;
try {
// @ts-ignore
var appid = window[MONA_APPID];
var data = webGetStorageSync(USER_AUTHORIZATION_CACHE);
if (data) {
var apiAuthSetting = (_a = JSON.parse(data)) === null || _a === void 0 ? void 0 : _a[appid];
(_b = options === null || options === void 0 ? void 0 : options.success) === null || _b === void 0 ? void 0 : _b.call(options, { apiAuthSetting: apiAuthSetting });
(_c = options === null || options === void 0 ? void 0 : options.complete) === null || _c === void 0 ? void 0 : _c.call(options, { apiAuthSetting: apiAuthSetting });
}
else {
(_d = options === null || options === void 0 ? void 0 : options.success) === null || _d === void 0 ? void 0 : _d.call(options, { apiAuthSetting: {} });
(_e = options === null || options === void 0 ? void 0 : options.complete) === null || _e === void 0 ? void 0 : _e.call(options, { apiAuthSetting: {} });
}
}
catch (e) {
console.log('e', e);
(_f = options === null || options === void 0 ? void 0 : options.fail) === null || _f === void 0 ? void 0 : _f.call(options, { errMsg: 'getSetting:fail' });
(_g = options === null || options === void 0 ? void 0 : options.complete) === null || _g === void 0 ? void 0 : _g.call(options, { errMsg: 'getSetting:fail' });
}
};
var removePrefix = function (apiName) {
if (typeof apiName === 'string') {
var arr = apiName.split('.');
return arr[arr.length - 1];
}
return apiName;
};
export var webAuthorize = function (options) {
var _a, _b;
var setAuthorzationCache = function (isTrue) {
var _a, _b;
var data = webGetStorageSync(USER_AUTHORIZATION_CACHE);
var dataObj;
// @ts-ignore
var appid = window[MONA_APPID];
if (data) {
// 有缓存
dataObj = JSON.parse(data);
dataObj[appid] = __assign(__assign({}, dataObj[appid]), (_a = {}, _a[options.apiName] = isTrue, _a));
}
else {
// 没有缓存
dataObj = {};
dataObj[appid] = (_b = {}, _b[options.apiName] = isTrue, _b);
}
webSetStorageSync(USER_AUTHORIZATION_CACHE, JSON.stringify(dataObj));
};
var rejectCallback = function () {
var _a, _b, _c, _d;
try {
setAuthorzationCache(false);
(_a = options === null || options === void 0 ? void 0 : options.success) === null || _a === void 0 ? void 0 : _a.call(options, false);
(_b = options === null || options === void 0 ? void 0 : options.complete) === null || _b === void 0 ? void 0 : _b.call(options, false);
}
catch (err) {
(_c = options === null || options === void 0 ? void 0 : options.fail) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: 'authorize:fail' });
(_d = options === null || options === void 0 ? void 0 : options.complete) === null || _d === void 0 ? void 0 : _d.call(options, { errMsg: 'authorize:fail' });
}
};
var allowCallback = function () {
var _a, _b, _c, _d;
try {
setAuthorzationCache(true);
(_a = options === null || options === void 0 ? void 0 : options.success) === null || _a === void 0 ? void 0 : _a.call(options, true);
(_b = options === null || options === void 0 ? void 0 : options.complete) === null || _b === void 0 ? void 0 : _b.call(options, true);
}
catch (err) {
(_c = options === null || options === void 0 ? void 0 : options.fail) === null || _c === void 0 ? void 0 : _c.call(options, { errMsg: 'authorize:fail' });
(_d = options === null || options === void 0 ? void 0 : options.complete) === null || _d === void 0 ? void 0 : _d.call(options, { errMsg: 'authorize:fail' });
}
};
var authorizationText =
// @ts-ignore
((_b = (_a = window[MONA_JSAPI_LIST]) === null || _a === void 0 ? void 0 : _a.find(function (item) { return (item === null || item === void 0 ? void 0 : item.jsApiName) === removePrefix(options.apiName); })) === null || _b === void 0 ? void 0 : _b.reqAuthDesc) ||
'请求您的授权';
// @ts-ignore
window[MONA_SHOW_AUTHORIZE_MODAL] &&
// @ts-ignore
window[MONA_SHOW_AUTHORIZE_MODAL](authorizationText, allowCallback, rejectCallback);
};
export var webOpen = function (url) { return window.open(url, '_blank', 'noopener,noreferrer'); };
export var webNavigateToApp = function (info, options) {
var _a;
(_a = window.__MONA_LIGHT_APP_NAVIGATE_CB) === null || _a === void 0 ? void 0 : _a.call(window, info, options);
};
//# sourceMappingURL=util.js.map