ThirdParty.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. //ThirdParty.js
  2. //回调相关
  3. function CallLoginFunc(func, para) {
  4. if (window.LOG_NET_DATA) console.log("CallLoginFunc ", func, " : ", para)
  5. if (g_Login) {
  6. if (g_Login[func]) g_Login[func](para);
  7. } else {
  8. if (window.LOG_NET_DATA) console.log("g_Login is null");
  9. }
  10. }
  11. function CallLobbyFunc(func, para) {
  12. if (window.LOG_NET_DATA) console.log("CallLobbyFunc ", func, " : ", para)
  13. if (g_Lobby != null) {
  14. if (g_Lobby[func]) g_Lobby[func](para);
  15. }
  16. }
  17. function CallTableFunc(func, para) {
  18. if (window.LOG_NET_DATA) console.log("CallTableFunc ", func, " : ", para)
  19. if (g_Table != null) {
  20. if (g_Table[func]) g_Table[func](para);
  21. }
  22. }
  23. function CallUploadSuccess() {
  24. if (g_CurScene && g_CurScene.OnUpload_Success) g_CurScene.OnUpload_Success();
  25. }
  26. function CallUploadFaild() {
  27. if (g_CurScene && g_CurScene.OnUpload_Faild) g_CurScene.OnUpload_Faild();
  28. }
  29. function CallUpdateProgress(progress) {
  30. if(g_UpdateManager) g_UpdateManager.OnUpdateProgress(progress);
  31. }
  32. //
  33. bShowEndGame = false;
  34. function CallSystemBack() {
  35. if (bShowEndGame) return
  36. bShowEndGame = true;
  37. if (g_CurScene) {
  38. g_CurScene.ShowAlert('确定退出游戏?', Alert_YesNo, function (Res) {
  39. bShowEndGame = false;
  40. if (Res) ThirdPartyExitGame();
  41. });
  42. }
  43. }
  44. function CallOpenGPS(Param) {
  45. if (g_CurScene && g_CurScene.m_bTipGPS) {
  46. g_CurScene.ShowAlert('请开启手机GPS功能,不开启GPS无法显示GPS地址!', Alert_Yes, function (Res) {
  47. //ThirdPartyGetAddress(Number(Param) == 1 ? true : false);
  48. });
  49. }
  50. }
  51. function onPickImageResult(base64) {
  52. let imgSprite = null;
  53. let image = new Image();
  54. image.onload = function() {
  55. var texture = new Texture2D();
  56. texture.image = new ImageAsset(base64);
  57. let spriteFrame = new SpriteFrame();
  58. spriteFrame.texture = texture;
  59. imgSprite = spriteFrame;
  60. }
  61. image.src = "data:image/png;base64," + base64;
  62. }
  63. //#region 购买商品回调
  64. function CallBoughtGoodFunc(payData) {
  65. if (window.LOG_NET_DATA) console.log("CallBoughtGoodFunc ", payData);
  66. const callbackName = "onBuyGoodCallback"
  67. if (g_Lobby != null && g_Lobby[callbackName]) {
  68. g_Lobby[callbackName](payData);
  69. }
  70. }
  71. //原生功能
  72. function ThirdPartyGetBattery() {
  73. var pLv = 1;
  74. if (cc.sys.isNative) {
  75. if (cc.sys.OS_IOS == cc.sys.os) {
  76. pLv = jsb.reflection.callStaticMethod('AppController', "GetBatteryLv");
  77. } else {
  78. pLv = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', "GetBatteryLv", "()Ljava/lang/String;");
  79. }
  80. }
  81. return pLv;
  82. }
  83. function ThirdPartyGetAddress(bInLobby) {
  84. // if (bInLobby) {
  85. // if (cc.sys.OS_IOS == cc.sys.os) {
  86. // jsb.reflection.callStaticMethod("AppController", "getAddressInLobby");
  87. // } else {
  88. // jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getAddressInLobby', "()V");
  89. // }
  90. // } else {
  91. // if (cc.sys.OS_IOS == cc.sys.os) {
  92. // jsb.reflection.callStaticMethod("AppController", "getAddress");
  93. // } else {
  94. // jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getAddress', "()V");
  95. // }
  96. // }
  97. }
  98. function ThirdPartyOpenUrl(address) {
  99. if(cc.sys.isBrowser) {
  100. window.location.href = address;
  101. } else {
  102. if (cc.sys.OS_IOS == cc.sys.os) {
  103. jsb.reflection.callStaticMethod("AppController", "OpenUrl:", address);
  104. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  105. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
  106. "OpenUrl",
  107. "(Ljava/lang/String;)V",
  108. address);
  109. }
  110. }
  111. }
  112. function ThirdPartyOpenApp(address) {
  113. if (cc.sys.OS_IOS == cc.sys.os) {
  114. jsb.reflection.callStaticMethod("AppController", "OpenApp:", address);
  115. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  116. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
  117. "OpenApp",
  118. "(Ljava/lang/String;)V",
  119. address);
  120. }
  121. }
  122. function ThirdPartyCopyClipper(address) {
  123. if (cc.sys.isNative) {
  124. if (cc.sys.OS_IOS == cc.sys.os) {
  125. jsb.reflection.callStaticMethod("AppController", "CopyClipper:", address);
  126. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  127. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity",
  128. "CopyClipper",
  129. "(Ljava/lang/String;)V",
  130. address);
  131. }
  132. } else {
  133. var input = address + '';
  134. var el = document.createElement('textarea');
  135. el.value = input;
  136. el.setAttribute('readonly', '');
  137. el.style.contain = 'strict';
  138. el.style.position = 'absolute';
  139. el.style.left = '-9999px';
  140. el.style.fontSize = '12pt'; // Prevent zooming on iOS
  141. var selection = getSelection();
  142. var originalRange = false;
  143. if (selection.rangeCount > 0) originalRange = selection.getRangeAt(0);
  144. document.body.appendChild(el);
  145. el.select();
  146. el.selectionStart = 0;
  147. el.selectionEnd = input.length;
  148. try {
  149. document.execCommand('copy');
  150. } catch (err) {}
  151. document.body.removeChild(el);
  152. if (originalRange) {
  153. selection.removeAllRanges();
  154. selection.addRange(originalRange);
  155. }
  156. }
  157. }
  158. function ThirdPartyExitGame() {
  159. if (cc.sys.isNative) {
  160. if (cc.sys.OS_IOS == cc.sys.os) {
  161. jsb.reflection.callStaticMethod("AppController", "GameClose", "()V");
  162. } else {
  163. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "GameClose", "()V");
  164. }
  165. } else {
  166. if (cc.share.IsH5_WX()) {
  167. WeixinJSBridge.invoke('closeWindow', {}, function (res) {});
  168. }
  169. }
  170. }
  171. function ThirdPartyUpdateGame(url,apkName,ver) {
  172. if (cc.sys.OS_ANDROID == cc.sys.os) {
  173. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "updateVerion", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", url,apkName,ver);
  174. }
  175. }
  176. //#region google登录
  177. function ThirdPartyGoogleLogin() {
  178. if (cc.sys.isNative) {
  179. if (cc.sys.OS_IOS == cc.sys.os) {
  180. jsb.reflection.callStaticMethod("AppController", "sendGoogleLogin");
  181. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  182. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sendGoogleLogin", "()V");
  183. }
  184. }
  185. }
  186. //#endregion
  187. //#region line登录
  188. function ThirdPartyLINELogin() {
  189. if (cc.sys.isNative) {
  190. if (cc.sys.OS_IOS == cc.sys.os) {
  191. jsb.reflection.callStaticMethod("AppController", "sendLINELogin");
  192. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  193. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sendLINELogin", "()V");
  194. }
  195. }
  196. }
  197. //#endregion
  198. //#region apple登录
  199. function ThirdPartyIOSLogin() {
  200. if (cc.sys.isNative) {
  201. if (cc.sys.OS_IOS == cc.sys.os) {
  202. jsb.reflection.callStaticMethod("AppController", "sendAppleLogin");
  203. }
  204. }
  205. }
  206. //#endregion
  207. //#region 微信登录
  208. function ThirdPartyWXLogin() {
  209. if (cc.sys.isNative) {
  210. if (cc.sys.OS_IOS == cc.sys.os) {
  211. jsb.reflection.callStaticMethod("AppController", "sendWXLogin");
  212. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  213. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sendWXLogin", "()V");
  214. }
  215. }
  216. }
  217. //#endregion
  218. //#region 商品购买
  219. //goodInfo { itemID }
  220. function ThirdPartyBuyGood(goodInfo) {
  221. if (cc.sys.isNative) {
  222. if (cc.sys.OS_IOS == cc.sys.os) {
  223. jsb.reflection.callStaticMethod("AppController", "sendBuyGood:", goodInfo);
  224. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  225. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sendBuyGood", "(Ljava/lang/String;)V", goodInfo);
  226. }
  227. }
  228. }
  229. //#endregion
  230. function ThirdPartyShareMessage(ShareInfo, isLine) {
  231. if (window.LOG_NET_DATA) console.log("WXShare ", isLine, " ", ShareInfo)
  232. if (cc.sys.isNative) {
  233. if (cc.sys.OS_IOS == cc.sys.os) {
  234. jsb.reflection.callStaticMethod("AppController", "WXSharetitle:description:url:IsTimeLine:", ShareInfo.title, ShareInfo.desc,
  235. ShareInfo.link, isLine);
  236. } else {
  237. jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'WXShare',
  238. "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
  239. ShareInfo.title, ShareInfo.desc,
  240. ShareInfo.link, isLine);
  241. }
  242. } else {
  243. if (cc.share.IsH5_WX()) {
  244. if (isLine == 1)
  245. wx.updateTimelineShareData(ShareInfo);
  246. else
  247. wx.updateAppMessageShareData(ShareInfo);
  248. } else {
  249. return false;
  250. }
  251. }
  252. return true
  253. }
  254. function ThirdPartyShareImg(Path, isLine) {
  255. if (window.LOG_NET_DATA) console.log("WXShareTex ", Path)
  256. isLine = isLine?isLine:'';
  257. if (cc.sys.isNative) {
  258. if (cc.sys.OS_IOS == cc.sys.os) {
  259. jsb.reflection.callStaticMethod("AppController", "WXShareTex:IsTimeLine:", Path, isLine);
  260. } else {
  261. jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'WXShareTex',
  262. "(Ljava/lang/String;Ljava/lang/String;)V",
  263. Path, isLine);
  264. }
  265. }
  266. }
  267. function ThirdPartyWXPay(Info) {
  268. if (window.LOG_NET_DATA) console.log("ThirdPartyWXPay " + Info)
  269. if (cc.sys.isNative) {
  270. if (cc.sys.OS_IOS == cc.sys.os) {
  271. jsb.reflection.callStaticMethod("AppController", "WXPay:", Info);
  272. } else {
  273. jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', "WXPay",
  274. "(Ljava/lang/String;)V", Info);
  275. }
  276. }
  277. }
  278. function ThirdPartyGVoiceOnRecord() {
  279. if (cc.sys.OS_IOS == cc.sys.os) {
  280. return jsb.reflection.callStaticMethod("AppController", "onRecord");
  281. } else {
  282. return jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "onRecord", "()Ljava/lang/String;");
  283. }
  284. }
  285. function ThirdPartyGVoicePlayVoice(Path) {
  286. if (cc.sys.OS_IOS == cc.sys.os) {
  287. jsb.reflection.callStaticMethod("AppController", "onPlay:", Path);
  288. } else {
  289. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "onPlay", "(Ljava/lang/String;)V", Path);
  290. }
  291. }
  292. ///////////////////////////////////////////////////////////////////////////
  293. //全平台语音
  294. function ThirdPartyVoiceInit(Hook) {
  295. if(cc.share.IsH5_WX()) {
  296. return (typeof wx != 'undefined');
  297. } else {
  298. if (cc.sys.isNative) return true;
  299. }
  300. }
  301. //开始录制
  302. function ThirdPartyVoiceOnRecord(Hook) {
  303. if (cc.sys.isNative) {
  304. if (cc.sys.OS_IOS == cc.sys.os) {
  305. return jsb.reflection.callStaticMethod("AppController", "onRecord");
  306. } else {
  307. return jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "startRecord", "()Ljava/lang/String;");
  308. }
  309. } else {
  310. if (typeof wx == 'undefined') {
  311. } else {
  312. wx.startRecord({
  313. success: function () {
  314. Hook.ShowRecording(true);
  315. },
  316. });
  317. }
  318. return '';
  319. }
  320. }
  321. //结束录制
  322. function ThirdPartyVoiceStopRecord(Hook) {
  323. if (cc.sys.isNative) {
  324. if (cc.sys.OS_IOS == cc.sys.os) {
  325. jsb.reflection.callStaticMethod("AppController", "onStopRecord:", "()V");
  326. } else {
  327. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "onStopRecord", "()V");
  328. }
  329. } else {
  330. if (typeof wx == 'undefined') {
  331. } else {
  332. var voice = {
  333. localId: '',
  334. serverId: '',
  335. viewID: 65535,
  336. };
  337. wx.stopRecord({
  338. success: function (res1) {
  339. voice.localId = res1.localId;
  340. wx.uploadVoice({
  341. localId: voice.localId,
  342. success: function (res2) {
  343. voice.serverId = res2.serverId;
  344. Hook.OnRecordOver(voice);
  345. }
  346. });
  347. }
  348. });
  349. }
  350. }
  351. }
  352. //上传结果
  353. function ThirdPartyVoiceOnUpload(Hook, Path) {
  354. if (cc.sys.isNative) {
  355. if (cc.sys.OS_IOS == cc.sys.os) {
  356. jsb.reflection.callStaticMethod("AppController", "onDownLoad:", VID);
  357. } else {
  358. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "onDownLoad", "(Ljava/lang/String;)V", VID);
  359. }
  360. } else {
  361. if (typeof wx == 'undefined') {
  362. return false;
  363. } else {
  364. return true;
  365. }
  366. }
  367. }
  368. function ThirdPartyGVoiceLoadVoice(VID) {
  369. if (cc.sys.OS_IOS == cc.sys.os) {
  370. jsb.reflection.callStaticMethod("AppController", "onDownLoad:", VID);
  371. } else {
  372. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "onDownLoad", "(Ljava/lang/String;)V", VID);
  373. }
  374. }
  375. /////////////////
  376. cc.voice = {
  377. MaxRecordTime: 59000,
  378. _startTime: 0,
  379. IsPlaying: false,
  380. IsRecording: false,
  381. _voiceH5: {},
  382. _queue: [],
  383. _ctrl: null,
  384. _engine: null,
  385. Type: cc.Enum({
  386. NULL: 0,
  387. RECORD_START: 1,
  388. RECORD_STOP: 2,
  389. PLAY: 3,
  390. PLAY_ALL_FINISH: 4
  391. }),
  392. Init: function (VoiceCtrl, GameEngine) {
  393. this._ctrl = VoiceCtrl;
  394. this._engine = GameEngine;
  395. if (cc.share.IsH5_WX()) {
  396. return (typeof wx != 'undefined');
  397. } else {
  398. if (cc.sys.isNative) return true;
  399. }
  400. },
  401. GetRecordTime: function () {
  402. if (!this._startTime) return null;
  403. return new Date().getTime() - this._startTime;
  404. },
  405. startRecord: function () {
  406. try {
  407. if (cc.share.IsH5_WX()) {
  408. if (typeof wx == 'undefined') {
  409. this._ctrl.onDispose(false, this.Type.RECORD_START);
  410. } else {
  411. wx.startRecord({
  412. success: function () {
  413. this.IsRecording = true;
  414. this._startTime = new Date().getTime();
  415. this._ctrl.onDispose(true, this.Type.RECORD_START);
  416. }.bind(this),
  417. fail: function () {
  418. this._ctrl.onDispose(false, this.Type.RECORD_START);
  419. }.bind(this),
  420. });
  421. }
  422. } else if (cc.sys.isNative) {
  423. this.IsRecording = true;
  424. this._startTime = new Date().getTime();
  425. this._ctrl.onDispose(true, this.Type.RECORD_START);
  426. if (cc.sys.OS_IOS == cc.sys.os) {
  427. jsb.reflection.callStaticMethod("AppController", "startRecord");
  428. } else {
  429. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "startRecord", "()V");
  430. }
  431. }
  432. } catch (e) {
  433. if (window.LOG_DEBUG) console.log(e);
  434. }
  435. },
  436. stopRecord: function () {
  437. try {
  438. if (this._startTime == null) return;
  439. this.IsRecording = false;
  440. var recordTime = new Date().getTime() - this._startTime;
  441. var str = (recordTime < 1000 ? '0' : '1');
  442. this._startTime = null;
  443. if (cc.share.IsH5_WX()) {
  444. if (typeof wx == 'undefined') {
  445. return this._ctrl.onDispose(false, this.Type.RECORD_STOP);
  446. } else if (str == '0' || typeof wx == 'undefined') {
  447. wx.stopRecord({
  448. success: function () {
  449. this._ctrl.onDispose(true, this.Type.RECORD_STOP);
  450. }.bind(this)
  451. });
  452. return this._ctrl.onDispose(false, this.Type.RECORD_STOP);
  453. } else {
  454. wx.stopRecord({
  455. success: function (res1) {
  456. this._ctrl.onDispose(true, this.Type.RECORD_STOP);
  457. wx.uploadVoice({
  458. localId: res1.localId,
  459. success: function (res2) {
  460. this._voiceH5[res2.serverId] = res1.localId;
  461. this.send(1, res2.serverId);
  462. }.bind(this)
  463. });
  464. }.bind(this)
  465. });
  466. }
  467. } else if (cc.sys.isNative) {
  468. var res = '';
  469. if (cc.sys.OS_IOS == cc.sys.os) { // IOS端需要走回调完成结束
  470. res = jsb.reflection.callStaticMethod("AppController", "stopRecord:", str);
  471. this._ctrl.onDispose(true, this.Type.RECORD_STOP);
  472. } else {
  473. res = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "stopRecord", "(Ljava/lang/String;)Ljava/lang/String;", str);
  474. if (str == '0') {
  475. return this._ctrl.onDispose(false, this.Type.RECORD_STOP);
  476. } else {
  477. this._ctrl.onDispose(true, this.Type.RECORD_STOP);
  478. this.send(0, res);
  479. }
  480. }
  481. }
  482. } catch (e) {
  483. if (window.LOG_DEBUG) console.log(e);
  484. }
  485. },
  486. onRecordFinish: function (res) { // IOS 回调
  487. if (res == '') {
  488. return this._ctrl.onDispose(false, this.Type.RECORD_STOP);
  489. }
  490. this.send(0, res);
  491. },
  492. receive: function (pVoice) {
  493. try {
  494. if (!!!this._queue) this._queue = [];
  495. // 原生
  496. if (pVoice.cbPlatform == 0) {
  497. if (!cc.share.IsH5_WX()) {
  498. if (!!!this._voice) this._voice = [];
  499. if (!!!this._voice[pVoice.dwSendUserID]) this._voice[pVoice.dwSendUserID] = '';
  500. this._voice[pVoice.dwSendUserID] += (pVoice.szVID.replace(/\n/g, ''));
  501. if (pVoice.bFinish) {
  502. this._pushVoice(pVoice.cbPlatform, pVoice.dwSendUserID, pVoice.dwTargetUserID, this._voice[pVoice.dwSendUserID]);
  503. this._voice[pVoice.dwSendUserID] = '';
  504. this._next();
  505. }
  506. }
  507. } else { // 微信H5
  508. if (cc.share.IsH5_WX()) {
  509. if (!!this._voiceH5[pVoice.szVID]) {
  510. this._pushVoice(pVoice.cbPlatform, pVoice.dwSendUserID, pVoice.dwTargetUserID, this._voiceH5[pVoice.szVID]);
  511. this._next();
  512. } else {
  513. wx.downloadVoice({
  514. serverId: pVoice.szVID,
  515. success: function (res) {
  516. this._voiceH5[pVoice.szVID] = res.localId;
  517. this._pushVoice(pVoice.cbPlatform, pVoice.dwSendUserID, pVoice.dwTargetUserID, this._voiceH5[pVoice.szVID]);
  518. this._next();
  519. }.bind(this)
  520. });
  521. }
  522. }
  523. }
  524. } catch (e) {
  525. if (window.LOG_DEBUG) console.log(e);
  526. }
  527. },
  528. _pushVoice: function (cbPlatform, dwSendUserID, dwTargetUserID, szVID) {
  529. this._queue.push({
  530. cbPlatform: cbPlatform,
  531. dwSendUserID: dwSendUserID,
  532. dwTargetUserID: dwTargetUserID,
  533. szVID: szVID,
  534. });
  535. },
  536. send: function (platform, str) {
  537. try {
  538. if (platform == 0) { // 原生
  539. while (str.length != 0) {
  540. if (str.length > 255) {
  541. this._engine.OnSendUserVoice(platform, str.slice(0, 255), false);
  542. str = str.slice(255);
  543. } else {
  544. this._engine.OnSendUserVoice(platform, str.slice(0), true);
  545. str = '';
  546. }
  547. }
  548. } else {
  549. this._engine.OnSendUserVoice(platform, str, true);
  550. }
  551. } catch (e) {
  552. if (window.LOG_DEBUG) console.log(e);
  553. }
  554. },
  555. _next: function () {
  556. if (this._queue.length > 0) {
  557. this.IsPlaying = true;
  558. return this.playVoice(this._queue.shift());
  559. } else {
  560. this.IsPlaying = false;
  561. // this._ctrl.onFinish();
  562. this._ctrl.onDispose(true, this.Type.PLAY_ALL_FINISH);
  563. }
  564. },
  565. // platform 0原生 1微信H5
  566. playVoice: function (data) {
  567. try {
  568. var platform = data.cbPlatform;
  569. var content = data.szVID;
  570. if (window.LOG_DEBUG) console.log(data);
  571. if (platform == 0) { // 原生
  572. if (cc.share.IsH5_WX()) return this.onPlayFinish();
  573. if (cc.sys.isNative) { // 原生播放
  574. // this._ctrl.onPlay(data);
  575. this._ctrl.onDispose(true, this.Type.PLAY, data);
  576. if (cc.sys.OS_IOS == cc.sys.os) {
  577. jsb.reflection.callStaticMethod("AppController", "playVoice:", content);
  578. } else {
  579. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "playVoice", "(Ljava/lang/String;)V", content);
  580. }
  581. } else { // 浏览器播放
  582. var video = document.createElement('video');
  583. video.src = 'data:audio/mp4;base64,' + content;
  584. video.autoplay = true;
  585. // this._ctrl.onPlay(data);
  586. this._ctrl.onDispose(true, this.Type.PLAY, data);
  587. video.addEventListener("ended", function () {
  588. video.remove();
  589. this.onPlayFinish();
  590. }.bind(this), false);
  591. }
  592. } else { // 微信H5
  593. if (!cc.share.IsH5_WX()) return this.onPlayFinish();
  594. if (typeof wx == 'undefined') {
  595. if (!!finishFunc) finishFunc();
  596. } else { // 微信播放
  597. wx.playVoice({
  598. localId: data.szVID,
  599. success: function () {
  600. // this._ctrl.onPlay(data);
  601. this._ctrl.onDispose(true, this.Type.PLAY, data);
  602. }.bind(this)
  603. });
  604. wx.onVoicePlayEnd({
  605. complete: function (res) {
  606. this.onPlayFinish();
  607. }.bind(this)
  608. });
  609. }
  610. }
  611. } catch (e) {
  612. if (window.LOG_DEBUG) console.log(e);
  613. }
  614. },
  615. onPlayFinish: function () { // 原生回调
  616. this._next();
  617. },
  618. }
  619. // 语音base64
  620. function ThirdPartyStartRecord(success, fail) {
  621. if(cc.share.IsH5_WX()) {
  622. if (typeof wx == 'undefined') {
  623. } else {
  624. wx.startRecord({
  625. success: function () {
  626. if(success) success();
  627. },
  628. fail: function() {
  629. if(fail) fail();
  630. },
  631. });
  632. }
  633. } else if (cc.sys.isNative) {
  634. if (cc.sys.OS_IOS == cc.sys.os) {
  635. jsb.reflection.callStaticMethod("AppController", "startRecord");
  636. } else {
  637. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "startRecord", "()V");
  638. }
  639. }
  640. }
  641. function ThirdPartyStopRecord(str) {
  642. var res = '';
  643. if (cc.sys.isNative) {
  644. if (cc.sys.OS_IOS == cc.sys.os) {
  645. res = jsb.reflection.callStaticMethod("AppController", "stopRecord:", str);
  646. } else {
  647. res = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "stopRecord", "(Ljava/lang/String;)Ljava/lang/String;", str);
  648. }
  649. }
  650. return res;
  651. }
  652. function ThirdPartyPlayVoice(str) {
  653. if (window.LOG_NET_DATA) console.log(str);
  654. if (cc.sys.isNative) {
  655. if (cc.sys.OS_IOS == cc.sys.os) {
  656. jsb.reflection.callStaticMethod("AppController", "playVoice:", str);
  657. } else {
  658. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "playVoice", "(Ljava/lang/String;)V", str);
  659. }
  660. } else {
  661. var video = document.createElement('video');
  662. video.src = 'data:audio/mp4;base64,' + str;
  663. video.autoplay = true;
  664. video.addEventListener("ended", function () {
  665. video.remove();
  666. g_Table.OnPlayFinish();
  667. }, false)
  668. if (typeof wx == 'undefined') {
  669. } else {
  670. }
  671. }
  672. }
  673. function ThirdPartyPickImgNew(type, needClip, clipSize) {
  674. const ChoosephotoType = {
  675. Album: 1,
  676. Camera: 2
  677. };
  678. if (needClip == undefined) {
  679. needClip = false;
  680. }
  681. var clipX = 0;
  682. var clipY = 0;
  683. if (clipSize) {
  684. clipX = clipSize.x;
  685. clipY = clipSize.y;
  686. }
  687. var dict = {
  688. needClip: needClip, //1是裁剪,2是不裁剪
  689. clipX: clipX, //裁剪x尺寸
  690. clipY: clipY, //数剪y尺寸
  691. }
  692. if (cc.sys.os === cc.sys.OS_ANDROID) {
  693. var methodName = "pickImage";
  694. if (type == ChoosephotoType.Album) {
  695. methodName ="useSystemAlbum";
  696. } else if (type == ChoosephotoType.Camera) {
  697. methodName ="useSystemCamera";
  698. }
  699. var ret = jsb.reflection.callstaticMethod("org/cocos2dx/javascript/AppActivity", methodName, "(Ljava/lang/string;)v", JSON.stringify(dict));
  700. console.log("androidTakePhotoret:", ret);
  701. return ret;
  702. } else if (cc.sys.os === cc.sys.OS_IOS) {
  703. console.log("js 请求相册");
  704. var ret = null;
  705. if (type == ChoosephotoType.Album) {
  706. ret = jsb.reflection.callstaticMethod("Appcontroller", "pickImage:", JSON.stringify(dict));
  707. } else if (type == ChoosephotoType.Camera) {
  708. ret = jsb.reflection.callstaticMethod("Appcontroller", "takePhoto:", JSON.stringify(dict));
  709. }
  710. return ret;
  711. }
  712. return true
  713. }
  714. function ThirdPartyPickImg(JsonObj, szNeedChip) {
  715. JsonObj.URL = window.PHP_UPLOAD_URL;
  716. JsonObj.PickEndCallback = 'OnPhotoPickEnd';
  717. if (cc.sys.isNative) {
  718. if (cc.sys.OS_IOS == cc.sys.os) {
  719. // jsb.reflection.callStaticMethod("AppController","WXSharetitle:description:url:IsTimeLine:",ShareInfo.title,ShareInfo.desc,ShareInfo.link, isLine);
  720. jsb.reflection.callStaticMethod("RootViewController", "pickImg:description:", JSON.stringify(JsonObj), szNeedChip);
  721. console.log(' ### ios ThirdPartyPickImg ' + JSON.stringify(JsonObj));
  722. } else {
  723. jsb.reflection.callStaticMethod(
  724. 'org/cocos2dx/javascript/AppActivity', 'pickImg', "(Ljava/lang/String;Ljava/lang/String;)V", JSON.stringify(JsonObj), szNeedChip
  725. );
  726. }
  727. } else {
  728. if (cc.share.IsH5_WX()) {
  729. //
  730. } else {
  731. return false;
  732. }
  733. }
  734. return true
  735. }
  736. function Restart (e) {
  737. if(window.LOG_DEBUG && e) console.log(e);
  738. cc.game.restart();
  739. }