pushAdBtn.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. var PushAdBtn = cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. icon: {
  14. default: null,
  15. type: cc.Sprite
  16. },
  17. tempIcon: {
  18. default: null,
  19. type: cc.Sprite
  20. },
  21. pushType: "",
  22. isLoop: true,
  23. loopTime: 10,
  24. actionTime: 0.4,
  25. pushDataType: "iconList",
  26. placement: "home",
  27. },
  28. onLoad() {
  29. this.initData();
  30. this.initListener();
  31. cc.log("onLoad~~~~~~~~~~");
  32. },
  33. start() {
  34. this.node.active = false;
  35. this.pushCall();
  36. cc.log("start~~~~~~~~~~");
  37. this.initLoop();
  38. },
  39. check(pushData) {
  40. if (!pushData || pushData.length <= 0) {
  41. this.node.active = false;
  42. return false;
  43. }
  44. return true;
  45. },
  46. initLoop() {
  47. if (!this.loopTime || this.loopTime <= 0) return;
  48. this.schedule(function () {
  49. this.loopNext();
  50. }.bind(this), this.loopTime);
  51. },
  52. initData() {
  53. this.data = null;
  54. this.dataIndex = -1;
  55. this.pushData = null;
  56. this.animation = this.icon.getComponent(cc.Animation);
  57. this.tempAnimation = this.tempIcon.getComponent(cc.Animation);
  58. this.tempIconPX = this.tempIcon.node.x;
  59. },
  60. initListener() {
  61. cc.director.on('wxPushData', function (event) {
  62. cc.log("wxPushData~~~~~~~~~~~");
  63. this.pushCall();
  64. }.bind(this));
  65. },
  66. pushCall(){
  67. if(!wb.wxData.pushData) return;
  68. if(this.havePushCall) return;
  69. this.havePushCall = true;
  70. this.pushData = wb.wxData.pushData[this.pushDataType];
  71. var index = this.getDataIndex();
  72. cc.log("pushCall " + index);
  73. // cc.log(this.pushData);
  74. this.data = this.pushData[index];
  75. this.setData(this.data);
  76. },
  77. getDataIndex() {
  78. return 0;
  79. },
  80. setDataByIndex(index) {
  81. this.setData(this.pushData[index]);
  82. },
  83. setData(data) {
  84. if (!data) return;
  85. this.data = data;
  86. this.createBtnSp();
  87. this.showPost();
  88. },
  89. createBtnSp() {
  90. if (!this.data) return;
  91. cc.log("createBtnSp1 this.data " + this.data.icon);
  92. cc.loader.load({ url: this.data.icon }, function (err, tex) {
  93. cc.log("createBtnSp2 this.data " + this.data.icon);
  94. if (tex) {
  95. this.anName = null;
  96. this.node.active = true;
  97. this.tex = tex;
  98. if (this.data.plist && this.data.plist.length > 0) {
  99. wb.PlistHelper.parsePlist(this.data.plist, function (data) {
  100. this.dealPlist(data);
  101. }.bind(this));
  102. } else {
  103. this.runTempIconAction(false);
  104. }
  105. }
  106. }.bind(this));
  107. },
  108. flushNameLabel(){
  109. },
  110. runTempIconAction(isAn) {
  111. var spf = null;
  112. // this.tempIcon.active = true;
  113. if (isAn) {
  114. if (!this.anName) return;
  115. //this.animation.play(this.anName);
  116. this.tempAnimation.play(this.anName);
  117. } else {
  118. //this.animation.stop();
  119. this.tempAnimation.stop();
  120. spf = new cc.SpriteFrame(this.tex);
  121. // this.icon.spriteFrame = spf;
  122. this.tempIcon.spriteFrame = spf;
  123. }
  124. this.isRunTempAn = true;
  125. var runTime = this.actionTime;
  126. if (!this.isFirst) {
  127. runTime = 0;
  128. this.isFirst = true;
  129. }
  130. var seq = cc.sequence(cc.moveTo(runTime, cc.v2(0, 0)), cc.callFunc(function () {
  131. this.isRunTempAn = false;
  132. if (isAn) {
  133. this.animation.play(this.anName);
  134. } else {
  135. this.animation.stop();
  136. this.icon.spriteFrame = spf;
  137. }
  138. this.tempIcon.node.x = this.tempIconPX;
  139. // this.tempIcon.active = false;
  140. }.bind(this), this));
  141. this.tempIcon.node.stopAllActions();
  142. this.tempIcon.node.runAction(seq);
  143. this.flushNameLabel();
  144. },
  145. dealPlist(plistData) {
  146. if (!plistData) return;
  147. var cAn = null;
  148. var anClipList = this.animation.getClips();
  149. for (var i = 0; i < anClipList.length; i++) {
  150. if (anClipList[i].name == this.data.icon) {
  151. cAn = anClipList[i];
  152. break;
  153. }
  154. }
  155. if (cAn) {
  156. this.anName = cAn.name;
  157. this.runTempIconAction(true);
  158. // this.animation.play(cAn.name);
  159. return;
  160. }
  161. var iconDataList = [];
  162. for (var item in plistData.frames) {
  163. iconDataList.push(plistData.frames[item]);
  164. }
  165. if (iconDataList.length <= 0) return;
  166. var sps = [];
  167. for (var i = 0; i < iconDataList.length; i++) {
  168. var rect = iconDataList[i].rect;
  169. var sp = new cc.SpriteFrame(this.tex, cc.rect(rect.x, rect.y, rect.width, rect.width));
  170. sps.push(sp);
  171. }
  172. var anClip = cc.AnimationClip.createWithSpriteFrames(sps, 6);
  173. anClip.wrapMode = cc.WrapMode.Loop;
  174. anClip.name = this.data.icon
  175. // cc.Animation
  176. this.anName = anClip.name;
  177. this.animation.addClip(anClip, anClip.name);
  178. // this.animation.play(anClip.name);
  179. this.tempAnimation.addClip(anClip, anClip.name);
  180. // this.tempAnimation.play(anClip.name);
  181. this.runTempIconAction(true);
  182. },
  183. onClick() {
  184. if (this.isRunTempAn) return;
  185. if (!this.data) return;
  186. cc.log("onClick~~~~~");
  187. var openType = this.data.open;
  188. if (!wb.wx.checkSupportMiniprogram()) openType = "image";
  189. if (openType == "image") {
  190. wb.wx.previewImage({
  191. urls: [this.data.pushImage]
  192. })
  193. } else if (openType == "url") {
  194. } else if (openType == "miniprogram") {
  195. var tjKey = "ad_" + openType;
  196. var selfData = this.data;
  197. wb.wx.navigateToMiniProgram({
  198. appId: selfData.pushAppID,
  199. path: selfData.openPath,
  200. extraData: {
  201. vigame_from: "miniprogram"
  202. },
  203. success: function (res) {
  204. }
  205. });
  206. }
  207. var tjData = {
  208. label: this.data.id,
  209. pushappid: this.data.pushAppID
  210. };
  211. wb.DataTJManager.event2(this.pushType, tjData, this.placement);
  212. this.loopNext();
  213. },
  214. loopNext() {
  215. if(!wb.wxData.pushData) return;
  216. if (!this.isLoop) return;
  217. this.dataIndex++;
  218. if (this.dataIndex >= this.pushData.length) this.dataIndex = 0;
  219. this.data = this.pushData[this.dataIndex];
  220. this.setData(this.data);
  221. },
  222. // 统计参数
  223. setPlacement(placement) {
  224. this.placement = placement;
  225. },
  226. // 展示数上报
  227. showPost() {
  228. var tjData = {
  229. label: this.data.id,
  230. pushappid: this.data.pushAppID,
  231. placement: this.placement
  232. };
  233. wb.DataTJManager.reportData("pushShow", this.pushType, tjData);
  234. }
  235. });
  236. module.exports = PushAdBtn;