ThirdParty.js 29 KB

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