// 账号 appSecret
const appSecret = '';
// 账号 appId
const appId = '';
const userInfo = {
// 昵称
nick: '观众昵称',
// 头像
pic: 'https://example.com/avatar.jpg',
// 观众id
userId: '1',
};
const channelInfo = {
// 频道号
channelId: '',
// 房间号
roomId: '',
// 频道场次id
sessionId: '',
};
updateConfig({
// 观众信息
userInfo: userInfo,
// 频道信息
channelInfo: channelInfo,
// 此处传入 socketio 实例,具体获取方式请看下文
socket: socket,
// viewerApiToken 更新函数
getViewerApiToken: (callback) => {
myViewerApiTokenGetter(callback);
},
})
async function myViewerApiTokenGetter(cb) {
var params = {
appId: appId, // 账号appId
channelId: channelId, // 频道号
timestamp: new Date().getTime(),// 时间戳
viewerId: userId, // 观看者用户Id
};
params.sign = getSign(params, appSecret);
$.ajax({
url: "https://api.polyv.net/live/v3/channel/watch/get-api-token",
type: "POST",
async: false,
data: params,
success: (data) => {
const token = data.data.token;
cb({viewerApiToken: token});
},
});
}
function getSign(obj, appSecret) {
const arr = Object.keys(obj)
.filter((item) => item !== 'sign')
.sort(); // 拿到除sign外字母顺序排列的key
let query = '';
arr.forEach((key) => {
let value = obj[key];
if (typeof value === 'object') {
value = JSON.stringify(value);
}
query += key + value;
});
// md5 加密
return md5(appSecret + query + appSecret)
.toString()
.toUpperCase();
}