1.0.0 • Published 1 year ago
gurobot-wx-basics v1.0.0
微信小程序 npm
1. 导入方式
"gurobot-wx-basics": "git+https://gitlab.gurobot.cn/gurobot-npm/gurobot-wx-basics.git#版本号",
2. HTTP
属性 | 类型 | 必填 | 说明 |
---|---|---|---|
path | string | 是 | 开发者服务器接口地址 |
method | string | 是 | HTTP 请求方法 |
header | Object | 否 | 设置请求的 header |
data | Object | 是 | 请求的参数 |
timeout | number | 否 | 超时时间,单位为毫秒。默认值为 50000 |
filePath | string | 要上传文件资源的路径 (本地路径) | |
name | string | 文件对应的 key | |
formData | Object | HTTP 请求中其他额外的 form data | |
showLoad | boolean | 否 | 是否显示加载中状态 |
- 请求示例
import { HTTP } from 'gurobot-wx-basics';
HTTP.GET({
path: '',
data: {},
}, { showLoad: false });
3. Logger
- 请求示例
import { Logger } from 'gurobot-wx-basics';
const logger = Logger('app');
logger.DEBUG('debug', {});
4. Eventemitter
监听器类
- 请求示例
import { Eventemitter } from 'gurobot-wx-basics';
// 事件监听,监听的过程就是订阅
Eventemitter.on()
// 解绑事件,取消订阅
Eventemitter.off()
// 事件触发,触发的过程就是发布
Eventemitter.emit()
5. media
媒体
5.1. AudioRecorder
录制音频
import { AudioRecorder } from 'gurobot-wx-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-wx-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-wx-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();
5.4. MediaEvent
媒体查询事件
// 录制
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)'
6. MQTT
6.1. MQTT
import { MQTT } from 'gurobot-wx-basics';
// mqtt连接状态
MQTT.isConnect();
// mqtt连接
// option:{ host, port, wssPort, username, password, clientId, subTopic}
MQTT.connect(option);
// mqtt订阅
// subTopic 订阅主题
MQTT.subscribe(subTopic);
// mqtt接收消息
// topic 主题;message 接收消息
MQTT.onMessageArrived(topic, message);
// mqtt发送
// topic 发送主题; payload:发送消息主题
MQTT.publish(topic, payload);
// mqtt断开连接
MQTT.disconnect();
6.2. MQTTEvent
// mqtt事件
import { MQTTEvent } from 'gurobot-wx-basics';
const { MQTT_EVENT_SUBSCRIBE, MQTT_EVENT_MESSAGE, MQTT_EVENT_OFFLINE } = MQTTEvent;
// 订阅事件
MQTT_EVENT_SUBSCRIBE
// 消息回调事件
MQTT_EVENT_MESSAGE
// 离线事件
MQTT_EVENT_OFFLINE
7. Storage
import { KV } from 'gurobot-wx-basics';
// test 项目名称
const Storage = new KV("test");
8. TASK
import { Task } from 'gurobot-wx-basics';
const task = new Task();
// 开启定时器任务
// fn 调用函数;time 间隔时长;duration 持续总时长
task.setTask(fn, time, duration);
// 关闭定时器任务
task.clearTask()
9. Utils
9.1. numberUtil
import { Utils } from 'gurobot-wx-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-wx-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-wx-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-wx-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-wx-basics';
const { tokenUtil } = Utils;
tokenUtil.setApiToken("1111");
console.log(tokenUtil.getApiToken);
9.6. funcUtil
import { Utils } from 'gurobot-wx-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.0
1 year ago