第一次提交
This commit is contained in:
98
pagesLive/lib/agora-rtm.js
Normal file
98
pagesLive/lib/agora-rtm.js
Normal file
@ -0,0 +1,98 @@
|
||||
const eventBus = require("./event-bus.js")
|
||||
const AgoraRTM = require("./rtm-miniapp.js")
|
||||
|
||||
class RTMClient {
|
||||
constructor (APPID) {
|
||||
this._client = AgoraRTM.createInstance(APPID)
|
||||
this._accountName = ''
|
||||
this._channel = null
|
||||
this.isLogin = false
|
||||
this.isOff = false
|
||||
this.messageCache = []
|
||||
this._eventBus = eventBus
|
||||
this.subscribeLoginEvents()
|
||||
}
|
||||
|
||||
login(token, accountName) {
|
||||
this._accountName = accountName
|
||||
const object = {
|
||||
token: token,
|
||||
uid: accountName
|
||||
}
|
||||
return this._client.login(object)
|
||||
}
|
||||
|
||||
subscribeLoginEvents() {
|
||||
this._client.on('ConnectionStateChanged', (newState, reason) => {
|
||||
this._eventBus.emit('ConnectionStateChanged', newState, reason)
|
||||
})
|
||||
this._client.on('MessageFromPeer', (message, peerId, { isOfflineMessage }) => {
|
||||
console.log('MessageFromPeer')
|
||||
// 判断是否是离线消息 如果是 进行消息缓存
|
||||
if(isOfflineMessage) {
|
||||
let object = {
|
||||
message: message.text,
|
||||
peerId: peerId,
|
||||
isOfflineMessage: isOfflineMessage
|
||||
}
|
||||
this.messageCache.push(object)
|
||||
}
|
||||
this._eventBus.emit('MessageFromPeer', message, peerId, isOfflineMessage)
|
||||
})
|
||||
}
|
||||
|
||||
logout() {
|
||||
return this._client.logout().then(() => {
|
||||
this._eventBus.clear()
|
||||
this.messageCache = []
|
||||
})
|
||||
}
|
||||
|
||||
sendPeer(msg, peerId) {
|
||||
return this._client.sendMessageToPeer({ text: msg }, peerId, {
|
||||
enableHistoricalMessaging: false,
|
||||
enableOfflineMessaging: this.isOff
|
||||
})
|
||||
}
|
||||
|
||||
joinChannel(id) {
|
||||
this._channel = this._client.createChannel(id)
|
||||
this.subscribeChannelEvents(id)
|
||||
return this._channel.join()
|
||||
}
|
||||
|
||||
subscribeChannelEvents() {
|
||||
this._channel.on('MemberJoined', (e) => {
|
||||
this._eventBus.emit('MemberJoined', e)
|
||||
})
|
||||
this._channel.on('MemberLeft', (e) => {
|
||||
this._eventBus.emit('MemberLeft', e)
|
||||
})
|
||||
this._channel.on('MemberCountUpdated', (e) => {
|
||||
this._eventBus.emit('MemberCountUpdated', e)
|
||||
})
|
||||
|
||||
this._channel.on('ChannelMessage', (message, memberId) => {
|
||||
this._eventBus.emit('ChannelMessage', message, memberId)
|
||||
})
|
||||
}
|
||||
|
||||
sendChannel(msg) {
|
||||
return this._channel.sendMessage({text: msg})
|
||||
}
|
||||
|
||||
getMembers() {
|
||||
return this._channel.getMembers()
|
||||
}
|
||||
|
||||
leaveChannel() {
|
||||
return this._channel.leave()
|
||||
}
|
||||
|
||||
on(event, callback) {
|
||||
this._eventBus.on(event, callback)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = RTMClient
|
||||
14
pagesLive/lib/config.js
Normal file
14
pagesLive/lib/config.js
Normal file
@ -0,0 +1,14 @@
|
||||
// 填入 appID
|
||||
const APPID = "ec75c8a43d8b4a9887f57b9a274a9da4";
|
||||
|
||||
if(APPID === ""){
|
||||
wx.showToast({
|
||||
title: `请在config.js中提供正确的appid`,
|
||||
icon: 'none',
|
||||
duration: 5000
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
APPID: APPID
|
||||
}
|
||||
253
pagesLive/lib/event-bus.js
Normal file
253
pagesLive/lib/event-bus.js
Normal file
@ -0,0 +1,253 @@
|
||||
/*!
|
||||
* iny-bus.js v1.1.0
|
||||
* (c) 2019-2020 landluck
|
||||
* Released under the MIT License.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 创建唯一id
|
||||
*/
|
||||
function createUid() {
|
||||
return Math.random()
|
||||
.toString()
|
||||
.substr(2);
|
||||
}
|
||||
function once(fn) {
|
||||
var called = false;
|
||||
return function () {
|
||||
var args = [];
|
||||
for (var _i = 0; _i < arguments.length; _i++) {
|
||||
args[_i] = arguments[_i];
|
||||
}
|
||||
if (!called) {
|
||||
called = true;
|
||||
fn.apply(this, args);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
var EventBus = /** @class */ (function () {
|
||||
function EventBus() {
|
||||
/**
|
||||
* 储存事件的容器
|
||||
*/
|
||||
this.events = [];
|
||||
}
|
||||
/**
|
||||
* on 新增事件监听
|
||||
* @param name 事件名
|
||||
* @param execute 回调函数
|
||||
* @param ctx 上下文 this
|
||||
* @returns { string } eventId 事件ID,用户取消该事件监听
|
||||
*/
|
||||
EventBus.prototype.on = function (name, execute, ctx) {
|
||||
return this.addEvent(name, execute, ctx);
|
||||
};
|
||||
/**
|
||||
* one 只允许添加一次事件监听
|
||||
* @param name 事件名
|
||||
* @param execute 回调函数
|
||||
* @param ctx 上下文 this
|
||||
* @returns { string } eventId 事件ID,用户取消该事件监听
|
||||
*/
|
||||
EventBus.prototype.once = function (name, execute, ctx) {
|
||||
return this.addEvent(name, once(execute), ctx);
|
||||
};
|
||||
/**
|
||||
* remove 移除事件监听
|
||||
* @param name 事件名
|
||||
* @param eventId 移除单个事件监听需传入
|
||||
* @returns { EventBus } EventBus EventBus 实例
|
||||
*/
|
||||
EventBus.prototype.remove = function (name, eventId) {
|
||||
var events = this.events;
|
||||
var index = events.findIndex(function (event) { return event.name === name; });
|
||||
if (index === -1) {
|
||||
return this;
|
||||
}
|
||||
if (!eventId) {
|
||||
events.splice(index, 1);
|
||||
return this;
|
||||
}
|
||||
var executeIndex = events[index].executes.findIndex(function (item) { return item.id === eventId; });
|
||||
if (executeIndex !== -1) {
|
||||
events[index].executes.splice(executeIndex, 1);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
/**
|
||||
* emit 派发事件
|
||||
* @param name 事件名
|
||||
* @param args 其余参数
|
||||
* @returns { EventBus } EventBus EventBus 实例
|
||||
*/
|
||||
EventBus.prototype.emit = function (name) {
|
||||
var args = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
args[_i - 1] = arguments[_i];
|
||||
}
|
||||
var event = this.find(name);
|
||||
if (!event) {
|
||||
return this;
|
||||
}
|
||||
var funcs = event.executes;
|
||||
funcs.forEach(function (func) {
|
||||
if (func.ctx) {
|
||||
return func.execute.apply(func.ctx, args);
|
||||
}
|
||||
func.execute.apply(func, args);
|
||||
});
|
||||
return this;
|
||||
};
|
||||
/**
|
||||
* 查找事件的方法
|
||||
* @param name
|
||||
*/
|
||||
EventBus.prototype.find = function (name) {
|
||||
var events = this.events;
|
||||
for (var i = 0; i < events.length; i++) {
|
||||
if (name === events[i].name) {
|
||||
return events[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
EventBus.prototype.clear = function () {
|
||||
this.events.length = 0;
|
||||
return this;
|
||||
};
|
||||
/**
|
||||
* 添加事件的方法
|
||||
* @param name
|
||||
* @param execute
|
||||
*/
|
||||
EventBus.prototype.addEvent = function (name, execute, ctx) {
|
||||
var eventId = createUid();
|
||||
var events = this.events;
|
||||
var event = this.find(name);
|
||||
if (event !== null) {
|
||||
event.executes.push({ id: eventId, execute: execute, ctx: ctx });
|
||||
return eventId;
|
||||
}
|
||||
events.push({
|
||||
name: name,
|
||||
executes: [
|
||||
{
|
||||
id: eventId,
|
||||
execute: execute,
|
||||
ctx: ctx
|
||||
}
|
||||
]
|
||||
});
|
||||
return eventId;
|
||||
};
|
||||
return EventBus;
|
||||
}());
|
||||
|
||||
function createInstance() {
|
||||
var bus = new EventBus();
|
||||
// @ts-ignore
|
||||
return bus;
|
||||
}
|
||||
var bus = createInstance();
|
||||
bus.create = function create() {
|
||||
return createInstance();
|
||||
};
|
||||
|
||||
function isAliPay() {
|
||||
// @ts-ignore
|
||||
return typeof my !== 'undefined' && my.getSystemInfo;
|
||||
}
|
||||
|
||||
var PAGE_LIFE_TIMES = {
|
||||
onLoad: 'onLoad',
|
||||
onUnload: 'onUnload'
|
||||
};
|
||||
var APP_LIFE_TIMES = {
|
||||
onLoad: 'onLaunch',
|
||||
onUnload: ''
|
||||
};
|
||||
var COMPONENT_LIFE_TIMES = isAliPay()
|
||||
? {
|
||||
onLoad: 'onInit',
|
||||
onUnload: 'didUnmount'
|
||||
}
|
||||
: {
|
||||
onLoad: 'created',
|
||||
onUnload: 'detached'
|
||||
};
|
||||
|
||||
function verifyEvents(busEvents) {
|
||||
if (!busEvents) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function onLoad(ctx, onLoad) {
|
||||
var func = ctx[onLoad];
|
||||
ctx[onLoad] = function (options) {
|
||||
var ids = addEvent(ctx.busEvents, this);
|
||||
ctx.__inyEventIds = ids;
|
||||
func && func.call(this, options);
|
||||
};
|
||||
}
|
||||
function onUnload(ctx, onUnload) {
|
||||
if (!onUnload) {
|
||||
return;
|
||||
}
|
||||
var func = ctx[onUnload];
|
||||
ctx[onUnload] = function () {
|
||||
ctx.__inyEventIds.forEach(function (event) { return bus.remove(event.name, event.id); });
|
||||
ctx.__inyEventIds = undefined;
|
||||
func && func.call(this);
|
||||
};
|
||||
}
|
||||
function addEvent(events, ctx) {
|
||||
return Object.keys(events).map(function (name) {
|
||||
var event = events[name];
|
||||
if (typeof event === 'function') {
|
||||
return { id: bus.on(name, event, ctx), name: name };
|
||||
}
|
||||
if (event.once) {
|
||||
return { id: bus.once(name, event.handler, ctx), name: name };
|
||||
}
|
||||
return { id: bus.on(name, event.handler, ctx), name: name };
|
||||
});
|
||||
}
|
||||
|
||||
function inyApp(ctx) {
|
||||
var busEvents = ctx.busEvents;
|
||||
if (!verifyEvents(busEvents)) {
|
||||
return ctx;
|
||||
}
|
||||
onLoad(ctx, APP_LIFE_TIMES.onLoad);
|
||||
onUnload(ctx, APP_LIFE_TIMES.onUnload);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
function InyPage(ctx) {
|
||||
var busEvents = ctx.busEvents;
|
||||
if (!verifyEvents(busEvents)) {
|
||||
return ctx;
|
||||
}
|
||||
onLoad(ctx, PAGE_LIFE_TIMES.onLoad);
|
||||
onUnload(ctx, PAGE_LIFE_TIMES.onUnload);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
function InyComponents(ctx) {
|
||||
var busEvents = ctx.busEvents;
|
||||
if (!verifyEvents(busEvents)) {
|
||||
return ctx;
|
||||
}
|
||||
onLoad(ctx, COMPONENT_LIFE_TIMES.onLoad);
|
||||
onUnload(ctx, COMPONENT_LIFE_TIMES.onUnload);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
bus.app = inyApp;
|
||||
bus.page = InyPage;
|
||||
bus.component = InyComponents;
|
||||
|
||||
module.exports = bus;
|
||||
15
pagesLive/lib/mini-app-sdk-production.js
Normal file
15
pagesLive/lib/mini-app-sdk-production.js
Normal file
File diff suppressed because one or more lines are too long
5
pagesLive/lib/rtm-miniapp.js
Normal file
5
pagesLive/lib/rtm-miniapp.js
Normal file
File diff suppressed because one or more lines are too long
32
pagesLive/lib/util.js
Normal file
32
pagesLive/lib/util.js
Normal file
@ -0,0 +1,32 @@
|
||||
const formatTime = date => {
|
||||
const year = date.getFullYear()
|
||||
const month = date.getMonth() + 1
|
||||
const day = date.getDate()
|
||||
const hour = date.getHours()
|
||||
const minute = date.getMinutes()
|
||||
const second = date.getSeconds()
|
||||
|
||||
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
|
||||
}
|
||||
|
||||
const formatNumber = n => {
|
||||
n = n.toString()
|
||||
return n[1] ? n : '0' + n
|
||||
}
|
||||
|
||||
const debounce = function(foo, t) {
|
||||
let timer
|
||||
return function() {
|
||||
if (timer !== undefined) {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
timer = setTimeout(() => {
|
||||
foo.apply(this, arguments)
|
||||
}, t)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatTime: formatTime,
|
||||
debounce: debounce
|
||||
}
|
||||
Reference in New Issue
Block a user