@tb-app/web-view-api
Version:
实现webview与淘宝小程序之间的通信
734 lines (648 loc) • 17.8 kB
JavaScript
import { subscribeOnce, publish, subscribe, unsubscribe } from '@tb-app/pub-sub';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
var prefix = 'registry-trigger-';
if (typeof my !== 'undefined') {
my.onMessage = function onMessage(result) {
var token = result.token, type = result.type, data = __rest(result, ["token", "type"]);
publish(token || type, data);
};
}
/**
* 只能在 web-view 端调用,使用小程序端注册的功能
* @param options
* @returns
*/
function invoke$1(options) {
return new Promise(function (resolve, reject) {
var token = subscribeOnce(options.type, function (_a) {
var success = _a.success, data = _a.data, error = _a.error;
if (success) {
resolve(data);
}
else {
reject(error);
}
});
var message = {
type: options.type,
data: options.data,
token: token,
};
my.postMessage(message);
});
}
/**
* 调用小程序my上的api, 不支持监听和创建上下文之类的api
* @param data
*/
function invokeMy(_a) {
var type = _a.type, data = _a.data;
return invoke$1({
type: prefix + "my",
data: {
api: type,
options: data,
},
});
}
/**
* 调用小程序cloud上的api
* @param data
*/
function invokeCloud(_a) {
var type = _a.type, data = _a.data;
return invoke$1({
type: prefix + "cloud",
data: {
api: type,
options: data,
},
});
}
/**
* 小程序的请求
* @param options
*/
function httpRequest$1(options) {
return invoke$1({
type: prefix + "httpRequest",
data: options,
});
}
/**
* 只能在 web-view 端调用, 同一类消息可以注册多个事件回调
* @param type
* @param callback
*/
function listen(type, callback) {
subscribe(type, callback);
}
/**
* 只能在 web-view 端调用,用于移除对应的事件监听
* @param type
*/
function removeListen(type) {
unsubscribe(type);
}
/**
* 跳转到指定 tabBar 页面,并关闭其他所有非 tabBar 页面
*/
function switchTab(options) {
return invokeMy({ type: 'switchTab', data: options });
}
/**
* 关闭当前所有页面,跳转到应用内的某个指定页面
*/
function reLaunch(options) {
return invokeMy({ type: 'reLaunch', data: options });
}
/**
* 关闭当前页面,跳转到应用内的某个指定页面
*/
function redirectTo(options) {
return invokeMy({ type: 'redirectTo', data: options });
}
/**
* 保留当前页面,跳转到应用内的某个指定页面
*/
function navigateTo(options) {
return invokeMy({ type: 'navigateTo', data: options });
}
/**
* 关闭当前页面,返回上一级或多级页面
*/
function navigateBack(options) {
return invokeMy({ type: 'navigateBack', data: options });
}
/**
* 跳转千牛容器打开H5页面
*/
function navigateToWebPage(options) {
return invokeMy({ type: 'qn.navigateToWebPage', data: options });
}
/**
* 小程序跳转到QAP
*/
function navigateToQAP(options) {
return invokeMy({ type: 'qn.navigateToQAP', data: options });
}
/**
* 关闭插件页面
*/
function closeQAP() {
return invokeMy({ type: 'qn.closeQAP' });
}
/**
* 显示 tabBar
*/
function showTabBar(options) {
return invokeMy({ type: 'showTabBar', data: options });
}
/**
* 隐藏 tabBar
*/
function hideTabBar(options) {
return invokeMy({ type: 'hideTabBar', data: options });
}
/**
* 动态设置 tabBar 某一项的内容
*/
function setTabBarItem(options) {
return invokeMy({ type: 'setTabBarItem', data: options });
}
/**
* 显示 tabBar 某一项的右上角的红点
*/
function showTabBarRedDot(options) {
return invokeMy({ type: 'showTabBarRedDot', data: options });
}
/**
* 隐藏 tabBar 某一项的右上角的红点
*/
function hideTabBarRedDot(options) {
return invokeMy({ type: 'hideTabBarRedDot', data: options });
}
/**
* 为 tabBar 某一项的右上角添加文本
*/
function setTabBarBadge(options) {
return invokeMy({ type: 'setTabBarBadge', data: options });
}
/**
* 移除 tabBar 某一项右上角的文本
*/
function removeTabBarBadge(options) {
return invokeMy({ type: 'removeTabBarBadge', data: options });
}
/**
* 切换左侧导航条选中项
*/
function switchTabEx(options) {
return invokeMy({ type: 'qn.switchTabEx', data: options });
}
/**
* 显示左侧导航栏中指定的菜单项
*/
function showTabEx(options) {
return invokeMy({ type: 'qn.showTabEx', data: options });
}
/**
* 隐藏左侧导航栏中指定的菜单项
*/
function hideTabEx(options) {
return invokeMy({ type: 'qn.hideTabEx', data: options });
}
/**
* 导航菜单显示红点
*/
function showTabBarRedDotEx(options) {
return invokeMy({ type: 'qn.showTabBarRedDotEx', data: options });
}
/**
* 导航菜单隐藏红点
*/
function hideTabBarRedDotEx(options) {
return invokeMy({ type: 'qn.hideTabBarRedDotEx', data: options });
}
/**
* 导航菜单显示提醒文字
*/
function setTabBarBadgeEx(options) {
return invokeMy({ type: 'qn.setTabBarBadgeEx', data: options });
}
/**
* 导航菜单移除提醒文字
*/
function removeTabBarBadgeEx(options) {
return invokeMy({ type: 'qn.removeTabBarBadgeEx', data: options });
}
/**
* 警告框:可以设置警告框的标题、内容、按钮文字等,暂不支持设置图片等样式
*/
function alert(options) {
return invokeMy({
type: 'alert',
data: options,
});
}
/**
* 确认框:弹窗样式不支持定制
*/
function confirm(options) {
return invokeMy({ type: 'confirm', data: options });
}
/**
* 显示一个弱提示,可选择多少秒之后消失
*/
function showToast(options) {
return invokeMy({ type: 'showToast', data: options });
}
/**
* 隐藏弱提示
*/
function hideToast() {
return invokeMy({ type: 'hideToast' });
}
/**
* 显示加载提示的过渡效果
*/
function showLoading(options) {
return invokeMy({ type: 'showLoading', data: options });
}
/**
* 隐藏加载提示
*/
function hideLoading(options) {
return invokeMy({ type: 'hideLoading', data: options });
}
/**
* 拍照或从手机相册中选择图片:请注意,淘宝小程序不支持设置sizeType字段。 另外,此API获取到的图片路径是伪路径,若要获取本地路径,需要配合my.getImageInfo 使用。
*/
function chooseImage(options) {
return invokeMy({ type: 'chooseImage', data: options });
}
/**
* 压缩图片
*/
function compressImage(options) {
return invokeMy({ type: 'compressImage', data: options });
}
/**
* 保存在线图片到手机相册
*/
function saveImage(options) {
return invokeMy({ type: 'saveImage', data: options });
}
/**
* 预览图片
*/
function previewImage(options) {
return invokeMy({ type: 'previewImage', data: options });
}
/**
* 获取图片信息
*/
function getImageInfo(options) {
return invokeMy({ type: 'getImageInfo', data: options });
}
/**
* 警查询数据库大小:该API提供查询当前小程序所使用的数据库大小,便于ISV实时看当前使用情况,避免超出数据库大小限制。
*/
function queryDBSize() {
return invokeMy({ type: 'qn.queryDBSize' });
}
/**
* 数据库:千牛为小程序提供了数据库存储能力(my.database),但对插件数据库的大小做了限制,务必仔细最后一部分说明,未处理数据超限警告可能会导致数据库缓存被删除。
*/
function database(options) {
return invokeMy({ type: 'qn.database', data: options });
}
/**
* 获取文件信息
*/
function getFileInfo(options) {
return invokeMy({ type: 'getFileInfo', data: options });
}
/**
* 保存文件到本地
*/
function saveFile(options) {
return invokeMy({ type: 'saveFile', data: options });
}
/**
* 获取保存的文件信息
*/
function getSavedFileInfo(options) {
return invokeMy({ type: 'getSavedFileInfo', data: options });
}
/**
* 获取保存的所有文件信息
*/
function getSavedFileList() {
return invokeMy({ type: 'getSavedFileList' });
}
/**
* 删除某个保存的文件
*/
function removeSavedFile(options) {
return invokeMy({ type: 'removeSavedFile', data: options });
}
/**
* 选择文件
*/
function chooseFile(options) {
return invokeMy({ type: 'qn.chooseFile', data: options });
}
/**
* 打开文件并获取内容
*/
function chooseFileAndGetContent(options) {
return invokeMy({ type: 'qn.chooseFileAndGetContent', data: options });
}
/**
* 保存文件到磁盘
*/
function saveFileToDisk(options) {
return invokeMy({ type: 'qn.saveFileToDisk', data: options });
}
/**
* 清除本地数据缓存的异步接口
*/
function clearStorage() {
return invokeMy({ type: 'clearStorage' });
}
/**
* 清除本地数据缓存的同步接口
*/
function clearStorageSync() {
return invokeMy({ type: 'clearStorageSync' });
}
/**
* 获取缓存数据的异步接口
*/
function getStorage(options) {
return invokeMy({ type: 'getStorage', data: options });
}
/**
* 获取当前 storage 的相关信息的异步接口
*/
function getStorageInfo() {
return invokeMy({ type: 'getStorageInfo' });
}
/**
* 获取当前 storage 相关信息的同步接口
*/
function getStorageInfoSync() {
return invokeMy({ type: 'getStorageInfoSync' });
}
/**
* 获取缓存数据的同步接口
*/
function getStorageSync(options) {
return invokeMy({ type: 'getStorageSync', data: options });
}
/**
* 删除缓存数据的异步接口
*/
function removeStorage(options) {
return invokeMy({ type: 'removeStorage', data: options });
}
/**
* 删除缓存数据的异步接口
*/
function removeStorageSync(options) {
return invokeMy({ type: 'removeStorageSync', data: options });
}
/**
* 将数据存储在本地缓存中指定的 key 中的异步接口
*/
function setStorage(options) {
return invokeMy({ type: 'setStorage', data: options });
}
/**
* 同步将数据存储在本地缓存中指定的 key 中的同步接口
*/
function setStorageSync(options) {
return invokeMy({ type: 'setStorageSync', data: options });
}
/**
* 文件上传
*/
function uploadFile$1(options) {
return invokeMy({ type: 'uploadFile', data: options });
}
/**
* 文件上传
*/
function downloadFile(options) {
return invokeMy({ type: 'downloadFile', data: options });
}
/**
* 判断当前小程序的 API、入参或返回值、组件、属性等在当前版本是否支持
*/
function canIUse(apiName) {
return invokeMy({
type: 'canIUse',
data: apiName,
});
}
/**
* 获取基础库版本号
*/
function SDKVersion() {
return invokeMy({ type: 'SDKVersion' });
}
/**
* 获取手机系统信息的同步接口
*/
function getSystemInfoSync() {
return invokeMy({ type: 'getSystemInfoSync' });
}
/**
* 获取手机系统信息
*/
function getSystemInfo() {
return invokeMy({ type: 'getSystemInfo' });
}
/**
* 获取当前网络状态
*/
function getNetworkType() {
return invokeMy({ type: 'getNetworkType' });
}
/**
* 设置剪贴板数据
*/
function setClipboard(options) {
return invokeMy({ type: 'setClipboard', data: options });
}
/**
* 获取剪贴板数据
*/
function getClipboard() {
return invokeMy({ type: 'getClipboard' });
}
/**
* 获取服务器时间
*/
function getServerTime() {
return invokeMy({ type: 'getServerTime' });
}
/**
* 发起用户授权
*/
function authorize(options) {
return invokeMy({
type: 'authorize',
data: options,
});
}
/**
* 清除三方授权token
*/
function cleanToken() {
return invokeMy({
type: 'qn.cleanToken',
});
}
/**
* 弹出子账号授权框
*/
function showSubAccountAuth(options) {
return invokeMy({ type: 'qn.showSubAccountAuth', data: options });
}
/**
* 获取用户信息
*/
function getAuthUserInfo() {
return invokeMy({ type: 'getAuthUserInfo' });
}
/**
* 打开聊天
*/
function openChat(options) {
return invokeMy({ type: 'qn.openChat', data: options });
}
/**
* 获取当前IM旺旺联系人
*/
function imGetActiveUser() {
return invokeMy({ type: 'qn.imGetActiveUser' });
}
/**
* 打开千牛插件接口
*/
function openPlugin(options) {
return invokeMy({ type: 'qn.openPlugin', data: options });
}
/**
* 打开千牛的插槽协议。目前支持 shangpinguanli:itemList,itemDetail, jiaoyiguanli:tradeList,tradeDetail
*/
function openCategory(options) {
return invokeMy({ type: 'qn.openCategory', data: options });
}
/**
* 千牛提供更改商品价格的能力
*/
function changePrice(options) {
return invokeMy({
type: 'changePrice',
data: options,
});
}
/**
* 跳转到其他小程序
*/
function navigateToMiniProgram(options) {
return invokeMy({ type: 'navigateToMiniProgram', data: options });
}
/**
* 退出当前小程序
*/
function exit() {
return invokeMy({ type: 'exit' });
}
/**
* 非对称加密
*/
function rsa(options) {
return invokeMy({ type: 'rsa', data: options });
}
/**
* 用于获取当前小程序的运行版本
*/
function getRunScene() {
return invokeMy({ type: 'getRunScene' });
}
/**
* 获取模板ext.json中的ext配置
*/
function getExtConfig() {
return invokeMy({ type: 'getExtConfig' });
}
/**
* 同步获取模板ext.json中的ext配置
*/
function getExtConfigSync() {
return invokeMy({ type: 'getExtConfigSync' });
}
/**
* 文件上传
* @param options
*/
function uploadFile(options) {
return invokeCloud({ type: 'file.uploadFile', data: options });
}
/**
* 删除文件
* @param options
*/
function deleteFile(options) {
return invokeCloud({ type: 'file.deleteFile', data: options });
}
/**
* 根据文件id(cloud://)获取授权的url
* @param options
*/
function getTempFileURL(options) {
return invokeCloud({ type: 'file.getTempFileURL', data: options });
}
/**
* 调用 top API
* @param options
* @returns
*/
function invoke(options) {
return invokeCloud({ type: 'topApi.invoke', data: options });
}
/**
* 初始化cloud
* @param options
* @returns
*/
function init(options) {
return invokeCloud({ type: 'init', data: options });
}
/**
* http 请求
*/
function httpRequest(options) {
return invokeCloud({ type: 'application.httpRequest', data: options });
}
var cloud = {
file: {
uploadFile: uploadFile,
deleteFile: deleteFile,
getTempFileURL: getTempFileURL,
},
topApi: {
invoke: invoke,
},
init: init,
application: {
httpRequest: httpRequest,
},
};
export { alert, authorize, canIUse, changePrice, chooseFile, chooseFileAndGetContent, chooseImage, cleanToken, clearStorage, clearStorageSync, closeQAP, cloud, compressImage, confirm, database, downloadFile, exit, getAuthUserInfo, getClipboard, getExtConfig, getExtConfigSync, getFileInfo, getImageInfo, getNetworkType, getRunScene, getSavedFileInfo, getSavedFileList, getServerTime, getStorage, getStorageInfo, getStorageInfoSync, getStorageSync, getSystemInfo, getSystemInfoSync, hideLoading, hideTabBar, hideTabBarRedDot, hideTabBarRedDotEx, hideTabEx, hideToast, httpRequest$1 as httpRequest, imGetActiveUser, invoke$1 as invoke, invokeCloud, invokeMy, listen, navigateBack, navigateTo, navigateToMiniProgram, navigateToQAP, navigateToWebPage, openCategory, openChat, openPlugin, previewImage, queryDBSize, reLaunch, redirectTo, removeListen, removeSavedFile, removeStorage, removeStorageSync, removeTabBarBadge, removeTabBarBadgeEx, rsa, saveFile, saveFileToDisk, saveImage, SDKVersion as sdkVersion, setClipboard, setStorage, setStorageSync, setTabBarBadge, setTabBarBadgeEx, setTabBarItem, showLoading, showSubAccountAuth, showTabBar, showTabBarRedDot, showTabBarRedDotEx, showTabEx, showToast, switchTab, switchTabEx, uploadFile$1 as uploadFile };