From ed589b0264f0875efd634958e7389b4fd0af28ef Mon Sep 17 00:00:00 2001 From: "yitan.lgh" Date: Wed, 23 Dec 2020 20:25:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=80=E6=9C=9F=E6=96=B0=E5=A2=9Eapi?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/error-handler/README.en-US.md | 77 +++++++++++++ packages/application/error-handler/README.md | 77 +++++++++++++ .../application/error-handler/demo/index.tsx | 25 ++++ .../error-handler/src/ali-miniapp/index.ts | 21 ++++ .../error-handler/src/byte-miniapp/index.ts | 20 ++++ .../application/error-handler/src/index.ts | 70 ++++++++++++ .../application/error-handler/src/types.ts | 17 +++ .../error-handler/src/web/index.ts | 20 ++++ .../error-handler/src/wechat-miniapp/index.ts | 20 ++++ packages/base/getApp/README.en.US.md | 43 ++++++- packages/base/getApp/README.md | 43 ++++++- packages/base/getApp/demo/index.tsx | 19 ++++ packages/base/getCurrentPages/README.en.US.md | 48 +++++++- packages/base/getCurrentPages/README.md | 48 +++++++- packages/base/getCurrentPages/demo/index.tsx | 28 +++++ packages/equipment/accelerometer/src/index.ts | 2 +- .../interface/alert/src/ali-miniapp/index.ts | 34 +++--- .../interface/alert/src/byte-miniapp/index.ts | 23 ++++ .../alert/src/bytedance-microapp/index.ts | 24 ---- packages/interface/alert/src/common.ts | 18 +++ .../interface/alert/src/quickapp/index.ts | 38 ------- .../alert/src/wechat-miniapp/index.ts | 23 ++++ .../alert/src/wechat-miniprogram/index.ts | 24 ---- packages/interface/alert/src/weex/index.ts | 18 --- .../showModal/src/ali-miniapp/index.ts | 4 +- .../showModal/src/byte-miniapp/index.ts | 2 +- packages/interface/showModal/src/common.ts | 27 ++--- .../showModal/src/wechat-miniapp/index.ts | 2 +- packages/interface/toast/README.en-US.md | 16 ++- packages/interface/toast/README.md | 12 +- packages/interface/toast/demo/index.tsx | 38 +++---- packages/interface/toast/package.json | 43 ------- .../interface/toast/src/__tests__/index.mp.ts | 22 ---- .../toast/src/__tests__/index.quickapp.ts | 23 ---- .../interface/toast/src/__tests__/index.ts | 49 -------- .../toast/src/__tests__/index.weex.ts | 26 ----- .../interface/toast/src/ali-miniapp/index.ts | 39 +++++++ .../interface/toast/src/byte-miniapp/index.ts | 41 +++++++ packages/interface/toast/src/index.ts | 61 ++++++---- .../toast/src/miniapp/ali-miniapp/index.ts | 28 ----- .../src/miniapp/bytedance-microapp/index.ts | 29 ----- .../src/miniapp/wechat-miniprogram/index.ts | 29 ----- packages/interface/toast/src/types.ts | 26 ++++- packages/interface/toast/src/utils/index.ts | 26 +++++ packages/interface/toast/src/web/index.ts | 107 +++++++++++++----- .../toast/src/wechat-miniapp/index.ts | 41 +++++++ packages/interface/toast/src/weex/index.ts | 31 ----- packages/location/src/index.ts | 22 ++-- packages/network/request/README.en-US.md | 6 +- .../network/request/src/ali-miniapp/index.ts | 5 +- .../network/request/src/byte-miniapp/index.ts | 5 +- packages/network/request/src/types.ts | 3 - packages/network/request/src/web/index.ts | 26 +---- .../request/src/wechat-miniapp/index.ts | 5 +- 54 files changed, 1001 insertions(+), 573 deletions(-) create mode 100644 packages/application/error-handler/README.en-US.md create mode 100644 packages/application/error-handler/README.md create mode 100644 packages/application/error-handler/demo/index.tsx create mode 100644 packages/application/error-handler/src/ali-miniapp/index.ts create mode 100644 packages/application/error-handler/src/byte-miniapp/index.ts create mode 100644 packages/application/error-handler/src/index.ts create mode 100644 packages/application/error-handler/src/types.ts create mode 100644 packages/application/error-handler/src/web/index.ts create mode 100644 packages/application/error-handler/src/wechat-miniapp/index.ts create mode 100644 packages/base/getApp/demo/index.tsx create mode 100644 packages/base/getCurrentPages/demo/index.tsx create mode 100644 packages/interface/alert/src/byte-miniapp/index.ts delete mode 100644 packages/interface/alert/src/bytedance-microapp/index.ts create mode 100644 packages/interface/alert/src/common.ts delete mode 100644 packages/interface/alert/src/quickapp/index.ts create mode 100644 packages/interface/alert/src/wechat-miniapp/index.ts delete mode 100644 packages/interface/alert/src/wechat-miniprogram/index.ts delete mode 100644 packages/interface/alert/src/weex/index.ts delete mode 100644 packages/interface/toast/package.json delete mode 100644 packages/interface/toast/src/__tests__/index.mp.ts delete mode 100644 packages/interface/toast/src/__tests__/index.quickapp.ts delete mode 100644 packages/interface/toast/src/__tests__/index.ts delete mode 100644 packages/interface/toast/src/__tests__/index.weex.ts create mode 100644 packages/interface/toast/src/ali-miniapp/index.ts create mode 100644 packages/interface/toast/src/byte-miniapp/index.ts delete mode 100644 packages/interface/toast/src/miniapp/ali-miniapp/index.ts delete mode 100644 packages/interface/toast/src/miniapp/bytedance-microapp/index.ts delete mode 100644 packages/interface/toast/src/miniapp/wechat-miniprogram/index.ts create mode 100644 packages/interface/toast/src/wechat-miniapp/index.ts delete mode 100644 packages/interface/toast/src/weex/index.ts diff --git a/packages/application/error-handler/README.en-US.md b/packages/application/error-handler/README.en-US.md new file mode 100644 index 00000000..0cf24438 --- /dev/null +++ b/packages/application/error-handler/README.en-US.md @@ -0,0 +1,77 @@ +--- +group: + title: application +title: errorEvent +--- + +# universal-error-event +[![npm](https://img.shields.io/npm/v/universal-error-event.svg)](https://www.npmjs.com/package/universal-error-event) + +Monitoring and cancellation of error events + +## Support +miniApp wechatMiniprogram bytedanceMicroApp + +## Install + +```bash +$ npm install universal-error-event --save +``` + +## Function + +### `onError(callback): void` + +Listens on the Mini Program error event. This event is triggered as a result of script error or failed API call. + +#### Parameters +function callback +The callback function for the Mini Program error event + +##### Parameters +string error +Error message, including stacks. + +### `offError(callback): void` + +Un-listens on Mini Program error event. + +#### Parameters +function callback +The callback function for the Mini Program error event + +### `onUnhandledRejection(callback): void` + +Listen for unhandled Promise rejection events. + +#### Parameters +function callback +The callback function for the Mini Program error event + +##### Parameters +Object res +|Property |Type |Description| +|:--|:--|:--| +|reason |string or Error| Reason for rejection, usually an Error object| +|promise| Promise.|Rejected Promise object| + +### `offUnhandledRejection(callback): void` + +Cancel listening for unhandled Promise rejection events. + +#### Parameters +function callback +The callback function for the Mini Program error event + +## Example + +```js +import errorEvent from 'universal-error-event'; + +errorEvent.onError(e => { + console.log(e); +}); + +errorEvent.offError(); +``` + diff --git a/packages/application/error-handler/README.md b/packages/application/error-handler/README.md new file mode 100644 index 00000000..2af46e06 --- /dev/null +++ b/packages/application/error-handler/README.md @@ -0,0 +1,77 @@ +--- +group: + title: 应用级事件 +title: errorEvent +--- + +# universal-error-event +[![npm](https://img.shields.io/npm/v/universal-error-event.svg)](https://www.npmjs.com/package/universal-error-event) + +错误事件的监听与取消 + +## 支持 +miniApp wechatMiniprogram bytedanceMicroApp + +## 安装 + +```bash +$ npm install universal-error-event --save +``` + +## 方法 + +### `onError(callback): void` + +监听小程序错误事件。如脚本错误或 API 调用报错等。 + +#### 参数 +function callback +小程序错误事件的回调函数 + +##### 参数 +string error +错误信息,包含堆栈 + +### `offError(callback): void` + +取消监听小程序错误事件。 + +#### 参数 +function callback +小程序错误事件的回调函数 + +### `onUnhandledRejection(callback): void` + +监听未处理的 Promise 拒绝事件。 + +#### 参数 +function callback +小程序错误事件的回调函数 + +##### 参数 +Object res +|属性 |类型 |说明| +|:--|:--|:--| +|reason |string or Error| 拒绝原因,一般是一个 Error 对象| +|promise| Promise.| 被拒绝的 Promise 对象| + +### `offUnhandledRejection(callback): void` + +取消监听未处理的 Promise 拒绝事件。 + +#### 参数 +function callback +小程序错误事件的回调函数 + +## 示例 + +```js +import errorEvent from 'universal-error-event'; + +errorEvent.onError(e => { + console.log(e); +}); + +errorEvent.offError(); +``` + diff --git a/packages/application/error-handler/demo/index.tsx b/packages/application/error-handler/demo/index.tsx new file mode 100644 index 00000000..b039957b --- /dev/null +++ b/packages/application/error-handler/demo/index.tsx @@ -0,0 +1,25 @@ +import { createElement, useEffect, useState, useRef } from 'rax'; +import View from 'rax-view'; +import Text from 'rax-text'; +import errorEvent from 'universal-error-event'; +import {showToast} from 'universal-toast'; + +export default function() { + const ref = useRef((res) => { + showToast('捕获到一个错误'); + }); + return ( + + errorEvent + { + errorEvent.onError(ref.current); + }}>监听错误 + { + throw new Error('我是一个错误'); + }}>触发错误 + { + errorEvent.offError(ref.current); + }}>取消监听 + + ); +} diff --git a/packages/application/error-handler/src/ali-miniapp/index.ts b/packages/application/error-handler/src/ali-miniapp/index.ts new file mode 100644 index 00000000..6651daf9 --- /dev/null +++ b/packages/application/error-handler/src/ali-miniapp/index.ts @@ -0,0 +1,21 @@ +import { isDingdingMiniapp } from 'universal-env'; +import {Callback, RejectCallback} from '../types'; + +export const onError = (cb: Callback) => { + return isDingdingMiniapp ? dd.onError(cb) : my.onError(cb); +}; +export const offError = (cb?: Callback) => { + return isDingdingMiniapp ? dd.offError(cb) : my.offError(cb); +}; +export const onUnhandledRejection = (cb: RejectCallback) => { + return isDingdingMiniapp ? dd.onUnhandledRejection(cb) : my.onUnhandledRejection(cb); +}; +export const offUnhandledRejection = (cb?: RejectCallback) => { + return isDingdingMiniapp ? dd.offUnhandledRejection(cb) : my.offUnhandledRejection(cb); +}; +export default { + onError, + offError, + onUnhandledRejection, + offUnhandledRejection, +}; \ No newline at end of file diff --git a/packages/application/error-handler/src/byte-miniapp/index.ts b/packages/application/error-handler/src/byte-miniapp/index.ts new file mode 100644 index 00000000..a4c40a51 --- /dev/null +++ b/packages/application/error-handler/src/byte-miniapp/index.ts @@ -0,0 +1,20 @@ +import {Callback, RejectCallback} from '../types'; + +export const onError = (cb: Callback) => { + return tt.onError(cb); +}; +export const offError = (cb?: Callback) => { + return tt.offError(cb); +}; +export const onUnhandledRejection = (cb: RejectCallback) => { + return tt.onUnhandledRejection(cb); +}; +export const offUnhandledRejection = (cb?: RejectCallback) => { + return tt.offUnhandledRejection(cb); +}; +export default { + onError, + offError, + onUnhandledRejection, + offUnhandledRejection, +}; \ No newline at end of file diff --git a/packages/application/error-handler/src/index.ts b/packages/application/error-handler/src/index.ts new file mode 100644 index 00000000..45d729e1 --- /dev/null +++ b/packages/application/error-handler/src/index.ts @@ -0,0 +1,70 @@ +import { isMiniApp, isWeChatMiniProgram, isWeb, isByteDanceMicroApp } from 'universal-env'; +import aliMiniAppModule from './ali-miniapp/index'; +import weChatModule from './wechat-miniapp/index'; +import bytedanceModule from './byte-miniapp/index'; +import webModule from './web/index'; +import {Callback, RejectCallback, ErrorEvent} from './types'; + +export const onError = (cb: Callback) => { + if (isWeChatMiniProgram) { + return weChatModule.onError(cb); + } else if (isByteDanceMicroApp) { + return bytedanceModule.onError(cb); + } else if (isMiniApp) { + return aliMiniAppModule.onError(cb); + } else if (isWeb) { + return webModule.onError(cb); + } else { + throw new Error('universal-api:ErrorEvent暂不支持'); + } +}; +export const offError = (cb: () => void) => { + if (isWeChatMiniProgram) { + return weChatModule.offError(cb); + } else if (isByteDanceMicroApp) { + return bytedanceModule.offError(cb); + } else if (isMiniApp) { + return aliMiniAppModule.offError(cb); + } else if (isWeb) { + return webModule.offError(cb); + } else { + throw new Error('universal-api:ErrorEvent暂不支持'); + } +}; +export const onUnhandledRejection = (cb: RejectCallback) => { + if (isWeChatMiniProgram) { + return weChatModule.onUnhandledRejection(cb); + } else if (isByteDanceMicroApp) { + return bytedanceModule.onUnhandledRejection(cb); + } else if (isMiniApp) { + return aliMiniAppModule.onUnhandledRejection(cb); + } else if (isWeb) { + return webModule.onUnhandledRejection(cb); + } else { + throw new Error('universal-api:ErrorEvent暂不支持'); + } +}; +export const offUnhandledRejection = (cb?: RejectCallback) => { + if (isWeChatMiniProgram) { + return weChatModule.offUnhandledRejection(cb); + } else if (isByteDanceMicroApp) { + return bytedanceModule.offUnhandledRejection(cb); + } else if (isMiniApp) { + return aliMiniAppModule.offUnhandledRejection(cb); + } else if (isWeb) { + return webModule.offUnhandledRejection(cb); + } else { + throw new Error('universal-api:ErrorEvent暂不支持'); + } +}; +let res: ErrorEvent; +if (isWeChatMiniProgram) { + res = weChatModule; +} else if (isByteDanceMicroApp) { + res = bytedanceModule; +} else if (isMiniApp) { + res = aliMiniAppModule; +} else { + throw new Error('universal-api:ErrorEvent暂不支持'); +} +export default res; \ No newline at end of file diff --git a/packages/application/error-handler/src/types.ts b/packages/application/error-handler/src/types.ts new file mode 100644 index 00000000..2d1054eb --- /dev/null +++ b/packages/application/error-handler/src/types.ts @@ -0,0 +1,17 @@ +export interface RejectCallbackArgs { + reason: string | Error | Event; + promise: Promise; +} +export interface Callback { + (res: string | Error | Event): void; +} +export interface RejectCallback { + (res: RejectCallbackArgs): void; +} + +export interface ErrorEvent{ + onError(callback: Callback): void; + offError(callback?: Callback): void; + onUnhandledRejection(callback: RejectCallback): void; + offUnhandledRejection(callback?: RejectCallback): void; +} diff --git a/packages/application/error-handler/src/web/index.ts b/packages/application/error-handler/src/web/index.ts new file mode 100644 index 00000000..75b58a46 --- /dev/null +++ b/packages/application/error-handler/src/web/index.ts @@ -0,0 +1,20 @@ +import {Callback, RejectCallback} from '../types'; + +export const onError = (cb: Callback) => { + return window.addEventListener('error', cb); +}; +export const offError = (cb?: Callback) => { + return window.removeEventListener('error', cb); +}; +export const onUnhandledRejection = (cb: RejectCallback) => { + return window.addEventListener('unhandledrejection', cb); +}; +export const offUnhandledRejection = (cb?: RejectCallback) => { + return window.removeEventListener('unhandledrejection', cb); +}; +export default { + onError, + offError, + onUnhandledRejection, + offUnhandledRejection, +}; \ No newline at end of file diff --git a/packages/application/error-handler/src/wechat-miniapp/index.ts b/packages/application/error-handler/src/wechat-miniapp/index.ts new file mode 100644 index 00000000..b4eba01b --- /dev/null +++ b/packages/application/error-handler/src/wechat-miniapp/index.ts @@ -0,0 +1,20 @@ +import {Callback, RejectCallback} from '../types'; + +export const onError = (cb: Callback) => { + return wx.onError(cb); +}; +export const offError = (cb?: Callback) => { + return wx.offError(cb); +}; +export const onUnhandledRejection = (cb: RejectCallback) => { + return wx.onUnhandledRejection(cb); +}; +export const offUnhandledRejection = (cb?: RejectCallback) => { + return wx.offUnhandledRejection(cb); +}; +export default { + onError, + offError, + onUnhandledRejection, + offUnhandledRejection, +}; \ No newline at end of file diff --git a/packages/base/getApp/README.en.US.md b/packages/base/getApp/README.en.US.md index 97e02615..7b0dc1fa 100644 --- a/packages/base/getApp/README.en.US.md +++ b/packages/base/getApp/README.en.US.md @@ -1,5 +1,44 @@ +--- +group: + title: Base +title: getApp +--- + # getApp -[![npm](https://img.shields.io/npm/v/evapi.svg)](https://www.npmjs.com/package/evapi) -[![npm](https://img.shields.io/npm/v/evapi-getApp.svg)](https://www.npmjs.com/package/evapi-getApp) +[![npm](https://img.shields.io/npm/v/universal-api.svg)](https://www.npmjs.com/package/universal-api) +[![npm](https://img.shields.io/npm/v/universal-getapp.svg)](https://www.npmjs.com/package/universal-getapp) + +Gets the globally unique App instance of the Mini Program. + +## Support + +browser miniApp wechatMiniprogram bytedanceMicroApp + +## Install + +```bash +$ npm install universal-getapp --save +``` + +## Usage + +```js +import { createElement, useEffect, Fragment } from 'rax'; +import getApp from 'universal-getapp'; + +function App() { + useEffect(() => { + const app = getApp(); + }, []); + + return (<> + + ) +} +``` +It can also be imported from the big package: +```js +import {getApp} from 'universal-api'; +``` \ No newline at end of file diff --git a/packages/base/getApp/README.md b/packages/base/getApp/README.md index 97e02615..3d4d07fc 100644 --- a/packages/base/getApp/README.md +++ b/packages/base/getApp/README.md @@ -1,5 +1,44 @@ +--- +group: + title: 基础 +title: getApp +--- + # getApp -[![npm](https://img.shields.io/npm/v/evapi.svg)](https://www.npmjs.com/package/evapi) -[![npm](https://img.shields.io/npm/v/evapi-getApp.svg)](https://www.npmjs.com/package/evapi-getApp) +[![npm](https://img.shields.io/npm/v/universal-api.svg)](https://www.npmjs.com/package/universal-api) +[![npm](https://img.shields.io/npm/v/universal-getapp.svg)](https://www.npmjs.com/package/universal-getapp) + +获取应用实例 + +## 支持 + +browser miniApp wechatMiniprogram bytedanceMicroApp + +## 安装 + +```bash +$ npm install universal-getapp --save +``` + +## 使用 + +```js +import { createElement, useEffect, Fragment } from 'rax'; +import getApp from 'universal-getapp'; + +function App() { + useEffect(() => { + const app = getApp(); + }, []); + + return (<> + + ) +} +``` +也可以从大包中引入: +```js +import {getApp} from 'universal-api'; +``` \ No newline at end of file diff --git a/packages/base/getApp/demo/index.tsx b/packages/base/getApp/demo/index.tsx new file mode 100644 index 00000000..fd912ebd --- /dev/null +++ b/packages/base/getApp/demo/index.tsx @@ -0,0 +1,19 @@ +import { createElement, useEffect, useState, useRef } from 'rax'; +import View from 'rax-view'; +import Text from 'rax-text'; +import getApp from 'universal-getapp'; +import {showToast} from 'universal-toast'; + +export default function() { + return ( + + errorEvent + { + getApp().globalData = 'I am global data'; + }}>设置全局属性 + { + showToast(getApp().globalData); + }}>获取全局属性 + + ); +} diff --git a/packages/base/getCurrentPages/README.en.US.md b/packages/base/getCurrentPages/README.en.US.md index e4f41edb..c141f0a7 100644 --- a/packages/base/getCurrentPages/README.en.US.md +++ b/packages/base/getCurrentPages/README.en.US.md @@ -1,5 +1,49 @@ +--- +group: + title: Base +title: getCurrentPages +--- + # getCurrentPages -[![npm](https://img.shields.io/npm/v/evapi.svg)](https://www.npmjs.com/package/evapi) -[![npm](https://img.shields.io/npm/v/evapi-getApp.svg)](https://www.npmjs.com/package/evapi-getCurrentPages) +[![npm](https://img.shields.io/npm/v/universal-api.svg)](https://www.npmjs.com/package/universal-api) +[![npm](https://img.shields.io/npm/v/universal-getcurrentpages.svg)](https://www.npmjs.com/package/universal-getCurrentPages) + +Gets the current page stack. In the array, the first element is the homepage and the last element is the current page. + +## Support + +browser miniApp wechatMiniprogram bytedanceMicroApp + +## Install + +```bash +$ npm install universal-getcurrentpages --save +``` + +## Usage + +```js +import { createElement, useEffect, Fragment } from 'rax'; +import getCurrentPages from 'universal-getcurrentpages'; + +function App() { + useEffect(() => { + const app = getCurrentPages(); + }, []); + + return (<> + + ) +} +``` +It can also be imported from the big package: + +```js +import {getCurrentPages} from 'universal-api'; +``` + +## Note: +- Do not modify the page stack as this will lead to routing and page status errors. +- Do not call getCurrentPages() when App.onLaunch is called since page is not generated. \ No newline at end of file diff --git a/packages/base/getCurrentPages/README.md b/packages/base/getCurrentPages/README.md index e4f41edb..c2258764 100644 --- a/packages/base/getCurrentPages/README.md +++ b/packages/base/getCurrentPages/README.md @@ -1,5 +1,49 @@ +--- +group: + title: 基础 +title: getCurrentPages +--- + # getCurrentPages -[![npm](https://img.shields.io/npm/v/evapi.svg)](https://www.npmjs.com/package/evapi) -[![npm](https://img.shields.io/npm/v/evapi-getApp.svg)](https://www.npmjs.com/package/evapi-getCurrentPages) +[![npm](https://img.shields.io/npm/v/universal-api.svg)](https://www.npmjs.com/package/universal-api) +[![npm](https://img.shields.io/npm/v/universal-getcurrentpages.svg)](https://www.npmjs.com/package/universal-getCurrentPages) + +获取当前页面栈。数组中第一个元素为首页,最后一个元素为当前页面。 + +## 支持 + +browser miniApp wechatMiniprogram bytedanceMicroApp + +## 安装 + +```bash +$ npm install universal-getcurrentpages --save +``` + +## 使用 + +```js +import { createElement, useEffect, Fragment } from 'rax'; +import getCurrentPages from 'universal-getcurrentpages'; + +function App() { + useEffect(() => { + const app = getCurrentPages(); + }, []); + + return (<> + + ) +} +``` +也可以从大包中引入: + +```js +import {getCurrentPages} from 'universal-api'; +``` + +## 注意: +- 不要尝试修改页面栈,会导致路由以及页面状态错误。 +- 不要在 App.onLaunch 的时候调用 getCurrentPages(),此时 page 还没有生成。 \ No newline at end of file diff --git a/packages/base/getCurrentPages/demo/index.tsx b/packages/base/getCurrentPages/demo/index.tsx new file mode 100644 index 00000000..035baef4 --- /dev/null +++ b/packages/base/getCurrentPages/demo/index.tsx @@ -0,0 +1,28 @@ +import { createElement, useEffect, useState, useRef } from 'rax'; +import View from 'rax-view'; +import Text from 'rax-text'; +import getCurrentPages from 'universal-getcurrentpages'; + +export default function() { + const [data, setData] = useState({ + firstPage: {}, + currentPage: {} + }); + return ( + + getCurrentPages + { + let pageInfo = getCurrentPages(); + setData({ + firstPage: { + viewId: pageInfo[0].$viewId, + }, + currentPage: { + viewId: pageInfo[pageInfo.length - 1].$viewId, + } + }); + }}>获取页面堆栈信息 + 当前页面id是:{data.currentPage.viewId} + + ); +} diff --git a/packages/equipment/accelerometer/src/index.ts b/packages/equipment/accelerometer/src/index.ts index 6584b3ae..879a84e1 100644 --- a/packages/equipment/accelerometer/src/index.ts +++ b/packages/equipment/accelerometer/src/index.ts @@ -15,7 +15,7 @@ export const onChange = (cb: Callback) => { throw new Error('universal-api:Accelerometer暂不支持'); } }; -export const offChange = (cb: () => void) => { +export const offChange = (cb?: Callback) => { if (isWeChatMiniProgram) { return weChatModule.offChange(cb); } else if (isByteDanceMicroApp) { diff --git a/packages/interface/alert/src/ali-miniapp/index.ts b/packages/interface/alert/src/ali-miniapp/index.ts index cd6619e8..48db163c 100644 --- a/packages/interface/alert/src/ali-miniapp/index.ts +++ b/packages/interface/alert/src/ali-miniapp/index.ts @@ -1,22 +1,22 @@ import { Options } from '../types'; +import {initApi} from '../common'; -declare const my: any; - -const alert = (options: Options): Promise => { - return new Promise((resolve, reject): void => { - const { title, content, buttonText = '确定' } = options; - my.alert({ - title, - content, - buttonText, - success: (): void => { - resolve(); - }, - fail: (): void => { - reject(); - } - }); +const alert = (options: Options): void => { + const { title, content, buttonText, success, fail, complete } = options; + my.alert({ + title, + content, + buttonText, + success: function() { + success && success(); + }, + fail: function(res) { + fail && fail(res); + }, + complete(res) { + complete && complete(res); + } }); }; -export default alert; +export default initApi(alert); diff --git a/packages/interface/alert/src/byte-miniapp/index.ts b/packages/interface/alert/src/byte-miniapp/index.ts new file mode 100644 index 00000000..e16d1ef2 --- /dev/null +++ b/packages/interface/alert/src/byte-miniapp/index.ts @@ -0,0 +1,23 @@ +import { Options } from '../types'; +import {initApi} from '../common'; + +const alert = (options: Options): void => { + const { title, content, buttonText, success, fail, complete } = options; + tt.showModal({ + showCancel: false, + title, + content, + confirmText: buttonText, + success: function(res) { + success && success(res); + }, + fail: function(res) { + fail && fail(res); + }, + complete(res) { + complete && complete(res); + } + }); +}; + +export default initApi(alert); diff --git a/packages/interface/alert/src/bytedance-microapp/index.ts b/packages/interface/alert/src/bytedance-microapp/index.ts deleted file mode 100644 index ae72fc37..00000000 --- a/packages/interface/alert/src/bytedance-microapp/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Options } from '../types'; - -declare const tt: any; - -const alert = (options: Options): Promise => { - return new Promise((resolve, reject): void => { - const { title = '', content = '', buttonText = '确定' } = options; - tt.showModal({ - showCancel: false, - ...options, - title, - content, - confirmText: buttonText, - success: (result): void => { - resolve(result); - }, - fail: (): void => { - reject(); - } - }); - }); -}; - -export default alert; diff --git a/packages/interface/alert/src/common.ts b/packages/interface/alert/src/common.ts new file mode 100644 index 00000000..00fc562a --- /dev/null +++ b/packages/interface/alert/src/common.ts @@ -0,0 +1,18 @@ +import { promisify } from '../../../utils/promisify'; + +export function styleOptions(options) { + const DEFAULT_REQUEST_OPTIONS = { + content: '', + title: '', + buttonText: '确定', + }; + return Object.assign({}, + DEFAULT_REQUEST_OPTIONS, + options); +} +export function initApi(api) { + return (options) => { + const afterOptions = styleOptions(options); + return promisify(api)(afterOptions); + }; +} \ No newline at end of file diff --git a/packages/interface/alert/src/quickapp/index.ts b/packages/interface/alert/src/quickapp/index.ts deleted file mode 100644 index 7c0277b3..00000000 --- a/packages/interface/alert/src/quickapp/index.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { Options } from '../types'; -import { isQuickApp } from 'universal-env'; -import otherModule from '../index'; - -let exportModule = {}; - -const alert = (options: Options = {}): Promise => { - const { title = '', content = '', buttonText = '确定' } = options; - const buttons = [ - { - text: buttonText - } - ]; - return new Promise((resolve, reject) => { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const prompt = require('@system.prompt'); - prompt.showDialog({ - title, - message: content, - buttons, - autocancel: false, - success: (): void => { - resolve(); - }, - cancel: (): void => { - reject(); - } - }); - }); -}; - -if (isQuickApp) { - exportModule = alert; -} else { - exportModule = otherModule; -} - -export default exportModule; diff --git a/packages/interface/alert/src/wechat-miniapp/index.ts b/packages/interface/alert/src/wechat-miniapp/index.ts new file mode 100644 index 00000000..5c66c463 --- /dev/null +++ b/packages/interface/alert/src/wechat-miniapp/index.ts @@ -0,0 +1,23 @@ +import { Options } from '../types'; +import {initApi} from '../common'; + +const alert = (options: Options): void => { + const { title, content, buttonText, success, fail, complete } = options; + wx.showModal({ + showCancel: false, + title, + content, + confirmText: buttonText, + success: function(res) { + success && success(res); + }, + fail: function(res) { + fail && fail(res); + }, + complete(res) { + complete && complete(res); + } + }); +}; + +export default initApi(alert); diff --git a/packages/interface/alert/src/wechat-miniprogram/index.ts b/packages/interface/alert/src/wechat-miniprogram/index.ts deleted file mode 100644 index eeac5c59..00000000 --- a/packages/interface/alert/src/wechat-miniprogram/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Options } from '../types'; - -declare const wx: any; - -const alert = (options: Options): Promise => { - return new Promise((resolve, reject): void => { - const { title = '', content = '', buttonText = '确定' } = options; - wx.showModal({ - showCancel: false, - ...options, - title, - content, - confirmText: buttonText, - success: (result): void => { - resolve(result); - }, - fail: (): void => { - reject(); - } - }); - }); -}; - -export default alert; diff --git a/packages/interface/alert/src/weex/index.ts b/packages/interface/alert/src/weex/index.ts deleted file mode 100644 index 8e49bc02..00000000 --- a/packages/interface/alert/src/weex/index.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Options } from '../types'; - -declare const weex: any; - -const alert = (options: Options): Promise => { - return new Promise((resolve): void => { - const modal = weex.requireModule('modal'); - const { content, buttonText } = options; - modal.alert({ - message: content, - okTitle: buttonText - }, function(): void { - resolve(); - }); - }); -}; - -export default alert; diff --git a/packages/interface/showModal/src/ali-miniapp/index.ts b/packages/interface/showModal/src/ali-miniapp/index.ts index 0d833878..4568fa74 100644 --- a/packages/interface/showModal/src/ali-miniapp/index.ts +++ b/packages/interface/showModal/src/ali-miniapp/index.ts @@ -6,9 +6,9 @@ const alert = isDingdingMiniapp ? dd.alert : my.alert; const showModal = (args) => { if (args.showCancel) { - confirm(args); + confirm({...args, confirmButtonText: args.confirmText, cancelButtonText: args.cancelText}); } else { - alert({args, buttonText: args.confirmButtonText}); + alert({...args, buttonText: args.confirmText}); } }; diff --git a/packages/interface/showModal/src/byte-miniapp/index.ts b/packages/interface/showModal/src/byte-miniapp/index.ts index 3dd3d321..5d1a8ed8 100644 --- a/packages/interface/showModal/src/byte-miniapp/index.ts +++ b/packages/interface/showModal/src/byte-miniapp/index.ts @@ -1,3 +1,3 @@ import { initApi } from '../common'; -export default initApi(tt.showModal) +export default initApi(tt.showModal); diff --git a/packages/interface/showModal/src/common.ts b/packages/interface/showModal/src/common.ts index e7245eab..0975a68a 100644 --- a/packages/interface/showModal/src/common.ts +++ b/packages/interface/showModal/src/common.ts @@ -4,37 +4,30 @@ import { ShowModalOptions, showModalRes } from './types'; /** * modal返回值格式化 - * @param res + * @param res */ const formatResponse = (res): showModalRes => { // success用于兼容支付宝alert - const confirm = (res && (res.confirm || res.success)) ? true : false; + const confirm = res && (res.confirm || res.success) ? true : false; const result = { confirm, cancel: !confirm - } + }; return result; -} +}; /** * modal参数格式化 - * @param options + * @param options */ function styleOptions(options: ShowModalOptions) { const args: any = { title: options.title || '', content: options.content || '', - showCancel: options.showCancel || true - } - const defaultConfirmText = '确定'; - const defaultCnacelText = '取消'; - if (isDingdingMiniapp || isMiniApp) { - args.confirmButtonText = options.confirmText || defaultConfirmText; - args.cancelButtonText = options.cancelText || defaultCnacelText; - } else { - args.confirmText = options.confirmText || defaultConfirmText; - args.cancelText = options.cancelText || defaultCnacelText; - } + showCancel: options.showCancel || true, + confirmText: options.confirmText || '确定', + cancelText: options.confirmText || '取消', + }; return { ...args, success: res => { @@ -46,7 +39,7 @@ function styleOptions(options: ShowModalOptions) { fail: err => { options.fail && options.fail(err); } - } + }; } export function initApi(api) { diff --git a/packages/interface/showModal/src/wechat-miniapp/index.ts b/packages/interface/showModal/src/wechat-miniapp/index.ts index cab648bb..b40128e3 100644 --- a/packages/interface/showModal/src/wechat-miniapp/index.ts +++ b/packages/interface/showModal/src/wechat-miniapp/index.ts @@ -1,3 +1,3 @@ import { initApi } from '../common'; -export default initApi(wx.showModal) +export default initApi(wx.showModal); diff --git a/packages/interface/toast/README.en-US.md b/packages/interface/toast/README.en-US.md index 006a2386..8c782729 100644 --- a/packages/interface/toast/README.en-US.md +++ b/packages/interface/toast/README.en-US.md @@ -7,7 +7,7 @@ title: toast Universal toast, you can choose how many seconds after disappearing ## Support -browser weex miniApp wechatMiniprogram bytedanceMicroApp quickApp +browser miniApp wechatMiniprogram bytedanceMicroApp ## Install @@ -20,19 +20,23 @@ $ npm install universal-toast --save ```js import Toast from 'universal-toast'; -// How to use it in quickapp -// import Toast from 'universal-toast/lib/quickapp; - Toast.show('Hi'); ``` ## Methods -### `Toast.show(message: string, duration?: number, options?: object)` +### `Toast.show(options: object | string)` #### Arguments | Property | Type | Description | Default | | -------- | -------- | ------------------------------------------- | :-----: | | message | `string` | content of the message | - | +| type | `success | fail | none` | Type of popup | none | | duration | `number` | Time in millisecond before Toast is closed. | 2000 | -| options | `object` | Other options used by WeChat MiniProgram/ByteDance MicroApp, see [docs](https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html) | {} | +| success | `function` | The callback function for a successful API call | - | +| fail | `function` | The callback function for a failed API call | - | +| complete | `function` | The callback function used when the API call completed (always executed whether the call succeeds or fails) | - | + +### `Toast.hide()` + +Hides the loading prompt box \ No newline at end of file diff --git a/packages/interface/toast/README.md b/packages/interface/toast/README.md index 5a4683b4..059717f8 100644 --- a/packages/interface/toast/README.md +++ b/packages/interface/toast/README.md @@ -7,7 +7,7 @@ title: toast 通用弱提示,可选择多少秒之后消失 ## 支持 -browser weex miniApp wechatMiniprogram bytedanceMicroApp quickApp +browser miniApp wechatMiniprogram bytedanceMicroApp ## 安装 @@ -30,18 +30,18 @@ Toast.hide(); ## 方法 -### `Toast.show(message: string, duration?: number, options?: object)` +### `Toast.show(options?: object)` #### 参数 | 成员 | 类型 | 描述 | 默认值 | | -------- | -------- | ------------------- | :----: | | message | `string` | 文字内容 | - | +| type | `success | fail | none` | 弹窗类型 | none | | duration | `number` | 显示时长,单位为 ms | 2000 | -| options | `object` | 其他参数,微信小程序/字节跳动小程序平台可用配置,参考[文档](https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html) | {} | +| success | `function` | 成功回调 | - | +| fail | `function` | 失败回调 | - | +| complete | `function` | 结束回调 | - | ### `Toast.hide()` -#### 支持 -browser miniApp wechatMiniprogram bytedanceMicroApp - 隐藏Toast diff --git a/packages/interface/toast/demo/index.tsx b/packages/interface/toast/demo/index.tsx index 6b379c65..33924b5b 100644 --- a/packages/interface/toast/demo/index.tsx +++ b/packages/interface/toast/demo/index.tsx @@ -1,35 +1,29 @@ /* eslint-disable import/no-extraneous-dependencies */ // @ts-ignore -import { createElement, render } from 'rax'; -import DriverUniversal from 'driver-universal'; +import { createElement } from 'rax'; import View from 'rax-view'; import Text from 'rax-text'; -import Toast from '../src/'; +import Toast from 'universal-toast'; -const App = () => { - let count = 0; - const handleClick = () => { - Toast.show('Hi ' + count++); - }; - const handleShowLong = () => { - Toast.show('A long time toast', 300000); - }; - const handleHide = () => { - Toast.hide(); - }; +const Index = () => { return ( - - click it! + Toast.show('默认提示')}> + 点击弹出普通弹窗 - - show a long time toast + Toast.show({type: 'success', message: 'success提示'})}> + 点击弹出success弹窗 - - hide toast + Toast.show({type: 'fail', message: 'fail提示'})}> + 点击弹出fail弹窗 + + Toast.show({type: 'none', message: '5s提示', duration: 5000})}> + 点击弹出5s弹窗 + + Toast.hide()}> + 点击关闭弹窗 ); }; - -render(, document.body, { driver: DriverUniversal }); +export default Index; \ No newline at end of file diff --git a/packages/interface/toast/package.json b/packages/interface/toast/package.json deleted file mode 100644 index c0b5c11d..00000000 --- a/packages/interface/toast/package.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "name": "universal-toast", - "author": "rax", - "version": "1.2.3", - "description": "", - "main": "lib/index.js", - "files": [ - "src", - "lib", - "dist" - ], - "scripts": { - "start": "build-scripts start", - "build": "build-scripts build" - }, - "pre-commit": [ - "lint" - ], - "keywords": [ - "Rax", - "rax-component" - ], - "engines": { - "npm": ">=3.0.0" - }, - "peerDependencies": { - "rax": "^1.1.0" - }, - "dependencies": { - "universal-env": "^3.1.0" - }, - "devDependencies": { - "@alib/build-scripts": "^0.1.0", - "@rax-types/rax": "^1.0.6", - "build-plugin-rax-component": "^0.2.0", - "driver-universal": "^3.1.0", - "rax": "^1.1.0", - "rax-test-renderer": "^1.0.0", - "rax-text": "^1.3.1", - "rax-view": "^1.1.2", - "typescript": "^3.7.5" - } -} diff --git a/packages/interface/toast/src/__tests__/index.mp.ts b/packages/interface/toast/src/__tests__/index.mp.ts deleted file mode 100644 index 1a2bfcdd..00000000 --- a/packages/interface/toast/src/__tests__/index.mp.ts +++ /dev/null @@ -1,22 +0,0 @@ -import Toast from '../index'; - -jest.mock('universal-env', (): object => { - return { - isWeex: false, - isMiniApp: true, - isWeb: false - }; -}); - -describe('Toast in miniapp', (): void => { - it('show works with call', (): void => { - const mockShow = jest.fn(); - // eslint-disable-next-line - (global as any).my = { - showToast: mockShow - }; - - Toast.show('Hello World'); - expect(mockShow.mock.calls.length).toBe(1); - }); -}); diff --git a/packages/interface/toast/src/__tests__/index.quickapp.ts b/packages/interface/toast/src/__tests__/index.quickapp.ts deleted file mode 100644 index 49c8ddc3..00000000 --- a/packages/interface/toast/src/__tests__/index.quickapp.ts +++ /dev/null @@ -1,23 +0,0 @@ -import Toast from '../index'; - -jest.mock('universal-env', (): object => { - return { - isWeex: false, - isMiniApp: false, - isWeb: false, - isQuickApp: true - }; -}); - -describe('Toast in quickapp', (): void => { - it('show works with call', (): void => { - const mockShow = jest.fn(); - // eslint-disable-next-line - (global as any).my = { - showToast: mockShow - }; - - Toast.show('Hello World'); - expect(mockShow.mock.calls.length).toBe(1); - }); -}); diff --git a/packages/interface/toast/src/__tests__/index.ts b/packages/interface/toast/src/__tests__/index.ts deleted file mode 100644 index 17175e08..00000000 --- a/packages/interface/toast/src/__tests__/index.ts +++ /dev/null @@ -1,49 +0,0 @@ -import Toast from '../index'; - -jest.mock('universal-env', (): object => { - return { - isWeex: false, - isWeb: true - }; -}); - -jest.useFakeTimers(); - -describe('default duration', () => { - Toast.show('Hello'); - - it('show toast', () => { - expect(setTimeout.mock.calls.length).toBe(1); - expect(setTimeout.mock.calls[0][1]).toBe(Toast.SHORT); - }); - - it('hide toast window', () => { - jest.runOnlyPendingTimers(); - - expect(setTimeout.mock.calls.length).toBe(3); - expect(setTimeout.mock.calls[1][1]).toBe(0); - }); - - it('show next toast in queue', () => { - jest.runOnlyPendingTimers(); - - expect(setTimeout.mock.calls.length).toBe(3); - expect(setTimeout.mock.calls[2][1]).toBe(600); - }); -}); - -it('long duration', () => { - Toast.show('Hello', Toast.LONG); - - expect(setTimeout.mock.calls.length).toBe(4); - expect(setTimeout.mock.calls[3][1]).toBe(Toast.LONG); -}); - -it('toast twice', () => { - Toast.show('Hello', Toast.LONG); - Toast.show('Hello', Toast.LONG); - - jest.runAllTimers(); - - expect(setTimeout.mock.calls.length).toBe(12); -}); diff --git a/packages/interface/toast/src/__tests__/index.weex.ts b/packages/interface/toast/src/__tests__/index.weex.ts deleted file mode 100644 index ba98e2d7..00000000 --- a/packages/interface/toast/src/__tests__/index.weex.ts +++ /dev/null @@ -1,26 +0,0 @@ -import Toast from '../index'; - -jest.mock('universal-env', (): object => { - return { - isWeex: true, - isWeb: false - }; -}); -// eslint-disable-next-line -(global as any).weex = { - requireModule: require -}; - -describe('Toast in weex', (): void => { - it('show works with call', (): void => { - const mockShow = jest.fn(); - jest.mock('modal', (): object => { - return { - toast: mockShow - }; - }, {virtual: true}); - - Toast.show('Hello World'); - expect(mockShow.mock.calls.length).toBe(1); - }); -}); diff --git a/packages/interface/toast/src/ali-miniapp/index.ts b/packages/interface/toast/src/ali-miniapp/index.ts new file mode 100644 index 00000000..0872fde5 --- /dev/null +++ b/packages/interface/toast/src/ali-miniapp/index.ts @@ -0,0 +1,39 @@ +import {LONG_DELAY, SHORT_DELAY, initApi} from '../utils/index'; +import { ToastOption, ShowToastOption, HideToastOption } from '../types'; +import { isDingdingMiniapp } from 'universal-env'; + +let Toast: ToastOption = { + SHORT: SHORT_DELAY, + LONG: LONG_DELAY, + + /* + * @param message {String} + * @param duration {Number} + * @param userStyle {Object} user defined style + */ + show: initApi((options: ShowToastOption): void => { + let { type, content, duration, success, fail, complete } = options; + const showToast = isDingdingMiniapp ? dd.showToast : my.showToast; + showToast({ + type, + content, + duration, + success: function() { + success && success(); + }, + fail: function(res) { + fail && fail(res); + }, + complete(res) { + complete && complete(res); + } + }); + }), + + hide: initApi((options?: HideToastOption): void => { + const hideToast = isDingdingMiniapp ? dd.hideToast : my.hideToast; + hideToast(options); + }) +}; + +export default Toast; diff --git a/packages/interface/toast/src/byte-miniapp/index.ts b/packages/interface/toast/src/byte-miniapp/index.ts new file mode 100644 index 00000000..aa85d5aa --- /dev/null +++ b/packages/interface/toast/src/byte-miniapp/index.ts @@ -0,0 +1,41 @@ +import {LONG_DELAY, SHORT_DELAY, initApi} from '../utils/index'; +import { ToastOption, ShowToastOption, HideToastOption } from '../types'; + +let Toast: ToastOption = { + SHORT: SHORT_DELAY, + LONG: LONG_DELAY, + + /* + * @param message {String} + * @param duration {Number} + * @param userStyle {Object} user defined style + */ + show: initApi((options: ShowToastOption): void => { + let { type, content, duration, success, fail, complete } = options; + let iconMap = { + success: 'success', + fail: 'error', + none: '' + }; + tt.showToast({ + icon: iconMap[type] || '', + title: content, + duration, + success: function() { + success && success(); + }, + fail: function(res) { + fail && fail(res); + }, + complete(res) { + complete && complete(res); + } + }); + }), + + hide: initApi((options?: HideToastOption): void => { + tt.hideToast(options); + }) +}; + +export default Toast; diff --git a/packages/interface/toast/src/index.ts b/packages/interface/toast/src/index.ts index 50616033..86589458 100644 --- a/packages/interface/toast/src/index.ts +++ b/packages/interface/toast/src/index.ts @@ -1,27 +1,42 @@ -import { isWeex, isMiniApp, isWeb, isWeChatMiniProgram, isByteDanceMicroApp } from 'universal-env'; -import webModule from './web/index'; -import weexModule from './weex/index'; -import miniAppModule from './miniapp/ali-miniapp/index'; -import weChatModule from './miniapp/wechat-miniprogram/index'; -import byteDanceModule from './miniapp/bytedance-microapp/index'; -import { ToastOption } from './types'; +import { isMiniApp, isWeChatMiniProgram, isByteDanceMicroApp } from 'universal-env'; +import aliMiniAppModule from './ali-miniapp/index'; +import weChatModule from './wechat-miniapp/index'; +import bytedanceModule from './byte-miniapp/index'; +import {ShowToastOption, HideToastOption, ToastOption} from './types'; +import {LONG_DELAY, SHORT_DELAY} from './utils/index'; -let Toast: ToastOption = {} as any; - -if (isWeb) { - // Aviod includes wechat or alipay sdk - Toast = webModule; -} else if (isWeex) { - Toast = weexModule; -} else if (isMiniApp) { - Toast = miniAppModule; -} else if (isWeChatMiniProgram) { - Toast = weChatModule; +export const LONG = LONG_DELAY; +export const SHORT = SHORT_DELAY; +export const show = (options: ShowToastOption) => { + if (isWeChatMiniProgram) { + return weChatModule.show(options); + } else if (isByteDanceMicroApp) { + return bytedanceModule.show(options); + } else if (isMiniApp) { + return aliMiniAppModule.show(options); + } else { + throw new Error('universal-api:Toast暂不支持'); + } +}; +export const hide = (options?: HideToastOption) => { + if (isWeChatMiniProgram) { + return weChatModule.hide(options); + } else if (isByteDanceMicroApp) { + return bytedanceModule.hide(options); + } else if (isMiniApp) { + return aliMiniAppModule.hide(options); + } else { + throw new Error('universal-api:Toast暂不支持'); + } +}; +let res: ToastOption; +if (isWeChatMiniProgram) { + res = weChatModule; } else if (isByteDanceMicroApp) { - Toast = byteDanceModule; + res = bytedanceModule; +} else if (isMiniApp) { + res = aliMiniAppModule; } else { - // Web as default - Toast = webModule; + throw new Error('universal-api:Toast暂不支持'); } - -export default Toast; +export default res; \ No newline at end of file diff --git a/packages/interface/toast/src/miniapp/ali-miniapp/index.ts b/packages/interface/toast/src/miniapp/ali-miniapp/index.ts deleted file mode 100644 index c1682509..00000000 --- a/packages/interface/toast/src/miniapp/ali-miniapp/index.ts +++ /dev/null @@ -1,28 +0,0 @@ -import {LONG_DELAY, SHORT_DELAY} from '../../utils/index'; -import { ToastOption } from '../../types'; - -declare const my: any; - -let Toast: ToastOption = { - SHORT: SHORT_DELAY, - LONG: LONG_DELAY, - - /* - * @param message {String} - * @param duration {Number} - * @param userStyle {Object} user defined style - */ - show(message: string, duration: number = SHORT_DELAY): void { - my.showToast({ - type: 'none', - content: message, - duration - }); - }, - - hide() { - my.hideToast(); - } -}; - -export default Toast; diff --git a/packages/interface/toast/src/miniapp/bytedance-microapp/index.ts b/packages/interface/toast/src/miniapp/bytedance-microapp/index.ts deleted file mode 100644 index 4bf43407..00000000 --- a/packages/interface/toast/src/miniapp/bytedance-microapp/index.ts +++ /dev/null @@ -1,29 +0,0 @@ -import {LONG_DELAY, SHORT_DELAY} from '../../utils/index'; -import { ToastOption } from '../../types'; - -declare const tt: any; - -let Toast: ToastOption = { - SHORT: SHORT_DELAY, - LONG: LONG_DELAY, - - /* - * @param message {String} - * @param duration {Number} - * @param userStyle {Object} user defined style - */ - show(message: string = '', duration: number = SHORT_DELAY, options: object = {}): void { - tt.showToast({ - icon: 'none', - title: message, - duration, - ...options - }); - }, - - hide() { - tt.hideToast(); - } -}; - -export default Toast; diff --git a/packages/interface/toast/src/miniapp/wechat-miniprogram/index.ts b/packages/interface/toast/src/miniapp/wechat-miniprogram/index.ts deleted file mode 100644 index 2b0a8387..00000000 --- a/packages/interface/toast/src/miniapp/wechat-miniprogram/index.ts +++ /dev/null @@ -1,29 +0,0 @@ -import {LONG_DELAY, SHORT_DELAY} from '../../utils/index'; -import { ToastOption } from '../../types'; - -declare const wx: any; - -let Toast: ToastOption = { - SHORT: SHORT_DELAY, - LONG: LONG_DELAY, - - /* - * @param message {String} - * @param duration {Number} - * @param userStyle {Object} user defined style - */ - show(message: string = '', duration: number = SHORT_DELAY, options: object = {}): void { - wx.showToast({ - icon: 'none', - title: message, - duration, - ...options - }); - }, - - hide() { - wx.hideToast(); - } -}; - -export default Toast; diff --git a/packages/interface/toast/src/types.ts b/packages/interface/toast/src/types.ts index 44154678..470b5960 100644 --- a/packages/interface/toast/src/types.ts +++ b/packages/interface/toast/src/types.ts @@ -1,6 +1,28 @@ export interface ToastOption { SHORT: number; LONG: number; - show: (message: string, duration?: number, options?: object) => void; - hide: () => void; + show: (options: ShowToastOption) => void; + hide: (options?: HideToastOption) => void; }; +export interface ShowToastOption { + content: string; + type?: 'success' | 'fail' | 'none'; + duration?: number; + success?: () => any; + fail?: (res) => any; + complete?: (res?) => any; +} +export interface HideToastOption { + success?: () => any; + fail?: (res) => any; + complete?: (res?) => any; +} + +export interface WebQueueOption { + message: string; + duration: number; + icon?: 'success' | 'fail' | 'none'; + success?: () => void; + fail?: (res) => void; + complete?: (res?) => void; +}; \ No newline at end of file diff --git a/packages/interface/toast/src/utils/index.ts b/packages/interface/toast/src/utils/index.ts index 9338d3ac..fcbb8750 100644 --- a/packages/interface/toast/src/utils/index.ts +++ b/packages/interface/toast/src/utils/index.ts @@ -1,2 +1,28 @@ +import { promisify } from '../../../../utils/promisify'; + export const LONG_DELAY = 3500; // 3.5 seconds export const SHORT_DELAY = 2000; // 2 seconds +export function styleOptions(options) { + const DEFAULT_REQUEST_OPTIONS = { + content: '', + type: 'none', + duration: SHORT_DELAY, + }; + if (typeof options === 'string') { + return Object.assign({}, + DEFAULT_REQUEST_OPTIONS, + { + content: options + }); + } else { + return Object.assign({}, + DEFAULT_REQUEST_OPTIONS, + options); + } +} +export function initApi(api) { + return (options?) => { + const afterOptions = styleOptions(options); + return promisify(api)(afterOptions); + }; +} \ No newline at end of file diff --git a/packages/interface/toast/src/web/index.ts b/packages/interface/toast/src/web/index.ts index 3eb0b6dd..1cbac7d9 100644 --- a/packages/interface/toast/src/web/index.ts +++ b/packages/interface/toast/src/web/index.ts @@ -1,14 +1,11 @@ -import { LONG_DELAY, SHORT_DELAY } from '../utils/index'; -import { ToastOption } from '../types'; +import { LONG_DELAY, SHORT_DELAY, initApi } from '../utils/index'; +import { ToastOption, WebQueueOption, ShowToastOption, HideToastOption } from '../types'; -interface QueueOption { - message: string; - duration: number; -}; - -let queue: QueueOption[] = []; +let queue: WebQueueOption[] = []; let isProcessing = false; let toastWin: HTMLElement; +let toastContent: HTMLElement; +let toastIcon: HTMLElement; const styles = { container: { @@ -18,6 +15,10 @@ const styles = { color: '#ffffff', padding: '8px 16px', position: 'fixed', + display: 'flex', + flexDirection: 'column', + alignItems: 'center', + justifyContent: 'center', left: '50%', bottom: '50%', fontSize: '16px', @@ -30,10 +31,14 @@ const styles = { transform: 'translateX(-50%)', webkitTransform: 'translateX(-50%)', zIndex: 9999 + }, + icon: { + marginBottom: '5px', + width: '45px', + height: '45px', } }; - -function showToastWindow(message: string): void { +function showToastWindow(message: string, iconUrl?: string): void { if (!toastWin) { toastWin = document.createElement('div'); toastWin.setAttribute('role', 'alert'); @@ -43,10 +48,21 @@ function showToastWindow(message: string): void { for (let key in styles.container) { toastWin.style[key] = styles.container[key]; } + if (iconUrl) { + toastIcon = document.createElement('img'); + // toastIcon.setAttribute('src', iconUrl); + for (let key in styles.icon) { + toastIcon.style[key] = styles.icon[key]; + } + toastWin.appendChild(toastIcon); + } + toastContent = document.createElement('div'); + toastWin.appendChild(toastContent); + document.body.appendChild(toastWin); } - - toastWin.textContent = message; + toastIcon.setAttribute('src', iconUrl); + toastContent.textContent = message; toastWin.style.transform = 'translateX(-50%)'; toastWin.style.webkitTransform = 'translateX(-50%)'; } @@ -77,15 +93,22 @@ const innerToast = { if (isProcessing) return; isProcessing = true; - let toastInfo: QueueOption = queue.shift() as QueueOption; - showToastWindow(toastInfo.message); - innerToast.hideTimer = setTimeout(() => innerToast.switchToNext(), toastInfo.duration); + let toastInfo: WebQueueOption = queue.shift() as WebQueueOption; + try { + showToastWindow(toastInfo.message, toastInfo.icon); + } catch (e) { + toastInfo.fail && toastInfo.fail(e); + toastInfo.complete && toastInfo.complete(e); + } + + innerToast.hideTimer = setTimeout(() => { + toastInfo.success && toastInfo.success(); + toastInfo.complete && toastInfo.complete(); + innerToast.switchToNext(); + }, toastInfo.duration); }, - push(message: string, duration: number): void { - queue.push({ - message, - duration - }); + push(options): void { + queue.push(options); innerToast.show(); }, // Switch to next message @@ -100,7 +123,7 @@ const innerToast = { innerToast.hideTimer = null; } } -} +}; const Toast: ToastOption = { @@ -112,15 +135,41 @@ const Toast: ToastOption = { * @param duration {Number} * @param userStyle {Object} user defined style */ - show(message: string, duration: number = SHORT_DELAY): void { - innerToast.push(message, duration); - }, - - hide() { + show: initApi((options: ShowToastOption): void => { + const { type, content, duration, success, fail, complete } = options; + const iconMap = { + success: 'https://gw.alicdn.com/imgextra/i1/O1CN01h684sE1Td4mwYyChK_!!6000000002404-2-tps-200-200.png', + fail: 'https://gw.alicdn.com/imgextra/i1/O1CN01yOywus1et4ORJzafk_!!6000000003928-2-tps-200-200.png', + none: '' + }; + innerToast.push({ + message: content, + duration, + icon: iconMap[type] || '', + success: function() { + success && success(); + }, + fail: function(res) { + fail && fail(res); + }, + complete(res) { + complete && complete(res); + } + }); + }), + hide: initApi((options?: HideToastOption): void => { + const { success, fail, complete } = options; // remove all queued messages - queue = []; - innerToast.switchToNext(); - } + try { + queue = []; + innerToast.switchToNext(); + success && success(); + complete && complete(); + } catch (e) { + fail && fail(e); + complete && complete(e); + } + }), }; export default Toast; diff --git a/packages/interface/toast/src/wechat-miniapp/index.ts b/packages/interface/toast/src/wechat-miniapp/index.ts new file mode 100644 index 00000000..0125f8a7 --- /dev/null +++ b/packages/interface/toast/src/wechat-miniapp/index.ts @@ -0,0 +1,41 @@ +import {LONG_DELAY, SHORT_DELAY, initApi} from '../utils/index'; +import { ToastOption, ShowToastOption, HideToastOption } from '../types'; + +let Toast: ToastOption = { + SHORT: SHORT_DELAY, + LONG: LONG_DELAY, + + /* + * @param message {String} + * @param duration {Number} + * @param userStyle {Object} user defined style + */ + show: initApi((options: ShowToastOption): void => { + let { type, content, duration, success, fail, complete } = options; + let iconMap = { + success: 'success', + fail: 'error', + none: '' + }; + wx.showToast({ + icon: iconMap[type] || '', + title: content, + duration, + success: function() { + success && success(); + }, + fail: function(res) { + fail && fail(res); + }, + complete(res) { + complete && complete(res); + } + }); + }), + + hide: initApi((options?: HideToastOption): void => { + wx.hideToast(options); + }) +}; + +export default Toast; diff --git a/packages/interface/toast/src/weex/index.ts b/packages/interface/toast/src/weex/index.ts deleted file mode 100644 index 3186c5a4..00000000 --- a/packages/interface/toast/src/weex/index.ts +++ /dev/null @@ -1,31 +0,0 @@ -import {LONG_DELAY, SHORT_DELAY} from '../utils/index'; -import { ToastOption } from '../types'; - -declare const weex: any; - -let Toast: ToastOption = { - SHORT: SHORT_DELAY, - LONG: LONG_DELAY, - - /* - * @param message {String} - * @param duration {Number} - * @param userStyle {Object} user defined style - */ - show(message: string, duration: number = SHORT_DELAY): void { - const modal = weex.requireModule('modal'); - - if (modal.toast) { - modal.toast({ - message, - duration: Number(duration) / 1000, - }); - } - }, - - hide() { - console.error('hide toast not supported yet'); - } -}; - -export default Toast; diff --git a/packages/location/src/index.ts b/packages/location/src/index.ts index fd06b7ce..342d4edc 100644 --- a/packages/location/src/index.ts +++ b/packages/location/src/index.ts @@ -4,31 +4,31 @@ import webModule from './web/index'; import weChatModule from './wechat-miniapp/index'; import bytedanceModule from './byte-miniapp/index'; -export const getLocation = (() => { +export const getLocation = (args) => { if (isWeChatMiniProgram) { - return weChatModule.getLocation; + return weChatModule.getLocation(args); } else if (isByteDanceMicroApp) { - return bytedanceModule.getLocation; + return bytedanceModule.getLocation(args); } else if (isMiniApp || isDingdingMiniapp) { - return aliMiniAppModule.getLocation; + return aliMiniAppModule.getLocation(args); } else if (isWeb) { - return webModule.getLocation; + return webModule.getLocation(args); } else { throw new Error('universal-api:getLocation暂不支持'); } -})(); +}; -export const openLocation = (() => { +export const openLocation = (args) => { if (isWeChatMiniProgram) { - return weChatModule.openLocation; + return weChatModule.openLocation(args); } else if (isByteDanceMicroApp) { - return bytedanceModule.openLocation; + return bytedanceModule.openLocation(args); } else if (isMiniApp || isDingdingMiniapp) { - return aliMiniAppModule.openLocation; + return aliMiniAppModule.openLocation(args); } else { throw new Error('universal-api:openLocation暂不支持'); } -})(); +}; export default { getLocation, diff --git a/packages/network/request/README.en-US.md b/packages/network/request/README.en-US.md index a88f9113..b04f5b3d 100644 --- a/packages/network/request/README.en-US.md +++ b/packages/network/request/README.en-US.md @@ -72,9 +72,9 @@ request({ | options.data | `object`  |
- GET or POST set headers['content-Type'] is equal to `application/x-www-form-urlencoded` data will apply to URL
- In other cases, the data will be converted to a JSON string as request body to the server.
| false | - | | options.timeout | `number`  | timeout | false| 20000 (ms) | | options.dataType | `string`  | Set return data type, `json` or `text`, If the conversion fails, returns as it is | false | `json`  | -| options.success | `Function`  | 成功的回调 | 否 | - | -| options.fail | `Function`  | 失败的回调 | 否 | - | -| options.complete | `Function`  | 结束的回调 | 否 | - | +| options.success | `Function`  | The callback function for a successful API call | 否 | - | +| options.fail | `Function`  | The callback function for a failed API call | 否 | - | +| options.complete | `Function`  | The callback function used when the API call completed (always executed whether the call succeeds or fails) | 否 | - | #### Return diff --git a/packages/network/request/src/ali-miniapp/index.ts b/packages/network/request/src/ali-miniapp/index.ts index b1cb96a4..229f89fc 100644 --- a/packages/network/request/src/ali-miniapp/index.ts +++ b/packages/network/request/src/ali-miniapp/index.ts @@ -7,7 +7,7 @@ import {initApi} from '../common'; function request(options: RequestOptions) { // return new Promise((resolve, reject) => { - let { url, method, data, dataType, headers, timeout, success, fail, complete, onSuccess, onFail, onComplete } = options; + let { url, method, data, dataType, headers, timeout, success, fail, complete } = options; const request = isDingdingMiniapp ? dd.httpRequest : my.request; request({ url, @@ -18,15 +18,12 @@ function request(options: RequestOptions) { dataType, success: function(res: ResponseData) { success && success(res); - onSuccess && onSuccess(res); }, fail: function(res) { fail && fail(res); - onFail && onFail(res); }, complete(res) { complete && complete(res); - onComplete && onComplete(res); } }); } diff --git a/packages/network/request/src/byte-miniapp/index.ts b/packages/network/request/src/byte-miniapp/index.ts index f07e37a2..839493e8 100644 --- a/packages/network/request/src/byte-miniapp/index.ts +++ b/packages/network/request/src/byte-miniapp/index.ts @@ -6,7 +6,7 @@ import {initApi} from '../common'; function request(options: RequestOptions) { // return new Promise((resolve, reject) => { - let { url, method, data, dataType, headers, timeout, success, fail, complete, onSuccess, onFail, onComplete } = options; + let { url, method, data, dataType, headers, timeout, success, fail, complete } = options; tt.request({ url, headers, @@ -16,15 +16,12 @@ function request(options: RequestOptions) { dataType, success: function(res: ResponseData) { success && success(res); - onSuccess && onSuccess(res); }, fail: function(res) { fail && fail(res); - onFail && onFail(res); }, complete(res) { complete && complete(res); - onComplete && onComplete(res); } }); } diff --git a/packages/network/request/src/types.ts b/packages/network/request/src/types.ts index 2dcc3706..953f3f06 100644 --- a/packages/network/request/src/types.ts +++ b/packages/network/request/src/types.ts @@ -34,11 +34,8 @@ export interface RequestOptions { timeout?: number; dataType?: DATA_TYPE; success?: (res) => any; - onSuccess?: (res) => any; fail?: (res) => any; - onFail?: (res) => any; complete?: (res) => any; - onComplete?: (res) => any; } /** diff --git a/packages/network/request/src/web/index.ts b/packages/network/request/src/web/index.ts index d37516c4..7e39c39c 100644 --- a/packages/network/request/src/web/index.ts +++ b/packages/network/request/src/web/index.ts @@ -25,7 +25,7 @@ function requestXHR(options: RequestOptions) { data, timeout, dataType, - success, fail, complete, onSuccess, onFail, onComplete, + success, fail, complete, } = Object.assign( { withCredentials: true, @@ -47,9 +47,7 @@ function requestXHR(options: RequestOptions) { () => { clean(); fail && fail(ERROR_REQUEST_TIMEOUT); - onFail && onFail(ERROR_REQUEST_TIMEOUT); complete && complete(ERROR_REQUEST_TIMEOUT); - onComplete && onComplete(ERROR_REQUEST_TIMEOUT); // reject(ERROR_REQUEST_TIMEOUT); xhr.abort(); }, @@ -68,24 +66,12 @@ function requestXHR(options: RequestOptions) { status: xhr.status })}` }); - onFail && onFail({ - code: ERROR_REQUEST_ABORT.code, - message: `${JSON.stringify({ - status: xhr.status - })}` - }); complete && complete({ code: ERROR_REQUEST_ABORT.code, message: `${JSON.stringify({ status: xhr.status })}` }); - onComplete && onComplete({ - code: ERROR_REQUEST_ABORT.code, - message: `${JSON.stringify({ - status: xhr.status - })}` - }); // return reject({ // code: ERROR_REQUEST_ABORT.code, // message: `${JSON.stringify({ @@ -107,21 +93,11 @@ function requestXHR(options: RequestOptions) { status: xhr.status, headers: headerMap, }); - onSuccess && onSuccess({ - data: xhr.response, - status: xhr.status, - headers: headerMap, - }); complete && complete({ data: xhr.response, status: xhr.status, headers: headerMap, }); - onComplete && onComplete({ - data: xhr.response, - status: xhr.status, - headers: headerMap, - }); // resolve({ // data: xhr.response, // status: xhr.status, diff --git a/packages/network/request/src/wechat-miniapp/index.ts b/packages/network/request/src/wechat-miniapp/index.ts index 2e494b8f..84db9bf2 100644 --- a/packages/network/request/src/wechat-miniapp/index.ts +++ b/packages/network/request/src/wechat-miniapp/index.ts @@ -6,7 +6,7 @@ import {initApi} from '../common'; function request(options: RequestOptions) { // return new Promise((resolve, reject) => { - let { url, method, data, dataType, headers, timeout, success, fail, complete, onSuccess, onFail, onComplete } = options; + let { url, method, data, dataType, headers, timeout, success, fail, complete } = options; wx.request({ url, headers, @@ -16,15 +16,12 @@ function request(options: RequestOptions) { dataType, success: function(res: ResponseData) { success && success(res); - onSuccess && onSuccess(res); }, fail: function(res) { fail && fail(res); - onFail && onFail(res); }, complete(res) { complete && complete(res); - onComplete && onComplete(res); } }); }