ThirdParty.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. function ThirdPartyReturnPayReceiptVerification(info) {
  230. if (cc.sys.isNative) {
  231. if (cc.sys.OS_IOS == cc.sys.os) {
  232. jsb.reflection.callStaticMethod("AppController", "sendReceiptVerification:", info);
  233. } else if (cc.sys.OS_ANDROID == cc.sys.os) {
  234. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "sendReceiptVerification", "(Ljava/lang/String;)V", goodInfo);
  235. }
  236. }
  237. }
  238. //#endregion
  239. function ThirdPartyShareMessage(ShareInfo, isLine) {
  240. if (window.LOG_NET_DATA) console.log("WXShare ", isLine, " ", ShareInfo)
  241. if (cc.sys.isNative) {
  242. if (cc.sys.OS_IOS == cc.sys.os) {
  243. jsb.reflection.callStaticMethod("AppController", "WXSharetitle:description:url:IsTimeLine:", ShareInfo.title, ShareInfo.desc,
  244. ShareInfo.link, isLine);
  245. } else {
  246. jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'WXShare',
  247. "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
  248. ShareInfo.title, ShareInfo.desc,
  249. ShareInfo.link, isLine);
  250. }
  251. } else {
  252. if (cc.share.IsH5_WX()) {
  253. if (isLine == 1)
  254. wx.updateTimelineShareData(ShareInfo);
  255. else
  256. wx.updateAppMessageShareData(ShareInfo);
  257. } else {
  258. return false;
  259. }
  260. }
  261. return true
  262. }
  263. function ThirdPartyShareImg(Path, isLine) {
  264. if (window.LOG_NET_DATA) console.log("WXShareTex ", Path)
  265. isLine = isLine?isLine:'';
  266. if (cc.sys.isNative) {
  267. if (cc.sys.OS_IOS == cc.sys.os) {
  268. jsb.reflection.callStaticMethod("AppController", "WXShareTex:IsTimeLine:", Path, isLine);
  269. } else {
  270. jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'WXShareTex',
  271. "(Ljava/lang/String;Ljava/lang/String;)V",
  272. Path, isLine);
  273. }
  274. }
  275. }
  276. function ThirdPartyWXPay(Info) {
  277. if (window.LOG_NET_DATA) console.log("ThirdPartyWXPay " + Info)
  278. if (cc.sys.isNative) {
  279. if (cc.sys.OS_IOS == cc.sys.os) {
  280. jsb.reflection.callStaticMethod("AppController", "WXPay:", Info);
  281. } else {
  282. jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', "WXPay",
  283. "(Ljava/lang/String;)V", Info);
  284. }
  285. }
  286. }
  287. function ThirdPartyGVoiceOnRecord() {
  288. if (cc.sys.OS_IOS == cc.sys.os) {
  289. return jsb.reflection.callStaticMethod("AppController", "onRecord");
  290. } else {
  291. return jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "onRecord", "()Ljava/lang/String;");
  292. }
  293. }
  294. function ThirdPartyGVoicePlayVoice(Path) {
  295. if (cc.sys.OS_IOS == cc.sys.os) {
  296. jsb.reflection.callStaticMethod("AppController", "onPlay:", Path);
  297. } else {
  298. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "onPlay", "(Ljava/lang/String;)V", Path);
  299. }
  300. }
  301. ///////////////////////////////////////////////////////////////////////////
  302. //全平台语音
  303. function ThirdPartyVoiceInit(Hook) {
  304. if(cc.share.IsH5_WX()) {
  305. return (typeof wx != 'undefined');
  306. } else {
  307. if (cc.sys.isNative) return true;
  308. }
  309. }
  310. //开始录制
  311. function ThirdPartyVoiceOnRecord(Hook) {
  312. if (cc.sys.isNative) {
  313. if (cc.sys.OS_IOS == cc.sys.os) {
  314. return jsb.reflection.callStaticMethod("AppController", "onRecord");
  315. } else {
  316. return jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "startRecord", "()Ljava/lang/String;");
  317. }
  318. } else {
  319. if (typeof wx == 'undefined') {
  320. } else {
  321. wx.startRecord({
  322. success: function () {
  323. Hook.ShowRecording(true);
  324. },
  325. });
  326. }
  327. return '';
  328. }
  329. }
  330. //结束录制
  331. function ThirdPartyVoiceStopRecord(Hook) {
  332. if (cc.sys.isNative) {
  333. if (cc.sys.OS_IOS == cc.sys.os) {
  334. jsb.reflection.callStaticMethod("AppController", "onStopRecord:", "()V");
  335. } else {
  336. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "onStopRecord", "()V");
  337. }
  338. } else {
  339. if (typeof wx == 'undefined') {
  340. } else {
  341. var voice = {
  342. localId: '',
  343. serverId: '',
  344. viewID: 65535,
  345. };
  346. wx.stopRecord({
  347. success: function (res1) {
  348. voice.localId = res1.localId;
  349. wx.uploadVoice({
  350. localId: voice.localId,
  351. success: function (res2) {
  352. voice.serverId = res2.serverId;
  353. Hook.OnRecordOver(voice);
  354. }
  355. });
  356. }
  357. });
  358. }
  359. }
  360. }
  361. //上传结果
  362. function ThirdPartyVoiceOnUpload(Hook, Path) {
  363. if (cc.sys.isNative) {
  364. if (cc.sys.OS_IOS == cc.sys.os) {
  365. jsb.reflection.callStaticMethod("AppController", "onDownLoad:", VID);
  366. } else {
  367. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "onDownLoad", "(Ljava/lang/String;)V", VID);
  368. }
  369. } else {
  370. if (typeof wx == 'undefined') {
  371. return false;
  372. } else {
  373. return true;
  374. }
  375. }
  376. }
  377. function ThirdPartyGVoiceLoadVoice(VID) {
  378. if (cc.sys.OS_IOS == cc.sys.os) {
  379. jsb.reflection.callStaticMethod("AppController", "onDownLoad:", VID);
  380. } else {
  381. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "onDownLoad", "(Ljava/lang/String;)V", VID);
  382. }
  383. }
  384. /////////////////
  385. cc.voice = {
  386. MaxRecordTime: 59000,
  387. _startTime: 0,
  388. IsPlaying: false,
  389. IsRecording: false,
  390. _voiceH5: {},
  391. _queue: [],
  392. _ctrl: null,
  393. _engine: null,
  394. Type: cc.Enum({
  395. NULL: 0,
  396. RECORD_START: 1,
  397. RECORD_STOP: 2,
  398. PLAY: 3,
  399. PLAY_ALL_FINISH: 4
  400. }),
  401. Init: function (VoiceCtrl, GameEngine) {
  402. this._ctrl = VoiceCtrl;
  403. this._engine = GameEngine;
  404. if (cc.share.IsH5_WX()) {
  405. return (typeof wx != 'undefined');
  406. } else {
  407. if (cc.sys.isNative) return true;
  408. }
  409. },
  410. GetRecordTime: function () {
  411. if (!this._startTime) return null;
  412. return new Date().getTime() - this._startTime;
  413. },
  414. startRecord: function () {
  415. try {
  416. if (cc.share.IsH5_WX()) {
  417. if (typeof wx == 'undefined') {
  418. this._ctrl.onDispose(false, this.Type.RECORD_START);
  419. } else {
  420. wx.startRecord({
  421. success: function () {
  422. this.IsRecording = true;
  423. this._startTime = new Date().getTime();
  424. this._ctrl.onDispose(true, this.Type.RECORD_START);
  425. }.bind(this),
  426. fail: function () {
  427. this._ctrl.onDispose(false, this.Type.RECORD_START);
  428. }.bind(this),
  429. });
  430. }
  431. } else if (cc.sys.isNative) {
  432. this.IsRecording = true;
  433. this._startTime = new Date().getTime();
  434. this._ctrl.onDispose(true, this.Type.RECORD_START);
  435. if (cc.sys.OS_IOS == cc.sys.os) {
  436. jsb.reflection.callStaticMethod("AppController", "startRecord");
  437. } else {
  438. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "startRecord", "()V");
  439. }
  440. }
  441. } catch (e) {
  442. if (window.LOG_DEBUG) console.log(e);
  443. }
  444. },
  445. stopRecord: function () {
  446. try {
  447. if (this._startTime == null) return;
  448. this.IsRecording = false;
  449. var recordTime = new Date().getTime() - this._startTime;
  450. var str = (recordTime < 1000 ? '0' : '1');
  451. this._startTime = null;
  452. if (cc.share.IsH5_WX()) {
  453. if (typeof wx == 'undefined') {
  454. return this._ctrl.onDispose(false, this.Type.RECORD_STOP);
  455. } else if (str == '0' || typeof wx == 'undefined') {
  456. wx.stopRecord({
  457. success: function () {
  458. this._ctrl.onDispose(true, this.Type.RECORD_STOP);
  459. }.bind(this)
  460. });
  461. return this._ctrl.onDispose(false, this.Type.RECORD_STOP);
  462. } else {
  463. wx.stopRecord({
  464. success: function (res1) {
  465. this._ctrl.onDispose(true, this.Type.RECORD_STOP);
  466. wx.uploadVoice({
  467. localId: res1.localId,
  468. success: function (res2) {
  469. this._voiceH5[res2.serverId] = res1.localId;
  470. this.send(1, res2.serverId);
  471. }.bind(this)
  472. });
  473. }.bind(this)
  474. });
  475. }
  476. } else if (cc.sys.isNative) {
  477. var res = '';
  478. if (cc.sys.OS_IOS == cc.sys.os) { // IOS端需要走回调完成结束
  479. res = jsb.reflection.callStaticMethod("AppController", "stopRecord:", str);
  480. this._ctrl.onDispose(true, this.Type.RECORD_STOP);
  481. } else {
  482. res = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "stopRecord", "(Ljava/lang/String;)Ljava/lang/String;", str);
  483. if (str == '0') {
  484. return this._ctrl.onDispose(false, this.Type.RECORD_STOP);
  485. } else {
  486. this._ctrl.onDispose(true, this.Type.RECORD_STOP);
  487. this.send(0, res);
  488. }
  489. }
  490. }
  491. } catch (e) {
  492. if (window.LOG_DEBUG) console.log(e);
  493. }
  494. },
  495. onRecordFinish: function (res) { // IOS 回调
  496. if (res == '') {
  497. return this._ctrl.onDispose(false, this.Type.RECORD_STOP);
  498. }
  499. this.send(0, res);
  500. },
  501. receive: function (pVoice) {
  502. try {
  503. if (!!!this._queue) this._queue = [];
  504. // 原生
  505. if (pVoice.cbPlatform == 0) {
  506. if (!cc.share.IsH5_WX()) {
  507. if (!!!this._voice) this._voice = [];
  508. if (!!!this._voice[pVoice.dwSendUserID]) this._voice[pVoice.dwSendUserID] = '';
  509. this._voice[pVoice.dwSendUserID] += (pVoice.szVID.replace(/\n/g, ''));
  510. if (pVoice.bFinish) {
  511. this._pushVoice(pVoice.cbPlatform, pVoice.dwSendUserID, pVoice.dwTargetUserID, this._voice[pVoice.dwSendUserID]);
  512. this._voice[pVoice.dwSendUserID] = '';
  513. this._next();
  514. }
  515. }
  516. } else { // 微信H5
  517. if (cc.share.IsH5_WX()) {
  518. if (!!this._voiceH5[pVoice.szVID]) {
  519. this._pushVoice(pVoice.cbPlatform, pVoice.dwSendUserID, pVoice.dwTargetUserID, this._voiceH5[pVoice.szVID]);
  520. this._next();
  521. } else {
  522. wx.downloadVoice({
  523. serverId: pVoice.szVID,
  524. success: function (res) {
  525. this._voiceH5[pVoice.szVID] = res.localId;
  526. this._pushVoice(pVoice.cbPlatform, pVoice.dwSendUserID, pVoice.dwTargetUserID, this._voiceH5[pVoice.szVID]);
  527. this._next();
  528. }.bind(this)
  529. });
  530. }
  531. }
  532. }
  533. } catch (e) {
  534. if (window.LOG_DEBUG) console.log(e);
  535. }
  536. },
  537. _pushVoice: function (cbPlatform, dwSendUserID, dwTargetUserID, szVID) {
  538. this._queue.push({
  539. cbPlatform: cbPlatform,
  540. dwSendUserID: dwSendUserID,
  541. dwTargetUserID: dwTargetUserID,
  542. szVID: szVID,
  543. });
  544. },
  545. send: function (platform, str) {
  546. try {
  547. if (platform == 0) { // 原生
  548. while (str.length != 0) {
  549. if (str.length > 255) {
  550. this._engine.OnSendUserVoice(platform, str.slice(0, 255), false);
  551. str = str.slice(255);
  552. } else {
  553. this._engine.OnSendUserVoice(platform, str.slice(0), true);
  554. str = '';
  555. }
  556. }
  557. } else {
  558. this._engine.OnSendUserVoice(platform, str, true);
  559. }
  560. } catch (e) {
  561. if (window.LOG_DEBUG) console.log(e);
  562. }
  563. },
  564. _next: function () {
  565. if (this._queue.length > 0) {
  566. this.IsPlaying = true;
  567. return this.playVoice(this._queue.shift());
  568. } else {
  569. this.IsPlaying = false;
  570. // this._ctrl.onFinish();
  571. this._ctrl.onDispose(true, this.Type.PLAY_ALL_FINISH);
  572. }
  573. },
  574. // platform 0原生 1微信H5
  575. playVoice: function (data) {
  576. try {
  577. var platform = data.cbPlatform;
  578. var content = data.szVID;
  579. if (window.LOG_DEBUG) console.log(data);
  580. if (platform == 0) { // 原生
  581. if (cc.share.IsH5_WX()) return this.onPlayFinish();
  582. if (cc.sys.isNative) { // 原生播放
  583. // this._ctrl.onPlay(data);
  584. this._ctrl.onDispose(true, this.Type.PLAY, data);
  585. if (cc.sys.OS_IOS == cc.sys.os) {
  586. jsb.reflection.callStaticMethod("AppController", "playVoice:", content);
  587. } else {
  588. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "playVoice", "(Ljava/lang/String;)V", content);
  589. }
  590. } else { // 浏览器播放
  591. var video = document.createElement('video');
  592. video.src = 'data:audio/mp4;base64,' + content;
  593. video.autoplay = true;
  594. // this._ctrl.onPlay(data);
  595. this._ctrl.onDispose(true, this.Type.PLAY, data);
  596. video.addEventListener("ended", function () {
  597. video.remove();
  598. this.onPlayFinish();
  599. }.bind(this), false);
  600. }
  601. } else { // 微信H5
  602. if (!cc.share.IsH5_WX()) return this.onPlayFinish();
  603. if (typeof wx == 'undefined') {
  604. if (!!finishFunc) finishFunc();
  605. } else { // 微信播放
  606. wx.playVoice({
  607. localId: data.szVID,
  608. success: function () {
  609. // this._ctrl.onPlay(data);
  610. this._ctrl.onDispose(true, this.Type.PLAY, data);
  611. }.bind(this)
  612. });
  613. wx.onVoicePlayEnd({
  614. complete: function (res) {
  615. this.onPlayFinish();
  616. }.bind(this)
  617. });
  618. }
  619. }
  620. } catch (e) {
  621. if (window.LOG_DEBUG) console.log(e);
  622. }
  623. },
  624. onPlayFinish: function () { // 原生回调
  625. this._next();
  626. },
  627. }
  628. // 语音base64
  629. function ThirdPartyStartRecord(success, fail) {
  630. if(cc.share.IsH5_WX()) {
  631. if (typeof wx == 'undefined') {
  632. } else {
  633. wx.startRecord({
  634. success: function () {
  635. if(success) success();
  636. },
  637. fail: function() {
  638. if(fail) fail();
  639. },
  640. });
  641. }
  642. } else if (cc.sys.isNative) {
  643. if (cc.sys.OS_IOS == cc.sys.os) {
  644. jsb.reflection.callStaticMethod("AppController", "startRecord");
  645. } else {
  646. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "startRecord", "()V");
  647. }
  648. }
  649. }
  650. function ThirdPartyStopRecord(str) {
  651. var res = '';
  652. if (cc.sys.isNative) {
  653. if (cc.sys.OS_IOS == cc.sys.os) {
  654. res = jsb.reflection.callStaticMethod("AppController", "stopRecord:", str);
  655. } else {
  656. res = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "stopRecord", "(Ljava/lang/String;)Ljava/lang/String;", str);
  657. }
  658. }
  659. return res;
  660. }
  661. function ThirdPartyPlayVoice(str) {
  662. if (window.LOG_NET_DATA) console.log(str);
  663. if (cc.sys.isNative) {
  664. if (cc.sys.OS_IOS == cc.sys.os) {
  665. jsb.reflection.callStaticMethod("AppController", "playVoice:", str);
  666. } else {
  667. jsb.reflection.callStaticMethod("org/cocos2dx/javascript/VoiceCtrl", "playVoice", "(Ljava/lang/String;)V", str);
  668. }
  669. } else {
  670. var video = document.createElement('video');
  671. video.src = 'data:audio/mp4;base64,' + str;
  672. video.autoplay = true;
  673. video.addEventListener("ended", function () {
  674. video.remove();
  675. g_Table.OnPlayFinish();
  676. }, false)
  677. if (typeof wx == 'undefined') {
  678. } else {
  679. }
  680. }
  681. }
  682. function ThirdPartyPickImgNew(type, needClip, clipSize) {
  683. const ChoosephotoType = {
  684. Album: 1,
  685. Camera: 2
  686. };
  687. if (needClip == undefined) {
  688. needClip = false;
  689. }
  690. var clipX = 0;
  691. var clipY = 0;
  692. if (clipSize) {
  693. clipX = clipSize.x;
  694. clipY = clipSize.y;
  695. }
  696. var dict = {
  697. needClip: needClip, //1是裁剪,2是不裁剪
  698. clipX: clipX, //裁剪x尺寸
  699. clipY: clipY, //数剪y尺寸
  700. }
  701. if (cc.sys.os === cc.sys.OS_ANDROID) {
  702. var methodName = "pickImage";
  703. if (type == ChoosephotoType.Album) {
  704. methodName ="useSystemAlbum";
  705. } else if (type == ChoosephotoType.Camera) {
  706. methodName ="useSystemCamera";
  707. }
  708. var ret = jsb.reflection.callstaticMethod("org/cocos2dx/javascript/AppActivity", methodName, "(Ljava/lang/string;)v", JSON.stringify(dict));
  709. console.log("androidTakePhotoret:", ret);
  710. return ret;
  711. } else if (cc.sys.os === cc.sys.OS_IOS) {
  712. console.log("js 请求相册");
  713. var ret = null;
  714. if (type == ChoosephotoType.Album) {
  715. ret = jsb.reflection.callstaticMethod("Appcontroller", "pickImage:", JSON.stringify(dict));
  716. } else if (type == ChoosephotoType.Camera) {
  717. ret = jsb.reflection.callstaticMethod("Appcontroller", "takePhoto:", JSON.stringify(dict));
  718. }
  719. return ret;
  720. }
  721. return true
  722. }
  723. function ThirdPartyPickImg(JsonObj, szNeedChip) {
  724. JsonObj.URL = window.PHP_UPLOAD_URL;
  725. JsonObj.PickEndCallback = 'OnPhotoPickEnd';
  726. if (cc.sys.isNative) {
  727. if (cc.sys.OS_IOS == cc.sys.os) {
  728. // jsb.reflection.callStaticMethod("AppController","WXSharetitle:description:url:IsTimeLine:",ShareInfo.title,ShareInfo.desc,ShareInfo.link, isLine);
  729. jsb.reflection.callStaticMethod("RootViewController", "pickImg:description:", JSON.stringify(JsonObj), szNeedChip);
  730. console.log(' ### ios ThirdPartyPickImg ' + JSON.stringify(JsonObj));
  731. } else {
  732. jsb.reflection.callStaticMethod(
  733. 'org/cocos2dx/javascript/AppActivity', 'pickImg', "(Ljava/lang/String;Ljava/lang/String;)V", JSON.stringify(JsonObj), szNeedChip
  734. );
  735. }
  736. } else {
  737. if (cc.share.IsH5_WX()) {
  738. //
  739. } else {
  740. return false;
  741. }
  742. }
  743. return true
  744. }
  745. function Restart (e) {
  746. if(window.LOG_DEBUG && e) console.log(e);
  747. cc.game.restart();
  748. }