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

158 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-06-06 11:51:36 +08:00
const Utils = require('../../utils/util.js')
2022-06-30 14:58:34 +08:00
const { APPID, setToken } = require("../../utils/config.js");
2022-06-06 11:51:36 +08:00
// 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
});
}
2022-06-30 14:58:34 +08:00
wx.request({
url: 'https://finogeeks-tools.finogeeks.club/test-report/agora/token',
method: 'post',
data: {
appId: APPID,
channel: 'finclip'
},
success(res) {
if (res.statusCode === 200 && res.data.token) {
setToken(res.data.token)
} else {
wx.showToast({
title: `获取 token 失败`,
icon: 'none',
duration: 5000
})
}
}
})
2022-06-06 11:51:36 +08:00
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
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 {
if(this.checkJoinLock()) {
this.lockJoin();
if (value === "agora") {
// go to test page if channel name is agora
wx.navigateTo({
url: `../test/test`
});
} else if (value === "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=${value}&uid=${uid}&role=${role}`
});
}
})
}
}
}
},
onInputChannel: function (e) {
let value = e.detail.value;
this.channel = value;
}
2022-06-30 14:58:34 +08:00
})