@scgczm/bridge 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ export interface ICommonResult<T> {
2
+ success: boolean;
3
+ data: T;
4
+ }
5
+ type TSyncFuns = "getSlideData" | "login" | "scan" | "close" | "openexplorer" | "openwechat" | "saveimage" | "share" | "toH5" | "toDetail" | "toLive" | "toTopic" | "setVolume" | "toSubSpecial" | "detailJson" | "focusWeb" | "mall" | "mainPage" | "changeStatebarColor" | "release" | "toSpecial";
6
+ type TAsyncFuns = "getToken" | "getUserId" | "getNickName" | "getAvatar" | "getPhoneNumber" | "getPromise";
7
+ export declare const setDevMode: () => void;
8
+ export declare const initAsyncCallbackPool: () => void;
9
+ declare function androidBridgeFn(fnName: TSyncFuns, data?: Record<string, any>): void;
10
+ declare function androidBridgeFn(fnName: TAsyncFuns, data?: Record<string, any>): Promise<any>;
11
+ export declare const commonBridgeFun: () => typeof androidBridgeFn;
12
+ /**
13
+ * @description 是否为app环境
14
+ * @return {boolean} true 是 false 否
15
+ */
16
+ export declare const isScgcApp: () => boolean;
17
+ export {};
@@ -0,0 +1,272 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ 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);
12
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
17
+ 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;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ var distinctionPlatform = function () {
38
+ var ua = navigator.userAgent;
39
+ if (/(iPhone|iPad|iPod)/i.test(ua)) {
40
+ return "ios";
41
+ }
42
+ else if (/Android/i.test(ua)) {
43
+ return "android";
44
+ }
45
+ else if (/openharmony/i.test(ua)) {
46
+ return "harmony";
47
+ }
48
+ else {
49
+ return "unknow";
50
+ }
51
+ };
52
+ var asyncFunsArr = [
53
+ "getToken",
54
+ "getUserId",
55
+ "getAvatar",
56
+ "getNickName",
57
+ "getPhoneNumber",
58
+ "getPromise", //测试用
59
+ ];
60
+ var androidAsyncFunsArr = ["getPromise"];
61
+ var devMode = false;
62
+ export var setDevMode = function () {
63
+ devMode = true;
64
+ };
65
+ export var initAsyncCallbackPool = function () {
66
+ window.bridgeCallbacks = {};
67
+ };
68
+ var devFn = function (platform, fnName, data) {
69
+ //动态判断是否为devMode,避免初始化commonBridgeFun比调用setDevMode的早的情况下,devMode参数已固定(损失的性能可以接受)
70
+ if (devMode) {
71
+ var devInfo = {
72
+ platform: platform,
73
+ fnName: fnName,
74
+ data: data,
75
+ };
76
+ console.log(devInfo);
77
+ }
78
+ };
79
+ function makeRandomId(fnName) {
80
+ return "".concat(fnName || "anonymous", "_").concat(Date.now());
81
+ }
82
+ function registerCallback(fnName, cb) {
83
+ if (!cb) {
84
+ return null;
85
+ }
86
+ var callbackId = makeRandomId(fnName);
87
+ if (!window.hasOwnProperty("bridgeCallbacks")) {
88
+ window.bridgeCallbacks = {};
89
+ }
90
+ window.bridgeCallbacks[callbackId] = function (data) {
91
+ var result;
92
+ if (typeof data === "object") {
93
+ result = data;
94
+ }
95
+ else if (typeof data === "string") {
96
+ try {
97
+ result = JSON.parse(data);
98
+ }
99
+ catch (e) {
100
+ result = data;
101
+ }
102
+ }
103
+ cb(result);
104
+ };
105
+ return "bridgeCallbacks.".concat(callbackId);
106
+ }
107
+ function androidBridgeFn(fnName, data) {
108
+ var _this = this;
109
+ var _a, _b;
110
+ var androidParams = {
111
+ Method: fnName,
112
+ Data: data,
113
+ };
114
+ devFn("android", fnName, data);
115
+ if (fnName == "getSlideData") {
116
+ (_a = window === null || window === void 0 ? void 0 : window.testInterface) === null || _a === void 0 ? void 0 : _a.getSlideData(JSON.stringify(androidParams));
117
+ }
118
+ else if (asyncFunsArr.includes(fnName)) {
119
+ var promiseRes = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
120
+ var res, result;
121
+ var _a, _b;
122
+ return __generator(this, function (_c) {
123
+ try {
124
+ //安卓使用了异步返回
125
+ if (androidAsyncFunsArr.includes(fnName)) {
126
+ (_a = window === null || window === void 0 ? void 0 : window.newBridge) === null || _a === void 0 ? void 0 : _a[fnName](JSON.stringify({
127
+ callback: registerCallback(fnName, resolve),
128
+ }));
129
+ }
130
+ else {
131
+ res = (_b = window === null || window === void 0 ? void 0 : window.newBridge) === null || _b === void 0 ? void 0 : _b[fnName]();
132
+ result = void 0;
133
+ if (typeof res === "object") {
134
+ result = res;
135
+ }
136
+ else if (typeof res === "string") {
137
+ try {
138
+ result = JSON.parse(res);
139
+ }
140
+ catch (e) {
141
+ result = { success: false, data: res };
142
+ }
143
+ }
144
+ if ((result === null || result === void 0 ? void 0 : result.success) == true) {
145
+ resolve(result);
146
+ }
147
+ else {
148
+ reject(result);
149
+ }
150
+ }
151
+ }
152
+ catch (error) {
153
+ reject(error);
154
+ }
155
+ return [2 /*return*/];
156
+ });
157
+ }); });
158
+ return promiseRes;
159
+ }
160
+ else {
161
+ (_b = window === null || window === void 0 ? void 0 : window.bridge) === null || _b === void 0 ? void 0 : _b.callRouter(JSON.stringify(androidParams), function () { });
162
+ }
163
+ }
164
+ function iosBridgeFn(fnName, data) {
165
+ var _this = this;
166
+ var _a, _b, _c;
167
+ devFn("ios", fnName, data);
168
+ if (asyncFunsArr.includes(fnName)) {
169
+ var promiseRes = new Promise(function (resolve, reject) {
170
+ var _a, _b, _c;
171
+ try {
172
+ var _cb = function (res) { return __awaiter(_this, void 0, void 0, function () {
173
+ return __generator(this, function (_a) {
174
+ try {
175
+ if ((res === null || res === void 0 ? void 0 : res.success) == "true") {
176
+ resolve({
177
+ success: true,
178
+ data: res === null || res === void 0 ? void 0 : res.data,
179
+ });
180
+ }
181
+ else {
182
+ reject({
183
+ success: false,
184
+ data: res === null || res === void 0 ? void 0 : res.data,
185
+ });
186
+ }
187
+ }
188
+ catch (error) {
189
+ reject({
190
+ success: false,
191
+ data: error,
192
+ });
193
+ }
194
+ return [2 /*return*/];
195
+ });
196
+ }); };
197
+ (_c = (_b = (_a = window === null || window === void 0 ? void 0 : window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b[fnName]) === null || _c === void 0 ? void 0 : _c.postMessage({
198
+ data: {},
199
+ callback: registerCallback(fnName, _cb),
200
+ });
201
+ }
202
+ catch (error) {
203
+ reject({
204
+ success: false,
205
+ data: error,
206
+ });
207
+ }
208
+ });
209
+ return promiseRes;
210
+ }
211
+ else {
212
+ (_c = (_b = (_a = window === null || window === void 0 ? void 0 : window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b[fnName]) === null || _c === void 0 ? void 0 : _c.postMessage(data);
213
+ }
214
+ }
215
+ function harmonyBridgeFn(fnName, data) {
216
+ var _a, _b;
217
+ devFn("harmony", fnName, data);
218
+ if (asyncFunsArr.includes(fnName)) {
219
+ var promiseRes = new Promise(function (resolve, reject) {
220
+ var _a;
221
+ try {
222
+ (_a = window === null || window === void 0 ? void 0 : window.harmonyBridge) === null || _a === void 0 ? void 0 : _a[fnName]({
223
+ data: data,
224
+ callback: registerCallback(fnName, resolve),
225
+ });
226
+ }
227
+ catch (error) {
228
+ reject(error);
229
+ }
230
+ });
231
+ return promiseRes;
232
+ }
233
+ else {
234
+ (_b = (_a = window === null || window === void 0 ? void 0 : window.harmonyBridge) === null || _a === void 0 ? void 0 : _a[fnName]) === null || _b === void 0 ? void 0 : _b.call(_a, data);
235
+ }
236
+ }
237
+ function defaultBridgeFn(fnName, data) {
238
+ console.log("请使用scgc app打开");
239
+ if (asyncFunsArr.includes(fnName)) {
240
+ var promiseRes = new Promise(function (resolve, reject) {
241
+ try {
242
+ resolve("请使用scgc app打开");
243
+ }
244
+ catch (error) {
245
+ reject(error);
246
+ }
247
+ });
248
+ return promiseRes;
249
+ }
250
+ }
251
+ export var commonBridgeFun = function () {
252
+ var platform = distinctionPlatform();
253
+ if (platform == "android") {
254
+ return androidBridgeFn;
255
+ }
256
+ else if (platform == "ios") {
257
+ return iosBridgeFn;
258
+ }
259
+ else if (platform == "harmony") {
260
+ return harmonyBridgeFn;
261
+ }
262
+ else {
263
+ return defaultBridgeFn;
264
+ }
265
+ };
266
+ /**
267
+ * @description 是否为app环境
268
+ * @return {boolean} true 是 false 否
269
+ */
270
+ export var isScgcApp = function () {
271
+ return navigator.userAgent.indexOf("SCGC_UA") > -1;
272
+ };
@@ -0,0 +1 @@
1
+ !function(n,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.scgcBridge=e():n.scgcBridge=e()}(self,()=>(()=>{"use strict";var __webpack_modules__={"./src/index.ts":(__unused_webpack_module,__webpack_exports__,__webpack_require__)=>{eval('{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ isScgcApp: () => (/* reexport safe */ _utils__WEBPACK_IMPORTED_MODULE_0__.isScgcApp),\n/* harmony export */ scgcChangeStateBarColor: () => (/* binding */ scgcChangeStateBarColor),\n/* harmony export */ scgcClose: () => (/* binding */ scgcClose),\n/* harmony export */ scgcFocusWeb: () => (/* binding */ scgcFocusWeb),\n/* harmony export */ scgcGetAvatar: () => (/* binding */ scgcGetAvatar),\n/* harmony export */ scgcGetNickName: () => (/* binding */ scgcGetNickName),\n/* harmony export */ scgcGetPhoneNumber: () => (/* binding */ scgcGetPhoneNumber),\n/* harmony export */ scgcGetSlideData: () => (/* binding */ scgcGetSlideData),\n/* harmony export */ scgcGetUserId: () => (/* binding */ scgcGetUserId),\n/* harmony export */ scgcLogin: () => (/* binding */ scgcLogin),\n/* harmony export */ scgcOpenexplorer: () => (/* binding */ scgcOpenexplorer),\n/* harmony export */ scgcOpenwechat: () => (/* binding */ scgcOpenwechat),\n/* harmony export */ scgcSaveimage: () => (/* binding */ scgcSaveimage),\n/* harmony export */ scgcScan: () => (/* binding */ scgcScan),\n/* harmony export */ scgcSetVolume: () => (/* binding */ scgcSetVolume),\n/* harmony export */ scgcShare: () => (/* binding */ scgcShare),\n/* harmony export */ scgcToAppHome: () => (/* binding */ scgcToAppHome),\n/* harmony export */ scgcToDetail: () => (/* binding */ scgcToDetail),\n/* harmony export */ scgcToH5: () => (/* binding */ scgcToH5),\n/* harmony export */ scgcToKuaipai: () => (/* binding */ scgcToKuaipai),\n/* harmony export */ scgcToLive: () => (/* binding */ scgcToLive),\n/* harmony export */ scgcToMall: () => (/* binding */ scgcToMall),\n/* harmony export */ scgcToMallDetail: () => (/* binding */ scgcToMallDetail),\n/* harmony export */ scgcToSpecial: () => (/* binding */ scgcToSpecial),\n/* harmony export */ scgcToSubSpecial: () => (/* binding */ scgcToSubSpecial),\n/* harmony export */ scgcToTopic: () => (/* binding */ scgcToTopic),\n/* harmony export */ setDevMode: () => (/* reexport safe */ _utils__WEBPACK_IMPORTED_MODULE_0__.setDevMode)\n/* harmony export */ });\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils */ "./src/utils.ts");\n/*#__PURE__*/\n\n(0,_utils__WEBPACK_IMPORTED_MODULE_0__.initAsyncCallbackPool)();\nvar commonBridge = (0,_utils__WEBPACK_IMPORTED_MODULE_0__.commonBridgeFun)();\n/**\n * @description 阿里滑动验证(使用的旧版api,目前官方文档已散佚,可参考其他项目)\n * @param {IGetSlideDataData} data\n */\nvar scgcGetSlideData = function (data) {\n commonBridge("getSlideData", data);\n};\n/**\n * @description 调用app登录\n */\nvar scgcLogin = function () {\n commonBridge("login");\n};\n/**\n * @description 调用app扫码\n */\nvar scgcScan = function () {\n commonBridge("scan");\n};\n/**\n * @description 关闭当前webview\n */\nvar scgcClose = function () {\n commonBridge("close");\n};\n/**\n * @description 使用系统浏览器打开指定的url\n * @param {IOpenexplorerData} data\n * @param {string} data.path 需要打开的url\n */\nvar scgcOpenexplorer = function (data) {\n commonBridge("openexplorer", data);\n};\n/**\n * @description 打开微信小程序\n * @param {IOpenwechatData} data\n * @param {string} data.appid 小程序appid,格式为gh开头\n * @param {string} data.path 小程序路径\n */\nvar scgcOpenwechat = function (data) {\n commonBridge("openwechat", data);\n};\n/**\n * @description 保存图片\n * @param {ISaveimageData} data\n * @param {string} data.data base64数据\n */\nvar scgcSaveimage = function (data) {\n commonBridge("saveimage", data);\n};\n/**\n * @description 配置app分享\n * @param {IShareData} data\n * @param {string} data.shareUrl 需要分享的地址\n * @param {string} data.shareImg 分享的封面\n * @param {string} data.shareTitle 分享的标题\n * @param {string} data.shareDesc 分享的说明\n * @param {boolean} data.doShare 是否在调用bridge时立即触发app的分享,默认为仅修改配置\n */\nvar scgcShare = function (data) {\n commonBridge("share", data);\n};\n/**\n * @description 打开一个webview\n * @param {ItoH5Data} data\n * @param {string} data.path 需要打开的url\n */\nvar scgcToH5 = function (data) {\n commonBridge("toH5", data);\n};\n/**\n * @description 打开app原生新闻页\n * @param {IToDetailData} data\n * @param {string} data.path 新闻json地址 --相对地址,没有域名前缀的形式\n * @param {number} data.newsType 新闻类型 --1.图文 2.音频 3.视频 4.纯视频新闻 6 外链\n */\nvar scgcToDetail = function (data) {\n commonBridge("toDetail", data);\n};\n/**\n * @description 打开app原生专题详情页\n * @param {IToSpecialData} data\n * @param {string} data.id 专题id\n */\nvar scgcToSpecial = function (data) {\n commonBridge("toSpecial", data);\n};\n/**\n * @description 打开app原生直播页\n * @param {IToLiveData} data\n * @param {string} data.id 直播id\n * @param {string} data.type 直播样式 1 横版 2 竖版\n */\nvar scgcToLive = function (data) {\n commonBridge("toLive", data);\n};\n/**\n * @description 打开app话题页\n * @param {IToTopicData} data\n * @param {string} data.name 话题名称\n * @param {string} data.cover 话题封面图(完整的图片地址)\n * @param {string} data.id 话题id\n */\nvar scgcToTopic = function (data) {\n commonBridge("toTopic", data);\n};\n/**\n * @description 设置app音量\n * @param {ISetVolumeData} data\n * @param {number} data.type 0 调小 1 调大\n */\nvar scgcSetVolume = function (data) {\n commonBridge("setVolume", data);\n};\n/**\n * @description 打开app原生专题新闻列表页\n * @param {IToSubSpecialData} data\n * @param {string} data.title 专题标题\n * @param {string} data.id 专题id\n */\nvar scgcToSubSpecial = function (data) {\n commonBridge("toSubSpecial", data);\n};\n/**\n * @description 打开积分商城商品详情\n * @param {IMallDetailData} data\n * @param {string} data.json 商品详情json地址\n * @param {string} data.url 商品详情网页地址\n * @param {string} data.ptype todo 未知\n * @param {number} data.isDark 0 普通模式 1 黑暗模式\n */\nvar scgcToMallDetail = function (data) {\n commonBridge("detailJson", data);\n};\n/**\n * @description 是否禁用app左右滑动(主要用户嵌入在app首页的swiper,避免滑动swiper的情况下,会滑动app首页的tab页)\n * @param {IMallDetailData} data\n * @param {boolean} data.focus 是否禁止app滑动\n *\n */\nvar scgcFocusWeb = function (data) {\n commonBridge("focusWeb", data);\n};\n/**\n * @description 跳转至积分商城\n */\nvar scgcToMall = function () {\n commonBridge("mall");\n};\n/**\n * @description 跳转至快拍\n */\nvar scgcToKuaipai = function () {\n commonBridge("release");\n};\n/**\n * @description 跳转至app首页\n */\nvar scgcToAppHome = function () {\n commonBridge("mainPage");\n};\n/**\n * @description 设置app顶部时间栏颜色\n * @param {IChangeStateBarColorData} data\n * @param {number} data.type 1 黑色 2 白色\n *\n */\nvar scgcChangeStateBarColor = function (data) {\n commonBridge("changeStatebarColor", data);\n};\n/**\n * @description 获取app 用户id\n * @return {Promise((success:boolean,data:string))} 返回promise\n *\n */\nvar scgcGetUserId = function () {\n return commonBridge("getUserId");\n};\n/**\n * @description 获取app 用户头像\n * @return {Promise((success:boolean,data:string))} 返回promise\n *\n */\nvar scgcGetAvatar = function () {\n return commonBridge("getAvatar");\n};\n/**\n * @description 获取app 用户昵称\n * @return {Promise((success:boolean,data:string))} 返回promise\n *\n */\nvar scgcGetNickName = function () {\n return commonBridge("getNickName");\n};\n// /**\n// * @description 获取app 用户手机号\n// * @return {Promise((success:boolean,data:string))} 返回promise\n// *\n// */\nvar scgcGetPhoneNumber = function () {\n return commonBridge("getPhoneNumber");\n};\n// /**\n// * @description 获取app token --暂不可用\n// * @return {Promise((success:boolean,data:string))} 返回promise,暂时不会有进catch的情况\n// *\n// */\n// export const scgcGetToken = () => {\n// return commonBridge("getToken");\n// };\n\n\n\n//# sourceURL=webpack://scgcBridge/./src/index.ts?\n}')},"./src/utils.ts":(__unused_webpack_module,__webpack_exports__,__webpack_require__)=>{eval('{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ commonBridgeFun: () => (/* binding */ commonBridgeFun),\n/* harmony export */ initAsyncCallbackPool: () => (/* binding */ initAsyncCallbackPool),\n/* harmony export */ isScgcApp: () => (/* binding */ isScgcApp),\n/* harmony export */ setDevMode: () => (/* binding */ setDevMode)\n/* harmony export */ });\nvar __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __generator = (undefined && undefined.__generator) || function (thisArg, body) {\n 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);\n return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError("Generator is already executing.");\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\n 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;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};\nvar distinctionPlatform = function () {\n var ua = navigator.userAgent;\n if (/(iPhone|iPad|iPod)/i.test(ua)) {\n return "ios";\n }\n else if (/Android/i.test(ua)) {\n return "android";\n }\n else if (/openharmony/i.test(ua)) {\n return "harmony";\n }\n else {\n return "unknow";\n }\n};\nvar asyncFunsArr = [\n "getToken",\n "getUserId",\n "getAvatar",\n "getNickName",\n "getPhoneNumber",\n "getPromise", //测试用\n];\nvar androidAsyncFunsArr = ["getPromise"];\nvar devMode = false;\nvar setDevMode = function () {\n devMode = true;\n};\nvar initAsyncCallbackPool = function () {\n window.bridgeCallbacks = {};\n};\nvar devFn = function (platform, fnName, data) {\n //动态判断是否为devMode,避免初始化commonBridgeFun比调用setDevMode的早的情况下,devMode参数已固定(损失的性能可以接受)\n if (devMode) {\n var devInfo = {\n platform: platform,\n fnName: fnName,\n data: data,\n };\n console.log(devInfo);\n }\n};\nfunction makeRandomId(fnName) {\n return "".concat(fnName || "anonymous", "_").concat(Date.now());\n}\nfunction registerCallback(fnName, cb) {\n if (!cb) {\n return null;\n }\n var callbackId = makeRandomId(fnName);\n if (!window.hasOwnProperty("bridgeCallbacks")) {\n window.bridgeCallbacks = {};\n }\n window.bridgeCallbacks[callbackId] = function (data) {\n var result;\n if (typeof data === "object") {\n result = data;\n }\n else if (typeof data === "string") {\n try {\n result = JSON.parse(data);\n }\n catch (e) {\n result = data;\n }\n }\n cb(result);\n };\n return "bridgeCallbacks.".concat(callbackId);\n}\nfunction androidBridgeFn(fnName, data) {\n var _this = this;\n var _a, _b;\n var androidParams = {\n Method: fnName,\n Data: data,\n };\n devFn("android", fnName, data);\n if (fnName == "getSlideData") {\n (_a = window === null || window === void 0 ? void 0 : window.testInterface) === null || _a === void 0 ? void 0 : _a.getSlideData(JSON.stringify(androidParams));\n }\n else if (asyncFunsArr.includes(fnName)) {\n var promiseRes = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {\n var res, result;\n var _a, _b;\n return __generator(this, function (_c) {\n try {\n //安卓使用了异步返回\n if (androidAsyncFunsArr.includes(fnName)) {\n (_a = window === null || window === void 0 ? void 0 : window.newBridge) === null || _a === void 0 ? void 0 : _a[fnName](JSON.stringify({\n callback: registerCallback(fnName, resolve),\n }));\n }\n else {\n res = (_b = window === null || window === void 0 ? void 0 : window.newBridge) === null || _b === void 0 ? void 0 : _b[fnName]();\n result = void 0;\n if (typeof res === "object") {\n result = res;\n }\n else if (typeof res === "string") {\n try {\n result = JSON.parse(res);\n }\n catch (e) {\n result = { success: false, data: res };\n }\n }\n if ((result === null || result === void 0 ? void 0 : result.success) == true) {\n resolve(result);\n }\n else {\n reject(result);\n }\n }\n }\n catch (error) {\n reject(error);\n }\n return [2 /*return*/];\n });\n }); });\n return promiseRes;\n }\n else {\n (_b = window === null || window === void 0 ? void 0 : window.bridge) === null || _b === void 0 ? void 0 : _b.callRouter(JSON.stringify(androidParams), function () { });\n }\n}\nfunction iosBridgeFn(fnName, data) {\n var _this = this;\n var _a, _b, _c;\n devFn("ios", fnName, data);\n if (asyncFunsArr.includes(fnName)) {\n var promiseRes = new Promise(function (resolve, reject) {\n var _a, _b, _c;\n try {\n var _cb = function (res) { return __awaiter(_this, void 0, void 0, function () {\n return __generator(this, function (_a) {\n try {\n if ((res === null || res === void 0 ? void 0 : res.success) == "true") {\n resolve({\n success: true,\n data: res === null || res === void 0 ? void 0 : res.data,\n });\n }\n else {\n reject({\n success: false,\n data: res === null || res === void 0 ? void 0 : res.data,\n });\n }\n }\n catch (error) {\n reject({\n success: false,\n data: error,\n });\n }\n return [2 /*return*/];\n });\n }); };\n (_c = (_b = (_a = window === null || window === void 0 ? void 0 : window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b[fnName]) === null || _c === void 0 ? void 0 : _c.postMessage({\n data: {},\n callback: registerCallback(fnName, _cb),\n });\n }\n catch (error) {\n reject({\n success: false,\n data: error,\n });\n }\n });\n return promiseRes;\n }\n else {\n (_c = (_b = (_a = window === null || window === void 0 ? void 0 : window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b[fnName]) === null || _c === void 0 ? void 0 : _c.postMessage(data);\n }\n}\nfunction harmonyBridgeFn(fnName, data) {\n var _a, _b;\n devFn("harmony", fnName, data);\n if (asyncFunsArr.includes(fnName)) {\n var promiseRes = new Promise(function (resolve, reject) {\n var _a;\n try {\n (_a = window === null || window === void 0 ? void 0 : window.harmonyBridge) === null || _a === void 0 ? void 0 : _a[fnName]({\n data: data,\n callback: registerCallback(fnName, resolve),\n });\n }\n catch (error) {\n reject(error);\n }\n });\n return promiseRes;\n }\n else {\n (_b = (_a = window === null || window === void 0 ? void 0 : window.harmonyBridge) === null || _a === void 0 ? void 0 : _a[fnName]) === null || _b === void 0 ? void 0 : _b.call(_a, data);\n }\n}\nfunction defaultBridgeFn(fnName, data) {\n console.log("请使用scgc app打开");\n if (asyncFunsArr.includes(fnName)) {\n var promiseRes = new Promise(function (resolve, reject) {\n try {\n resolve("请使用scgc app打开");\n }\n catch (error) {\n reject(error);\n }\n });\n return promiseRes;\n }\n}\nvar commonBridgeFun = function () {\n var platform = distinctionPlatform();\n if (platform == "android") {\n return androidBridgeFn;\n }\n else if (platform == "ios") {\n return iosBridgeFn;\n }\n else if (platform == "harmony") {\n return harmonyBridgeFn;\n }\n else {\n return defaultBridgeFn;\n }\n};\n/**\n * @description 是否为app环境\n * @return {boolean} true 是 false 否\n */\nvar isScgcApp = function () {\n return navigator.userAgent.indexOf("SCGC_UA") > -1;\n};\n\n\n//# sourceURL=webpack://scgcBridge/./src/utils.ts?\n}')}},__webpack_module_cache__={};function __webpack_require__(n){var e=__webpack_module_cache__[n];if(void 0!==e)return e.exports;var a=__webpack_module_cache__[n]={exports:{}};return __webpack_modules__[n](a,a.exports,__webpack_require__),a.exports}__webpack_require__.d=(n,e)=>{for(var a in e)__webpack_require__.o(e,a)&&!__webpack_require__.o(n,a)&&Object.defineProperty(n,a,{enumerable:!0,get:e[a]})},__webpack_require__.o=(n,e)=>Object.prototype.hasOwnProperty.call(n,e),__webpack_require__.r=n=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})};var __webpack_exports__=__webpack_require__("./src/index.ts");return __webpack_exports__})());
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@scgczm/bridge",
3
+ "version": "1.0.0",
4
+ "description": "scgczm app jsbridge",
5
+ "main": "./dist/cjs/index.js",
6
+ "module": "./dist/esm/index.js",
7
+ "scripts": {
8
+ "build": "node build.js"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "keywords": [],
14
+ "author": "",
15
+ "license": "ISC",
16
+ "publishConfig": {
17
+ "access": "public",
18
+ "registry": "https://registry.npmjs.org/"
19
+ },
20
+ "devDependencies": {
21
+ "@types/node": "^20.14.2",
22
+ "terser-webpack-plugin": "^5.3.10",
23
+ "ts-loader": "^9.5.1",
24
+ "typescript": "^5.4.5",
25
+ "webpack": "^5.91.0",
26
+ "webpack-cli": "^5.1.4",
27
+ "rimraf": "^5.0.7"
28
+ }
29
+ }
package/readme.dev.md ADDED
@@ -0,0 +1,5 @@
1
+ # 开发者须知
2
+
3
+ # umd 打包好之后放至 oss
4
+
5
+ ## oss://scgc-sctv-bucket/sctv/dependencies/bridge/