diff --git a/README.md b/README.md index 16c3a13..408100a 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,12 @@ English | [简体中文](./README_CN.md) A mini development library for Zepp OS mini programs. Currently integrates network requests, communication and other functions. ## API_LEVEL Required + This library requires **API_LEVEL 3.0 or above**. +### Please Notice + +ZML`0.0.28` requires **API_LEVEL 3.6 or above**. ## Usage diff --git a/README_CN.md b/README_CN.md index f7e2fe6..52cf087 100644 --- a/README_CN.md +++ b/README_CN.md @@ -5,8 +5,13 @@ 一个为 Zepp OS 小程序的开发迷你开发库。目前集成了网络请求,通信等功能。 ## API_LEVEL 要求 + 这个库要求 **API_LEVEL 3.0** 以上 +### 请注意 + +ZML`0.0.28` 要求 **API_LEVEL 3.6** 以上。 + ## 使用 ### httpRequest API diff --git a/docs/known-issues.md b/docs/known-issues.md index a8fbd1d..dd3cbcc 100644 --- a/docs/known-issues.md +++ b/docs/known-issues.md @@ -2,4 +2,6 @@ ## API_LEVEL 3.0 Required -`zml` requires `API_LEVEL>=3.0` or higher to use all functions. Lower versions of `API_LEVEL` will result in some functions being unavailable. \ No newline at end of file +`zml` requires `API_LEVEL>=3.0` or higher to use all functions. Lower versions of `API_LEVEL` will result in some functions being unavailable. + +The latest `API_LEVEL` requirement is `3.6`. diff --git a/docs/zh/known-issues.md b/docs/zh/known-issues.md index 30cee50..cfeafa1 100644 --- a/docs/zh/known-issues.md +++ b/docs/zh/known-issues.md @@ -3,3 +3,5 @@ ## API_LEVEL 3.0 要求 `zml` 要求 `API_LEVEL>=3.0` 以上,才可以使用全部功能,较低版本的 `API_LEVEL` 会导致部分功能无法使用 + +最新版本的 `API_LEVEL` 要求为 `3.6`。 diff --git a/package-lock.json b/package-lock.json index 1404997..cb74f6f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@zeppos/zml", - "version": "0.0.27", + "version": "0.0.28", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@zeppos/zml", - "version": "0.0.27", + "version": "0.0.28", "license": "Apache-2.0", "devDependencies": { "@rollup/plugin-alias": "^5.0.0", diff --git a/package.json b/package.json index 24ff5bb..36e9bda 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@zeppos/zml", - "version": "0.0.27", + "version": "0.0.28", "description": "A Mini Library of ZeppOS MiniApp", "zeppos": true, "typings": "./zml.d.ts", diff --git a/src/core/common/common.js b/src/core/common/common.js index ae03a02..a1da8b9 100644 --- a/src/core/common/common.js +++ b/src/core/common/common.js @@ -99,3 +99,13 @@ export function isZeppOS() { export function isSideService() { return typeof messaging !== 'undefined' } + +let zosApp = null + +if (isZeppOS1()) { + zosApp = hmApp +} else if (isZeppOS2()) { + zosApp = _r('@zos/app') +} + +export { zosApp } diff --git a/src/core/device/messaging/app-plugin.js b/src/core/device/messaging/app-plugin.js index d86bae1..27c0b23 100644 --- a/src/core/device/messaging/app-plugin.js +++ b/src/core/device/messaging/app-plugin.js @@ -1,5 +1,20 @@ import { createDeviceMessage } from './device-message.js' import { httpRequest } from './httpRequest.js' +import { zosApp } from '../../common/common.js' +import { Deferred } from '../../../shared/defer.js' + +export function generateRandom12() { + let result = '' + for (let i = 0; i < 12; i++) { + if (i === 0) { + // 确保第一位不为0 + result += Math.floor(Math.random() * 9) + 1 + } else { + result += Math.floor(Math.random() * 10) + } + } + return result +} export function appPlugin(opts) { const messaging = createDeviceMessage() @@ -14,7 +29,37 @@ export function appPlugin(opts) { this.messaging.offOnCall().offOnRequest().disConnect() }, request(data, opts = {}) { - return this.messaging.request(data, opts) + const defer = Deferred() + + const id = generateRandom12() + const eventResult = 'response:result:' + id + const eventError = 'response:error:' + id + + function resolve(e, ...args) { + defer.resolve(...args) + } + + function reject(e, ...args) { + defer.reject(...args) + } + + zosApp.onMessage(eventResult, resolve) + zosApp.onMessage(eventError, reject) + + this.messaging + .request(data, opts) + .then((r) => { + zosApp.postMessage(eventResult, r) + }) + .catch((e) => { + zosApp.postMessage(eventError, e) + }) + .finally(() => { + zosApp.offMessage(eventResult) + zosApp.offMessage(eventError) + }) + + return defer.promise }, call(data) { return this.messaging.call(data) diff --git a/src/core/device/messaging/httpRequest.js b/src/core/device/messaging/httpRequest.js index eaf7fa2..57625b0 100644 --- a/src/core/device/messaging/httpRequest.js +++ b/src/core/device/messaging/httpRequest.js @@ -1,5 +1,5 @@ export function httpRequest(data, opts = {}) { - return this.messaging.request( + return this.request( { method: 'http.request', params: data, diff --git a/src/core/device/messaging/page-plugin.js b/src/core/device/messaging/page-plugin.js index 8104be9..6befa41 100644 --- a/src/core/device/messaging/page-plugin.js +++ b/src/core/device/messaging/page-plugin.js @@ -1,4 +1,19 @@ import { httpRequest } from './httpRequest.js' +import { zosApp } from '../../common/common.js' +import { Deferred } from '../../../shared/defer.js' + +export function generateRandom12() { + let result = '' + for (let i = 0; i < 12; i++) { + if (i === 0) { + // 确保第一位不为0 + result += Math.floor(Math.random() * 9) + 1 + } else { + result += Math.floor(Math.random() * 10) + } + } + return result +} function getDeviceMessage() { const { messaging } = getApp()._options.globalData @@ -24,7 +39,37 @@ export function pagePlugin(opts) { } }, request(data, opts = {}) { - return this.messaging.request(data, opts) + const defer = Deferred() + + const id = generateRandom12() + const eventResult = 'response:result:' + id + const eventError = 'response:error:' + id + + function resolve(e, ...args) { + defer.resolve(...args) + } + + function reject(e, ...args) { + defer.reject(...args) + } + + zosApp.onMessage(eventResult, resolve) + zosApp.onMessage(eventError, reject) + + this.messaging + .request(data, opts) + .then((r) => { + zosApp.postMessage(eventResult, r) + }) + .catch((e) => { + zosApp.postMessage(eventError, e) + }) + .finally(() => { + zosApp.offMessage(eventResult) + zosApp.offMessage(eventError) + }) + + return defer.promise }, call(data) { return this.messaging.call(data)