0.0.38 • Published 9 months ago
@zeppos/zml v0.0.38
ZML
English | 简体中文
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
ZML0.0.28 requires API_LEVEL 3.6 or above.
Usage
httpRequest API
Use http requests directly
Use in app.js
import { BaseApp } from '@zeppos/zml/base-app'
App(
  BaseApp({
    globalData: {},
    onCreate() {},
    onDestroy(opts) {},
  }),
)Use in page module
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')
    },
  }),
)Use in sideService module
import { BaseSideService } from '@zeppos/zml/base-side'
AppSideService(BaseSideService())request APIs
APIs related to communication with the phone
- Use request, call to send data
- Use onRequest, onCall to receive data
Use in app.js
import { BaseApp } from '@zeppos/zml/base-app'
App(
  BaseApp({
    globalData: {},
    onCreate() {},
    onDestroy() {},
  }),
)Use in page module
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')
    },
  }),
)Use in sideService module
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() {
    }
  }),
)More complex example
License
Licensed under the Apache License, Version 2.0 license.
Copyright (c) 2023-present Zepp Health. All Rights Reserved.
0.0.38
9 months ago
0.0.35
9 months ago
0.0.34
9 months ago
0.0.33
1 year ago
0.0.27-1
1 year ago
0.0.29
1 year ago
0.0.27
1 year ago
0.0.26
1 year ago
0.0.25
1 year ago
0.0.24
1 year ago
0.0.23
2 years ago
0.0.22
2 years ago
0.0.19
2 years ago
0.0.18
2 years ago
0.0.17
2 years ago
0.0.16
2 years ago
0.0.15
2 years ago
0.0.14
2 years ago
0.0.13
2 years ago
0.0.12
2 years ago
0.0.11
2 years ago
0.0.10
2 years ago
0.0.9
2 years ago
0.0.8
2 years ago