/**
|
* BridgeTelling - 跨端通讯,通知接收基类
|
* @author Tevin
|
*/
|
|
import Taro from '@tarojs/taro';
|
import { $bridge } from '@components/common/Bridge';
|
import { Fetcher } from '@components/bases/Fetcher';
|
|
export class BridgeTelling {
|
constructor(tellings) {
|
this._init(tellings);
|
}
|
|
_init(tellings) {
|
tellings.forEach(method => {
|
const name = Fetcher.prototype.stringToCamel(method);
|
if (typeof this[name] === 'undefined') {
|
console.warn('BridgeTelling:未发现“' + method + '”对于接收器!');
|
} else {
|
// 注册App通知接收器
|
$bridge.register(method, (res, callback) => {
|
this[name](method, res, callback);
|
});
|
}
|
});
|
}
|
|
$getCurrentPage() {
|
const pages = Taro.getCurrentPages();
|
return pages[pages.length - 1];
|
}
|
|
$getCurrentPageUrl() {
|
return this.$getCurrentPage().path;
|
}
|
|
$isCurrentPage(url) {
|
const pages = Taro.getCurrentPages();
|
const curPage = pages[pages.length - 1];
|
if (curPage.path.split('?')[0] === url.split('?')[0]) {
|
return curPage;
|
} else {
|
return null;
|
}
|
}
|
|
_checkPage(url, callback) {
|
let curPage = this.$isCurrentPage(url);
|
if (curPage) {
|
callback(curPage);
|
} else {
|
setTimeout(() => {
|
this._checkPage(url, callback);
|
}, 100);
|
}
|
}
|
|
$openPage(url, callback) {
|
const curPage = this.$isCurrentPage(url);
|
// 需打开的是当前页面
|
if (curPage) {
|
callback(curPage);
|
} else {
|
Taro.navigateTo({
|
url,
|
success: () => {
|
this._checkPage(url, callback);
|
},
|
});
|
}
|
}
|
|
$pageBridge(page, method, res, callback) {
|
try {
|
res = typeof res === 'string' ? JSON.parse(res) : res;
|
} catch (e) {
|
}
|
page.$component.$onBridge(method, res, callback);
|
}
|
|
}
|