WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2025-03-24 433cf5bfd7d0bd602c3147c15720a654ea47b028
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * BridgeCenterBase - 跨端通讯,通知接收基类
 * @author Tevin
 */
 
import Taro from '@tarojs/taro';
import { $bridge } from '@components/common/Bridge';
import { Fetcher } from '@components/bases/Fetcher';
 
export class BridgeCenterBase {
    constructor(tellings) {
        this._init(tellings);
    }
 
    _init(tellings) {
        tellings.forEach(method => {
            const name = Fetcher.prototype.stringToCamel(method);
            // BridgeCenter 已注册,但没有定义接收器的协议,提示
            if (typeof this[name] === 'undefined') {
                console.warn('BridgeTelling:协议【' + method + '】未发现对应接收器!');
            }
            // BridgeCenter 已注册已定义接收器的协议,注册接收器
            else {
                $bridge.register(method, (res, callback) => {
                    this[name](method, res, callback);
                });
            }
        });
    }
 
    /**
     * 获取当前页面
     * @return {Taro.Page}
     */
    $getCurrentPage() {
        const pages = Taro.getCurrentPages();
        return pages[pages.length - 1];
    }
 
    /**
     * 获取当前页面路径
     * @return {String}
     */
    $getCurrentPagePath() {
        return this.$getCurrentPage().path.split('?')[0];
    }
 
    /**
     * 判断地址是否为当前页面
     * @param {String} url
     * @return {Taro.Page|null}
     */
    $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;
        }
    }
 
    /**
     * 判断当前页面是否在列表中
     * @param {Array} allows=[]
     * @return {boolean}
     */
    $isCurPageInside(allows = []) {
        const url = this.$getCurrentPagePath();
        return allows.indexOf(url) >= 0;
    }
 
    _waitCurPage(url, callback) {
        let curPage = this.$isCurrentPage(url);
        if (curPage) {
            callback(curPage);
        } else {
            setTimeout(() => {
                this._waitCurPage(url, callback);
            }, 100);
        }
    }
 
    /**
     * 打开指定页面
     * @param {String} url
     * @param {Function} callback
     */
    $openPage(url, callback) {
        const curPage = this.$isCurrentPage(url);
        // 需打开的是当前页面
        if (curPage) {
            callback(curPage);
        } else {
            Taro.navigateTo({ url });
            setTimeout(() => {
                this._waitCurPage(url, callback);
            }, 100);
        }
    }
 
    /**
     * 当前页下发通讯
     * @param {String} method
     * @param {Object} res
     * @param {Function} [callback]
     */
    $curPageBridge(method, res, callback) {
        this.$pageBridge(this.$getCurrentPage(), method, res, callback);
    }
 
    /**
     * 对某个页面下发通知
     * @param {Taro.Page} page
     * @param {String} method
     * @param {Object} res
     * @param {Function} [callback]
     */
    $pageBridge(page, method, res, callback) {
        if (!page.$component || !page.$component.$onBridge) {
            return;
        }
        try {
            res = typeof res === 'string' ? JSON.parse(res) : res;
        } catch (e) {}
        page.$component.$onBridge(method, res, callback);
    }
}