EachWindowItem.ts 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { _decorator, Node, Label, Sprite, Texture2D, SpriteFrame, ImageAsset, ProgressBar } from 'cc';
  2. import { JSB } from 'cc/env';
  3. import InfiniteCell from '../../core/InfiniteList/InfiniteCell';
  4. import { Http } from '../../core/net/Http';
  5. import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils';
  6. import { g } from '../../Data/g';
  7. import { platform } from '../../Data/platform';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('EachWindowItem')
  10. export class EachWindowItem extends InfiniteCell {
  11. @property({ type: Label, tooltip: "名字文本" }) titleLabel: Label;
  12. @property({ type: Label, tooltip: "描述文本" }) decLabel: Label;
  13. @property({ type: Sprite, tooltip: "图标" }) icon: Sprite;
  14. @property({ type: Http, tooltip: "Http组件" }) http: Http;
  15. @property({ type: Node, tooltip: "安装" }) installNode: Node;
  16. @property({ type: Node, tooltip: "启动" }) startNode: Node;
  17. @property({ type: Node, tooltip: "下载" }) downLoadNode: Node;
  18. @property({ type: Node, tooltip: "进度" }) progressNode: Node;
  19. @property({ type: ProgressBar, tooltip: "下载进度条" }) progressBar: ProgressBar;
  20. @property({ type: Label, tooltip: "下载进度文本" }) progressLabel: Label;
  21. private url: string = "";
  22. private nebulaAppId: string = "";
  23. private id: number;
  24. private packageName: string = '';
  25. private downProgress = 0;
  26. private isInstrall = false;
  27. private hasApk = false;
  28. async UpdateContent(itemData: any) {
  29. this.installNode.active = this.startNode.active = this.downLoadNode.active = this.progressNode.active = false;
  30. this.id = itemData.id;
  31. this.titleLabel.string = itemData.title;
  32. this.decLabel.string = itemData.dec;
  33. this.nebulaAppId = itemData.nebulaAppId;
  34. this.packageName = itemData.packageName;
  35. this.url = itemData.url;
  36. if (JSB) {
  37. let img = await ResourcesUtils.loadRemote<ImageAsset>(itemData.icon, { ext: ".png" }, this);
  38. const spriteFrame = new SpriteFrame();
  39. const texture = new Texture2D();
  40. texture.image = img;
  41. spriteFrame.texture = texture;
  42. this.icon.spriteFrame = spriteFrame;
  43. this.setState();
  44. }
  45. }
  46. private setState() {
  47. this.isInstrall = platform.isInstalled(this.packageName);//是否有安装
  48. if (this.isInstrall) {//显示启动
  49. this.startNode.active = true;
  50. } else {
  51. this.hasApk = platform.isExistAPK(this.titleLabel.string + ".apk");//是否存在安装包
  52. if (this.hasApk) {//显示安装
  53. this.installNode.active = true
  54. } else {
  55. this.downLoadNode.active = true;
  56. //显示下载
  57. }
  58. }
  59. let str = '{"downProgress' + this.id + '":' + this.downProgress + '}';
  60. let data = JSON.parse(str);
  61. g.downLoadData.setData(data);
  62. }
  63. update() {
  64. if (g.downLoadData["downProgress" + this.id] != this.downProgress) {
  65. this.downProgress = g.downLoadData["downProgress" + this.id];
  66. this.downProgress = this.downProgress || 0;
  67. this.progressBar.progress = this.downProgress / 100;
  68. this.progressLabel.string = this.downProgress + '%';
  69. if (this.downProgress == 100) {
  70. g.downLoadData["downProgress" + this.id] = 0;
  71. this.progressNode.active = false;
  72. this.installNode.active = true;
  73. this.onInstall();
  74. }
  75. }
  76. }
  77. private onClick(): void {
  78. platform.downloadFile(this.url, this.titleLabel.string + ".apk", 'g.downLoadData["downProgress' + this.id + '"]');
  79. this.http.send("/api/recommend/AppDownloadLog", { downloadAppId: this.nebulaAppId, unionId: null, imei: g.deviceData.imei, idfa: g.deviceData.idfa, androId: g.deviceData.androId, oaid: g.deviceData.oaid, mac: g.deviceData.mac });
  80. this.downLoadNode.active = false;
  81. this.progressNode.active = true;
  82. }
  83. private onInstall() {
  84. platform.installApp(this.titleLabel.string + ".apk");
  85. }
  86. private startApp() {
  87. platform.startApp(this.packageName);
  88. }
  89. }