123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- import { _decorator, Asset, AssetManager, assetManager, Component, log, Node, error, instantiate, Prefab, sys, System, UITransform, View, ResolutionPolicy, view, Widget } from 'cc';
- import PlatformService from './Platform/PlatformService';
- import { loginParams } from './Platform/MiniGameApiBase';
- import { HttpRequest } from './Manager/HttpRequest';
- import { User } from './Manager/LocalDataMgr';
- const { ccclass, property } = _decorator;
- @ccclass('mainscene')
- export class mainscene extends Component {
- // 单例实例
- private static _instance: mainscene;
- // 全局访问点
- public static get instance(): mainscene {
- return this._instance;
- }
- // Example() {
- // // GET 请求示例
- // HttpRequest.get('https://api.example.com/data', { page: 1, limit: 10 })
- // .then(response => {
- // console.log('GET Success:', response);
- // })
- // .catch(error => {
- // console.error('GET Error:', error);
- // });
- // // POST 请求示例
- // const postData = {
- // username: 'test',
- // password: '123456'
- // };
- // HttpRequest.post('https://api.example.com/login', postData)
- // .then(response => {
- // console.log('POST Success:', response);
- // })
- // .catch(error => {
- // console.error('POST Error:', error);
- // });
- // }
- ge: GravityAnalyticsAPI = null;
- /**
- * @param {string} name 用户名,可以理解成用户在业务中的昵称,如果没有,可以填用户唯一ID(必填)
- * @param {number} version 用户初始化的程序发布更新的版本号(必填)
- * @param {string} openid open id (小程序/小游戏必填)
- * @param {string} enable_sync_attribution 是否开启同步获取归因信息,具体请参考同步归因:https://doc.gravity-engine.com/turbo-integrated/sync_attribution.html
- */
- InitSdk() {
- if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
- const config = {
- accessToken: "zhdTlDf41taJrgRyaOtGjhlELu6AiqwQ", // 项目通行证,在:网站后台-->设置-->应用列表中找到Access Token列 复制(首次使用可能需要先新增应用)
- clientId: User.userData.openid, // 用户唯一标识,如产品为小游戏,则必须填用户openid(注意,不是小游戏的APPID!!!)
- name: "ge", // 全局变量名称
- debugMode: "none", //debug// 是否开启测试模式,开启测试模式后,可以在 网站后台--设置--元数据--事件流中查看实时数据上报结果。(测试时使用,上线之后一定要关掉,改成none或者删除)
- };
- mainscene.instance.ge = new GravityAnalyticsAPI(config);
- mainscene.instance.ge.setupAndStart();
- mainscene.instance.ge.initialize({
- name: User.userData.openid,
- version: 123,
- openid: User.userData.openid,
- enable_sync_attribution: false,
- })
- .then((res) => {
- console.log("initialize success " + res);
- })
- .catch((err) => {
- console.log("initialize failed, error is " + err);
- });
- this.schedule(() => {
- this.onlineTime++
- mainscene.instance.ge.track(
- "onlineTime", //追踪事件的名称
- {
- version: "123",
- PlayTimes: this.onlineTime,
- } //需要上传的事件属性
- );
- }, 60)
- }
- }
- onlineTime: number = 0
- adaptiveScreen() {
- let { width, height } = view.getVisibleSize()
- if (height / (width - 1) > 16 / 9) {
- view.setResolutionPolicy(ResolutionPolicy.FIXED_WIDTH)
- } else {
- view.setResolutionPolicy(ResolutionPolicy.FIXED_HEIGHT)
- }
- this.node.getComponent(Widget).enabled = false
- this.node.getComponent(UITransform).setContentSize(720, 1460)//(720, 1280)//(645*1.2, 1398*1.2)
- this.monLoad()
- }
- onLoad() {
- if (sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
- this.scheduleOnce(this.adaptiveScreen, 0.01)
- } else {
- this.monLoad()
- }
- }
- protected monLoad(): void {
- // if (sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
- // this.scheduleOnce(() => {
- // const uiTransform = this.node.getComponent(UITransform);
- // const currentWidth = uiTransform.width;
- // const currentHeight = uiTransform.height;
- // const targetAspectRatio = 9 / 16;
- // const currentAspectRatio = currentWidth / currentHeight;
- // if (currentAspectRatio > targetAspectRatio) {
- // // 如果当前宽高比大于目标宽高比,则根据高度调整宽度
- // uiTransform.width = currentHeight * targetAspectRatio;
- // } else {
- // // 否则根据宽度调整高度
- // uiTransform.height = currentWidth / targetAspectRatio;
- // }
- // }, 0);
- // }
- if (mainscene._instance) {
- this.node.destroy();
- return;
- }
- mainscene._instance = this;
- if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
- Promise.resolve(
- PlatformService.getInstance().platformApi.getLaunchOptionsSync()
- ).then((options) => {
- log(options);
- const params: loginParams = {
- timeout: 100,
- success: (data) => {
- console.error(data)
- const RequestParameters = {
- "appid": "tt20b55608bf84081c02",
- "code": data.code,
- }
- HttpRequest.post('https://cygame-platform.cygame666.cn/dy/openid/get', RequestParameters)
- .then(response => {
- console.log('get openid success:', response);
- if (User.userData.openid == "demo") {
- User.setUserData({ openid: response.res.openid })
- }
- this.InitSdk()
- this.Init();
- })
- .catch(error => {
- console.error('get openid error:', error);
- });
- },
- fail: () => {
- this.Init();
- },
- complete: () => {
- }
- }
- PlatformService.getInstance().platformApi.login(params)
- })
- } else {
- this.Init();
- }
- // Promise.resolve(
- // PlatformService.getInstance().platformApi.getLaunchOptionsSync()
- // ).then((options) => {
- // log(options);
- // const params: loginParams = {
- // timeout: 100,
- // success: (data) => {
- // console.error(data)
- // const postData = {
- // "code": data.code, //状态码
- // "mp_id": "tt833cf5811c08369602", //游戏的appid
- // "userportrait": null,
- // "aid": options.query.aid,
- // "adid": options.query.adid,
- // "clickid": options.query.clickid,
- // "creativeid": options.query.creativeid,
- // "creativetype": options.creativetype,
- // "mpversion": options.query.mpversion,
- // "promotionid": options.query.promotionid,
- // "oaid": options.query.oaid,
- // "callback": options.query.callback,
- // "userName": null,
- // "advertiser_id": options.query.advertiser_id,
- // "projectid": options.query.projectid
- // }
- // HttpRequest.post('https://game.renyouwangluo.cn/prod-api/ruoyi-ticktok/ticktok/Login', postData)
- // .then(response => {
- // console.log('POST Login postdate Success:', response);
- // if (User.userData.openid == "demo") {
- // User.setUserData({ openid: response.data.openid })
- // }
- // })
- // .catch(error => {
- // console.error('POST Login postdate Error:', error);
- // });
- // },
- // fail: () => {
- // },
- // complete: () => {
- // }
- // }
- // PlatformService.getInstance().platformApi.login(params)
- // })
- }
- start() {
- }
- Init() {
- if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
- mainscene.instance.ge.track(
- "loading_start", //追踪事件的名称
- {
- version: "123",
- } //需要上传的事件属性
- );
- }
- const starttime = Date.now();
- assetManager.loadBundle("hall", (err: Error, bundle: AssetManager.Bundle) => {
- if (err) {
- error(err);
- } else {
- if (sys.Platform.BYTEDANCE_MINI_GAME == sys.platform) {
- const loadtime = Date.now() - starttime;
- mainscene.instance.ge.track(
- "loading_finish", //追踪事件的名称
- {
- version: "123",
- } //需要上传的事件属性
- );
- mainscene.instance.ge.track(
- "loading_time", //追踪事件的名称
- {
- version: "123",
- loading_time: loadtime,
- } //需要上传的事件属性
- );
- }
- bundle.load("prefab/page_start", (err: Error, asset: Asset) => {
- if (err) {
- error(err)
- return
- }
- let node = instantiate(asset as Prefab);
- node.parent = this.node;
- })
- }
- })
- }
- }
|