-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathezuikit-talk.js
More file actions
225 lines (218 loc) · 6.68 KB
/
ezuikit-talk.js
File metadata and controls
225 lines (218 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* ezuikit-talk v0.0.1-beta
*/
(function (global, factory) {
"use strict";
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = global.document ?
factory(global, true) :
function (w) {
if (!w.document) {
throw new Error("EZUIPlayer requires a window with a document");
}
return factory(w);
};
} else {
factory(global);
}
// Pass this if window is not defined yet
})(typeof window !== "undefined" ? window : this, function (window, noGlobal) {
function PrefixCode(code,msg) {
if(parseInt(code,10) > 0){
var PRECODE = 102;
var retcode = '102' + (code/Math.pow(10,5)).toFixed(5).substr(2);
} else if (code == -1) {
retcode = -1;
}else if (typeof code === 'undefined'){
retcode = 0;
}
return {
code : retcode,
data: msg,
}
// function PrefixInteger(num, length) {
// return (num/Math.pow(10,length)).toFixed(length).substr(2);
// }
}
// 加载js
function addJs(filepath, callback) {
var oJs = document.createElement("script");
oJs.setAttribute("src", filepath);
oJs.onload = callback;
document.getElementsByTagName("head")[0].appendChild(oJs);
}
// 通用请求方法
function request(url, method, params, header, success, error) {
var _url = url;
var http_request = new XMLHttpRequest();
http_request.onreadystatechange = function () {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
var _data = JSON.parse(http_request.responseText);
success(_data);
}
}
};
http_request.open(method, _url, true);
// http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var data = new FormData();
for (i in params) {
data.append(i, params[i]);
}
http_request.send(data);
};
var EZUITalk = function (params) {
this.params = params;
this.opt = {
apiDomain: 'https://open.ys7.com/api/lapp/live/talk/url',
filePath: '',
accessToken: undefined,
deviceSerial: undefined,
channelNo: undefined,
talkLink: '',
rtcUrl: '',
ttsUrl: '',
stream: '',
}
if(params.accessToken){
this.opt.accessToken = params.accessToken;
}
if(params.url){
this.opt.deviceSerial = params.url.split("/")[3];
this.opt.channelNo = params.url.split("/")[4].split(".")[0];
}
if(params.filePath){
this.opt.filePath = params.filePath;
}
var _this = this;
function apiSuccess(data){
console.log("data",data);
if(data.code == 200){
var apiResult = data.data;
if(apiResult){
// 临时将https转换为websocket
var rtcTrunk = apiResult.rtcUrl;
if(rtcTrunk.indexOf("ws") === -1){
rtcTrunk = rtcTrunk.replace("https","wss").replace("rtcgw","rtcgw-ws");
}
_this.opt.rtcUrl = rtcTrunk;
_this.opt.ttsUrl = "tts://" + apiResult.ttsUrl;
var talk = "talk://" + _this.opt.deviceSerial + ":0:" + _this.opt.channelNo + ":cas.ys7.com:6500";
_this.opt.talkLink = _this.opt.ttsUrl + "/" + talk;
_this.opt.stream = apiResult.stream;
console.log("_this.opt",_this.opt)
// 加载依赖
// this.init();
var adapeterJS = _this.opt.filePath + '/adapeter.js';
var janusJS = _this.opt.filePath + '/talk-rtcgw.js';
var ttsJS = _this.opt.filePath + '/talk-tts.js';
console.log("加载jquery.js");
addJs(adapeterJS,function(){
console.log("加载adapeter.js");
addJs(janusJS,function(){
console.log("加载janus.js");
addJs(ttsJS,function(){
console.log("加载tts.js");
// 文件加载完毕;
if (typeof params.onMessage === 'function') {
params.onMessage({
code : 10200001,
data: "插件加载成功"
});
}
})
})
})
} else {
if (typeof params.onError === 'function') {
params.onError(PrefixCode(data.code,data));
}
}
} else {
if (typeof params.onError === 'function') {
params.onError(PrefixCode(data.code,data));
}
}
}
function apiError(err){
if(params.onError){
params.onError(err);
}
}
request(
this.opt.apiDomain,
'POST',
{
accessToken: this.opt.accessToken,
deviceSerial: this.opt.deviceSerial,
channelNo: this.opt.channelNo
},
'',
apiSuccess,
apiError
);
console.log("this.opt",this.opt)
}
// EZUITalk.prototype.init = function () {
// console.log();
// $.ajax({
// type: 'POST',
// url: 'https://test12open.ys7.com/api/lapp/live/talk/url',
// // contentType: "application/json;charset=utf-8",
// dataType: 'json',
// data: {
// accessToken: accessToken,
// deviceSerial: serial,
// channelNo: channelNo,
// },
// success: function (data) {
// log("对讲api调用成功" + JSON.stringify(data));
// if (data.code == 200) {
// var result = data.data;
// ttsUrl = result.ttsUrl,
// rtcUrl = "wss://test12.ys7.com/rtcgw-ws", //result.ttsUrl
// stream = result.stream;
// $('#tts_url').attr("value", matchTalkLink);
// $("#start").attr('disabled', false);
// } else {
// }
// },
// error: function (err) {
// },
// })
// }
// this.prototype.init = function(){
// var adapeterJS = this.opt.filepath + '/js/adapeter.js';
// addJs(adapeterJS,function(){
// console.log("加载adapeter.js")
// })
// }
EZUITalk.prototype.startTalk = function(){
var _this = this;
var apiSuccess = function(data) {
if (data.code == 200) {
var apiResult = data.data;
if (apiResult) {
// 临时将https转换为websocket
_this.opt.stream = apiResult.stream;
window.startTalk();
}
}
}
var apiError = function() {
layer.msg("获取对讲token失败")
}
request(_this.opt.apiDomain, 'POST', {
accessToken: _this.opt.accessToken,
deviceSerial: _this.opt.deviceSerial,
channelNo: _this.opt.channelNo
}, '', apiSuccess, apiError);
}
EZUITalk.prototype.stopTalk = function(){
window.stopTalk();
}
if (!noGlobal) {
window.EZUITalk = EZUITalk;
}
return EZUITalk;
})