1
0
Fork 0
Agora-Miniapp-Tutorial/pages/index/index.js

163 lines
3.4 KiB
JavaScript
Executable File

const app = getApp()
const Utils = require('../../utils/util.js')
const { APPID, setToken } = require("../../utils/config.js");
// pages/index/index.js.js
Page({
/**
* 页面的初始数据
*/
data: {
// used to store user info like portrait & nickname
userInfo: {},
hasUserInfo: false,
// whether to disable join btn or not
disableJoin: false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.channel = "";
this.uid = Utils.getUid();
this.lock = false;
let userInfo = wx.getStorageSync("userInfo");
if (userInfo){
this.setData({
hasUserInfo: true,
userInfo: userInfo
});
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 只有提供了该回调才会出现转发选项
*/
onShareAppMessage() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* callback to get user info
* using wechat open-type
*/
onGotUserInfo: function(e){
let userInfo = e.detail.userInfo || {};
// store data for next launch use
wx.setStorage({
key: 'userInfo',
data: userInfo,
})
this.onJoin(userInfo);
},
/**
* check if join is locked now, this is mainly to prevent from clicking join btn to start multiple new pages
*/
checkJoinLock: function() {
return !(this.lock || false);
},
lockJoin: function() {
this.lock = true;
},
unlockJoin: function() {
this.lock = false;
},
onJoin: function (userInfo) {
userInfo = userInfo || {};
let value = this.channel || "";
let uid = this.uid;
if (!value) {
wx.showToast({
title: '请提供一个有效的房间名',
icon: 'none',
duration: 2000
})
} else {
wx.request({
url: 'https://finogeeks-tools.finogeeks.club/test-report/agora/token',
method: 'post',
data: {
appId: APPID,
channel: value
},
success: (res) => {
if (res.statusCode === 200 && res.data.token) {
setToken(res.data.token);
this.joinRoom(value, uid);
} else {
wx.showToast({
title: `获取 token 失败`,
icon: 'none',
duration: 5000
})
}
}
})
}
},
joinRoom: function(channel, uid) {
if(this.checkJoinLock()) {
this.lockJoin();
if (channel === "agora") {
// go to test page if channel name is agora
wx.navigateTo({
url: `../test/test`
});
} else if (channel === "agora2") {
// go to test page if channel name is agora
wx.navigateTo({
url: `../test2/test2`
});
} else {
wx.showModal({
title: '是否推流',
content: '选择取消则作为观众加入,观众模式不推流',
showCancel: true,
success: function (res) {
let role = "audience";
if (res.confirm) {
role = "broadcaster";
}
wx.navigateTo({
url: `../meeting/meeting?channel=${channel}&uid=${uid}&role=${role}`
});
}
})
}
}
},
onInputChannel: function (e) {
let value = e.detail.value;
this.channel = value;
}
})