@lemon-fe/mini-app 0.1.102 → 0.1.103

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.
@@ -94,6 +94,55 @@ function App(props) {
94
94
  return prev;
95
95
  });
96
96
  }, []);
97
+ var replace = useCallback(function (path, otherParams) {
98
+ setState(function (prev) {
99
+ var routes = prev.routes;
100
+
101
+ var _parsePath2 = parsePath(path),
102
+ pathname = _parsePath2.pathname,
103
+ search = _parsePath2.search;
104
+
105
+ var params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
106
+ Object.assign(params, otherParams);
107
+ var routeIndex = routes.findIndex(function (item) {
108
+ return item.route.path === pathname;
109
+ });
110
+
111
+ if (routeIndex >= 0) {
112
+ var nextRoutes = routes.slice(0, routeIndex + 1);
113
+ nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
114
+ params: params
115
+ });
116
+ return {
117
+ routes: nextRoutes
118
+ };
119
+ }
120
+
121
+ var page = pages.find(function (item) {
122
+ return item.path === pathname;
123
+ });
124
+
125
+ if (page !== undefined) {
126
+ var newRoute = {
127
+ route: page,
128
+ params: params
129
+ };
130
+
131
+ if (routes.length <= 0) {
132
+ return {
133
+ routes: [newRoute]
134
+ };
135
+ }
136
+
137
+ console.log(routes.slice(0, -1).concat(newRoute));
138
+ return {
139
+ routes: routes.slice(0, -1).concat(newRoute)
140
+ };
141
+ }
142
+
143
+ return prev;
144
+ });
145
+ }, []);
97
146
  var goBack = useCallback(function () {
98
147
  setState(function (prev) {
99
148
  var nextRoutes = _toConsumableArray(prev.routes);
@@ -125,11 +174,12 @@ function App(props) {
125
174
  return {
126
175
  navigation: {
127
176
  navigate: navigate,
128
- goBack: goBack
177
+ goBack: goBack,
178
+ replace: replace
129
179
  },
130
180
  headerStyle: headerStyle
131
181
  };
132
- }, [navigate, goBack, headerStyle]);
182
+ }, [navigate, goBack, replace, headerStyle]);
133
183
  var screens = /*#__PURE__*/React.createElement(TransitionGroup, {
134
184
  className: "lemon-routes"
135
185
  }, state.routes.map(function (item, index) {
@@ -1 +1,13 @@
1
1
  export declare const GO_BACK = "GO_BACK";
2
+ export declare const METHOD = "METHOD";
3
+ export declare const METHOD_CALLBACK = "METHOD_CALLBACK";
4
+ export declare const INIT_IFRAME = "INIT_IFRAME";
5
+ export declare const LEMON_APP_EXTRA_PAYLOAD = "LEMON_APP_EXTRA_PAYLOAD";
6
+ export declare const SCAN_CODE = "SCAN_CODE";
7
+ export declare const EXIT = "EXIT";
8
+ export declare const SET_STATUS_BAR_STYLE = "SET_STATUS_BAR_STYLE";
9
+ export declare const GET_USER_INFO = "GET_USER_INFO";
10
+ export declare const GET_ACCESS_TOKEN = "GET_ACCESS_TOKEN";
11
+ export declare const CHECK_ACCESS_TOKEN = "CHECK_ACCESS_TOKEN";
12
+ export declare const SET_CAPSULE = "SET_CAPSULE";
13
+ export declare const SET_EXTRA_PAYLOAD = "SET_EXTRA_PAYLOAD";
@@ -1 +1,14 @@
1
- export var GO_BACK = 'GO_BACK';
1
+ export var GO_BACK = 'GO_BACK';
2
+ export var METHOD = 'METHOD';
3
+ export var METHOD_CALLBACK = 'METHOD_CALLBACK';
4
+ export var INIT_IFRAME = 'INIT_IFRAME';
5
+ export var LEMON_APP_EXTRA_PAYLOAD = 'LEMON_APP_EXTRA_PAYLOAD'; // methods
6
+
7
+ export var SCAN_CODE = 'SCAN_CODE';
8
+ export var EXIT = 'EXIT';
9
+ export var SET_STATUS_BAR_STYLE = 'SET_STATUS_BAR_STYLE';
10
+ export var GET_USER_INFO = 'GET_USER_INFO';
11
+ export var GET_ACCESS_TOKEN = 'GET_ACCESS_TOKEN';
12
+ export var CHECK_ACCESS_TOKEN = 'CHECK_ACCESS_TOKEN';
13
+ export var SET_CAPSULE = 'SET_CAPSULE';
14
+ export var SET_EXTRA_PAYLOAD = 'SET_EXTRA_PAYLOAD';
@@ -3,6 +3,7 @@ export declare const NavigationContext: import("react").Context<{
3
3
  navigation: {
4
4
  navigate: (path: string, params?: Record<string, any> | undefined) => void;
5
5
  goBack: () => void;
6
+ replace: (path: string, params?: Record<string, any> | undefined) => void;
6
7
  };
7
8
  headerStyle?: CSSProperties | undefined;
8
9
  }>;
@@ -2,7 +2,8 @@ import { createContext } from 'react';
2
2
  export var NavigationContext = /*#__PURE__*/createContext({
3
3
  navigation: {
4
4
  navigate: function navigate() {},
5
- goBack: function goBack() {}
5
+ goBack: function goBack() {},
6
+ replace: function replace() {}
6
7
  }
7
8
  });
8
9
  export var ScreenContext = /*#__PURE__*/createContext({
package/es/global.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { UserInfo } from './typings';
1
+ import type { UserInfo } from './typings';
2
2
  declare global {
3
3
  interface Window {
4
+ mini?: typeof mini;
4
5
  ReactNativeWebView?: {
5
- postMessage(e: string): void;
6
+ postMessage: (e: string) => void;
6
7
  };
7
- mini?: typeof mini;
8
8
  }
9
9
  }
10
10
  export declare const mini: {
@@ -19,7 +19,7 @@ export declare const mini: {
19
19
  left: number;
20
20
  };
21
21
  };
22
- getExtraPayload(): unknown;
22
+ getExtraPayload(): any;
23
23
  methodRegister(name: string, opts?: {
24
24
  success?: ((value: any) => void) | undefined;
25
25
  fail?: ((err: Error) => void) | undefined;
package/es/global.js CHANGED
@@ -1,6 +1,8 @@
1
- var methodID = 0;
1
+ import { CHECK_ACCESS_TOKEN, EXIT, GET_ACCESS_TOKEN, GET_USER_INFO, INIT_IFRAME, LEMON_APP_EXTRA_PAYLOAD, METHOD, METHOD_CALLBACK, SCAN_CODE, SET_CAPSULE, SET_EXTRA_PAYLOAD, SET_STATUS_BAR_STYLE } from './constants';
2
2
  var methods = {};
3
3
  var listeners = [];
4
+ var methodID = 0;
5
+ var whiteList = ['localhost', 'csp.nhsoft.cn', '127.0.0.1'];
4
6
  export var mini = {
5
7
  loaded: false,
6
8
  getSystemInfoSync: function getSystemInfoSync() {
@@ -16,23 +18,22 @@ export var mini = {
16
18
  };
17
19
  },
18
20
  getExtraPayload: function getExtraPayload() {
19
- return;
21
+ return JSON.parse(window.localStorage.getItem('LEMON_APP_EXTRA_PAYLOAD') || '{}');
20
22
  },
21
23
  methodRegister: function methodRegister(name) {
22
24
  var _window$ReactNativeWe;
23
25
 
24
26
  var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
25
- var mOpts = opts || {};
26
27
  methods[methodID] = {
27
- success: mOpts.success,
28
- fail: mOpts.fail
28
+ success: opts.success,
29
+ fail: opts.fail
29
30
  };
30
31
  (_window$ReactNativeWe = window.ReactNativeWebView) === null || _window$ReactNativeWe === void 0 ? void 0 : _window$ReactNativeWe.postMessage(JSON.stringify({
31
- type: 'METHOD',
32
+ type: METHOD,
32
33
  target: {
33
34
  id: methodID,
34
35
  name: name,
35
- params: mOpts.params
36
+ params: opts.params
36
37
  }
37
38
  }));
38
39
 
@@ -78,25 +79,25 @@ export var mini = {
78
79
  });
79
80
  },
80
81
  scanCode: function scanCode(opts) {
81
- mini.methodRegister('SCAN_CODE', opts);
82
+ mini.methodRegister(SCAN_CODE, opts);
82
83
  },
83
84
  exit: function exit() {
84
- mini.methodRegister('EXIT');
85
+ mini.methodRegister(EXIT);
85
86
  },
86
87
  setStatusBarStyle: function setStatusBarStyle(opts) {
87
- mini.methodRegister('SET_STATUS_BAR_STYLE', opts);
88
+ mini.methodRegister(SET_STATUS_BAR_STYLE, opts);
88
89
  },
89
90
  getUserInfo: function getUserInfo(opts) {
90
- mini.methodRegister('GET_USER_INFO', opts);
91
+ mini.methodRegister(GET_USER_INFO, opts);
91
92
  },
92
93
  getAccessToken: function getAccessToken(opts) {
93
- mini.methodRegister('GET_ACCESS_TOKEN', opts);
94
+ mini.methodRegister(GET_ACCESS_TOKEN, opts);
94
95
  },
95
96
  checkAccessToken: function checkAccessToken(opts) {
96
- mini.methodRegister('CHECK_ACCESS_TOKEN', opts);
97
+ mini.methodRegister(CHECK_ACCESS_TOKEN, opts);
97
98
  },
98
99
  setCapsule: function setCapsule(opts) {
99
- mini.methodRegister('SET_CAPSULE', {
100
+ mini.methodRegister(SET_CAPSULE, {
100
101
  params: opts
101
102
  });
102
103
  },
@@ -108,7 +109,7 @@ export var mini = {
108
109
  */
109
110
  listenScanCode: function listenScanCode(opts) {
110
111
  return mini.addListener(function (e, value) {
111
- if (e === 'SCAN_CODE' && value) {
112
+ if (e === SCAN_CODE && value) {
112
113
  opts.success(value);
113
114
  }
114
115
  });
@@ -118,21 +119,49 @@ window.mini = mini;
118
119
  window.addEventListener('message', function (evt) {
119
120
  var result = /^(([^:\/\s]+):\/?\/?([^\/\s@]*@)?([^\/@:]*)?:?(\d+)?)?(\/[^?]*)?(\?([^#]*))?(#[\s\S]*)?$/.exec(evt.origin);
120
121
 
121
- if (result !== null && (result[4] === 'localhost' || result[4] === 'csp.nhsoft.cn' || result[4] === '127.0.0.1')) {
122
- if (evt.data === 'INIT_IFRAME') {
122
+ if (result !== null && whiteList.includes(result[4])) {
123
+ if (evt.data === INIT_IFRAME) {
123
124
  window.ReactNativeWebView = {
124
125
  postMessage: function postMessage(e) {
125
126
  window.parent.postMessage(e, '*');
126
127
  }
127
128
  };
128
129
  mini.loaded = true;
130
+ var nodeList = document.getElementsByTagName('link');
131
+ var favicon = undefined;
132
+
133
+ for (var i = 0; i < nodeList.length; i++) {
134
+ if (nodeList[i].getAttribute('rel') == 'icon' || nodeList[i].getAttribute('rel') == 'shortcut icon') {
135
+ favicon = nodeList[i].getAttribute('href');
136
+ break;
137
+ }
138
+ }
139
+
140
+ window.parent.postMessage(JSON.stringify({
141
+ type: INIT_IFRAME,
142
+ target: {
143
+ title: document.title,
144
+ icon: favicon
145
+ }
146
+ }), '*');
129
147
  } else {
130
- var msg = JSON.parse(evt.data);
148
+ var action = evt.data;
149
+
150
+ switch (action.type) {
151
+ case SET_EXTRA_PAYLOAD:
152
+ window.localStorage.setItem(LEMON_APP_EXTRA_PAYLOAD, JSON.stringify(action.payload));
153
+ break;
154
+
155
+ case METHOD_CALLBACK:
156
+ var msg = action.payload;
157
+
158
+ if (msg.type === 'success') {
159
+ mini.methodSuccess(msg.id, msg.data);
160
+ } else {
161
+ mini.methodFail(msg.id, new Error(msg.data));
162
+ }
131
163
 
132
- if (msg.type === 'success') {
133
- mini.methodSuccess(msg.id, msg.data);
134
- } else {
135
- mini.methodFail(msg.id, new Error(msg.data));
164
+ default:
136
165
  }
137
166
  }
138
167
  }
@@ -1,6 +1,7 @@
1
1
  export declare function useNavigation(): {
2
2
  navigate: (path: string, params?: Record<string, any> | undefined) => void;
3
3
  goBack: () => void;
4
+ replace: (path: string, params?: Record<string, any> | undefined) => void;
4
5
  };
5
6
  export declare function useParams<T extends Record<string, any> | void = void>(): T;
6
7
  export declare function useIsFocused(): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lemon-fe/mini-app",
3
- "version": "0.1.102",
3
+ "version": "0.1.103",
4
4
  "description": "> TODO: description",
5
5
  "author": "鲁盛杰 <lusj@cnlemon.net>",
6
6
  "homepage": "",
@@ -35,5 +35,5 @@
35
35
  "react": "^17.0.2",
36
36
  "react-dom": "17.0.2"
37
37
  },
38
- "gitHead": "57ac759d854789b6b3ca42b91014fffa6ed08720"
38
+ "gitHead": "b9730ec895f4fc20b1ff2ed94a4b974f2b62a49b"
39
39
  }