WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2021-10-18 9dd17569f69f6e93c94d1a8c7918f46fd1e21bb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
 * 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) {
        page.$component.$onBridge(method, res, callback);
    }
 
}