mainscene.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import { _decorator, Asset, AssetManager, assetManager, Component, log, Node, error, instantiate, Prefab, sys, System, UITransform, View, ResolutionPolicy, view, Widget } from 'cc';
  2. import PlatformService from './Platform/PlatformService';
  3. import { loginParams } from './Platform/MiniGameApiBase';
  4. import { HttpRequest } from './Manager/HttpRequest';
  5. import { User } from './Manager/LocalDataMgr';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('mainscene')
  8. export class mainscene extends Component {
  9. // 单例实例
  10. private static _instance: mainscene;
  11. // 全局访问点
  12. public static get instance(): mainscene {
  13. return this._instance;
  14. }
  15. // Example() {
  16. // // GET 请求示例
  17. // HttpRequest.get('https://api.example.com/data', { page: 1, limit: 10 })
  18. // .then(response => {
  19. // console.log('GET Success:', response);
  20. // })
  21. // .catch(error => {
  22. // console.error('GET Error:', error);
  23. // });
  24. // // POST 请求示例
  25. // const postData = {
  26. // username: 'test',
  27. // password: '123456'
  28. // };
  29. // HttpRequest.post('https://api.example.com/login', postData)
  30. // .then(response => {
  31. // console.log('POST Success:', response);
  32. // })
  33. // .catch(error => {
  34. // console.error('POST Error:', error);
  35. // });
  36. // }
  37. ge: GravityAnalyticsAPI = null;
  38. /**
  39. * @param {string} name 用户名,可以理解成用户在业务中的昵称,如果没有,可以填用户唯一ID(必填)
  40. * @param {number} version 用户初始化的程序发布更新的版本号(必填)
  41. * @param {string} openid open id (小程序/小游戏必填)
  42. * @param {string} enable_sync_attribution 是否开启同步获取归因信息,具体请参考同步归因:https://doc.gravity-engine.com/turbo-integrated/sync_attribution.html
  43. */
  44. InitSdk() {
  45. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  46. const config = {
  47. accessToken: "zhdTlDf41taJrgRyaOtGjhlELu6AiqwQ", // 项目通行证,在:网站后台-->设置-->应用列表中找到Access Token列 复制(首次使用可能需要先新增应用)
  48. clientId: User.userData.openid, // 用户唯一标识,如产品为小游戏,则必须填用户openid(注意,不是小游戏的APPID!!!)
  49. name: "ge", // 全局变量名称
  50. debugMode: "none", //debug// 是否开启测试模式,开启测试模式后,可以在 网站后台--设置--元数据--事件流中查看实时数据上报结果。(测试时使用,上线之后一定要关掉,改成none或者删除)
  51. };
  52. mainscene.instance.ge = new GravityAnalyticsAPI(config);
  53. mainscene.instance.ge.setupAndStart();
  54. mainscene.instance.ge.initialize({
  55. name: User.userData.openid,
  56. version: 123,
  57. openid: User.userData.openid,
  58. enable_sync_attribution: false,
  59. })
  60. .then((res) => {
  61. console.log("initialize success " + res);
  62. })
  63. .catch((err) => {
  64. console.log("initialize failed, error is " + err);
  65. });
  66. this.schedule(() => {
  67. this.onlineTime++
  68. mainscene.instance.ge.track(
  69. "onlineTime", //追踪事件的名称
  70. {
  71. version: "123",
  72. PlayTimes: this.onlineTime,
  73. } //需要上传的事件属性
  74. );
  75. }, 60)
  76. }
  77. }
  78. onlineTime: number = 0
  79. adaptiveScreen() {
  80. let { width, height } = view.getVisibleSize()
  81. if (height / (width - 1) > 16 / 9) {
  82. view.setResolutionPolicy(ResolutionPolicy.FIXED_WIDTH)
  83. } else {
  84. view.setResolutionPolicy(ResolutionPolicy.FIXED_HEIGHT)
  85. }
  86. this.node.getComponent(Widget).enabled = false
  87. this.node.getComponent(UITransform).setContentSize(720, 1460)//(720, 1280)//(645*1.2, 1398*1.2)
  88. this.monLoad()
  89. }
  90. onLoad() {
  91. if (sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
  92. this.scheduleOnce(this.adaptiveScreen, 0.01)
  93. } else {
  94. this.monLoad()
  95. }
  96. }
  97. protected monLoad(): void {
  98. // if (sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
  99. // this.scheduleOnce(() => {
  100. // const uiTransform = this.node.getComponent(UITransform);
  101. // const currentWidth = uiTransform.width;
  102. // const currentHeight = uiTransform.height;
  103. // const targetAspectRatio = 9 / 16;
  104. // const currentAspectRatio = currentWidth / currentHeight;
  105. // if (currentAspectRatio > targetAspectRatio) {
  106. // // 如果当前宽高比大于目标宽高比,则根据高度调整宽度
  107. // uiTransform.width = currentHeight * targetAspectRatio;
  108. // } else {
  109. // // 否则根据宽度调整高度
  110. // uiTransform.height = currentWidth / targetAspectRatio;
  111. // }
  112. // }, 0);
  113. // }
  114. if (mainscene._instance) {
  115. this.node.destroy();
  116. return;
  117. }
  118. mainscene._instance = this;
  119. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  120. Promise.resolve(
  121. PlatformService.getInstance().platformApi.getLaunchOptionsSync()
  122. ).then((options) => {
  123. log(options);
  124. const params: loginParams = {
  125. timeout: 100,
  126. success: (data) => {
  127. console.error(data)
  128. const RequestParameters = {
  129. "appid": "tt20b55608bf84081c02",
  130. "code": data.code,
  131. }
  132. HttpRequest.post('https://cygame-platform.cygame666.cn/dy/openid/get', RequestParameters)
  133. .then(response => {
  134. console.log('get openid success:', response);
  135. if (User.userData.openid == "demo") {
  136. User.setUserData({ openid: response.res.openid })
  137. }
  138. this.InitSdk()
  139. this.Init();
  140. })
  141. .catch(error => {
  142. console.error('get openid error:', error);
  143. });
  144. },
  145. fail: () => {
  146. this.Init();
  147. },
  148. complete: () => {
  149. }
  150. }
  151. PlatformService.getInstance().platformApi.login(params)
  152. })
  153. } else {
  154. this.Init();
  155. }
  156. // Promise.resolve(
  157. // PlatformService.getInstance().platformApi.getLaunchOptionsSync()
  158. // ).then((options) => {
  159. // log(options);
  160. // const params: loginParams = {
  161. // timeout: 100,
  162. // success: (data) => {
  163. // console.error(data)
  164. // const postData = {
  165. // "code": data.code, //状态码
  166. // "mp_id": "tt833cf5811c08369602", //游戏的appid
  167. // "userportrait": null,
  168. // "aid": options.query.aid,
  169. // "adid": options.query.adid,
  170. // "clickid": options.query.clickid,
  171. // "creativeid": options.query.creativeid,
  172. // "creativetype": options.creativetype,
  173. // "mpversion": options.query.mpversion,
  174. // "promotionid": options.query.promotionid,
  175. // "oaid": options.query.oaid,
  176. // "callback": options.query.callback,
  177. // "userName": null,
  178. // "advertiser_id": options.query.advertiser_id,
  179. // "projectid": options.query.projectid
  180. // }
  181. // HttpRequest.post('https://game.renyouwangluo.cn/prod-api/ruoyi-ticktok/ticktok/Login', postData)
  182. // .then(response => {
  183. // console.log('POST Login postdate Success:', response);
  184. // if (User.userData.openid == "demo") {
  185. // User.setUserData({ openid: response.data.openid })
  186. // }
  187. // })
  188. // .catch(error => {
  189. // console.error('POST Login postdate Error:', error);
  190. // });
  191. // },
  192. // fail: () => {
  193. // },
  194. // complete: () => {
  195. // }
  196. // }
  197. // PlatformService.getInstance().platformApi.login(params)
  198. // })
  199. }
  200. start() {
  201. }
  202. Init() {
  203. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  204. mainscene.instance.ge.track(
  205. "loading_start", //追踪事件的名称
  206. {
  207. version: "123",
  208. } //需要上传的事件属性
  209. );
  210. }
  211. const starttime = Date.now();
  212. assetManager.loadBundle("hall", (err: Error, bundle: AssetManager.Bundle) => {
  213. if (err) {
  214. error(err);
  215. } else {
  216. if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
  217. const loadtime = Date.now() - starttime;
  218. mainscene.instance.ge.track(
  219. "loading_finish", //追踪事件的名称
  220. {
  221. version: "123",
  222. } //需要上传的事件属性
  223. );
  224. mainscene.instance.ge.track(
  225. "loading_time", //追踪事件的名称
  226. {
  227. version: "123",
  228. loading_time: loadtime,
  229. } //需要上传的事件属性
  230. );
  231. }
  232. bundle.load("prefab/page_start", (err: Error, asset: Asset) => {
  233. if (err) {
  234. error(err)
  235. return
  236. }
  237. let node = instantiate(asset as Prefab);
  238. node.parent = this.node;
  239. })
  240. }
  241. })
  242. }
  243. }