@wlydfe/flutter-bridge 0.0.1

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.
Files changed (47) hide show
  1. package/.babelrc +8 -0
  2. package/README.md +0 -0
  3. package/assets/main.css +166 -0
  4. package/assets/vconsole.min.js +10 -0
  5. package/index.html +150 -0
  6. package/lib/index.js +215 -0
  7. package/lib/index.js.map +1 -0
  8. package/lib/index.umd.js +222 -0
  9. package/lib/index.umd.js.map +1 -0
  10. package/lib/src/api/chooseImage.d.ts +14 -0
  11. package/lib/src/api/getLocation.d.ts +8 -0
  12. package/lib/src/api/index.d.ts +8 -0
  13. package/lib/src/api/login.d.ts +8 -0
  14. package/lib/src/api/navigateBack.d.ts +2 -0
  15. package/lib/src/api/navigateTo.d.ts +6 -0
  16. package/lib/src/api/previewImage.d.ts +14 -0
  17. package/lib/src/api/wxShare.d.ts +7 -0
  18. package/lib/src/core/bridgeCore.d.ts +2 -0
  19. package/lib/src/index.d.ts +1 -0
  20. package/lib/src/types/index.d.ts +5 -0
  21. package/lib/types/src/api/chooseImage.d.ts +14 -0
  22. package/lib/types/src/api/getLocation.d.ts +8 -0
  23. package/lib/types/src/api/index.d.ts +8 -0
  24. package/lib/types/src/api/login.d.ts +8 -0
  25. package/lib/types/src/api/navigateBack.d.ts +2 -0
  26. package/lib/types/src/api/navigateTo.d.ts +6 -0
  27. package/lib/types/src/api/previewImage.d.ts +14 -0
  28. package/lib/types/src/api/wxShare.d.ts +7 -0
  29. package/lib/types/src/core/bridgeCore.d.ts +2 -0
  30. package/lib/types/src/index.d.ts +1 -0
  31. package/lib/types/src/types/index.d.ts +5 -0
  32. package/log.md +4 -0
  33. package/package.json +42 -0
  34. package/rollup.config.dev.js +26 -0
  35. package/rollup.config.js +45 -0
  36. package/src/api/chooseImage.ts +27 -0
  37. package/src/api/getLocation.ts +21 -0
  38. package/src/api/index.ts +9 -0
  39. package/src/api/login.ts +19 -0
  40. package/src/api/navigateBack.ts +16 -0
  41. package/src/api/navigateTo.ts +18 -0
  42. package/src/api/previewImage.ts +27 -0
  43. package/src/api/wxShare.ts +19 -0
  44. package/src/core/bridgeCore.ts +65 -0
  45. package/src/index.ts +14 -0
  46. package/src/types/index.ts +5 -0
  47. package/tsconfig.json +19 -0
@@ -0,0 +1,8 @@
1
+ import { chooseImage } from "./chooseImage";
2
+ import { getLocation } from "./getLocation";
3
+ import { previewImage } from "./previewImage";
4
+ import { login } from "./login";
5
+ import { navigateTo } from "./navigateTo";
6
+ import { wxShare } from "./wxShare";
7
+ import { navigateBack } from "./navigateBack";
8
+ export { chooseImage, getLocation, previewImage, login, navigateTo, wxShare, navigateBack };
@@ -0,0 +1,8 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface LoginOptions extends Omit<BaseOptions, 'success'> {
3
+ success?: (result: {
4
+ loginSuccess: boolean;
5
+ userId: string;
6
+ }) => void;
7
+ }
8
+ export declare const login: (options: LoginOptions) => void;
@@ -0,0 +1,2 @@
1
+ import { BaseOptions } from "../types";
2
+ export declare const navigateBack: (options?: BaseOptions) => void;
@@ -0,0 +1,6 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface NavigateToOptions extends BaseOptions {
3
+ route: string;
4
+ params: any;
5
+ }
6
+ export declare const navigateTo: (options: NavigateToOptions) => void;
@@ -0,0 +1,14 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface PreviewImageOptions extends BaseOptions {
3
+ urls: string[];
4
+ }
5
+ /**
6
+ * 预览图片
7
+ * @param options 预览图片参数
8
+ * @param options.urls 图片URL列表
9
+ * @param options.success 成功回调
10
+ * @param options.fail 失败回调
11
+ * @param options.complete 完成回调
12
+ * @returns
13
+ */
14
+ export declare const previewImage: (options: PreviewImageOptions) => void;
@@ -0,0 +1,7 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface WxShareOptions extends BaseOptions {
3
+ title: string;
4
+ url: string;
5
+ shareType: ['appMessage', 'timeline', 'favorite'];
6
+ }
7
+ export declare const wxShare: (options: WxShareOptions) => void;
@@ -0,0 +1,2 @@
1
+ export declare const initBridgeCore: () => void;
2
+ export declare const invoke: (type: any, params?: any, format?: any) => any;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export type BaseOptions = {
2
+ success?: () => void;
3
+ fail?: () => void;
4
+ complete?: () => void;
5
+ };
@@ -0,0 +1,14 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface ChooseImageOptions extends Omit<BaseOptions, 'success'> {
3
+ count: number;
4
+ sizeType: ['original', 'compressed'];
5
+ sourceType: ['album', 'camera'];
6
+ success?: (result: {
7
+ tempFilePaths: string[];
8
+ tempFiles: {
9
+ size: number;
10
+ type: string;
11
+ }[];
12
+ }) => void;
13
+ }
14
+ export declare const chooseImage: (options: ChooseImageOptions) => void;
@@ -0,0 +1,8 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface GetLocationOptions extends Omit<BaseOptions, 'success'> {
3
+ success?: (result: {
4
+ latitude: number;
5
+ longitude: number;
6
+ }) => void;
7
+ }
8
+ export declare const getLocation: (options: GetLocationOptions) => void;
@@ -0,0 +1,8 @@
1
+ import { chooseImage } from "./chooseImage";
2
+ import { getLocation } from "./getLocation";
3
+ import { previewImage } from "./previewImage";
4
+ import { login } from "./login";
5
+ import { navigateTo } from "./navigateTo";
6
+ import { wxShare } from "./wxShare";
7
+ import { navigateBack } from "./navigateBack";
8
+ export { chooseImage, getLocation, previewImage, login, navigateTo, wxShare, navigateBack };
@@ -0,0 +1,8 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface LoginOptions extends Omit<BaseOptions, 'success'> {
3
+ success?: (result: {
4
+ loginSuccess: boolean;
5
+ userId: string;
6
+ }) => void;
7
+ }
8
+ export declare const login: (options: LoginOptions) => void;
@@ -0,0 +1,2 @@
1
+ import { BaseOptions } from "../types";
2
+ export declare const navigateBack: (options?: BaseOptions) => void;
@@ -0,0 +1,6 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface NavigateToOptions extends BaseOptions {
3
+ route: string;
4
+ params: any;
5
+ }
6
+ export declare const navigateTo: (options: NavigateToOptions) => void;
@@ -0,0 +1,14 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface PreviewImageOptions extends BaseOptions {
3
+ urls: string[];
4
+ }
5
+ /**
6
+ * 预览图片
7
+ * @param options 预览图片参数
8
+ * @param options.urls 图片URL列表
9
+ * @param options.success 成功回调
10
+ * @param options.fail 失败回调
11
+ * @param options.complete 完成回调
12
+ * @returns
13
+ */
14
+ export declare const previewImage: (options: PreviewImageOptions) => void;
@@ -0,0 +1,7 @@
1
+ import { BaseOptions } from "../types";
2
+ export interface WxShareOptions extends BaseOptions {
3
+ title: string;
4
+ url: string;
5
+ shareType: ['appMessage', 'timeline', 'favorite'];
6
+ }
7
+ export declare const wxShare: (options: WxShareOptions) => void;
@@ -0,0 +1,2 @@
1
+ export declare const initBridgeCore: () => void;
2
+ export declare const invoke: (type: any, params?: any, format?: any) => any;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export type BaseOptions = {
2
+ success?: () => void;
3
+ fail?: () => void;
4
+ complete?: () => void;
5
+ };
package/log.md ADDED
@@ -0,0 +1,4 @@
1
+ # wlyd-flutter-bridge
2
+
3
+ 发版内容
4
+ ## 0.0.1
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@wlydfe/flutter-bridge",
3
+ "version": "0.0.1",
4
+ "description": "flutter uniapp bridge",
5
+ "main": "lib/index.umd.js",
6
+ "module": "lib/index.js",
7
+ "typings": "lib/types/src/index.d.ts",
8
+ "scripts": {
9
+ "dev": "rollup -wc rollup.config.dev.js",
10
+ "build": "rollup -c rollup.config.js",
11
+ "build:types": "tsc"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://codeup.aliyun.com/610b3c9d86508f8da8b08436/fe-npm/flutter-uni-bridge.git"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "author": "",
21
+ "license": "MIT",
22
+ "devDependencies": {
23
+ "@babel/core": "7.28.4",
24
+ "@babel/plugin-transform-class-properties": "7.25.9",
25
+ "@babel/preset-env": "7.28.3",
26
+ "@rollup/plugin-babel": "6.0.4",
27
+ "@rollup/plugin-commonjs": "28.0.1",
28
+ "@rollup/plugin-json": "6.1.0",
29
+ "@rollup/plugin-node-resolve": "15.2.3",
30
+ "@rollup/plugin-terser": "0.4.4",
31
+ "rollup": "3.29.4",
32
+ "rollup-plugin-copy": "^3.5.0",
33
+ "rollup-plugin-filesize": "10.0.0",
34
+ "rollup-plugin-typescript2": "0.36.0",
35
+ "ts-node": "10.9.2",
36
+ "tslib": "2.8.1",
37
+ "typescript": "5.9.2"
38
+ },
39
+ "dependencies": {
40
+ "rollup-plugin-serve": "^3.0.0"
41
+ }
42
+ }
@@ -0,0 +1,26 @@
1
+ import serve from "rollup-plugin-serve";
2
+ import resolve from '@rollup/plugin-node-resolve';
3
+ import commonjs from '@rollup/plugin-commonjs';
4
+ import typescript from 'rollup-plugin-typescript2';
5
+
6
+ export default {
7
+ input: 'src/index.ts',
8
+ output: {
9
+ name: 'wlydTrack',
10
+ file: 'lib/index.js',
11
+ format: 'umd',
12
+ sourcemap: true
13
+ },
14
+ plugins: [
15
+ resolve(),
16
+ commonjs(),
17
+ typescript({}),
18
+ serve({
19
+ open: true, // 是否打开浏览器
20
+ contentBase: "./", // 入口 html 文件位置
21
+ historyApiFallback: true, // 设置为 true 返回 index.html 而不是 404
22
+ host: "0.0.0.0", //
23
+ port: 8000 // 端口号
24
+ }),
25
+ ]
26
+ }
@@ -0,0 +1,45 @@
1
+ // import filesize from "rollup-plugin-filesize";
2
+ import resolve from '@rollup/plugin-node-resolve';
3
+ import commonjs from '@rollup/plugin-commonjs';
4
+ import typescript from 'rollup-plugin-typescript2';
5
+ import copy from 'rollup-plugin-copy';
6
+
7
+ export default [
8
+ {
9
+ input: 'src/index.ts',
10
+ output: [
11
+ {
12
+ name: 'wlydTrack',
13
+ file: 'lib/index.umd.js',
14
+ format: 'umd',
15
+ inlineDynamicImports: true,
16
+ sourcemap: true
17
+ },
18
+ {
19
+ name: 'wlydTrack',
20
+ file: 'lib/index.js',
21
+ format: 'es',
22
+ inlineDynamicImports: true,
23
+ sourcemap: true
24
+ }
25
+ ],
26
+ external: [
27
+ 'react',
28
+ 'react-dom',
29
+ '../../assets/hina-uni.esm.full.js'
30
+ ],
31
+ plugins: [
32
+ resolve(),
33
+ commonjs(),
34
+ typescript({
35
+ useTsconfigDeclarationDir: true,
36
+ }),
37
+ copy({
38
+ targets: [
39
+ { src: 'assets/hina-uni.esm.full.js', dest: 'lib' }
40
+ ],
41
+ hook: 'buildStart'
42
+ })
43
+ ]
44
+ },
45
+ ]
@@ -0,0 +1,27 @@
1
+ import { invoke } from "../core/bridgeCore";
2
+ import { BaseOptions } from "../types";
3
+
4
+ export interface ChooseImageOptions extends Omit<BaseOptions, 'success'> {
5
+ count: number;
6
+ sizeType: ['original', 'compressed'];
7
+ sourceType: ['album', 'camera'];
8
+ success?: (result: {
9
+ tempFilePaths: string[];
10
+ tempFiles: {
11
+ size: number;
12
+ type: string;
13
+ }[];
14
+ }) => void;
15
+ }
16
+
17
+ // 选择图片
18
+ export const chooseImage = (options: ChooseImageOptions) => {
19
+ const { success, fail, complete, ...restParams } = options;
20
+ invoke('chooseImage', restParams).then(data => {
21
+ if (data.success) {
22
+ success?.(data);
23
+ } else {
24
+ fail?.();
25
+ }
26
+ }).catch(fail).finally(complete);
27
+ }
@@ -0,0 +1,21 @@
1
+ import { invoke } from "../core/bridgeCore";
2
+ import { BaseOptions } from "../types";
3
+
4
+ export interface GetLocationOptions extends Omit<BaseOptions, 'success'> {
5
+ success?: (result: {
6
+ latitude: number;
7
+ longitude: number;
8
+ }) => void;
9
+ }
10
+
11
+ // 获取定位
12
+ export const getLocation = (options: GetLocationOptions) => {
13
+ const { success, fail, complete, ...restParams } = options;
14
+ invoke('getLocation', restParams).then(data => {
15
+ if (data.latitude && data.longitude) {
16
+ success?.(data);
17
+ } else {
18
+ fail?.();
19
+ }
20
+ }).catch(fail).finally(complete);
21
+ }
@@ -0,0 +1,9 @@
1
+ import { chooseImage } from "./chooseImage";
2
+ import { getLocation } from "./getLocation";
3
+ import { previewImage } from "./previewImage";
4
+ import { login } from "./login";
5
+ import { navigateTo } from "./navigateTo";
6
+ import { wxShare } from "./wxShare";
7
+ import { navigateBack } from "./navigateBack";
8
+
9
+ export { chooseImage, getLocation, previewImage, login, navigateTo, wxShare, navigateBack };
@@ -0,0 +1,19 @@
1
+ import { invoke } from "../core/bridgeCore";
2
+ import { BaseOptions } from "../types";
3
+ export interface LoginOptions extends Omit<BaseOptions, 'success'> {
4
+ success?: (result: {
5
+ loginSuccess: boolean;
6
+ userId: string;
7
+ }) => void;
8
+ }
9
+
10
+ export const login = (options: LoginOptions) => {
11
+ const { success, fail, complete, ...restParams } = options;
12
+ invoke('login', restParams).then(data => {
13
+ if (data.success) {
14
+ success?.(data);
15
+ } else {
16
+ fail?.();
17
+ }
18
+ }).catch(fail).finally(complete);
19
+ }
@@ -0,0 +1,16 @@
1
+ import { invoke } from "../core/bridgeCore";
2
+ import { BaseOptions } from "../types";
3
+
4
+ // 返回
5
+ export const navigateBack = (options?: BaseOptions) => {
6
+ const { success, fail, complete, ...restParams } = options;
7
+ invoke('back', restParams).then(data => {
8
+ console.log('navigateBack data', data);
9
+ // if (data.success) {
10
+ // success?.();
11
+ // } else {
12
+ // fail?.();
13
+ // }
14
+ }).catch(fail).finally(complete);
15
+ // .then(() => showToast('返回已触发', 2000, 'success'));
16
+ }
@@ -0,0 +1,18 @@
1
+ import { invoke } from "../core/bridgeCore";
2
+ import { BaseOptions } from "../types";
3
+
4
+ export interface NavigateToOptions extends BaseOptions {
5
+ route: string;
6
+ params: any;
7
+ }
8
+
9
+ export const navigateTo = (options: NavigateToOptions) => {
10
+ const { success, fail, complete, ...restParams } = options;
11
+ invoke('navigateTo', restParams).then(data => {
12
+ if (data.success) {
13
+ success?.();
14
+ } else {
15
+ fail?.();
16
+ }
17
+ }).catch(fail).finally(complete);
18
+ }
@@ -0,0 +1,27 @@
1
+ import { invoke } from "../core/bridgeCore";
2
+ import { BaseOptions } from "../types";
3
+
4
+ export interface PreviewImageOptions extends BaseOptions {
5
+ urls: string[];
6
+ }
7
+
8
+ /**
9
+ * 预览图片
10
+ * @param options 预览图片参数
11
+ * @param options.urls 图片URL列表
12
+ * @param options.success 成功回调
13
+ * @param options.fail 失败回调
14
+ * @param options.complete 完成回调
15
+ * @returns
16
+ */
17
+ export const previewImage = (options: PreviewImageOptions) => {
18
+ if (!options.urls || options.urls.length === 0) {
19
+ options.fail?.();
20
+ return;
21
+ }
22
+ invoke('previewImage', {
23
+ urls: options.urls
24
+ }).then(options.success)
25
+ .catch(options.fail)
26
+ .finally(options.complete);
27
+ }
@@ -0,0 +1,19 @@
1
+ import { invoke } from "../core/bridgeCore";
2
+ import { BaseOptions } from "../types";
3
+
4
+ export interface WxShareOptions extends BaseOptions {
5
+ title: string;
6
+ url: string;
7
+ shareType: ['appMessage', 'timeline', 'favorite'];
8
+ }
9
+ // 分享
10
+ export const wxShare = (options: WxShareOptions) => {
11
+ const { success, fail, complete, ...restParams } = options;
12
+ invoke('wxShare', restParams).then(data => {
13
+ if (data.success) {
14
+ success?.();
15
+ } else {
16
+ fail?.();
17
+ }
18
+ }).catch(fail).finally(complete);
19
+ }
@@ -0,0 +1,65 @@
1
+ let AppBridge = null;
2
+
3
+ export const initBridgeCore = () => {
4
+ // 全局回调ID和回调函数存储
5
+ let cbId = 0;
6
+ // 全局回调池,存储「回调 ID → {resolve, reject}」的映射
7
+ const cbs = {};
8
+ const version = "1.0";
9
+
10
+ /*
11
+ 定义一个全局的 AppBridge 对象,提供 invoke 方法,用于 H5 调用 Flutter 暴露的原生 API,
12
+ 核心是通过 Promise 来处理异步通信的结果回调.
13
+
14
+ WLYDBridge: flutter 与 H5 约定的 bridge 名字
15
+ */
16
+ AppBridge = {
17
+ invoke(type, data = {}) {
18
+ //1. 检查 Flutter 桥接是否就绪(WLYDBridge 是 Flutter 注入的全局对象)
19
+
20
+ if (!(window as any).WLYDBridge || typeof (window as any).WLYDBridge.postMessage !== 'function') {
21
+ console.log("bridge not ready");
22
+ return Promise.reject(new Error('bridge not ready'));
23
+ }
24
+ return new Promise((resolve, reject) => {
25
+ // 生成唯一的回调 ID,用于匹配 Flutter 回调的结果
26
+ const id = 'cb_' + (++cbId);
27
+ cbs[id] = { resolve, reject };
28
+ (window as any).WLYDBridge.postMessage(JSON.stringify({
29
+ type,
30
+ data,
31
+ callbackId: id,// 唯一回调 ID,Flutter 处理完会带回这个 ID
32
+ version
33
+ }));
34
+ });
35
+ }
36
+ };
37
+
38
+ // 初始化AppBridge
39
+ // Flutter回调处理
40
+ (window as any).__onFlutterCallback = (payload) => {
41
+ const { callbackId, status, data, error } = payload || {};
42
+ const cb = cbs[callbackId];
43
+
44
+ if (!cb) {
45
+ console.error(`操作失败: callbackId==${callbackId}`);
46
+ return;
47
+ }
48
+
49
+ if (status === 'success') {
50
+ cb.resolve(data);
51
+ } else {
52
+ cb.reject(error);
53
+ }
54
+ delete cbs[callbackId];
55
+ };
56
+ }
57
+
58
+ // 通用桥接调用封装
59
+ export const invoke = (type, params?: any, format?) => {
60
+ return AppBridge.invoke(type, params)
61
+ .then(data => typeof format === 'function' ? format(data) : data)
62
+ .catch(err => {
63
+ throw err;
64
+ });
65
+ }
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ import * as api from './api'
2
+ import { initBridgeCore } from './core/bridgeCore'
3
+
4
+ const initBridge = () => {
5
+ initBridgeCore()
6
+
7
+ if (!(window as any).uni) {
8
+ (window as any).uni = {}
9
+ }
10
+ const uniApi: any = (window as any).uni
11
+ Object.assign(uniApi, api)
12
+ }
13
+
14
+ initBridge()
@@ -0,0 +1,5 @@
1
+ export type BaseOptions = {
2
+ success?: () => void;
3
+ fail?: () => void;
4
+ complete?: () => void;
5
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "rootDir": ".",
4
+ "declaration": true,
5
+ "declarationDir": "./lib/types",
6
+ "module": "esnext",
7
+ "target": "es5",
8
+ "lib": ["es6", "dom", "es2016", "es2017"],
9
+ "sourceMap": true,
10
+ "jsx": "react",
11
+ "moduleResolution": "node",
12
+ "allowSyntheticDefaultImports": true,
13
+ "esModuleInterop": true
14
+ },
15
+ "exclude": [
16
+ "node_modules",
17
+ "lib"
18
+ ]
19
+ }