Skip to content

Commit

Permalink
Merge branch 'feat/ZOS-5115' into 'main'
Browse files Browse the repository at this point in the history
Resolve ZOS-5115 "Feat/"

Closes ZOS-5115

See merge request sec.app.web/zeppos/zml!2
  • Loading branch information
Tao HUANG(黄涛-前端开发工程师) committed Jul 25, 2024
2 parents 3e5e0da + 143ddb5 commit 177f1fc
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
一个为 Zepp OS 小程序的开发迷你开发库。目前集成了网络请求,通信等功能。

## API_LEVEL 要求

这个库要求 **API_LEVEL 3.0** 以上

### 请注意

ZML`0.0.28` 要求 **API_LEVEL 3.6** 以上。

## 使用

### httpRequest API
Expand Down
4 changes: 3 additions & 1 deletion docs/known-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
`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`.
2 changes: 2 additions & 0 deletions docs/zh/known-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
## API_LEVEL 3.0 要求

`zml` 要求 `API_LEVEL>=3.0` 以上,才可以使用全部功能,较低版本的 `API_LEVEL` 会导致部分功能无法使用

最新版本的 `API_LEVEL` 要求为 `3.6`
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 10 additions & 0 deletions src/core/common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
47 changes: 46 additions & 1 deletion src/core/device/messaging/app-plugin.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/core/device/messaging/httpRequest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function httpRequest(data, opts = {}) {
return this.messaging.request(
return this.request(
{
method: 'http.request',
params: data,
Expand Down
47 changes: 46 additions & 1 deletion src/core/device/messaging/page-plugin.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 177f1fc

Please sign in to comment.