@r2d2cn/rk-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.
package/base.d.ts ADDED
@@ -0,0 +1,117 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import type { AiChatRecord, AppInfo, BridgeEventType, Credentials, DeviceInfo, DirectoryFileItem, DirectoryFileListRequest, HttpResponse, LanguageOption, MediaItem } from './types';
3
+ export interface IRKBridge {
4
+ initSDK(params: string): Promise<void>;
5
+ registerClientId(clientId: string): Promise<void>;
6
+ navigate(url: string): Promise<void>;
7
+ goBack(): Promise<void>;
8
+ getAuthCode(): Promise<string>;
9
+ getUserInfo(): Promise<any>;
10
+ getAppToken(): Promise<string>;
11
+ getUserToken(): Promise<string>;
12
+ getCredentials(): Promise<Credentials>;
13
+ getDeviceInfo(): Promise<DeviceInfo>;
14
+ getLocationInfo(): Promise<any>;
15
+ getStatusBarHeight(): Promise<number>;
16
+ getSdkToken(): Promise<string>;
17
+ setVolume(volume: number): Promise<void>;
18
+ setBrightness(brightness: number): Promise<void>;
19
+ makePhone(phoneNumber: string): Promise<void>;
20
+ getFileProvider(): Promise<string>;
21
+ sendEventToNative(event: string): Promise<void>;
22
+ startBluetoothScan(): Promise<void>;
23
+ stopBluetoothScan(): Promise<void>;
24
+ connectBluetooth(deviceId: string): Promise<void>;
25
+ startWifiP2PDiscoverPeers(): Promise<void>;
26
+ stopWifiP2PDiscoverPeers(): Promise<void>;
27
+ connectWifiP2P(deviceAddress: string): Promise<void>;
28
+ getChatList(): Promise<AiChatRecord[]>;
29
+ getSourceLanguageOptions(): Promise<LanguageOption[]>;
30
+ getTargetLanguageOptions(): Promise<LanguageOption[]>;
31
+ getSourceLanguage(): Promise<string>;
32
+ getTargetLanguage(): Promise<string>;
33
+ setLanguage(sourceLang: string, targetLang: string): Promise<void>;
34
+ getAlbumFileList(): Promise<MediaItem[]>;
35
+ getDirectoryFileList(params: DirectoryFileListRequest): Promise<DirectoryFileItem[]>;
36
+ httpRequest(params: string): Promise<HttpResponse>;
37
+ sendTextMessageByClassicBT(message: string): Promise<void>;
38
+ sendTextMessageByBle(message: string): Promise<void>;
39
+ sendTextMessageByP2P(message: string): Promise<void>;
40
+ sendStreamData(tag: string, message: string): Promise<void>;
41
+ sendGlassBroadcast(action: string): Promise<void>;
42
+ getApps(): Promise<AppInfo[]>;
43
+ getAppInfo(packageName: string): Promise<AppInfo>;
44
+ installApp(apkfilePath: string): Promise<void>;
45
+ uninstallApp(packageName: string): Promise<void>;
46
+ launchApp(packageName: string): Promise<void>;
47
+ stopApp(packageName: string): Promise<void>;
48
+ downloadApk(apkUrl: string): Promise<void>;
49
+ getActiveInstallTask(): Promise<void>;
50
+ subscribeAppProgress(): Promise<void>;
51
+ unsubscribeAppProgress(): Promise<void>;
52
+ /**
53
+ * 设置/取消开机自启。packageName 为空字符串表示取消。
54
+ * success 表示眼镜端已确认生效;Native 未合入时 reject(-999)。
55
+ */
56
+ setBootApp(packageName: string): Promise<void>;
57
+ on<T>(event: BridgeEventType, callback: (data: T) => void): void;
58
+ off(event: BridgeEventType, callback: (data: any) => void): void;
59
+ }
60
+ export declare abstract class BaseBridge {
61
+ protected emitter: EventEmitter<string | symbol, any>;
62
+ protected hasBackground: boolean;
63
+ constructor();
64
+ protected abstract initCallback(): void;
65
+ protected abstract initNativeEventListener(): void;
66
+ abstract initSDK(params: string): Promise<void>;
67
+ abstract registerClientId(clientId: string): Promise<void>;
68
+ abstract navigate(url: string): Promise<void>;
69
+ abstract goBack(): Promise<void>;
70
+ abstract getDeviceInfo(): Promise<DeviceInfo>;
71
+ abstract getStatusBarHeight(): Promise<number>;
72
+ abstract getCredentials(): Promise<Credentials>;
73
+ abstract getLocationInfo(): Promise<any>;
74
+ abstract getAuthCode(): Promise<string>;
75
+ abstract getUserInfo(): Promise<any>;
76
+ abstract getAppToken(): Promise<string>;
77
+ abstract getUserToken(): Promise<string>;
78
+ abstract getSdkToken(): Promise<string>;
79
+ abstract setVolume(volume: number): Promise<void>;
80
+ abstract setBrightness(brightness: number): Promise<void>;
81
+ abstract makePhone(phoneNumber: string): Promise<void>;
82
+ abstract getFileProvider(): Promise<string>;
83
+ abstract sendEventToNative(event: string): Promise<void>;
84
+ abstract startBluetoothScan(): Promise<void>;
85
+ abstract stopBluetoothScan(): Promise<void>;
86
+ abstract connectBluetooth(deviceId: string): Promise<void>;
87
+ abstract startWifiP2PDiscoverPeers(): Promise<void>;
88
+ abstract stopWifiP2PDiscoverPeers(): Promise<void>;
89
+ abstract connectWifiP2P(deviceAddress: string): Promise<void>;
90
+ abstract getChatList(): Promise<AiChatRecord[]>;
91
+ abstract getSourceLanguageOptions(): Promise<LanguageOption[]>;
92
+ abstract getTargetLanguageOptions(): Promise<LanguageOption[]>;
93
+ abstract getSourceLanguage(): Promise<string>;
94
+ abstract getTargetLanguage(): Promise<string>;
95
+ abstract setLanguage(sourceLang: string, targetLang: string): Promise<void>;
96
+ abstract getAlbumFileList(): Promise<MediaItem[]>;
97
+ abstract getDirectoryFileList(params: DirectoryFileListRequest): Promise<DirectoryFileItem[]>;
98
+ abstract httpRequest(params: string): Promise<HttpResponse>;
99
+ abstract sendTextMessageByClassicBT(message: string): Promise<void>;
100
+ abstract sendTextMessageByBle(message: string): Promise<void>;
101
+ abstract sendTextMessageByP2P(message: string): Promise<void>;
102
+ abstract sendStreamData(tag: string, message: string): Promise<void>;
103
+ abstract sendGlassBroadcast(action: string): Promise<void>;
104
+ abstract getApps(): Promise<AppInfo[]>;
105
+ abstract getAppInfo(packageName: string): Promise<AppInfo>;
106
+ abstract installApp(apkfilePath: string): Promise<void>;
107
+ abstract uninstallApp(packageName: string): Promise<void>;
108
+ abstract launchApp(packageName: string): Promise<void>;
109
+ abstract stopApp(packageName: string): Promise<void>;
110
+ abstract downloadApk(apkUrl: string): Promise<void>;
111
+ abstract getActiveInstallTask(): Promise<void>;
112
+ abstract subscribeAppProgress(): Promise<void>;
113
+ abstract unsubscribeAppProgress(): Promise<void>;
114
+ abstract setBootApp(packageName: string): Promise<void>;
115
+ on<T>(event: BridgeEventType, callback: (data: T) => void): void;
116
+ off(event: BridgeEventType, callback: (data: any) => void): void;
117
+ }
package/index.cjs.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var S=Object.defineProperty;var y=(r,n,t)=>n in r?S(r,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[n]=t;var e=(r,n,t)=>y(r,typeof n!="symbol"?n+"":n,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const K=require("uuid"),v=require("eventemitter3"),A="1048576-10772480",L="66D9EF553887B5054FFC4AC1CB2CB590",m=window.lx;function I(){return new Promise((r,n)=>{console.log("获取 authCode 参数 appId: ",A),m.biz.getAuthCode({appId:A,success:function(t){console.log("获取 authCode 成功",t),r(t)},fail:function(t){console.log("获取 authCode 失败",t),n(t)}})})}class _{constructor(n){e(this,"baseURL");e(this,"baseRequest");this.baseURL=(n==null?void 0:n.baseURL)||"",this.baseRequest=(n==null?void 0:n.baseRequest)||(()=>Promise.resolve({statusCode:-1,body:""}))}async httpRequest(n){try{const{statusCode:t,body:s}=await this.baseRequest(JSON.stringify(n));console.log("native http response:",t,s);const i=JSON.parse(s);if(i.errCode===0)return i.data;throw new Error(i.errMsg||"未知错误")}catch(t){throw console.warn("native http request error:",t),t}}get(n,t){return this.httpRequest({method:"GET",url:this.baseURL+n,queryParams:t==null?void 0:t.params,headers:{"Content-Type":"application/json",...(t==null?void 0:t.headers)??{}}})}post(n,t){return this.httpRequest({method:"POST",url:this.baseURL+n,body:t==null?void 0:t.data,headers:{"Content-Type":"application/json",...(t==null?void 0:t.headers)??{}}})}}var P,T;const R=new _({baseURL:"http://20.65.2.12:10443/open/apigw",baseRequest:typeof window<"u"&&((T=(P=window==null?void 0:window.RKBridge)==null?void 0:P.Http)==null?void 0:T.request)||void 0});class N{constructor(n){e(this,"baseURL");e(this,"baseRequest");this.baseURL=(n==null?void 0:n.baseURL)||"",this.baseRequest=(n==null?void 0:n.baseRequest)||(()=>Promise.resolve({statusCode:-1,body:""}))}async httpRequest(n){try{const{statusCode:t,body:s}=await this.baseRequest(JSON.stringify(n));console.log("native http response:",t,s);const i=JSON.parse(s);if(i.resultCode===0)return i;throw new Error(i.message||"未知错误")}catch(t){throw console.warn("native http request error:",t),t}}get(n,t){return this.httpRequest({method:"GET",url:this.baseURL+n,queryParams:t==null?void 0:t.params,headers:{...(t==null?void 0:t.headers)??{}}})}post(n,t){return this.httpRequest({method:"POST",url:this.baseURL+n,body:t==null?void 0:t.data,headers:{...(t==null?void 0:t.headers)??{}}})}}var b,k;new N({baseURL:"http://20.65.1.157",baseRequest:typeof window<"u"&&((k=(b=window==null?void 0:window.RKBridge)==null?void 0:b.Http)==null?void 0:k.request)||void 0});function D(){return R.get("/v1/apptoken/create",{params:{grant_type:"client_credential",appid:A,secret:L}})}function q(r,n){return R.get("/v1/usertoken/create",{params:{grant_type:"authorization_code",app_token:n,code:r}})}function M(r,n){return R.get("/v1/users/fetch",{params:{user_token:n,app_token:r}})}class f{constructor(){e(this,"emitter",new v);e(this,"hasBackground",!1);this.initCallback(),this.initNativeEventListener()}on(n,t){this.emitter.on(n,t)}off(n,t){this.emitter.off(n,t)}}function E(r){window[r]&&(window.RKBridge=window[r],window.RKBridge_GlassBluetooth=window[r+"_GlassBluetooth"],window.RKBridge_GlassWifiP2P=window[r+"_GlassWifiP2P"],window.RKBridge_GlassAiChat=window[r+"_GlassAiChat"],window.RKBridge_GlassTranslation=window[r+"_GlassTranslation"],window.RKBridge_GlassAlbum=window[r+"_GlassAlbum"],window.RKBridge_GlassFileSystem=window[r+"_GlassFileSystem"],window.RKBridge_GlassMessage=window[r+"_GlassMessage"],window.RKBridge_GlassAppInstall=window[r+"_GlassAppInstall"],window.RKBridge_Http=window[r+"_Http"])}let O=class extends f{constructor(){super();e(this,"emitter",new v);e(this,"appToken","");e(this,"userToken","");e(this,"hasBackground",!1);e(this,"initSDK",this.generateBridgeFunc("initSDK"));e(this,"registerClientId",this.generateBridgeFunc("registerClientId"));e(this,"navigate",t=>(t+=(t.includes("?")?"&":"?")+"adjust_fullscreen_inset=true",t+="&rkbridge=true",console.log("跳转页面: ",t),window.lx.ui.openView({mode:"webview",navigationBarBackgroundColor:"#FFFFFF",navigationBarFrontStyle:"#0C120D",url:t,useSplitScreen:!1}),Promise.resolve()));e(this,"goBack",()=>(window.lx.ui.closeWindow(),Promise.resolve()));e(this,"getDeviceInfo",this.generateBridgeFunc("getDeviceInfo"));e(this,"getLocationInfo",this.generateBridgeFunc("getLocationInfo"));e(this,"getStatusBarHeight",this.generateBridgeFunc("getStatusBarHeight"));e(this,"getCredentials",()=>{try{const t=localStorage.getItem("appCredential")||"",s=localStorage.getItem("userCredential")||"",i={appCredential:t,userCredential:s};return Promise.resolve(i)}catch(t){return console.warn("获取解析 credentials 失败",t),Promise.resolve({})}});e(this,"getAuthCode",async()=>(await I()).authCode);e(this,"getUserInfo",async()=>{const t=await this.getAuthCode();console.log("获取 authCode 成功",t);const s=await this.getAppToken();console.log("获取 appToken 成功",s);const i=await this.getUserToken();return console.log("获取 userToken 成功",i),await M(s,i)});e(this,"getAppToken",async()=>{const t=await D();return this.appToken=t.appToken,t.appToken});e(this,"getUserToken",async()=>{const t=await this.getAuthCode(),s=this.appToken,i=await q(t,s);return this.userToken=i.userToken,i.userToken});e(this,"getSdkToken",this.generateBridgeFunc("getSdkToken"));e(this,"setVolume",this.generateBridgeFunc("setVolume"));e(this,"setBrightness",this.generateBridgeFunc("setBrightness"));e(this,"makePhone",this.generateBridgeFunc("makePhone"));e(this,"getFileProvider",this.generateBridgeFunc("getFileProvider"));e(this,"sendEventToNative",this.generateBridgeFunc("sendEventToNative"));e(this,"startBluetoothScan",this.generateBridgeFunc("startScan","GlassBluetooth"));e(this,"stopBluetoothScan",this.generateBridgeFunc("stopScan","GlassBluetooth"));e(this,"connectBluetooth",this.generateBridgeFunc("connect","GlassBluetooth"));e(this,"startWifiP2PDiscoverPeers",this.generateBridgeFunc("startDiscoverPeers","GlassWifiP2P"));e(this,"stopWifiP2PDiscoverPeers",this.generateBridgeFunc("stopDiscoverPeers","GlassWifiP2P"));e(this,"connectWifiP2P",this.generateBridgeFunc("connect","GlassWifiP2P"));e(this,"getChatList",this.generateBridgeFunc("getChatList","GlassAiChat"));e(this,"getSourceLanguageOptions",this.generateBridgeFunc("getSourceLanguageOptions","GlassTranslation"));e(this,"getTargetLanguageOptions",this.generateBridgeFunc("getTargetLanguageOptions","GlassTranslation"));e(this,"getSourceLanguage",this.generateBridgeFunc("getSourceLanguage","GlassTranslation"));e(this,"getTargetLanguage",this.generateBridgeFunc("getTargetLanguage","GlassTranslation"));e(this,"setLanguage",this.generateBridgeFunc("setLanguage","GlassTranslation"));e(this,"getAlbumFileList",this.generateBridgeFunc("getAlbumFileList","GlassAlbum"));e(this,"getDirectoryFileListNative",this.generateBridgeFunc("getDirectoryFileList","GlassFileSystem"));e(this,"getDirectoryFileList",t=>this.getDirectoryFileListNative(JSON.stringify(t)));e(this,"httpRequest",this.generateBridgeFunc("request","Http"));e(this,"getApps",this.generateBridgeFunc("getApps","GlassAppInstall"));e(this,"getAppInfo",this.generateBridgeFunc("getAppInfo","GlassAppInstall"));e(this,"installApp",this.generateBridgeFunc("installApp","GlassAppInstall"));e(this,"uninstallApp",this.generateBridgeFunc("uninstallApp","GlassAppInstall"));e(this,"launchApp",this.generateBridgeFunc("launchApp","GlassAppInstall"));e(this,"stopApp",this.generateBridgeFunc("stopApp","GlassAppInstall"));e(this,"downloadApk",this.generateBridgeFunc("downloadApk","GlassAppInstall"));e(this,"getActiveInstallTask",this.generateBridgeFunc("getActiveInstallTask","GlassAppInstall"));e(this,"subscribeAppProgress",this.generateBridgeFunc("subscribeAppProgress","GlassAppInstall"));e(this,"unsubscribeAppProgress",this.generateBridgeFunc("unsubscribeAppProgress","GlassAppInstall"));e(this,"setBootAppNative",this.generateBridgeFunc("setBootApp","GlassAppInstall"));e(this,"setBootApp",t=>{var i;const s=(i=window.RKBridge)==null?void 0:i.GlassAppInstall;return!s||typeof s.setBootApp!="function"?Promise.reject(-999):this.setBootAppNative(t)});e(this,"sendTextMessageByClassicBT",this.generateBridgeFunc("sendTextMessageByClassicBT","GlassMessage"));e(this,"sendTextMessageByBle",this.generateBridgeFunc("sendTextMessageByBLE","GlassMessage"));e(this,"sendTextMessageByP2P",this.generateBridgeFunc("sendTextMessageByP2P","GlassMessage"));e(this,"sendStreamData",this.generateBridgeFunc("sendStreamData","GlassMessage"));e(this,"sendGlassBroadcast",this.generateBridgeFunc("sendGlassBroadcast","GlassMessage"))}initCallback(){E("BKBridge"),window.RKBridgeCallback||(window.RKBridgeCallback={_onCallback:(s,i,a,g)=>{s&&this.emitter.emit(s,i,a,g)}}),window.RKBridge&&(window.RKBridge.GlassBluetooth=window.RKBridge_GlassBluetooth,window.RKBridge.GlassWifiP2P=window.RKBridge_GlassWifiP2P,window.RKBridge.GlassAiChat=window.RKBridge_GlassAiChat,window.RKBridge.GlassTranslation=window.RKBridge_GlassTranslation,window.RKBridge.GlassAlbum=window.RKBridge_GlassAlbum,window.RKBridge.GlassFileSystem=window.RKBridge_GlassFileSystem,window.RKBridge.GlassMessage=window.RKBridge_GlassMessage,window.RKBridge.GlassAppInstall=window.RKBridge_GlassAppInstall,window.RKBridge.Http=window.RKBridge_Http);const t=window.RKBridgeCallback._onCallback;window.RKBridgeCallback._onCallback=(s,i,a,g)=>{t==null||t(s,i,a,g),s&&this.emitter.emit(s,i,a,g)}}generateBridgeFunc(t,s){return(...i)=>{var g;const a=s?(g=window.RKBridge)==null?void 0:g[s]:window.RKBridge;return!a||!a[t]?(console.warn(`Bridge method ${s?`${s}.`:""}${t} is not available, returning default value.`),Promise.resolve(void 0)):new Promise((p,F)=>{var d,c,u,B;const l=`cb_${K.v4()}`;this.emitter.on(l,(h,o,w)=>{if(h.toLowerCase()==="success"){if(o)try{o=JSON.parse(o)}catch(G){console.warn("解析 callback 结果失败",G)}p(o)}else h.toLowerCase()==="error"&&F(w);this.emitter.off(l)}),s?(c=(d=window.RKBridge[s])[t])==null||c.call(d,...i,l):(B=(u=window.RKBridge)[t])==null||B.call(u,...i,l)})}}initNativeEventListener(){const t=(s,i)=>{this.emitter.emit(s,i.data)};if(window.dispatchNativeEvent){const s=window.dispatchNativeEvent;window.dispatchNativeEvent=(...i)=>{s(...i),t(...i)}}else window.dispatchNativeEvent=t}};const U=new O;class x extends f{constructor(){super(...arguments);e(this,"emitter",new v);e(this,"hasBackground",!0);e(this,"initSDK",this.generateBridgeFunc("initSDK"));e(this,"registerClientId",this.generateBridgeFunc("registerClientId"));e(this,"navigate",t=>(window.JSBridge.navigate(t),Promise.resolve()));e(this,"goBack",()=>(window.JSBridge.goBack(),Promise.resolve()));e(this,"getDeviceInfo",this.generateBridgeFunc("getDeviceInfo"));e(this,"getStatusBarHeight",this.generateBridgeFunc("getStatusBarHeight"));e(this,"getLocationInfo",this.generateBridgeFunc("getLocationInfo"));e(this,"getAuthCode",this.generateBridgeFunc("getAuthCode"));e(this,"getUserInfo",()=>window.JSBridge.getUserInfo());e(this,"getCredentials",()=>{var t,s;try{const i=JSON.parse((s=(t=window.JSBridge)==null?void 0:t.getCredentials)==null?void 0:s.call(t));return Promise.resolve(i)}catch(i){return console.warn("获取解析 credentials 失败",i),Promise.resolve({})}});e(this,"getAppToken",this.generateBridgeFunc("getAppToken"));e(this,"getUserToken",this.generateBridgeFunc("getUserToken"));e(this,"getSdkToken",this.generateBridgeFunc("getSdkToken"));e(this,"setVolume",this.generateBridgeFunc("setVolume"));e(this,"setBrightness",this.generateBridgeFunc("setBrightness"));e(this,"makePhone",this.generateBridgeFunc("makePhone"));e(this,"sendEventToNative",this.generateBridgeFunc("sendEventToNative"));e(this,"getFileProvider",this.generateBridgeFunc("getFileProvider"));e(this,"startBluetoothScan",this.generateBridgeFunc("startScan","GlassBluetooth"));e(this,"stopBluetoothScan",this.generateBridgeFunc("stopScan","GlassBluetooth"));e(this,"connectBluetooth",this.generateBridgeFunc("connect","GlassBluetooth"));e(this,"startWifiP2PDiscoverPeers",this.generateBridgeFunc("startDiscoverPeers","GlassWifiP2P"));e(this,"stopWifiP2PDiscoverPeers",this.generateBridgeFunc("stopDiscoverPeers","GlassWifiP2P"));e(this,"connectWifiP2P",this.generateBridgeFunc("connect","GlassWifiP2P"));e(this,"getChatList",this.generateBridgeFunc("getChatList","GlassAiChat"));e(this,"getSourceLanguageOptions",this.generateBridgeFunc("getSourceLanguageOptions","GlassTranslation"));e(this,"getTargetLanguageOptions",this.generateBridgeFunc("getTargetLanguageOptions","GlassTranslation"));e(this,"getSourceLanguage",this.generateBridgeFunc("getSourceLanguage","GlassTranslation"));e(this,"getTargetLanguage",this.generateBridgeFunc("getTargetLanguage","GlassTranslation"));e(this,"setLanguage",this.generateBridgeFunc("setLanguage","GlassTranslation"));e(this,"getAlbumFileList",this.generateBridgeFunc("getAlbumFileList","GlassAlbum"));e(this,"getDirectoryFileListNative",this.generateBridgeFunc("getDirectoryFileList","GlassFileSystem"));e(this,"getDirectoryFileList",t=>this.getDirectoryFileListNative(JSON.stringify(t)));e(this,"httpRequest",this.generateBridgeFunc("request","Http"));e(this,"getApps",this.generateBridgeFunc("getApps","GlassAppInstall"));e(this,"getAppInfo",this.generateBridgeFunc("getAppInfo","GlassAppInstall"));e(this,"installApp",this.generateBridgeFunc("installApp","GlassAppInstall"));e(this,"uninstallApp",this.generateBridgeFunc("uninstallApp","GlassAppInstall"));e(this,"launchApp",this.generateBridgeFunc("launchApp","GlassAppInstall"));e(this,"stopApp",this.generateBridgeFunc("stopApp","GlassAppInstall"));e(this,"downloadApk",this.generateBridgeFunc("downloadApk","GlassAppInstall"));e(this,"getActiveInstallTask",this.generateBridgeFunc("getActiveInstallTask","GlassAppInstall"));e(this,"subscribeAppProgress",this.generateBridgeFunc("subscribeAppProgress","GlassAppInstall"));e(this,"unsubscribeAppProgress",this.generateBridgeFunc("unsubscribeAppProgress","GlassAppInstall"));e(this,"setBootAppNative",this.generateBridgeFunc("setBootApp","GlassAppInstall"));e(this,"setBootApp",t=>{var i;const s=(i=window.RKBridge)==null?void 0:i.GlassAppInstall;return!s||typeof s.setBootApp!="function"?Promise.reject(-999):this.setBootAppNative(t)});e(this,"sendTextMessageByClassicBT",this.generateBridgeFunc("sendTextMessageByClassicBT","GlassMessage"));e(this,"sendTextMessageByBle",this.generateBridgeFunc("sendTextMessageByBLE","GlassMessage"));e(this,"sendTextMessageByP2P",this.generateBridgeFunc("sendTextMessageByP2P","GlassMessage"));e(this,"sendStreamData",this.generateBridgeFunc("sendStreamData","GlassMessage"));e(this,"sendGlassBroadcast",this.generateBridgeFunc("sendGlassBroadcast","GlassMessage"))}initCallback(){window.RKBridgeCallback||(window.RKBridgeCallback={_onCallback:(s,i,a,g)=>{s&&this.emitter.emit(s,i,a,g)}}),window.RKBridge&&(window.RKBridge.GlassBluetooth=window.RKBridge_GlassBluetooth,window.RKBridge.GlassWifiP2P=window.RKBridge_GlassWifiP2P,window.RKBridge.GlassAiChat=window.RKBridge_GlassAiChat,window.RKBridge.GlassTranslation=window.RKBridge_GlassTranslation,window.RKBridge.GlassAlbum=window.RKBridge_GlassAlbum,window.RKBridge.GlassFileSystem=window.RKBridge_GlassFileSystem,window.RKBridge.GlassMessage=window.RKBridge_GlassMessage,window.RKBridge.GlassAppInstall=window.RKBridge_GlassAppInstall);const t=window.RKBridgeCallback._onCallback;window.RKBridgeCallback._onCallback=(s,i,a,g)=>{t==null||t(s,i,a,g),s&&this.emitter.emit(s,i,a,g)}}generateBridgeFunc(t,s){return(...i)=>{var g;const a=s?(g=window.RKBridge)==null?void 0:g[s]:window.RKBridge;return!a||!a[t]?(console.warn(`Bridge method ${s?`${s}.`:""}${t} is not available, returning default value.`),Promise.resolve(void 0)):new Promise((p,F)=>{var d,c,u,B;const l=`cb_${K.v4()}`;this.emitter.on(l,(h,o,w)=>{if(h.toLowerCase()==="success"){if(o)try{o=JSON.parse(o)}catch(G){console.warn("解析 callback 结果失败",G),console.log(t,s,o,w)}p(o)}else h.toLowerCase()==="error"&&F(w);this.emitter.off(l)}),s?(c=(d=window.RKBridge[s])[t])==null||c.call(d,...i,l):(B=(u=window.RKBridge)[t])==null||B.call(u,...i,l)})}}initNativeEventListener(){const t=(s,i)=>{this.emitter.emit(s,i.data)};if(window.dispatchNativeEvent){const s=window.dispatchNativeEvent;window.dispatchNativeEvent=(...i)=>{s(...i),t(...i)}}else window.dispatchNativeEvent=t}}const W=new x;var C;const J=((C=window==null?void 0:window.navigator)==null?void 0:C.userAgent)||"",H=J.includes("lanxin")?U:W;exports.BaseBridge=f;exports.rkBridge=H;
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { BaseBridge } from './base';
2
+ export { BaseBridge };
3
+ export type { IRKBridge } from './base';
4
+ export declare const rkBridge: BaseBridge;
5
+ export * from './types';
package/index.esm.js ADDED
@@ -0,0 +1,532 @@
1
+ var m = Object.defineProperty;
2
+ var L = (r, n, t) => n in r ? m(r, n, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[n] = t;
3
+ var e = (r, n, t) => L(r, typeof n != "symbol" ? n + "" : n, t);
4
+ import { v4 as K } from "uuid";
5
+ import v from "eventemitter3";
6
+ const A = "1048576-10772480", S = "66D9EF553887B5054FFC4AC1CB2CB590", y = window.lx;
7
+ function I() {
8
+ return new Promise((r, n) => {
9
+ console.log("获取 authCode 参数 appId: ", A), y.biz.getAuthCode({
10
+ appId: A,
11
+ success: function(t) {
12
+ console.log("获取 authCode 成功", t), r(t);
13
+ },
14
+ fail: function(t) {
15
+ console.log("获取 authCode 失败", t), n(t);
16
+ }
17
+ });
18
+ });
19
+ }
20
+ class _ {
21
+ constructor(n) {
22
+ e(this, "baseURL");
23
+ e(this, "baseRequest");
24
+ this.baseURL = (n == null ? void 0 : n.baseURL) || "", this.baseRequest = (n == null ? void 0 : n.baseRequest) || (() => Promise.resolve({ statusCode: -1, body: "" }));
25
+ }
26
+ async httpRequest(n) {
27
+ try {
28
+ const { statusCode: t, body: s } = await this.baseRequest(JSON.stringify(n));
29
+ console.log("native http response:", t, s);
30
+ const i = JSON.parse(s);
31
+ if (i.errCode === 0)
32
+ return i.data;
33
+ throw new Error(i.errMsg || "未知错误");
34
+ } catch (t) {
35
+ throw console.warn("native http request error:", t), t;
36
+ }
37
+ }
38
+ get(n, t) {
39
+ return this.httpRequest({
40
+ method: "GET",
41
+ url: this.baseURL + n,
42
+ queryParams: t == null ? void 0 : t.params,
43
+ headers: {
44
+ "Content-Type": "application/json",
45
+ ...(t == null ? void 0 : t.headers) ?? {}
46
+ }
47
+ });
48
+ }
49
+ post(n, t) {
50
+ return this.httpRequest({
51
+ method: "POST",
52
+ url: this.baseURL + n,
53
+ body: t == null ? void 0 : t.data,
54
+ headers: {
55
+ "Content-Type": "application/json",
56
+ ...(t == null ? void 0 : t.headers) ?? {}
57
+ }
58
+ });
59
+ }
60
+ }
61
+ var f, P;
62
+ const R = new _({
63
+ baseURL: "http://20.65.2.12:10443/open/apigw",
64
+ baseRequest: typeof window < "u" && ((P = (f = window == null ? void 0 : window.RKBridge) == null ? void 0 : f.Http) == null ? void 0 : P.request) || void 0
65
+ });
66
+ class N {
67
+ constructor(n) {
68
+ e(this, "baseURL");
69
+ e(this, "baseRequest");
70
+ this.baseURL = (n == null ? void 0 : n.baseURL) || "", this.baseRequest = (n == null ? void 0 : n.baseRequest) || (() => Promise.resolve({ statusCode: -1, body: "" }));
71
+ }
72
+ async httpRequest(n) {
73
+ try {
74
+ const { statusCode: t, body: s } = await this.baseRequest(JSON.stringify(n));
75
+ console.log("native http response:", t, s);
76
+ const i = JSON.parse(s);
77
+ if (i.resultCode === 0)
78
+ return i;
79
+ throw new Error(i.message || "未知错误");
80
+ } catch (t) {
81
+ throw console.warn("native http request error:", t), t;
82
+ }
83
+ }
84
+ get(n, t) {
85
+ return this.httpRequest({
86
+ method: "GET",
87
+ url: this.baseURL + n,
88
+ queryParams: t == null ? void 0 : t.params,
89
+ headers: {
90
+ // 'Content-Type': 'application/json',
91
+ ...(t == null ? void 0 : t.headers) ?? {}
92
+ }
93
+ });
94
+ }
95
+ post(n, t) {
96
+ return this.httpRequest({
97
+ method: "POST",
98
+ url: this.baseURL + n,
99
+ body: t == null ? void 0 : t.data,
100
+ headers: {
101
+ // 'Content-Type': 'application/json',
102
+ ...(t == null ? void 0 : t.headers) ?? {}
103
+ }
104
+ });
105
+ }
106
+ }
107
+ var T, b;
108
+ new N({
109
+ baseURL: "http://20.65.1.157",
110
+ baseRequest: typeof window < "u" && ((b = (T = window == null ? void 0 : window.RKBridge) == null ? void 0 : T.Http) == null ? void 0 : b.request) || void 0
111
+ });
112
+ function D() {
113
+ return R.get("/v1/apptoken/create", {
114
+ params: {
115
+ grant_type: "client_credential",
116
+ appid: A,
117
+ secret: S
118
+ }
119
+ });
120
+ }
121
+ function q(r, n) {
122
+ return R.get("/v1/usertoken/create", {
123
+ params: {
124
+ grant_type: "authorization_code",
125
+ app_token: n,
126
+ code: r
127
+ }
128
+ });
129
+ }
130
+ function M(r, n) {
131
+ return R.get("/v1/users/fetch", {
132
+ params: {
133
+ user_token: n,
134
+ app_token: r
135
+ }
136
+ });
137
+ }
138
+ class k {
139
+ constructor() {
140
+ e(this, "emitter", new v());
141
+ e(this, "hasBackground", !1);
142
+ this.initCallback(), this.initNativeEventListener();
143
+ }
144
+ // --- bridge 事件 ---
145
+ on(n, t) {
146
+ this.emitter.on(n, t);
147
+ }
148
+ off(n, t) {
149
+ this.emitter.off(n, t);
150
+ }
151
+ }
152
+ function E(r) {
153
+ window[r] && (window.RKBridge = window[r], window.RKBridge_GlassBluetooth = window[r + "_GlassBluetooth"], window.RKBridge_GlassWifiP2P = window[r + "_GlassWifiP2P"], window.RKBridge_GlassAiChat = window[r + "_GlassAiChat"], window.RKBridge_GlassTranslation = window[r + "_GlassTranslation"], window.RKBridge_GlassAlbum = window[r + "_GlassAlbum"], window.RKBridge_GlassFileSystem = window[r + "_GlassFileSystem"], window.RKBridge_GlassMessage = window[r + "_GlassMessage"], window.RKBridge_GlassAppInstall = window[r + "_GlassAppInstall"], window.RKBridge_Http = window[r + "_Http"]);
154
+ }
155
+ let x = class extends k {
156
+ constructor() {
157
+ super();
158
+ e(this, "emitter", new v());
159
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
160
+ // @ts-ignore
161
+ e(this, "appToken", "");
162
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
163
+ // @ts-ignore
164
+ e(this, "userToken", "");
165
+ e(this, "hasBackground", !1);
166
+ // --- bridge 函数 ---
167
+ // 初始化SDK
168
+ e(this, "initSDK", this.generateBridgeFunc("initSDK"));
169
+ // 注册ClientId
170
+ e(this, "registerClientId", this.generateBridgeFunc("registerClientId"));
171
+ // 路由-打开页面
172
+ // override navigate = this.generateBridgeFunc<void, [string]>('navigate');
173
+ e(this, "navigate", (t) => (t += (t.includes("?") ? "&" : "?") + "adjust_fullscreen_inset=true", t += "&rkbridge=true", console.log("跳转页面: ", t), window.lx.ui.openView({
174
+ mode: "webview",
175
+ navigationBarBackgroundColor: "#FFFFFF",
176
+ navigationBarFrontStyle: "#0C120D",
177
+ url: t,
178
+ useSplitScreen: !1
179
+ }), Promise.resolve()));
180
+ // 路由-返回
181
+ // override goBack = this.generateBridgeFunc<void, []>('goBack');
182
+ e(this, "goBack", () => (window.lx.ui.closeWindow(), Promise.resolve()));
183
+ // 通用-获取设备信息
184
+ e(this, "getDeviceInfo", this.generateBridgeFunc("getDeviceInfo"));
185
+ // 通用-获取位置信息
186
+ e(this, "getLocationInfo", this.generateBridgeFunc("getLocationInfo"));
187
+ // 通用-获取状态栏高度
188
+ e(this, "getStatusBarHeight", this.generateBridgeFunc("getStatusBarHeight"));
189
+ // 通用-获取凭证
190
+ e(this, "getCredentials", () => {
191
+ try {
192
+ const t = localStorage.getItem("appCredential") || "", s = localStorage.getItem("userCredential") || "", i = {
193
+ appCredential: t,
194
+ userCredential: s
195
+ };
196
+ return Promise.resolve(i);
197
+ } catch (t) {
198
+ return console.warn("获取解析 credentials 失败", t), Promise.resolve({});
199
+ }
200
+ });
201
+ // 通用-获取授权码
202
+ e(this, "getAuthCode", async () => (await I()).authCode);
203
+ // 通用-获取用户信息
204
+ e(this, "getUserInfo", async () => {
205
+ const t = await this.getAuthCode();
206
+ console.log("获取 authCode 成功", t);
207
+ const s = await this.getAppToken();
208
+ console.log("获取 appToken 成功", s);
209
+ const i = await this.getUserToken();
210
+ return console.log("获取 userToken 成功", i), await M(s, i);
211
+ });
212
+ // 通用-获取应用token
213
+ e(this, "getAppToken", async () => {
214
+ const t = await D();
215
+ return this.appToken = t.appToken, t.appToken;
216
+ });
217
+ // 通用-获取用户token
218
+ e(this, "getUserToken", async () => {
219
+ const t = await this.getAuthCode(), s = this.appToken, i = await q(t, s);
220
+ return this.userToken = i.userToken, i.userToken;
221
+ });
222
+ // 通用-获取SDKToken
223
+ e(this, "getSdkToken", this.generateBridgeFunc("getSdkToken"));
224
+ // 通用-设置音量
225
+ e(this, "setVolume", this.generateBridgeFunc("setVolume"));
226
+ // 通用-设置亮度
227
+ e(this, "setBrightness", this.generateBridgeFunc("setBrightness"));
228
+ // 通用-打电话
229
+ e(this, "makePhone", this.generateBridgeFunc("makePhone"));
230
+ // 通用-获取应用包名
231
+ e(this, "getFileProvider", this.generateBridgeFunc("getFileProvider"));
232
+ // 通用-发送事件
233
+ e(this, "sendEventToNative", this.generateBridgeFunc("sendEventToNative"));
234
+ // 蓝牙-开始扫描
235
+ e(this, "startBluetoothScan", this.generateBridgeFunc("startScan", "GlassBluetooth"));
236
+ // 蓝牙-停止扫描
237
+ e(this, "stopBluetoothScan", this.generateBridgeFunc("stopScan", "GlassBluetooth"));
238
+ // 蓝牙-发起连接
239
+ e(this, "connectBluetooth", this.generateBridgeFunc("connect", "GlassBluetooth"));
240
+ // p2p-发起设备扫描
241
+ e(this, "startWifiP2PDiscoverPeers", this.generateBridgeFunc("startDiscoverPeers", "GlassWifiP2P"));
242
+ // p2p-停止设备扫描
243
+ e(this, "stopWifiP2PDiscoverPeers", this.generateBridgeFunc("stopDiscoverPeers", "GlassWifiP2P"));
244
+ // p2p-发起连接
245
+ e(this, "connectWifiP2P", this.generateBridgeFunc("connect", "GlassWifiP2P"));
246
+ // ai-chat-获取列表
247
+ e(this, "getChatList", this.generateBridgeFunc("getChatList", "GlassAiChat"));
248
+ // 翻译-获取源语言列表
249
+ e(this, "getSourceLanguageOptions", this.generateBridgeFunc(
250
+ "getSourceLanguageOptions",
251
+ "GlassTranslation"
252
+ ));
253
+ // 翻译-获取目标语言列表
254
+ e(this, "getTargetLanguageOptions", this.generateBridgeFunc(
255
+ "getTargetLanguageOptions",
256
+ "GlassTranslation"
257
+ ));
258
+ // 翻译-获取当前源语言
259
+ e(this, "getSourceLanguage", this.generateBridgeFunc("getSourceLanguage", "GlassTranslation"));
260
+ // 翻译-获取当前目标语言
261
+ e(this, "getTargetLanguage", this.generateBridgeFunc("getTargetLanguage", "GlassTranslation"));
262
+ // 翻译-设置语言
263
+ e(this, "setLanguage", this.generateBridgeFunc("setLanguage", "GlassTranslation"));
264
+ // 相册-获取相册文件列表
265
+ e(this, "getAlbumFileList", this.generateBridgeFunc("getAlbumFileList", "GlassAlbum"));
266
+ e(this, "getDirectoryFileListNative", this.generateBridgeFunc(
267
+ "getDirectoryFileList",
268
+ "GlassFileSystem"
269
+ ));
270
+ // 文件系统-获取目录文件列表
271
+ e(this, "getDirectoryFileList", (t) => this.getDirectoryFileListNative(JSON.stringify(t)));
272
+ // http 请求
273
+ e(this, "httpRequest", this.generateBridgeFunc("request", "Http"));
274
+ // 应用管理
275
+ e(this, "getApps", this.generateBridgeFunc("getApps", "GlassAppInstall"));
276
+ e(this, "getAppInfo", this.generateBridgeFunc("getAppInfo", "GlassAppInstall"));
277
+ e(this, "installApp", this.generateBridgeFunc("installApp", "GlassAppInstall"));
278
+ e(this, "uninstallApp", this.generateBridgeFunc("uninstallApp", "GlassAppInstall"));
279
+ e(this, "launchApp", this.generateBridgeFunc("launchApp", "GlassAppInstall"));
280
+ e(this, "stopApp", this.generateBridgeFunc("stopApp", "GlassAppInstall"));
281
+ e(this, "downloadApk", this.generateBridgeFunc("downloadApk", "GlassAppInstall"));
282
+ e(this, "getActiveInstallTask", this.generateBridgeFunc("getActiveInstallTask", "GlassAppInstall"));
283
+ e(this, "subscribeAppProgress", this.generateBridgeFunc("subscribeAppProgress", "GlassAppInstall"));
284
+ e(this, "unsubscribeAppProgress", this.generateBridgeFunc("unsubscribeAppProgress", "GlassAppInstall"));
285
+ e(this, "setBootAppNative", this.generateBridgeFunc("setBootApp", "GlassAppInstall"));
286
+ e(this, "setBootApp", (t) => {
287
+ var i;
288
+ const s = (i = window.RKBridge) == null ? void 0 : i.GlassAppInstall;
289
+ return !s || typeof s.setBootApp != "function" ? Promise.reject(-999) : this.setBootAppNative(t);
290
+ });
291
+ // 眼镜消息通信
292
+ // 通过经典蓝牙发送文本消息
293
+ e(this, "sendTextMessageByClassicBT", this.generateBridgeFunc(
294
+ "sendTextMessageByClassicBT",
295
+ "GlassMessage"
296
+ ));
297
+ // 通过 BLE 发送文本消息
298
+ e(this, "sendTextMessageByBle", this.generateBridgeFunc("sendTextMessageByBLE", "GlassMessage"));
299
+ // 通过P2P发送文本消息
300
+ e(this, "sendTextMessageByP2P", this.generateBridgeFunc("sendTextMessageByP2P", "GlassMessage"));
301
+ // 蓝牙发送长文本
302
+ e(this, "sendStreamData", this.generateBridgeFunc("sendStreamData", "GlassMessage"));
303
+ // 广播通知Glass
304
+ e(this, "sendGlassBroadcast", this.generateBridgeFunc("sendGlassBroadcast", "GlassMessage"));
305
+ }
306
+ initCallback() {
307
+ E("BKBridge"), window.RKBridgeCallback || (window.RKBridgeCallback = {
308
+ _onCallback: (s, i, a, g) => {
309
+ s && this.emitter.emit(s, i, a, g);
310
+ }
311
+ }), window.RKBridge && (window.RKBridge.GlassBluetooth = window.RKBridge_GlassBluetooth, window.RKBridge.GlassWifiP2P = window.RKBridge_GlassWifiP2P, window.RKBridge.GlassAiChat = window.RKBridge_GlassAiChat, window.RKBridge.GlassTranslation = window.RKBridge_GlassTranslation, window.RKBridge.GlassAlbum = window.RKBridge_GlassAlbum, window.RKBridge.GlassFileSystem = window.RKBridge_GlassFileSystem, window.RKBridge.GlassMessage = window.RKBridge_GlassMessage, window.RKBridge.GlassAppInstall = window.RKBridge_GlassAppInstall, window.RKBridge.Http = window.RKBridge_Http);
312
+ const t = window.RKBridgeCallback._onCallback;
313
+ window.RKBridgeCallback._onCallback = (s, i, a, g) => {
314
+ t == null || t(s, i, a, g), s && this.emitter.emit(s, i, a, g);
315
+ };
316
+ }
317
+ // todo progress
318
+ generateBridgeFunc(t, s) {
319
+ return (...i) => {
320
+ var g;
321
+ const a = s ? (g = window.RKBridge) == null ? void 0 : g[s] : window.RKBridge;
322
+ return !a || !a[t] ? (console.warn(`Bridge method ${s ? `${s}.` : ""}${t} is not available, returning default value.`), Promise.resolve(void 0)) : new Promise((p, F) => {
323
+ var d, c, u, B;
324
+ const l = `cb_${K()}`;
325
+ this.emitter.on(l, (h, o, w) => {
326
+ if (h.toLowerCase() === "success") {
327
+ if (o)
328
+ try {
329
+ o = JSON.parse(o);
330
+ } catch (G) {
331
+ console.warn("解析 callback 结果失败", G);
332
+ }
333
+ p(o);
334
+ } else h.toLowerCase() === "error" && F(w);
335
+ this.emitter.off(l);
336
+ }), s ? (c = (d = window.RKBridge[s])[t]) == null || c.call(d, ...i, l) : (B = (u = window.RKBridge)[t]) == null || B.call(u, ...i, l);
337
+ });
338
+ };
339
+ }
340
+ initNativeEventListener() {
341
+ const t = (s, i) => {
342
+ this.emitter.emit(s, i.data);
343
+ };
344
+ if (window.dispatchNativeEvent) {
345
+ const s = window.dispatchNativeEvent;
346
+ window.dispatchNativeEvent = (...i) => {
347
+ s(...i), t(...i);
348
+ };
349
+ } else
350
+ window.dispatchNativeEvent = t;
351
+ }
352
+ };
353
+ const O = new x();
354
+ class U extends k {
355
+ constructor() {
356
+ super(...arguments);
357
+ e(this, "emitter", new v());
358
+ e(this, "hasBackground", !0);
359
+ // --- bridge 函数 ---
360
+ // 初始化SDK
361
+ e(this, "initSDK", this.generateBridgeFunc("initSDK"));
362
+ // 注册ClientId
363
+ e(this, "registerClientId", this.generateBridgeFunc("registerClientId"));
364
+ // 路由-打开页面
365
+ e(this, "navigate", (t) => (window.JSBridge.navigate(t), Promise.resolve()));
366
+ // 路由-返回
367
+ e(this, "goBack", () => (window.JSBridge.goBack(), Promise.resolve()));
368
+ // 通用-获取设备信息
369
+ e(this, "getDeviceInfo", this.generateBridgeFunc("getDeviceInfo"));
370
+ // 通用-获取状态栏高度
371
+ e(this, "getStatusBarHeight", this.generateBridgeFunc("getStatusBarHeight"));
372
+ // 通用-获取位置信息
373
+ e(this, "getLocationInfo", this.generateBridgeFunc("getLocationInfo"));
374
+ // 通用-获取授权码
375
+ e(this, "getAuthCode", this.generateBridgeFunc("getAuthCode"));
376
+ // 通用-获取用户信息
377
+ e(this, "getUserInfo", () => window.JSBridge.getUserInfo());
378
+ // 通用-获取凭证
379
+ e(this, "getCredentials", () => {
380
+ var t, s;
381
+ try {
382
+ const i = JSON.parse((s = (t = window.JSBridge) == null ? void 0 : t.getCredentials) == null ? void 0 : s.call(t));
383
+ return Promise.resolve(i);
384
+ } catch (i) {
385
+ return console.warn("获取解析 credentials 失败", i), Promise.resolve({});
386
+ }
387
+ });
388
+ // 通用-获取应用token
389
+ e(this, "getAppToken", this.generateBridgeFunc("getAppToken"));
390
+ // 通用-获取用户token
391
+ e(this, "getUserToken", this.generateBridgeFunc("getUserToken"));
392
+ // 通用-获取SDKToken
393
+ e(this, "getSdkToken", this.generateBridgeFunc("getSdkToken"));
394
+ // 通用-设置音量
395
+ e(this, "setVolume", this.generateBridgeFunc("setVolume"));
396
+ // 通用-设置亮度
397
+ e(this, "setBrightness", this.generateBridgeFunc("setBrightness"));
398
+ // 通用-打电话
399
+ e(this, "makePhone", this.generateBridgeFunc("makePhone"));
400
+ // 通用-发送事件
401
+ e(this, "sendEventToNative", this.generateBridgeFunc("sendEventToNative"));
402
+ // 通用-获取应用包名
403
+ e(this, "getFileProvider", this.generateBridgeFunc("getFileProvider"));
404
+ // 蓝牙-开始扫描
405
+ e(this, "startBluetoothScan", this.generateBridgeFunc("startScan", "GlassBluetooth"));
406
+ // 蓝牙-停止扫描
407
+ e(this, "stopBluetoothScan", this.generateBridgeFunc("stopScan", "GlassBluetooth"));
408
+ // 蓝牙-发起连接
409
+ e(this, "connectBluetooth", this.generateBridgeFunc("connect", "GlassBluetooth"));
410
+ // p2p-发起设备扫描
411
+ e(this, "startWifiP2PDiscoverPeers", this.generateBridgeFunc("startDiscoverPeers", "GlassWifiP2P"));
412
+ // p2p-停止设备扫描
413
+ e(this, "stopWifiP2PDiscoverPeers", this.generateBridgeFunc("stopDiscoverPeers", "GlassWifiP2P"));
414
+ // p2p-发起连接
415
+ e(this, "connectWifiP2P", this.generateBridgeFunc("connect", "GlassWifiP2P"));
416
+ // ai-chat-获取列表
417
+ e(this, "getChatList", this.generateBridgeFunc("getChatList", "GlassAiChat"));
418
+ // 翻译-获取源语言列表
419
+ e(this, "getSourceLanguageOptions", this.generateBridgeFunc(
420
+ "getSourceLanguageOptions",
421
+ "GlassTranslation"
422
+ ));
423
+ // 翻译-获取目标语言列表
424
+ e(this, "getTargetLanguageOptions", this.generateBridgeFunc(
425
+ "getTargetLanguageOptions",
426
+ "GlassTranslation"
427
+ ));
428
+ // 翻译-获取当前源语言
429
+ e(this, "getSourceLanguage", this.generateBridgeFunc("getSourceLanguage", "GlassTranslation"));
430
+ // 翻译-获取当前目标语言
431
+ e(this, "getTargetLanguage", this.generateBridgeFunc("getTargetLanguage", "GlassTranslation"));
432
+ // 翻译-设置语言
433
+ e(this, "setLanguage", this.generateBridgeFunc("setLanguage", "GlassTranslation"));
434
+ // 相册-获取相册文件列表
435
+ e(this, "getAlbumFileList", this.generateBridgeFunc("getAlbumFileList", "GlassAlbum"));
436
+ e(this, "getDirectoryFileListNative", this.generateBridgeFunc(
437
+ "getDirectoryFileList",
438
+ "GlassFileSystem"
439
+ ));
440
+ // 文件系统-获取目录文件列表
441
+ e(this, "getDirectoryFileList", (t) => this.getDirectoryFileListNative(JSON.stringify(t)));
442
+ // http 请求
443
+ e(this, "httpRequest", this.generateBridgeFunc("request", "Http"));
444
+ // 应用管理
445
+ e(this, "getApps", this.generateBridgeFunc("getApps", "GlassAppInstall"));
446
+ e(this, "getAppInfo", this.generateBridgeFunc("getAppInfo", "GlassAppInstall"));
447
+ e(this, "installApp", this.generateBridgeFunc("installApp", "GlassAppInstall"));
448
+ e(this, "uninstallApp", this.generateBridgeFunc("uninstallApp", "GlassAppInstall"));
449
+ e(this, "launchApp", this.generateBridgeFunc("launchApp", "GlassAppInstall"));
450
+ e(this, "stopApp", this.generateBridgeFunc("stopApp", "GlassAppInstall"));
451
+ e(this, "downloadApk", this.generateBridgeFunc("downloadApk", "GlassAppInstall"));
452
+ e(this, "getActiveInstallTask", this.generateBridgeFunc("getActiveInstallTask", "GlassAppInstall"));
453
+ e(this, "subscribeAppProgress", this.generateBridgeFunc("subscribeAppProgress", "GlassAppInstall"));
454
+ e(this, "unsubscribeAppProgress", this.generateBridgeFunc("unsubscribeAppProgress", "GlassAppInstall"));
455
+ e(this, "setBootAppNative", this.generateBridgeFunc("setBootApp", "GlassAppInstall"));
456
+ /**
457
+ * 开机自启:Native 未合入时 reject(-999),避免 generateBridgeFunc 缺方法时 resolve(undefined) 误判成功。
458
+ */
459
+ e(this, "setBootApp", (t) => {
460
+ var i;
461
+ const s = (i = window.RKBridge) == null ? void 0 : i.GlassAppInstall;
462
+ return !s || typeof s.setBootApp != "function" ? Promise.reject(-999) : this.setBootAppNative(t);
463
+ });
464
+ // 眼镜消息通信
465
+ // 通过经典蓝牙发送文本消息
466
+ e(this, "sendTextMessageByClassicBT", this.generateBridgeFunc(
467
+ "sendTextMessageByClassicBT",
468
+ "GlassMessage"
469
+ ));
470
+ // 通过 BLE 发送文本消息
471
+ e(this, "sendTextMessageByBle", this.generateBridgeFunc("sendTextMessageByBLE", "GlassMessage"));
472
+ // 通过P2P发送文本消息
473
+ e(this, "sendTextMessageByP2P", this.generateBridgeFunc("sendTextMessageByP2P", "GlassMessage"));
474
+ // 蓝牙发送长文本
475
+ e(this, "sendStreamData", this.generateBridgeFunc("sendStreamData", "GlassMessage"));
476
+ // 广播通知Glass
477
+ e(this, "sendGlassBroadcast", this.generateBridgeFunc("sendGlassBroadcast", "GlassMessage"));
478
+ }
479
+ initCallback() {
480
+ window.RKBridgeCallback || (window.RKBridgeCallback = {
481
+ _onCallback: (s, i, a, g) => {
482
+ s && this.emitter.emit(s, i, a, g);
483
+ }
484
+ }), window.RKBridge && (window.RKBridge.GlassBluetooth = window.RKBridge_GlassBluetooth, window.RKBridge.GlassWifiP2P = window.RKBridge_GlassWifiP2P, window.RKBridge.GlassAiChat = window.RKBridge_GlassAiChat, window.RKBridge.GlassTranslation = window.RKBridge_GlassTranslation, window.RKBridge.GlassAlbum = window.RKBridge_GlassAlbum, window.RKBridge.GlassFileSystem = window.RKBridge_GlassFileSystem, window.RKBridge.GlassMessage = window.RKBridge_GlassMessage, window.RKBridge.GlassAppInstall = window.RKBridge_GlassAppInstall);
485
+ const t = window.RKBridgeCallback._onCallback;
486
+ window.RKBridgeCallback._onCallback = (s, i, a, g) => {
487
+ t == null || t(s, i, a, g), s && this.emitter.emit(s, i, a, g);
488
+ };
489
+ }
490
+ // todo progress
491
+ generateBridgeFunc(t, s) {
492
+ return (...i) => {
493
+ var g;
494
+ const a = s ? (g = window.RKBridge) == null ? void 0 : g[s] : window.RKBridge;
495
+ return !a || !a[t] ? (console.warn(`Bridge method ${s ? `${s}.` : ""}${t} is not available, returning default value.`), Promise.resolve(void 0)) : new Promise((p, F) => {
496
+ var d, c, u, B;
497
+ const l = `cb_${K()}`;
498
+ this.emitter.on(l, (h, o, w) => {
499
+ if (h.toLowerCase() === "success") {
500
+ if (o)
501
+ try {
502
+ o = JSON.parse(o);
503
+ } catch (G) {
504
+ console.warn("解析 callback 结果失败", G), console.log(t, s, o, w);
505
+ }
506
+ p(o);
507
+ } else h.toLowerCase() === "error" && F(w);
508
+ this.emitter.off(l);
509
+ }), s ? (c = (d = window.RKBridge[s])[t]) == null || c.call(d, ...i, l) : (B = (u = window.RKBridge)[t]) == null || B.call(u, ...i, l);
510
+ });
511
+ };
512
+ }
513
+ initNativeEventListener() {
514
+ const t = (s, i) => {
515
+ this.emitter.emit(s, i.data);
516
+ };
517
+ if (window.dispatchNativeEvent) {
518
+ const s = window.dispatchNativeEvent;
519
+ window.dispatchNativeEvent = (...i) => {
520
+ s(...i), t(...i);
521
+ };
522
+ } else
523
+ window.dispatchNativeEvent = t;
524
+ }
525
+ }
526
+ const W = new U();
527
+ var C;
528
+ const J = ((C = window == null ? void 0 : window.navigator) == null ? void 0 : C.userAgent) || "", z = J.includes("lanxin") ? O : W;
529
+ export {
530
+ k as BaseBridge,
531
+ z as rkBridge
532
+ };
package/index.umd.js ADDED
@@ -0,0 +1 @@
1
+ (function(a,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("uuid"),require("eventemitter3")):typeof define=="function"&&define.amd?define(["exports","uuid","eventemitter3"],l):(a=typeof globalThis<"u"?globalThis:a||self,l(a["rk-bridge"]={},a.uuid,a.EventEmitter))})(this,function(a,l,c){"use strict";var J=Object.defineProperty;var H=(a,l,c)=>l in a?J(a,l,{enumerable:!0,configurable:!0,writable:!0,value:c}):a[l]=c;var e=(a,l,c)=>H(a,typeof l!="symbol"?l+"":l,c);var b,k,C,K,y;const A="1048576-10772480",S="66D9EF553887B5054FFC4AC1CB2CB590",L=window.lx;function m(){return new Promise((r,n)=>{console.log("获取 authCode 参数 appId: ",A),L.biz.getAuthCode({appId:A,success:function(t){console.log("获取 authCode 成功",t),r(t)},fail:function(t){console.log("获取 authCode 失败",t),n(t)}})})}class I{constructor(n){e(this,"baseURL");e(this,"baseRequest");this.baseURL=(n==null?void 0:n.baseURL)||"",this.baseRequest=(n==null?void 0:n.baseRequest)||(()=>Promise.resolve({statusCode:-1,body:""}))}async httpRequest(n){try{const{statusCode:t,body:s}=await this.baseRequest(JSON.stringify(n));console.log("native http response:",t,s);const i=JSON.parse(s);if(i.errCode===0)return i.data;throw new Error(i.errMsg||"未知错误")}catch(t){throw console.warn("native http request error:",t),t}}get(n,t){return this.httpRequest({method:"GET",url:this.baseURL+n,queryParams:t==null?void 0:t.params,headers:{"Content-Type":"application/json",...(t==null?void 0:t.headers)??{}}})}post(n,t){return this.httpRequest({method:"POST",url:this.baseURL+n,body:t==null?void 0:t.data,headers:{"Content-Type":"application/json",...(t==null?void 0:t.headers)??{}}})}}const f=new I({baseURL:"http://20.65.2.12:10443/open/apigw",baseRequest:typeof window<"u"&&((k=(b=window==null?void 0:window.RKBridge)==null?void 0:b.Http)==null?void 0:k.request)||void 0});class _{constructor(n){e(this,"baseURL");e(this,"baseRequest");this.baseURL=(n==null?void 0:n.baseURL)||"",this.baseRequest=(n==null?void 0:n.baseRequest)||(()=>Promise.resolve({statusCode:-1,body:""}))}async httpRequest(n){try{const{statusCode:t,body:s}=await this.baseRequest(JSON.stringify(n));console.log("native http response:",t,s);const i=JSON.parse(s);if(i.resultCode===0)return i;throw new Error(i.message||"未知错误")}catch(t){throw console.warn("native http request error:",t),t}}get(n,t){return this.httpRequest({method:"GET",url:this.baseURL+n,queryParams:t==null?void 0:t.params,headers:{...(t==null?void 0:t.headers)??{}}})}post(n,t){return this.httpRequest({method:"POST",url:this.baseURL+n,body:t==null?void 0:t.data,headers:{...(t==null?void 0:t.headers)??{}}})}}new _({baseURL:"http://20.65.1.157",baseRequest:typeof window<"u"&&((K=(C=window==null?void 0:window.RKBridge)==null?void 0:C.Http)==null?void 0:K.request)||void 0});function N(){return f.get("/v1/apptoken/create",{params:{grant_type:"client_credential",appid:A,secret:S}})}function D(r,n){return f.get("/v1/usertoken/create",{params:{grant_type:"authorization_code",app_token:n,code:r}})}function q(r,n){return f.get("/v1/users/fetch",{params:{user_token:n,app_token:r}})}class v{constructor(){e(this,"emitter",new c);e(this,"hasBackground",!1);this.initCallback(),this.initNativeEventListener()}on(n,t){this.emitter.on(n,t)}off(n,t){this.emitter.off(n,t)}}function M(r){window[r]&&(window.RKBridge=window[r],window.RKBridge_GlassBluetooth=window[r+"_GlassBluetooth"],window.RKBridge_GlassWifiP2P=window[r+"_GlassWifiP2P"],window.RKBridge_GlassAiChat=window[r+"_GlassAiChat"],window.RKBridge_GlassTranslation=window[r+"_GlassTranslation"],window.RKBridge_GlassAlbum=window[r+"_GlassAlbum"],window.RKBridge_GlassFileSystem=window[r+"_GlassFileSystem"],window.RKBridge_GlassMessage=window[r+"_GlassMessage"],window.RKBridge_GlassAppInstall=window[r+"_GlassAppInstall"],window.RKBridge_Http=window[r+"_Http"])}let E=class extends v{constructor(){super();e(this,"emitter",new c);e(this,"appToken","");e(this,"userToken","");e(this,"hasBackground",!1);e(this,"initSDK",this.generateBridgeFunc("initSDK"));e(this,"registerClientId",this.generateBridgeFunc("registerClientId"));e(this,"navigate",t=>(t+=(t.includes("?")?"&":"?")+"adjust_fullscreen_inset=true",t+="&rkbridge=true",console.log("跳转页面: ",t),window.lx.ui.openView({mode:"webview",navigationBarBackgroundColor:"#FFFFFF",navigationBarFrontStyle:"#0C120D",url:t,useSplitScreen:!1}),Promise.resolve()));e(this,"goBack",()=>(window.lx.ui.closeWindow(),Promise.resolve()));e(this,"getDeviceInfo",this.generateBridgeFunc("getDeviceInfo"));e(this,"getLocationInfo",this.generateBridgeFunc("getLocationInfo"));e(this,"getStatusBarHeight",this.generateBridgeFunc("getStatusBarHeight"));e(this,"getCredentials",()=>{try{const t=localStorage.getItem("appCredential")||"",s=localStorage.getItem("userCredential")||"",i={appCredential:t,userCredential:s};return Promise.resolve(i)}catch(t){return console.warn("获取解析 credentials 失败",t),Promise.resolve({})}});e(this,"getAuthCode",async()=>(await m()).authCode);e(this,"getUserInfo",async()=>{const t=await this.getAuthCode();console.log("获取 authCode 成功",t);const s=await this.getAppToken();console.log("获取 appToken 成功",s);const i=await this.getUserToken();return console.log("获取 userToken 成功",i),await q(s,i)});e(this,"getAppToken",async()=>{const t=await N();return this.appToken=t.appToken,t.appToken});e(this,"getUserToken",async()=>{const t=await this.getAuthCode(),s=this.appToken,i=await D(t,s);return this.userToken=i.userToken,i.userToken});e(this,"getSdkToken",this.generateBridgeFunc("getSdkToken"));e(this,"setVolume",this.generateBridgeFunc("setVolume"));e(this,"setBrightness",this.generateBridgeFunc("setBrightness"));e(this,"makePhone",this.generateBridgeFunc("makePhone"));e(this,"getFileProvider",this.generateBridgeFunc("getFileProvider"));e(this,"sendEventToNative",this.generateBridgeFunc("sendEventToNative"));e(this,"startBluetoothScan",this.generateBridgeFunc("startScan","GlassBluetooth"));e(this,"stopBluetoothScan",this.generateBridgeFunc("stopScan","GlassBluetooth"));e(this,"connectBluetooth",this.generateBridgeFunc("connect","GlassBluetooth"));e(this,"startWifiP2PDiscoverPeers",this.generateBridgeFunc("startDiscoverPeers","GlassWifiP2P"));e(this,"stopWifiP2PDiscoverPeers",this.generateBridgeFunc("stopDiscoverPeers","GlassWifiP2P"));e(this,"connectWifiP2P",this.generateBridgeFunc("connect","GlassWifiP2P"));e(this,"getChatList",this.generateBridgeFunc("getChatList","GlassAiChat"));e(this,"getSourceLanguageOptions",this.generateBridgeFunc("getSourceLanguageOptions","GlassTranslation"));e(this,"getTargetLanguageOptions",this.generateBridgeFunc("getTargetLanguageOptions","GlassTranslation"));e(this,"getSourceLanguage",this.generateBridgeFunc("getSourceLanguage","GlassTranslation"));e(this,"getTargetLanguage",this.generateBridgeFunc("getTargetLanguage","GlassTranslation"));e(this,"setLanguage",this.generateBridgeFunc("setLanguage","GlassTranslation"));e(this,"getAlbumFileList",this.generateBridgeFunc("getAlbumFileList","GlassAlbum"));e(this,"getDirectoryFileListNative",this.generateBridgeFunc("getDirectoryFileList","GlassFileSystem"));e(this,"getDirectoryFileList",t=>this.getDirectoryFileListNative(JSON.stringify(t)));e(this,"httpRequest",this.generateBridgeFunc("request","Http"));e(this,"getApps",this.generateBridgeFunc("getApps","GlassAppInstall"));e(this,"getAppInfo",this.generateBridgeFunc("getAppInfo","GlassAppInstall"));e(this,"installApp",this.generateBridgeFunc("installApp","GlassAppInstall"));e(this,"uninstallApp",this.generateBridgeFunc("uninstallApp","GlassAppInstall"));e(this,"launchApp",this.generateBridgeFunc("launchApp","GlassAppInstall"));e(this,"stopApp",this.generateBridgeFunc("stopApp","GlassAppInstall"));e(this,"downloadApk",this.generateBridgeFunc("downloadApk","GlassAppInstall"));e(this,"getActiveInstallTask",this.generateBridgeFunc("getActiveInstallTask","GlassAppInstall"));e(this,"subscribeAppProgress",this.generateBridgeFunc("subscribeAppProgress","GlassAppInstall"));e(this,"unsubscribeAppProgress",this.generateBridgeFunc("unsubscribeAppProgress","GlassAppInstall"));e(this,"setBootAppNative",this.generateBridgeFunc("setBootApp","GlassAppInstall"));e(this,"setBootApp",t=>{var i;const s=(i=window.RKBridge)==null?void 0:i.GlassAppInstall;return!s||typeof s.setBootApp!="function"?Promise.reject(-999):this.setBootAppNative(t)});e(this,"sendTextMessageByClassicBT",this.generateBridgeFunc("sendTextMessageByClassicBT","GlassMessage"));e(this,"sendTextMessageByBle",this.generateBridgeFunc("sendTextMessageByBLE","GlassMessage"));e(this,"sendTextMessageByP2P",this.generateBridgeFunc("sendTextMessageByP2P","GlassMessage"));e(this,"sendStreamData",this.generateBridgeFunc("sendStreamData","GlassMessage"));e(this,"sendGlassBroadcast",this.generateBridgeFunc("sendGlassBroadcast","GlassMessage"))}initCallback(){M("BKBridge"),window.RKBridgeCallback||(window.RKBridgeCallback={_onCallback:(s,i,g,o)=>{s&&this.emitter.emit(s,i,g,o)}}),window.RKBridge&&(window.RKBridge.GlassBluetooth=window.RKBridge_GlassBluetooth,window.RKBridge.GlassWifiP2P=window.RKBridge_GlassWifiP2P,window.RKBridge.GlassAiChat=window.RKBridge_GlassAiChat,window.RKBridge.GlassTranslation=window.RKBridge_GlassTranslation,window.RKBridge.GlassAlbum=window.RKBridge_GlassAlbum,window.RKBridge.GlassFileSystem=window.RKBridge_GlassFileSystem,window.RKBridge.GlassMessage=window.RKBridge_GlassMessage,window.RKBridge.GlassAppInstall=window.RKBridge_GlassAppInstall,window.RKBridge.Http=window.RKBridge_Http);const t=window.RKBridgeCallback._onCallback;window.RKBridgeCallback._onCallback=(s,i,g,o)=>{t==null||t(s,i,g,o),s&&this.emitter.emit(s,i,g,o)}}generateBridgeFunc(t,s){return(...i)=>{var o;const g=s?(o=window.RKBridge)==null?void 0:o[s]:window.RKBridge;return!g||!g[t]?(console.warn(`Bridge method ${s?`${s}.`:""}${t} is not available, returning default value.`),Promise.resolve(void 0)):new Promise((R,P)=>{var B,h,w,p;const u=`cb_${l.v4()}`;this.emitter.on(u,(F,d,G)=>{if(F.toLowerCase()==="success"){if(d)try{d=JSON.parse(d)}catch(T){console.warn("解析 callback 结果失败",T)}R(d)}else F.toLowerCase()==="error"&&P(G);this.emitter.off(u)}),s?(h=(B=window.RKBridge[s])[t])==null||h.call(B,...i,u):(p=(w=window.RKBridge)[t])==null||p.call(w,...i,u)})}}initNativeEventListener(){const t=(s,i)=>{this.emitter.emit(s,i.data)};if(window.dispatchNativeEvent){const s=window.dispatchNativeEvent;window.dispatchNativeEvent=(...i)=>{s(...i),t(...i)}}else window.dispatchNativeEvent=t}};const O=new E;class x extends v{constructor(){super(...arguments);e(this,"emitter",new c);e(this,"hasBackground",!0);e(this,"initSDK",this.generateBridgeFunc("initSDK"));e(this,"registerClientId",this.generateBridgeFunc("registerClientId"));e(this,"navigate",t=>(window.JSBridge.navigate(t),Promise.resolve()));e(this,"goBack",()=>(window.JSBridge.goBack(),Promise.resolve()));e(this,"getDeviceInfo",this.generateBridgeFunc("getDeviceInfo"));e(this,"getStatusBarHeight",this.generateBridgeFunc("getStatusBarHeight"));e(this,"getLocationInfo",this.generateBridgeFunc("getLocationInfo"));e(this,"getAuthCode",this.generateBridgeFunc("getAuthCode"));e(this,"getUserInfo",()=>window.JSBridge.getUserInfo());e(this,"getCredentials",()=>{var t,s;try{const i=JSON.parse((s=(t=window.JSBridge)==null?void 0:t.getCredentials)==null?void 0:s.call(t));return Promise.resolve(i)}catch(i){return console.warn("获取解析 credentials 失败",i),Promise.resolve({})}});e(this,"getAppToken",this.generateBridgeFunc("getAppToken"));e(this,"getUserToken",this.generateBridgeFunc("getUserToken"));e(this,"getSdkToken",this.generateBridgeFunc("getSdkToken"));e(this,"setVolume",this.generateBridgeFunc("setVolume"));e(this,"setBrightness",this.generateBridgeFunc("setBrightness"));e(this,"makePhone",this.generateBridgeFunc("makePhone"));e(this,"sendEventToNative",this.generateBridgeFunc("sendEventToNative"));e(this,"getFileProvider",this.generateBridgeFunc("getFileProvider"));e(this,"startBluetoothScan",this.generateBridgeFunc("startScan","GlassBluetooth"));e(this,"stopBluetoothScan",this.generateBridgeFunc("stopScan","GlassBluetooth"));e(this,"connectBluetooth",this.generateBridgeFunc("connect","GlassBluetooth"));e(this,"startWifiP2PDiscoverPeers",this.generateBridgeFunc("startDiscoverPeers","GlassWifiP2P"));e(this,"stopWifiP2PDiscoverPeers",this.generateBridgeFunc("stopDiscoverPeers","GlassWifiP2P"));e(this,"connectWifiP2P",this.generateBridgeFunc("connect","GlassWifiP2P"));e(this,"getChatList",this.generateBridgeFunc("getChatList","GlassAiChat"));e(this,"getSourceLanguageOptions",this.generateBridgeFunc("getSourceLanguageOptions","GlassTranslation"));e(this,"getTargetLanguageOptions",this.generateBridgeFunc("getTargetLanguageOptions","GlassTranslation"));e(this,"getSourceLanguage",this.generateBridgeFunc("getSourceLanguage","GlassTranslation"));e(this,"getTargetLanguage",this.generateBridgeFunc("getTargetLanguage","GlassTranslation"));e(this,"setLanguage",this.generateBridgeFunc("setLanguage","GlassTranslation"));e(this,"getAlbumFileList",this.generateBridgeFunc("getAlbumFileList","GlassAlbum"));e(this,"getDirectoryFileListNative",this.generateBridgeFunc("getDirectoryFileList","GlassFileSystem"));e(this,"getDirectoryFileList",t=>this.getDirectoryFileListNative(JSON.stringify(t)));e(this,"httpRequest",this.generateBridgeFunc("request","Http"));e(this,"getApps",this.generateBridgeFunc("getApps","GlassAppInstall"));e(this,"getAppInfo",this.generateBridgeFunc("getAppInfo","GlassAppInstall"));e(this,"installApp",this.generateBridgeFunc("installApp","GlassAppInstall"));e(this,"uninstallApp",this.generateBridgeFunc("uninstallApp","GlassAppInstall"));e(this,"launchApp",this.generateBridgeFunc("launchApp","GlassAppInstall"));e(this,"stopApp",this.generateBridgeFunc("stopApp","GlassAppInstall"));e(this,"downloadApk",this.generateBridgeFunc("downloadApk","GlassAppInstall"));e(this,"getActiveInstallTask",this.generateBridgeFunc("getActiveInstallTask","GlassAppInstall"));e(this,"subscribeAppProgress",this.generateBridgeFunc("subscribeAppProgress","GlassAppInstall"));e(this,"unsubscribeAppProgress",this.generateBridgeFunc("unsubscribeAppProgress","GlassAppInstall"));e(this,"setBootAppNative",this.generateBridgeFunc("setBootApp","GlassAppInstall"));e(this,"setBootApp",t=>{var i;const s=(i=window.RKBridge)==null?void 0:i.GlassAppInstall;return!s||typeof s.setBootApp!="function"?Promise.reject(-999):this.setBootAppNative(t)});e(this,"sendTextMessageByClassicBT",this.generateBridgeFunc("sendTextMessageByClassicBT","GlassMessage"));e(this,"sendTextMessageByBle",this.generateBridgeFunc("sendTextMessageByBLE","GlassMessage"));e(this,"sendTextMessageByP2P",this.generateBridgeFunc("sendTextMessageByP2P","GlassMessage"));e(this,"sendStreamData",this.generateBridgeFunc("sendStreamData","GlassMessage"));e(this,"sendGlassBroadcast",this.generateBridgeFunc("sendGlassBroadcast","GlassMessage"))}initCallback(){window.RKBridgeCallback||(window.RKBridgeCallback={_onCallback:(s,i,g,o)=>{s&&this.emitter.emit(s,i,g,o)}}),window.RKBridge&&(window.RKBridge.GlassBluetooth=window.RKBridge_GlassBluetooth,window.RKBridge.GlassWifiP2P=window.RKBridge_GlassWifiP2P,window.RKBridge.GlassAiChat=window.RKBridge_GlassAiChat,window.RKBridge.GlassTranslation=window.RKBridge_GlassTranslation,window.RKBridge.GlassAlbum=window.RKBridge_GlassAlbum,window.RKBridge.GlassFileSystem=window.RKBridge_GlassFileSystem,window.RKBridge.GlassMessage=window.RKBridge_GlassMessage,window.RKBridge.GlassAppInstall=window.RKBridge_GlassAppInstall);const t=window.RKBridgeCallback._onCallback;window.RKBridgeCallback._onCallback=(s,i,g,o)=>{t==null||t(s,i,g,o),s&&this.emitter.emit(s,i,g,o)}}generateBridgeFunc(t,s){return(...i)=>{var o;const g=s?(o=window.RKBridge)==null?void 0:o[s]:window.RKBridge;return!g||!g[t]?(console.warn(`Bridge method ${s?`${s}.`:""}${t} is not available, returning default value.`),Promise.resolve(void 0)):new Promise((R,P)=>{var B,h,w,p;const u=`cb_${l.v4()}`;this.emitter.on(u,(F,d,G)=>{if(F.toLowerCase()==="success"){if(d)try{d=JSON.parse(d)}catch(T){console.warn("解析 callback 结果失败",T),console.log(t,s,d,G)}R(d)}else F.toLowerCase()==="error"&&P(G);this.emitter.off(u)}),s?(h=(B=window.RKBridge[s])[t])==null||h.call(B,...i,u):(p=(w=window.RKBridge)[t])==null||p.call(w,...i,u)})}}initNativeEventListener(){const t=(s,i)=>{this.emitter.emit(s,i.data)};if(window.dispatchNativeEvent){const s=window.dispatchNativeEvent;window.dispatchNativeEvent=(...i)=>{s(...i),t(...i)}}else window.dispatchNativeEvent=t}}const U=new x,W=(((y=window==null?void 0:window.navigator)==null?void 0:y.userAgent)||"").includes("lanxin")?O:U;a.BaseBridge=v,a.rkBridge=W,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
@@ -0,0 +1,3 @@
1
+ export declare function getAuthCode(): Promise<{
2
+ authCode: string;
3
+ }>;
@@ -0,0 +1,2 @@
1
+ export declare const appId = "1048576-10772480";
2
+ export declare const appSecret = "66D9EF553887B5054FFC4AC1CB2CB590";
@@ -0,0 +1,13 @@
1
+ import type { LXUserinfo } from '@/lx-sdk/server';
2
+ export * from './client';
3
+ export * from './config';
4
+ export * from './server';
5
+ export type LXUserinfoWithToken = LXUserinfo & {
6
+ appToken: string;
7
+ userToken: string;
8
+ userCredential: string;
9
+ appCredential: string;
10
+ };
11
+ export declare function getLXUserinfo(options?: {
12
+ authCode?: string;
13
+ }): Promise<LXUserinfoWithToken>;
@@ -0,0 +1,23 @@
1
+ import type { HttpRequestParams } from '@/types.ts';
2
+ declare class NativeRequest2 {
3
+ private baseURL;
4
+ private baseRequest;
5
+ constructor(options?: {
6
+ baseURL?: string;
7
+ baseRequest?: (params: string) => Promise<{
8
+ statusCode: number;
9
+ body: string;
10
+ }>;
11
+ });
12
+ httpRequest(params: HttpRequestParams): Promise<any>;
13
+ get(path: string, options?: {
14
+ params?: any;
15
+ headers?: any;
16
+ }): Promise<any>;
17
+ post(path: string, options?: {
18
+ data: any;
19
+ headers?: any;
20
+ }): Promise<any>;
21
+ }
22
+ declare const lxRequest2: NativeRequest2;
23
+ export { lxRequest2 };
@@ -0,0 +1,23 @@
1
+ import type { HttpRequestParams } from '@/types.ts';
2
+ declare class NativeRequest {
3
+ private baseURL;
4
+ private baseRequest;
5
+ constructor(options?: {
6
+ baseURL?: string;
7
+ baseRequest?: (params: string) => Promise<{
8
+ statusCode: number;
9
+ body: string;
10
+ }>;
11
+ });
12
+ httpRequest(params: HttpRequestParams): Promise<any>;
13
+ get(path: string, options?: {
14
+ params?: any;
15
+ headers?: any;
16
+ }): Promise<any>;
17
+ post(path: string, options?: {
18
+ data: any;
19
+ headers?: any;
20
+ }): Promise<any>;
21
+ }
22
+ declare const lxRequest: NativeRequest;
23
+ export { lxRequest };
@@ -0,0 +1,2 @@
1
+ declare const lxRequest: import("axios").AxiosInstance;
2
+ export { lxRequest };
@@ -0,0 +1,41 @@
1
+ export declare function getAppToken(): Promise<{
2
+ appToken: string;
3
+ }>;
4
+ export declare function getUserToken(authCode: string, appToken: string): Promise<{
5
+ userToken: string;
6
+ }>;
7
+ export type LXUserinfo = {
8
+ staffId: string;
9
+ name: string;
10
+ orgId: string;
11
+ orgName: string;
12
+ avatarId: string;
13
+ avatarUrl: string;
14
+ mobilePhone: {
15
+ countryCode: string;
16
+ number: string;
17
+ };
18
+ email: string;
19
+ employeeNumber: string;
20
+ LoginName: string;
21
+ externalId: string;
22
+ department: {
23
+ id: string;
24
+ name: string;
25
+ };
26
+ };
27
+ export declare function getUserInfo(appToken: string, userToken: string): Promise<LXUserinfo>;
28
+ export type LXUserDetailParams = {
29
+ appToken: string;
30
+ userToken: string;
31
+ staffId: string;
32
+ };
33
+ export type LXUserDetail = LXUserinfo & {
34
+ name: string;
35
+ signature: string;
36
+ idNumber: string;
37
+ };
38
+ export declare function getUserDetail(params: LXUserDetailParams): Promise<LXUserDetail>;
39
+ export declare function getUserCredential(appCredential: string, idCard: string): Promise<{
40
+ userCredential: string;
41
+ }>;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@r2d2cn/rk-bridge",
3
+ "version": "1.0.0",
4
+ "description": "Rokid mobile bridge JS SDK",
5
+ "type": "module",
6
+ "main": "./index.cjs.js",
7
+ "module": "./index.esm.js",
8
+ "unpkg": "./index.umd.js",
9
+ "types": "./index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./index.d.ts",
13
+ "require": "./index.cjs.js",
14
+ "import": "./index.esm.js"
15
+ },
16
+ "./base": {
17
+ "types": "./base.d.ts",
18
+ "import": "./base.d.ts",
19
+ "require": "./base.d.ts"
20
+ }
21
+ },
22
+ "files": [
23
+ "*.js",
24
+ "*.d.ts",
25
+ "rokid",
26
+ "zjt",
27
+ "lx-sdk"
28
+ ],
29
+ "dependencies": {},
30
+ "peerDependencies": {
31
+ "uuid": "^8.3.2",
32
+ "eventemitter3": "^5.0.1"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public",
36
+ "registry": "https://registry.npmjs.org/"
37
+ }
38
+ }
@@ -0,0 +1,66 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { BaseBridge } from '../base.ts';
3
+ import type { AiChatRecord, AppInfo, BridgeFuncAsync, Credentials, DeviceInfo, DirectoryFileItem, DirectoryFileListRequest, HttpResponse, LanguageOption, MediaItem } from '../types.ts';
4
+ declare class RKBridge extends BaseBridge {
5
+ emitter: EventEmitter<string | symbol, any>;
6
+ hasBackground: boolean;
7
+ initCallback(): void;
8
+ generateBridgeFunc<R, P extends any[]>(name: string, module?: string): BridgeFuncAsync<R, P>;
9
+ initSDK: BridgeFuncAsync<void, [string]>;
10
+ registerClientId: BridgeFuncAsync<void, [string]>;
11
+ navigate: (url: string) => Promise<void>;
12
+ goBack: () => Promise<void>;
13
+ getDeviceInfo: BridgeFuncAsync<DeviceInfo, []>;
14
+ getStatusBarHeight: BridgeFuncAsync<number, []>;
15
+ getLocationInfo: BridgeFuncAsync<any, []>;
16
+ getAuthCode: BridgeFuncAsync<any, []>;
17
+ getUserInfo: () => any;
18
+ getCredentials: () => Promise<Credentials>;
19
+ getAppToken: BridgeFuncAsync<string, []>;
20
+ getUserToken: BridgeFuncAsync<string, []>;
21
+ getSdkToken: BridgeFuncAsync<string, []>;
22
+ setVolume: BridgeFuncAsync<void, [number]>;
23
+ setBrightness: BridgeFuncAsync<void, [number]>;
24
+ makePhone: BridgeFuncAsync<void, [string]>;
25
+ sendEventToNative: BridgeFuncAsync<void, [string]>;
26
+ getFileProvider: BridgeFuncAsync<string, []>;
27
+ startBluetoothScan: BridgeFuncAsync<void, []>;
28
+ stopBluetoothScan: BridgeFuncAsync<void, []>;
29
+ connectBluetooth: BridgeFuncAsync<void, [string]>;
30
+ startWifiP2PDiscoverPeers: BridgeFuncAsync<void, []>;
31
+ stopWifiP2PDiscoverPeers: BridgeFuncAsync<void, []>;
32
+ connectWifiP2P: BridgeFuncAsync<void, [string]>;
33
+ getChatList: BridgeFuncAsync<AiChatRecord[], []>;
34
+ getSourceLanguageOptions: BridgeFuncAsync<LanguageOption[], []>;
35
+ getTargetLanguageOptions: BridgeFuncAsync<LanguageOption[], []>;
36
+ getSourceLanguage: BridgeFuncAsync<string, []>;
37
+ getTargetLanguage: BridgeFuncAsync<string, []>;
38
+ setLanguage: BridgeFuncAsync<void, [string, string]>;
39
+ getAlbumFileList: BridgeFuncAsync<MediaItem[], []>;
40
+ private getDirectoryFileListNative;
41
+ getDirectoryFileList: (params: DirectoryFileListRequest) => Promise<DirectoryFileItem[]>;
42
+ httpRequest: BridgeFuncAsync<HttpResponse, [string]>;
43
+ getApps: BridgeFuncAsync<AppInfo[], []>;
44
+ getAppInfo: BridgeFuncAsync<AppInfo, [string]>;
45
+ installApp: BridgeFuncAsync<void, [string]>;
46
+ uninstallApp: BridgeFuncAsync<void, [string]>;
47
+ launchApp: BridgeFuncAsync<void, [string]>;
48
+ stopApp: BridgeFuncAsync<void, [string]>;
49
+ downloadApk: BridgeFuncAsync<void, [string]>;
50
+ getActiveInstallTask: BridgeFuncAsync<any, []>;
51
+ subscribeAppProgress: BridgeFuncAsync<void, []>;
52
+ unsubscribeAppProgress: BridgeFuncAsync<void, []>;
53
+ private setBootAppNative;
54
+ /**
55
+ * 开机自启:Native 未合入时 reject(-999),避免 generateBridgeFunc 缺方法时 resolve(undefined) 误判成功。
56
+ */
57
+ setBootApp: (packageName: string) => Promise<void>;
58
+ sendTextMessageByClassicBT: BridgeFuncAsync<void, [string]>;
59
+ sendTextMessageByBle: BridgeFuncAsync<void, [string]>;
60
+ sendTextMessageByP2P: BridgeFuncAsync<void, [string]>;
61
+ sendStreamData: BridgeFuncAsync<void, [string]>;
62
+ sendGlassBroadcast: BridgeFuncAsync<void, [string]>;
63
+ initNativeEventListener(): void;
64
+ }
65
+ declare const rkBridge: RKBridge;
66
+ export default rkBridge;
package/types.d.ts ADDED
@@ -0,0 +1,117 @@
1
+ export type BridgeCallbackType = 'success' | 'error' | 'process';
2
+ export type BridgeCallback<T> = {
3
+ onSuccess?: (data?: T) => void;
4
+ onError?: (errorCode: number) => void;
5
+ };
6
+ export type BridgeFuncAsync<R, P extends any[]> = (...params: P) => Promise<R>;
7
+ export type BridgeEventType = 'deviceInfoChanged' | 'bluetoothConnect' | 'bluetoothDeviceFound' | 'bluetoothScanFinished' | 'bluetoothConnectRejected' | 'wifiP2PEnabled' | 'aiChatQuestionUpdate' | 'aiChatAnswerUpdate' | 'albumUpdate' | 'directoryFileChanged' | 'taskCenterEvent' | 'classicBTTextMessage' | 'p2pTextMessage' | 'appInstallStart' | 'appInstallProgress' | 'appSendFileComplete' | 'appInstallComplete' | 'appInstallFailed' | 'apkDownloadFailed' | 'apkDownloadComplete' | 'apkDownloadProgress';
8
+ export type BridgeEventData<T> = {
9
+ data: T;
10
+ };
11
+ export type BluetoothDeviceInfo = {
12
+ name: string;
13
+ address: string;
14
+ };
15
+ export type BluetoothConnectRejectedInfo = {
16
+ code: number;
17
+ reason: string;
18
+ };
19
+ export type DeviceInfo = {
20
+ name: string;
21
+ bluetoothConnected: boolean;
22
+ wifiP2PConnected: boolean;
23
+ bluetoothAddress: string;
24
+ isCharge: boolean;
25
+ powerValue: number;
26
+ brightness: number;
27
+ maxBrightness: number;
28
+ curVolume: number;
29
+ maxVolume: number;
30
+ deviceId: string;
31
+ version: string;
32
+ appVersion: string;
33
+ };
34
+ export type AiChatRecord = {
35
+ questionId: string;
36
+ question: string;
37
+ imageUrl?: string;
38
+ answer?: string;
39
+ time: number;
40
+ };
41
+ export type LanguageOption = {
42
+ label: string;
43
+ value: string;
44
+ };
45
+ export type MediaItem = {
46
+ fileBrowserPath: string;
47
+ filePath: string;
48
+ fileTime: number;
49
+ fileMd5: string;
50
+ fileUrl?: string;
51
+ };
52
+ export type DirectoryFileType = 'video' | 'image' | 'audio' | 'other';
53
+ export type DirectoryFileListRequest = {
54
+ dir: string;
55
+ recursive?: boolean;
56
+ fileTypes?: DirectoryFileType[];
57
+ };
58
+ export type DirectoryFileItem = {
59
+ fileName: string;
60
+ filePath: string;
61
+ fileBrowserPath: string;
62
+ fileTime: number;
63
+ fileMd5: string;
64
+ fileSize: number;
65
+ mimeType: string;
66
+ fileType: DirectoryFileType;
67
+ thumbnailPath?: string;
68
+ isUploaded?: boolean;
69
+ fileUrl?: string;
70
+ };
71
+ export type DirectoryFileChangedPayload = {
72
+ dir: string;
73
+ /** updated:原生补齐视频封面等元数据后的增量更新 */
74
+ changeType: 'added' | 'modified' | 'updated' | 'deleted' | 'refresh';
75
+ file?: DirectoryFileItem;
76
+ };
77
+ export type HttpRequestParams = {
78
+ url: string;
79
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE';
80
+ headers?: Record<string, string>;
81
+ queryParams?: Record<string, string>;
82
+ body?: any;
83
+ timeoutMillis?: number;
84
+ };
85
+ export type HttpResponse = {
86
+ statusCode: number;
87
+ body: string;
88
+ };
89
+ export type Credentials = {
90
+ appCredential: string;
91
+ userCredential: string;
92
+ };
93
+ export type AppInfo = {
94
+ packageName: string;
95
+ appName: string;
96
+ versionName: string;
97
+ versionCode: string;
98
+ installTime: number;
99
+ updateTime: number;
100
+ apkSize: number;
101
+ isSystemApp: boolean;
102
+ isRunning: boolean;
103
+ /**
104
+ * 是否为当前开机自启应用;字段缺失时按 false。
105
+ * 全列表最多一项为 true,以 getApps 返回为准。
106
+ */
107
+ isBootApp?: boolean;
108
+ /** 本地图标 URL(FileProvider);无图时为 null / 缺失,勿使用 iconBase64 */
109
+ iconUrl?: string | null;
110
+ };
111
+ export type GlassAction = {
112
+ action: string;
113
+ pkg: string;
114
+ cls: string;
115
+ method: string;
116
+ params: string;
117
+ };
package/zjt/index.d.ts ADDED
@@ -0,0 +1,66 @@
1
+ import EventEmitter from 'eventemitter3';
2
+ import { BaseBridge } from '../base.ts';
3
+ import type { AiChatRecord, AppInfo, BridgeFuncAsync, Credentials, DeviceInfo, DirectoryFileItem, DirectoryFileListRequest, HttpResponse, LanguageOption, MediaItem } from '../types.ts';
4
+ declare class RKBridge extends BaseBridge {
5
+ emitter: EventEmitter<string | symbol, any>;
6
+ private appToken;
7
+ private userToken;
8
+ hasBackground: boolean;
9
+ constructor();
10
+ initCallback(): void;
11
+ generateBridgeFunc<R, P extends any[]>(name: string, module?: string): BridgeFuncAsync<R, P>;
12
+ initSDK: BridgeFuncAsync<void, [string]>;
13
+ registerClientId: BridgeFuncAsync<void, [string]>;
14
+ navigate: (url: string) => Promise<void>;
15
+ goBack: () => Promise<void>;
16
+ getDeviceInfo: BridgeFuncAsync<DeviceInfo, []>;
17
+ getLocationInfo: BridgeFuncAsync<any, []>;
18
+ getStatusBarHeight: BridgeFuncAsync<number, []>;
19
+ getCredentials: () => Promise<Credentials>;
20
+ getAuthCode: () => Promise<string>;
21
+ getUserInfo: () => Promise<import("@/lx-sdk/server").LXUserinfo>;
22
+ getAppToken: () => Promise<string>;
23
+ getUserToken: () => Promise<string>;
24
+ getSdkToken: BridgeFuncAsync<string, []>;
25
+ setVolume: BridgeFuncAsync<void, [number]>;
26
+ setBrightness: BridgeFuncAsync<void, [number]>;
27
+ makePhone: BridgeFuncAsync<void, [string]>;
28
+ getFileProvider: BridgeFuncAsync<string, []>;
29
+ sendEventToNative: BridgeFuncAsync<void, [string]>;
30
+ startBluetoothScan: BridgeFuncAsync<void, []>;
31
+ stopBluetoothScan: BridgeFuncAsync<void, []>;
32
+ connectBluetooth: BridgeFuncAsync<void, [string]>;
33
+ startWifiP2PDiscoverPeers: BridgeFuncAsync<void, []>;
34
+ stopWifiP2PDiscoverPeers: BridgeFuncAsync<void, []>;
35
+ connectWifiP2P: BridgeFuncAsync<void, [string]>;
36
+ getChatList: BridgeFuncAsync<AiChatRecord[], []>;
37
+ getSourceLanguageOptions: BridgeFuncAsync<LanguageOption[], []>;
38
+ getTargetLanguageOptions: BridgeFuncAsync<LanguageOption[], []>;
39
+ getSourceLanguage: BridgeFuncAsync<string, []>;
40
+ getTargetLanguage: BridgeFuncAsync<string, []>;
41
+ setLanguage: BridgeFuncAsync<void, [string, string]>;
42
+ getAlbumFileList: BridgeFuncAsync<MediaItem[], []>;
43
+ private getDirectoryFileListNative;
44
+ getDirectoryFileList: (params: DirectoryFileListRequest) => Promise<DirectoryFileItem[]>;
45
+ httpRequest: BridgeFuncAsync<HttpResponse, [string]>;
46
+ getApps: BridgeFuncAsync<AppInfo[], []>;
47
+ getAppInfo: BridgeFuncAsync<AppInfo, [string]>;
48
+ installApp: BridgeFuncAsync<void, [string]>;
49
+ uninstallApp: BridgeFuncAsync<void, [string]>;
50
+ launchApp: BridgeFuncAsync<void, [string]>;
51
+ stopApp: BridgeFuncAsync<void, [string]>;
52
+ downloadApk: BridgeFuncAsync<void, [string]>;
53
+ getActiveInstallTask: BridgeFuncAsync<any, []>;
54
+ subscribeAppProgress: BridgeFuncAsync<void, []>;
55
+ unsubscribeAppProgress: BridgeFuncAsync<void, []>;
56
+ private setBootAppNative;
57
+ setBootApp: (packageName: string) => Promise<void>;
58
+ sendTextMessageByClassicBT: BridgeFuncAsync<void, [string]>;
59
+ sendTextMessageByBle: BridgeFuncAsync<void, [string]>;
60
+ sendTextMessageByP2P: BridgeFuncAsync<void, [string]>;
61
+ sendStreamData: BridgeFuncAsync<void, [string]>;
62
+ sendGlassBroadcast: BridgeFuncAsync<void, [string]>;
63
+ initNativeEventListener(): void;
64
+ }
65
+ declare const rkBridge: RKBridge;
66
+ export default rkBridge;