1.0.2 • Published 1 year ago

gurobot-uni-basics v1.0.2

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
1 year ago

UniApp npm

1. 导入方式

"gurobot-uni-basics": "git+https://gitlab.gurobot.cn/gurobot-npm/gurobot-uni-basics.git#版本号",

2. 蓝牙

import { Bluetooth } from 'gurobot-uni-basics';
// { filterCriteria: [], duration: 30000 }
// filterCriteria 增加蓝牙筛选头部信息,例如['SP_']
// duration 蓝牙搜索时间,默认30s
this.bluetooth = new Bluetooth(option);

// 连接蓝牙
this.bluetooth.connect();
// 写入wifi
this.bluetooth.writeDevice(ssid, password, token)
// 关闭蓝牙
this.bluetooth.clearBluetooth();

3. HTTP

属性类型必填说明
pathstring开发者服务器接口地址
methodstringHTTP 请求方法
headerObject设置请求的 header
dataObject请求的参数
timeoutnumber超时时间,单位为毫秒。默认值为 50000
filePathstring要上传文件资源的路径 (本地路径)
namestring文件对应的 key
formDataObjectHTTP 请求中其他额外的 form data
showLoadboolean是否显示加载中状态
  • 请求示例
import { HTTP } from 'gurobot-uni-basics';
HTTP.GET({
    path: '',
    data: {},
}, { showLoad: false });

4. Logger

  • 请求示例
import { Logger } from 'gurobot-uni-basics';
const logger = Logger('app');
logger.DEBUG('debug', {});

5. media

媒体

5.1. AudioRecorder

录制音频

import { AudioRecorder } from 'gurobot-uni-basics';
this.audioRecorder = new AudioRecorder();

// 开始录制
// option:{ duration, sampleRate, numberOfChannels, encodeBitRate, format, frameSize }
this.audioRecorder.start(options);
// 停止录制
this.audioRecorder.stop();
5.2. BackgroundAudio

背景音频播放

import { BackgroundAudio } from 'gurobot-uni-basics';
this.backgroundAudio = new BackgroundAudio();

// start: 开始播放
// options:{src, startTime, title, coverImgUrl, protocol, playbackRate}
this.backgroundAudio.start(options);
// 播放
this.backgroundAudio.play();
// 暂停播放
this.backgroundAudio.pause();
// 跳转到指定位置
// 跳转的时间,单位 s。精确到小数点后 3 位,即支持 ms 级别精确度
this.backgroundAudio.seek(num);
// 停止播放
this.backgroundAudio.stop();
// 销毁当前实例
this.backgroundAudio.destroy();
5.3. InnerAudio

音频播放

import { InnerAudio } from 'gurobot-uni-basics';
this.innerAudio = new InnerAudio();

// start: 开始播放
// options:{src, startTime, autoplay, loop, obeyMuteSwitch, volume, playbackRate }
this.innerAudio.start(options);
// 播放
this.innerAudio.play();
// 暂停播放
this.innerAudio.pause();
// 跳转到指定位置
// 跳转的时间,单位 s。精确到小数点后 3 位,即支持 ms 级别精确度
this.innerAudio.seek(num);
// 停止播放
this.innerAudio.stop();
// 销毁当前实例
this.innerAudio.destroy();

6. Event

媒体查询事件

// bluetooth
CONNECTION_STATUS: '蓝牙连接状态事件',
BLUETOOTH_LINK_ERR: '蓝牙搜索失败事件',
RECEIVE_WIFI_INFO: '返回wifi信息事件',

// logger
LOGGER_ERROR: '错误日志事件',

// 录制
RECORDER_START: '监听录音开始事件',
RECORDER_DURATION: '监听录音时长事件',
RECORDER_STOP: '监听录音结束事件',
RECORDER_FRAME: '监听已录制完指定帧大小的文件事件',
RECORDER_ERROR: '监听录音错误事件',
INTERRUPTION_BEGIN: '监听录音因为受到系统占用而被中断开始事件',

// 音频
AUDIO_PLAY: '监听音频播放事件',
AUDIO_PAUSE: '监听音频暂停事件',
AUDIO_STOP: '监听音频停止事件',
AUDIO_ENDED: '监听音频自然播放至结束的事件',
AUDIO_ERROR: '监听音频播放错误事件',

// 背景音频
BACKGROUND_AUDIO_PLAY: '监听背景音频播放事件',
BACKGROUND_AUDIO_PAUSE: '监听背景音频暂停事件',
BACKGROUND_AUDIO_STOP: '监听背景音频停止事件',
BACKGROUND_AUDIO_ENDED: '监听背景音频自然播放至结束的事件',
BACKGROUND_AUDIO_ERROR: '监听背景音频播放错误事件',
BACKGROUND_AUDIO_NEXT: '监听用户在系统音乐播放面板点击下一曲事件(仅iOS)',
BACKGROUND_AUDIO_PREV: '监听用户在系统音乐播放面板点击上一曲事件(仅iOS)'

7. Storage

import { KV } from 'gurobot-uni-basics';
// test 项目名称
const Storage = new KV("test");

8. TASK

import { Task } from 'gurobot-uni-basics';
const task = new Task();

// 开启定时器任务
// fn 调用函数;time 间隔时长;duration 持续总时长
task.setTask(fn, time, duration);

// 关闭定时器任务
task.clearTask()

9. Utils

9.1. numberUtil
import { Utils } from 'gurobot-uni-basics';
const { numberUtil } = Utils;

// 随机数[min,max)
console.log(numberUtil.getRandomArbitrary(0, 10));
// 随机整数 [min,max)
console.log(numberUtil.getRandomInt(0, 10));
// 随机整数 [min,max]
console.log(numberUtil.getRandomIntInclusive(0, 10));
9.2. regexUtil
import { Utils } from 'gurobot-uni-basics';
const { regexUtil } = Utils;

// 是否为有效的url链接
console.log(regexUtil.validURL('http://www.baidu.com'));
// 是否为特殊字符
console.log(regexUtil.validSpecial('中文'));
9.3. retryUtil

(目前在HTTP内使用,接口重试)

import { Utils } from 'gurobot-uni-basics';
const { retryUtil } = Utils;

function createPromise() {
    count += 1;
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (count > 2) {
                resolve(new Date());
            } else {
                reject(new Date());
            }
        }, 1000);
    });
}

retryUtil.asyncRetry(async () => createPromise(), 500, 3).then(e => {
    console.log(e);
}).catch(e => {
    console.log(e);
});
9.4. timeUtil
import { Utils } from 'gurobot-uni-basics';
const { timeUtil } = Utils;

// 时间格式
console.log(timeUtil.formatTime(new Date(), 'Y年M月D日 h时m分s秒'));
// 月内天数
console.log(timeUtil.daysInMonth(2, 2022);
// 星期
console.log(timeUtil.weekday(new Date());
// 天数间距
console.log(timeUtil.diffDays(new Date('2022-12-20'), new Date('2022-12-25'));
// 月间距
console.log(timeUtil.monthDiff(new Date('2022-01-20'), new Date('2022-12-25'));
// 年间距
console.log(timeUtil.yearDiff(new Date('2022-01-20'), new Date('2022-12-25'));
// 年龄
console.log(timeUtil.constAge('2000');
9.5. tokenUtil

(目前使用于接口token存储)

import { Utils } from 'gurobot-uni-basics';
const { tokenUtil } = Utils;

tokenUtil.setApiToken("1111");
console.log(tokenUtil.getApiToken);
9.6. funcUtil
import { Utils } from 'gurobot-uni-basics';
const { funcUtil } = Utils;

function handleThrottle() {
    console.log("throttle...");
}

function handleDebounce() {
    console.log("debounce...");
}

var key = 0;
var time = setInterval(() => {
    // 函数节流
    funcUtil.throttle(handleThrottle, 3000);
    // 函数防抖
    funcUtil.debounce(handleDebounce, 2000);
    if (key > 6) {
        clearInterval(time);
    }
    key++;
}, 500);
1.0.2

1 year ago