PlatformManager.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**
  2. * 平台管理器
  3. *
  4. * @format
  5. */
  6. import {Data, Mgr} from '../GameControl'
  7. import {WxUtils} from '../utils/WxUtils'
  8. import {CHANNEL, LANGUAGE_TYPE} from '../enums/Enum'
  9. import {ConstValue} from '../data/ConstValue'
  10. import {AdvertisementConfig} from '../config/AdvertisementConfig'
  11. export class PlatformManager {
  12. public isBackGround = false
  13. /**
  14. * 平台管理器各个平台工具初始化
  15. */
  16. public init() {
  17. this.checkUpdate()
  18. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  19. WxUtils.init()
  20. }
  21. cc.game.on(cc.game.EVENT_HIDE, () => {
  22. if (!this.isBackGround) {
  23. this.isBackGround = true
  24. }
  25. Mgr.audio.onViewHide()
  26. Mgr.net.stopKeepAlive()
  27. })
  28. cc.game.on(cc.game.EVENT_SHOW, () => {
  29. if (this.isBackGround) {
  30. this.isBackGround = false
  31. }
  32. Mgr.audio.onViewShow()
  33. if (Mgr.net.webSocket && Mgr.net.webSocket.readyState === WebSocket.OPEN) Mgr.net.sendKeepAlive()
  34. })
  35. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this)
  36. Data.main.safeTop = this.getSafeTop()
  37. Data.main.channel = this.getCurChannelName()
  38. if (this.getCurChannelName() == CHANNEL.googleplay) {
  39. Object.keys(AdvertisementConfig).forEach(v => {
  40. if (parseInt(v) < 99902) this.googleLoadMobAD(v)
  41. })
  42. }
  43. }
  44. public reset() {
  45. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this)
  46. }
  47. /**
  48. * 是否是微信小游戏环境
  49. */
  50. public isWeChat() {
  51. // @ts-ignore
  52. let _global = typeof window === 'undefined' ? global : window
  53. // @ts-ignore
  54. return cc.sys.platform == cc.sys.WECHAT_GAME && !cc.sys.isBrowser && typeof _global.qq === 'undefined'
  55. }
  56. /**
  57. * 是否是微信H5环境
  58. */
  59. public isWeChatH5() {
  60. return cc.sys.isBrowser && navigator.userAgent.includes('MicroMessenger')
  61. }
  62. /**
  63. * 是否是Android本地环境
  64. */
  65. public isAndroid() {
  66. return cc.sys.os === cc.sys.OS_ANDROID && cc.sys.isNative
  67. }
  68. /**
  69. * 是否是IOS本地环境
  70. */
  71. public isIOS() {
  72. return cc.sys.os === cc.sys.OS_IOS && cc.sys.isNative
  73. }
  74. /**
  75. * 是否是微信小游戏运行在android环境下
  76. */
  77. public isWeChatAndroid() {
  78. return this.isWeChat() && cc.sys.os === cc.sys.OS_ANDROID
  79. }
  80. /**
  81. * 是否是微信小游戏运行在IOS环境下
  82. */
  83. public isWeChatIOS() {
  84. return this.isWeChat() && cc.sys.os === cc.sys.OS_IOS
  85. }
  86. /**
  87. * 是否是Android本地环境
  88. */
  89. public isWindows() {
  90. return cc.sys.os === cc.sys.OS_WINDOWS && cc.sys.isNative
  91. }
  92. /**
  93. * 是否在开发环境下
  94. */
  95. public isWindowsDev() {
  96. return CC_DEV
  97. }
  98. /**
  99. * 判断是否在普通的html5环境下
  100. */
  101. public isH5() {
  102. return cc.sys.isBrowser && !navigator.userAgent.includes('MicroMessenger')
  103. }
  104. /**
  105. * 获取(安全的显示高度)刘海屏高度
  106. */
  107. public getSafeTop() {
  108. if (this.isWeChat()) {
  109. // @ts-ignore
  110. const systemInfo = wx.getSystemInfoSync()
  111. // @ts-ignore
  112. let marginTop = systemInfo.safeArea.top
  113. console.log('systemInfo', systemInfo)
  114. if (marginTop > 20) {
  115. if (cc.sys.os === cc.sys.OS_IOS) {
  116. marginTop -= 10 // 适配全面屏iphone
  117. }
  118. } else if (marginTop == 20) {
  119. marginTop = 0 // 适配老版iphone 和全部android手机
  120. }
  121. const paddingTop = cc.view.getVisibleSize().height * (marginTop / systemInfo.screenHeight)
  122. return paddingTop
  123. } else if (this.isIOS()) {
  124. return cc.view.getVisibleSize().height - cc.sys.getSafeAreaRect().height // IOS原生 cocos已封装
  125. } else if (this.isAndroid()) {
  126. let height = jsb.reflection.callStaticMethod(
  127. 'org/cocos2dx/javascript/service/GameSDK',
  128. 'getNotchHeight',
  129. '()I',
  130. )
  131. //console.log('getSafeTop--android-->', height)
  132. return height
  133. } else if (this.isWindowsDev()) {
  134. return 0
  135. } else if (this.isH5()) {
  136. return cc.view.getVisibleSize().height - cc.sys.getSafeAreaRect().height
  137. } else if (this.isWeChatH5()) {
  138. return cc.view.getVisibleSize().height - cc.sys.getSafeAreaRect().height
  139. } else {
  140. return 0
  141. }
  142. }
  143. public gc() {
  144. console.log('gc')
  145. if (this.isWeChat()) {
  146. // @ts-ignore
  147. wx['triggerGC'] && wx['triggerGC']()
  148. } else if (this.isIOS() || this.isAndroid()) {
  149. if (CC_JSB) {
  150. cc.sys.garbageCollect()
  151. }
  152. }
  153. }
  154. /**
  155. * 检测用户有没有授权
  156. * @param success
  157. * @param fail
  158. */
  159. public checkUserIsAuthorize(success: Function, fail: Function) {
  160. if (this.isWeChat()) {
  161. WxUtils.checkUserIsAuthorize(success, fail)
  162. }
  163. }
  164. public createAuthorizeButton(success, fail, node: cc.Node) {
  165. let btn = null
  166. if (this.isWeChat()) {
  167. btn = WxUtils.createAuthorizeButton(success, fail, node)
  168. }
  169. return btn
  170. }
  171. //复制功能
  172. public copyToClipboard(text: string) {
  173. if (this.isWindowsDev()) {
  174. navigator.clipboard?.writeText(text).then(() => {
  175. Mgr.ui.tip(LANGUAGE_TYPE.copySuccess)
  176. })
  177. } else if (cc.sys.os === cc.sys.OS_ANDROID) {
  178. Mgr.ui.tip(LANGUAGE_TYPE.copySuccess)
  179. jsb.reflection.callStaticMethod(
  180. 'org/cocos2dx/javascript/service/GameSDK',
  181. 'copyTextToClipboard',
  182. '(Ljava/lang/String;)V',
  183. text,
  184. )
  185. }
  186. }
  187. public launchBack() {
  188. if (this.isWeChat()) {
  189. WxUtils.launchBack()
  190. }
  191. }
  192. public onShow(callBack?: Function) {
  193. if (this.isWeChat()) {
  194. WxUtils.onShow(callBack)
  195. }
  196. }
  197. public checkUpdate() {
  198. if (this.isWeChat()) {
  199. WxUtils.checkUpdate()
  200. }
  201. }
  202. public playVideoAD(adid: number | string, onsuccess: Function, onerr?: Function) {
  203. adid = adid.toString()
  204. if (this.isWeChat()) {
  205. WxUtils.showRewardAd(adid, onsuccess, onerr)
  206. } else if (this.isWindowsDev()) {
  207. onsuccess && onsuccess()
  208. } else if (this.isAndroid()) {
  209. if (ConstValue.DEBUG) {
  210. onsuccess && onsuccess()
  211. } else {
  212. window['SDKUtils'].adSuccessCB = onsuccess
  213. window['SDKUtils'].adFailCB = errCode => {
  214. if (errCode == 1001) {
  215. Mgr.ui.tip(LANGUAGE_TYPE.adNotInit)
  216. } else if (errCode == 1002) {
  217. Mgr.ui.tip(LANGUAGE_TYPE.adLoading)
  218. }
  219. onerr && onerr(errCode)
  220. }
  221. this.googleMobAD(adid)
  222. }
  223. }
  224. }
  225. public getCurChannelName() {
  226. let ret = CHANNEL.none
  227. if (Mgr.platform.isWeChat()) {
  228. } else if (Mgr.platform.isH5()) {
  229. } else if (Mgr.platform.isWeChatH5()) {
  230. } else if (Mgr.platform.isAndroid()) {
  231. ret = jsb.reflection.callStaticMethod(
  232. 'org/cocos2dx/javascript/service/GameSDK',
  233. 'getChannel',
  234. '()Ljava/lang/String;',
  235. )
  236. }
  237. return ret
  238. }
  239. public onKeyDown(event) {
  240. switch (event.keyCode) {
  241. case cc.macro.KEY.back:
  242. if (this.isAndroid()) {
  243. let className = 'org/cocos2dx/javascript/SDKUtils'
  244. let methodName = 'gotoBackPressed'
  245. let methodSignature = '()V'
  246. jsb.reflection.callStaticMethod(className, methodName, methodSignature)
  247. }
  248. break
  249. }
  250. }
  251. public logout() {
  252. if (this.isAndroid()) {
  253. //0测试1游客2facebook3google
  254. let funcNameArr = ['', '', 'facebookLogout', 'googleLogout']
  255. let funcName = funcNameArr[Data.main.loginType]
  256. if (funcName) jsb.reflection.callStaticMethod('org/cocos2dx/javascript/service/GameSDK', funcName, '()V')
  257. }
  258. }
  259. public facebookLogin() {
  260. if (this.isAndroid()) {
  261. jsb.reflection.callStaticMethod('org/cocos2dx/javascript/service/GameSDK', 'facebookLogin', '()V')
  262. }
  263. }
  264. public googleLogin() {
  265. if (this.isAndroid()) {
  266. jsb.reflection.callStaticMethod(
  267. 'org/cocos2dx/javascript/service/GameSDK',
  268. 'googleLogin',
  269. '(Ljava/lang/String;)V',
  270. '1053688457546-tfl1l8l5mkc04v0qng2ogn2jkopafh49.apps.googleusercontent.com',
  271. )
  272. }
  273. }
  274. public facebookAD(AdID) {
  275. AdID = 'YOUR_PLACEMENT_ID'
  276. if (this.isAndroid()) {
  277. jsb.reflection.callStaticMethod(
  278. 'org/cocos2dx/javascript/service/GameSDK',
  279. 'facebookAd',
  280. '(Ljava/lang/String;)V',
  281. AdID,
  282. )
  283. }
  284. }
  285. public googleMobAD(AdID) {
  286. let googleAdID = AdvertisementConfig[AdID].google
  287. if (this.isAndroid()) {
  288. jsb.reflection.callStaticMethod(
  289. 'org/cocos2dx/javascript/service/GameSDK',
  290. 'googleMobAD',
  291. '(Ljava/lang/String;)V',
  292. googleAdID,
  293. )
  294. }
  295. }
  296. public googleLoadMobAD(AdID) {
  297. let googleAdID = AdvertisementConfig[AdID].google
  298. if (this.isAndroid()) {
  299. jsb.reflection.callStaticMethod(
  300. 'org/cocos2dx/javascript/service/GameSDK',
  301. 'googleReadyRewardedVideoAd',
  302. '(Ljava/lang/String;)V',
  303. googleAdID,
  304. )
  305. }
  306. }
  307. public getUniqueDeviceId() {
  308. if (this.isAndroid()) {
  309. return jsb.reflection.callStaticMethod(
  310. 'org/cocos2dx/javascript/service/GameSDK',
  311. 'getUniqueDeviceId',
  312. '()Ljava/lang/String;',
  313. )
  314. }
  315. }
  316. public buyItem(productId: string, orderId: string) {
  317. if (this.isAndroid()) {
  318. if (Data.main.channel == CHANNEL.googleplay) {
  319. jsb.reflection.callStaticMethod(
  320. 'org/cocos2dx/javascript/service/GameSDK',
  321. 'googlePay',
  322. '(Ljava/lang/String;Ljava/lang/String;)V',
  323. productId,
  324. orderId,
  325. )
  326. }
  327. }
  328. }
  329. public hideSplash() {
  330. if (this.isAndroid()) {
  331. return jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'hideSplash', '()V')
  332. }
  333. }
  334. }