ThirdParty.js 27 KB

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