urlSpriteFrame.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. var Global = require("Global");
  2. var MyMd5 = require("MyMd5");
  3. var CGPIndividualMission = require("CGPIndividualMission");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. _sizeNode:null,
  8. },
  9. setUrl: function(headurl, callBack) {
  10. if(typeof headurl == "number") {
  11. let spr = cc.vv.globalUserInfo.getHeadSpriteFrameByFaceID(headurl);
  12. if(spr) {
  13. this.node.getComponent(cc.Sprite).spriteFrame = spr;
  14. if(callBack){
  15. callBack();
  16. }
  17. }else{
  18. cc.resources.load('Atlas/headPortrait/avatar_'+headurl, cc.SpriteFrame, (err, res)=>{
  19. if (err) {
  20. cc.log('---------setUrl '+headurl+' err: ' + JSON.stringify(err));
  21. return;
  22. }
  23. this.node.getComponent(cc.Sprite).spriteFrame = res;
  24. if(callBack){
  25. callBack();
  26. }
  27. })
  28. }
  29. return;
  30. }
  31. if(headurl == ""){return}
  32. var self = this;
  33. var imgurl = Global.changeHead(headurl);
  34. this.callBack = callBack;
  35. cc.assetManager.loadRemote(imgurl, function (err, tex) {
  36. if (!err) {
  37. var spriteFrame = new cc.SpriteFrame(tex);
  38. self.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
  39. if(self.callBack){
  40. self.callBack();
  41. self.callBack = null;
  42. }
  43. }
  44. });
  45. },
  46. // LIFE-CYCLE CALLBACKS:
  47. loadAvatar: function(headurl, callBack, sizeNode) {
  48. if (sizeNode) {
  49. this.node.getComponent(cc.Sprite).sizeMode = cc.Sprite.SizeMode.CUSTOM;
  50. this.node.width = sizeNode.width;
  51. this.node.height = sizeNode.height;
  52. }
  53. if(typeof headurl == "number") {
  54. this.node.getComponent(cc.Sprite).spriteFrame = cc.vv.globalUserInfo.getHeadSpriteFrameByFaceID(headurl);
  55. if(callBack){
  56. callBack();
  57. }
  58. return;
  59. }
  60. if (!headurl || headurl == "") {
  61. // console.log(`loadAvator:: url is null`);
  62. return;
  63. }
  64. var self = this;
  65. var headimgurl = headurl;//Global.changeHead(headurl);
  66. this.callBack = callBack;
  67. // if(!this.mPlayerHeadRecord) {
  68. // this.mPlayerHeadRecord = {};
  69. // }
  70. if ("undefined" == typeof jsb) {
  71. cc.assetManager.loadRemote(headimgurl,function (err, tex) {
  72. if (!err) {
  73. var spriteFrame = new cc.SpriteFrame(tex);
  74. self.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
  75. if(self.callBack){
  76. self.callBack();
  77. self.callBack = null;
  78. }
  79. }
  80. });
  81. return;
  82. }
  83. Global.print("headimgurl = " + headimgurl );
  84. this.loadAvatarImage(headimgurl, function(spriteFrame) {
  85. if (spriteFrame) {
  86. if (sizeNode) {
  87. self.node.getComponent(cc.Sprite).sizeMode = cc.Sprite.SizeMode.CUSTOM;
  88. self.node.width = sizeNode.width;
  89. self.node.height = sizeNode.height;
  90. }
  91. Global.print("loadAvatarImage self.node.width0 = " + self.node.width );
  92. Global.print("loadAvatarImage self.node.height0 = " + self.node.height );
  93. Global.print("loadAvatarImage sizeMode0 = " + self.node.getComponent(cc.Sprite).sizeMode);
  94. self.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
  95. Global.print("loadAvatarImage self.node.width1 = " + self.node.width );
  96. Global.print("loadAvatarImage self.node.height1 = " + self.node.height );
  97. Global.print("loadAvatarImage sizeMode1 = " + self.node.getComponent(cc.Sprite).sizeMode);
  98. if (sizeNode) {
  99. self.node.width = sizeNode.width;
  100. self.node.height = sizeNode.height;
  101. }
  102. Global.print("loadAvatarImage self.node.width2 = " + self.node.width );
  103. Global.print("loadAvatarImage self.node.height2 = " + self.node.height );
  104. if(self.callBack){
  105. self.callBack();
  106. self.callBack = null;
  107. }
  108. }else {
  109. cc.assetManager.loadRemote(headimgurl, function (err, tex) {
  110. if (!err) {
  111. var spriteFrame = new cc.SpriteFrame(tex);
  112. if (sizeNode) {
  113. self.node.getComponent(cc.Sprite).sizeMode = cc.Sprite.SizeMode.CUSTOM;
  114. self.node.width = sizeNode.width;
  115. self.node.height = sizeNode.height;
  116. }
  117. Global.print("loadAvatarImage self.node.width0 = " + self.node.width );
  118. Global.print("loadAvatarImage self.node.height0 = " + self.node.height );
  119. Global.print("loadAvatarImage sizeMode0 = " + self.node.getComponent(cc.Sprite).sizeMode);
  120. self.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
  121. Global.print("loadAvatarImage self.node.width1 = " + self.node.width );
  122. Global.print("loadAvatarImage self.node.height1 = " + self.node.height );
  123. Global.print("loadAvatarImage sizeMode1 = " + self.node.getComponent(cc.Sprite).sizeMode);
  124. if (sizeNode) {
  125. self.node.width = sizeNode.width;
  126. self.node.height = sizeNode.height;
  127. }
  128. Global.print("loadAvatarImage self.node.width2 = " + self.node.width );
  129. Global.print("loadAvatarImage self.node.height2 = " + self.node.height );
  130. if(self.callBack){
  131. self.callBack();
  132. self.callBack = null;
  133. }
  134. }
  135. });
  136. }
  137. }.bind(this));
  138. },
  139. updateAccountHttpIP: function (userID) {
  140. if (!this._kReqPlayerInfo) {
  141. this._kReqPlayerInfo = new CGPIndividualMission({ URL: cc.vv.config.Address, Port: cc.vv.config.Port });
  142. this._kReqPlayerInfo.setMissionSink(this);
  143. }
  144. this._kReqPlayerInfo.queryIndividual(userID);
  145. },
  146. onGPAccountInfoHttpIP: function (kNetInfo) {
  147. // cc.vv.globalUserInfo.setUserHead(kNetInfo.HeadHttp);
  148. this.loadAvatar(kNetInfo.HeadHttp,this.callBack, this._sizeNode);
  149. },
  150. setUserId: function(userId,callBack, sizeNode) {
  151. this.updateAccountHttpIP(userId);
  152. this.callBack = callBack;
  153. this._sizeNode = sizeNode;
  154. },
  155. loadAvatarImage(url, callback){
  156. var dirpath = jsb.fileUtils.getWritablePath() + 'Avatar/';
  157. let md5URL = MyMd5(url);
  158. var filepath = dirpath + md5URL + '.png';
  159. // console.log(`loadAvator:: filepath=${filepath}`);
  160. function loadEnd(path) {
  161. cc.assetManager.loadRemote(path, function(err, tex){
  162. if( err ){
  163. cc.error(err);
  164. }else{
  165. var spriteFrame = new cc.SpriteFrame(tex);
  166. if( spriteFrame ){
  167. //spriteFrame.retain();
  168. callback(spriteFrame);
  169. }
  170. }
  171. });
  172. }
  173. if( jsb.fileUtils.isFileExist(filepath) ){
  174. Global.print('urlSpriteFrame imageLoadTool filepath isFileExist = ' + filepath);
  175. // console.log(`loadAvator:: filepath is exist`);
  176. loadEnd(filepath);
  177. return;
  178. }
  179. var saveFile = function(data){
  180. if (!data) {
  181. callback(null);
  182. return;
  183. }
  184. if( typeof data !== 'undefined' ){
  185. if(!jsb.fileUtils.isDirectoryExist(dirpath) ){
  186. jsb.fileUtils.createDirectory(dirpath);
  187. }
  188. // new Uint8Array(data) writeDataToFile writeStringToFile
  189. if( jsb.fileUtils.writeDataToFile(new Uint8Array(data) , filepath)){
  190. Global.print('urlSpriteFrame imageLoadTool writeDataToFile succeed.');
  191. loadEnd(filepath);
  192. }else{
  193. Global.print('urlSpriteFrame imageLoadTool writeDataToFile failed.');
  194. }
  195. }else{
  196. Global.print('urlSpriteFrame imageLoadTool writeDataToFile failed.');
  197. }
  198. };
  199. var xhr = new XMLHttpRequest();
  200. xhr.onreadystatechange = function () {
  201. // console.log(`loadAvator:: state:${xhr.readyState}\tstatus:${xhr.status}\tresponse:${xhr.response}`);
  202. if (xhr.readyState === 4 ) {
  203. if(xhr.status === 200){
  204. saveFile(xhr.response);
  205. }else{
  206. //saveFile(null);
  207. callback(null);
  208. }
  209. }
  210. }.bind(this);
  211. xhr.responseType = 'arraybuffer';
  212. xhr.open("GET", url, true);
  213. xhr.send();
  214. },
  215. });