ezuikit-talk.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * ezuikit-talk v0.0.1-beta
  3. */
  4. (function (global, factory) {
  5. "use strict";
  6. if (typeof module === "object" && typeof module.exports === "object") {
  7. module.exports = global.document ?
  8. factory(global, true) :
  9. function (w) {
  10. if (!w.document) {
  11. throw new Error("EZUIPlayer requires a window with a document");
  12. }
  13. return factory(w);
  14. };
  15. } else {
  16. factory(global);
  17. }
  18. // Pass this if window is not defined yet
  19. })(typeof window !== "undefined" ? window : this, function (window, noGlobal) {
  20. // 加载js
  21. function addJs(filepath, callback) {
  22. var oJs = document.createElement("script");
  23. oJs.setAttribute("src", filepath);
  24. oJs.onload = callback;
  25. document.getElementsByTagName("head")[0].appendChild(oJs);
  26. }
  27. // 通用请求方法
  28. function request(url, method, params, header, success, error) {
  29. var _url = url;
  30. var http_request = new XMLHttpRequest();
  31. http_request.onreadystatechange = function () {
  32. if (http_request.readyState == 4) {
  33. if (http_request.status == 200) {
  34. var _data = JSON.parse(http_request.responseText);
  35. success(_data);
  36. }
  37. }
  38. };
  39. http_request.open(method, _url, true);
  40. // http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  41. var data = new FormData();
  42. for (i in params) {
  43. data.append(i, params[i]);
  44. }
  45. http_request.send(data);
  46. };
  47. var EZUITalk = function (params) {
  48. console.log("params", params);
  49. // this.opt = {
  50. // apiDomain: 'https://test12open.ys7.com/api/lapp/live/talk/url'
  51. // }
  52. this.opt = {
  53. apiDomain: 'https://open.ys7.com/api/lapp/live/talk/url',
  54. filePath: '',
  55. accessToken: undefined,
  56. deviceSerial: undefined,
  57. channelNo: undefined,
  58. talkLink: '',
  59. rtcUrl: '',
  60. ttsUrl: '',
  61. stream: '',
  62. }
  63. if (params.accessToken) {
  64. this.opt.accessToken = params.accessToken;
  65. }
  66. if (params.url) {
  67. this.opt.deviceSerial = params.url.split("/")[3];
  68. this.opt.channelNo = params.url.split("/")[4].split(".")[0];
  69. }
  70. if (params.filePath) {
  71. this.opt.filePath = params.filePath;
  72. }
  73. var _this = this;
  74. function apiSuccess(data) {
  75. console.log("data", data);
  76. if (data.code == 200) {
  77. var apiResult = data.data;
  78. if (apiResult) {
  79. // 临时将https转换为websocket
  80. var rtcTrunk = apiResult.rtcUrl;
  81. if (rtcTrunk.indexOf("ws") === -1) {
  82. rtcTrunk = rtcTrunk.replace("https", "wss").replace("rtcgw", "rtcgw-ws");
  83. }
  84. _this.opt.rtcUrl = rtcTrunk;
  85. _this.opt.ttsUrl = "tts://" + apiResult.ttsUrl;
  86. var talk = "talk://" + _this.opt.deviceSerial + ":0:" + _this.opt.channelNo + ":cas.ys7.com:6500";
  87. _this.opt.talkLink = _this.opt.ttsUrl + "/" + talk;
  88. _this.opt.stream = apiResult.stream;
  89. console.log("_this.opt", _this.opt)
  90. // 加载依赖
  91. // this.init();
  92. var adapeterJS = _this.opt.filePath + '/adapeter.js';
  93. var janusJS = _this.opt.filePath + '/janus.js';
  94. var ttsJS = _this.opt.filePath + '/tts.js';
  95. console.log("加载jquery.js");
  96. addJs(adapeterJS, function () {
  97. console.log("加载adapeter.js");
  98. addJs(janusJS, function () {
  99. console.log("加载janus.js");
  100. addJs(ttsJS, function () {
  101. console.log("加载tts.js");
  102. // 文件加载完毕;
  103. })
  104. })
  105. })
  106. }
  107. }
  108. }
  109. function apiError(err) {
  110. if (params.handleError) {
  111. params.handleError(err);
  112. }
  113. }
  114. request(
  115. this.opt.apiDomain,
  116. 'POST',
  117. {
  118. accessToken: this.opt.accessToken,
  119. deviceSerial: this.opt.deviceSerial,
  120. channelNo: this.opt.channelNo
  121. },
  122. '',
  123. apiSuccess,
  124. apiError
  125. );
  126. console.log("this.opt", this.opt)
  127. }
  128. // EZUITalk.prototype.init = function () {
  129. // console.log();
  130. // $.ajax({
  131. // type: 'POST',
  132. // url: 'https://test12open.ys7.com/api/lapp/live/talk/url',
  133. // // contentType: "application/json;charset=utf-8",
  134. // dataType: 'json',
  135. // data: {
  136. // accessToken: accessToken,
  137. // deviceSerial: serial,
  138. // channelNo: channelNo,
  139. // },
  140. // success: function (data) {
  141. // log("对讲api调用成功" + JSON.stringify(data));
  142. // if (data.code == 200) {
  143. // var result = data.data;
  144. // ttsUrl = result.ttsUrl,
  145. // rtcUrl = "wss://test12.ys7.com/rtcgw-ws", //result.ttsUrl
  146. // stream = result.stream;
  147. // $('#tts_url').attr("value", matchTalkLink);
  148. // $("#start").attr('disabled', false);
  149. // } else {
  150. // }
  151. // },
  152. // error: function (err) {
  153. // },
  154. // })
  155. // }
  156. // this.prototype.init = function(){
  157. // var adapeterJS = this.opt.filepath + '/js/adapeter.js';
  158. // addJs(adapeterJS,function(){
  159. // console.log("加载adapeter.js")
  160. // })
  161. // }
  162. EZUITalk.prototype.startTalk = function () {
  163. window.startTalk();
  164. }
  165. EZUITalk.prototype.stopTalk = function () {
  166. window.stopTalk();
  167. }
  168. if (!noGlobal) {
  169. window.EZUITalk = EZUITalk;
  170. }
  171. return EZUITalk;
  172. })