English | 简体中文
一个为 Zepp OS 小程序的开发迷你开发库。目前集成了网络请求,通信等功能。
这个库要求 API_LEVEL 3.0 以上
ZML0.0.28
要求 API_LEVEL 3.6 以上。
直接使用 http 请求
import { BaseApp } from '@zeppos/zml/base-app'
App(
BaseApp({
globalData: {},
onCreate() {},
onDestroy(opts) {},
}),
)
import { BasePage } from '@zeppos/zml/base-page'
Page(
BasePage({
state: {},
build() {},
onInit() {
this.getYourData()
},
getYourData() {
return this.httpRequest({
method: 'get',
url: 'your url',
})
.then((result) => {
console.log('result.status', result.status)
console.log('result.statusText', result.statusText)
console.log('result.headers', result.headers)
console.log('result.body', result.body)
})
.catch((error) => {
console.error('error=>', error)
})
},
onDestroy() {
console.log('page onDestroy invoked')
},
}),
)
import { BaseSideService } from '@zeppos/zml/base-side'
AppSideService(BaseSideService())
跟手机通信相关api
- 使用 request, call 发送数据
- 使用 onRequest, onCall 接受数据
import { BaseApp } from '@zeppos/zml/base-app'
App(
BaseApp({
globalData: {},
onCreate() {},
onDestroy() {},
}),
)
import { BasePage } from '@zeppos/zml/base-page'
Page(
BasePage({
build() {},
onInit() {
this.getDataFromMobile()
},
getDataFromMobile() {
return this.request({
method: 'your.method1',
params: {
param1: 'param1',
param2: 'param2',
},
})
.then((result) => {
// receive your data
console.log('result=>', result)
})
.catch((error) => {
// receive your error
console.error('error=>', error)
})
},
notifyMobile() {
this.call({
method: 'your.method3',
params: {
param1: 'param1',
param2: 'param2',
},
})
},
onRequest(req, res) {
// need reply
// node style callback
// first param is error
// second param is your data
if (req.method === 'your.method2') {
// do something
console.log('req=>', JSON.string(req))
res(null, {
test: 1,
})
} else {
res('error happened')
}
},
onCall(data) {
// no reply
if (req.method === 'your.method4') {
// do something
console.log('req=>', JSON.string(data))
}
},
onDestroy() {
console.log('page onDestroy invoked')
},
}),
)
import { BaseSideService } from '@zeppos/zml/base-side'
AppSideService(
BaseSideService({
onInit() {
},
onRun() {
},
getDataFromDevice() {
return this.request({
method: 'your.method2',
params: {
param1: 'param1',
param2: 'param2'
}
})
.then((result) => {
// receive your data
console.log('result=>', result)
})
.catch((error) => {
// receive your error
console.error('error=>', error)
})
},
notifyDevice() {
this.call({
method: 'your.method4',
params: {
param1: 'param1',
param2: 'param2'
}
})
},
onRequest(req, res) {
// need reply
// node style callback
// first param is error
// second param is your data
if (req.method === 'your.method1') {
// do something
res(null, {
test: 1
})
} else {
res('error happened')
}
},
onCall(data) {
onCall(data) {
// no reply
if (req.method === 'your.method3') {
// do something
}
},
},
onDestroy() {
}
}),
)
本项目遵循 Apache License Version 2.0 协议,详情见 LICENSE 声明。
Copyright (c) 2023-present Zepp Health. All Rights Reserved.