123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- var Global = require("Global");
- var MyMd5 = require("MyMd5");
- var CGPIndividualMission = require("CGPIndividualMission");
- cc.Class({
- extends: cc.Component,
- properties: {
- _sizeNode:null,
- },
- setUrl: function(headurl, callBack) {
- if(typeof headurl == "number") {
- let spr = cc.vv.globalUserInfo.getHeadSpriteFrameByFaceID(headurl);
- if(spr) {
- this.node.getComponent(cc.Sprite).spriteFrame = spr;
- if(callBack){
- callBack();
- }
- }else{
- cc.resources.load('Atlas/headPortrait/avatar_'+headurl, cc.SpriteFrame, (err, res)=>{
- if (err) {
- cc.log('---------setUrl '+headurl+' err: ' + JSON.stringify(err));
- return;
- }
- this.node.getComponent(cc.Sprite).spriteFrame = res;
- if(callBack){
- callBack();
- }
- })
- }
-
- return;
- }
- if(headurl == ""){return}
- var self = this;
- var imgurl = Global.changeHead(headurl);
- this.callBack = callBack;
- cc.assetManager.loadRemote(imgurl, function (err, tex) {
- if (!err) {
- var spriteFrame = new cc.SpriteFrame(tex);
- self.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
- if(self.callBack){
- self.callBack();
- self.callBack = null;
- }
- }
- });
- },
- // LIFE-CYCLE CALLBACKS:
- loadAvatar: function(headurl, callBack, sizeNode) {
- if (sizeNode) {
- this.node.getComponent(cc.Sprite).sizeMode = cc.Sprite.SizeMode.CUSTOM;
- this.node.width = sizeNode.width;
- this.node.height = sizeNode.height;
- }
- if(typeof headurl == "number") {
- this.node.getComponent(cc.Sprite).spriteFrame = cc.vv.globalUserInfo.getHeadSpriteFrameByFaceID(headurl);
- if(callBack){
- callBack();
- }
- return;
- }
- if (!headurl || headurl == "") {
- // console.log(`loadAvator:: url is null`);
- return;
- }
- var self = this;
- var headimgurl = headurl;//Global.changeHead(headurl);
-
- this.callBack = callBack;
- // if(!this.mPlayerHeadRecord) {
- // this.mPlayerHeadRecord = {};
- // }
- if ("undefined" == typeof jsb) {
- cc.assetManager.loadRemote(headimgurl,function (err, tex) {
- if (!err) {
- var spriteFrame = new cc.SpriteFrame(tex);
- self.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
- if(self.callBack){
- self.callBack();
- self.callBack = null;
- }
- }
- });
- return;
- }
- Global.print("headimgurl = " + headimgurl );
- this.loadAvatarImage(headimgurl, function(spriteFrame) {
- if (spriteFrame) {
- if (sizeNode) {
- self.node.getComponent(cc.Sprite).sizeMode = cc.Sprite.SizeMode.CUSTOM;
- self.node.width = sizeNode.width;
- self.node.height = sizeNode.height;
- }
- Global.print("loadAvatarImage self.node.width0 = " + self.node.width );
- Global.print("loadAvatarImage self.node.height0 = " + self.node.height );
- Global.print("loadAvatarImage sizeMode0 = " + self.node.getComponent(cc.Sprite).sizeMode);
- self.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
- Global.print("loadAvatarImage self.node.width1 = " + self.node.width );
- Global.print("loadAvatarImage self.node.height1 = " + self.node.height );
- Global.print("loadAvatarImage sizeMode1 = " + self.node.getComponent(cc.Sprite).sizeMode);
- if (sizeNode) {
- self.node.width = sizeNode.width;
- self.node.height = sizeNode.height;
- }
- Global.print("loadAvatarImage self.node.width2 = " + self.node.width );
- Global.print("loadAvatarImage self.node.height2 = " + self.node.height );
- if(self.callBack){
- self.callBack();
- self.callBack = null;
- }
- }else {
- cc.assetManager.loadRemote(headimgurl, function (err, tex) {
- if (!err) {
- var spriteFrame = new cc.SpriteFrame(tex);
- if (sizeNode) {
- self.node.getComponent(cc.Sprite).sizeMode = cc.Sprite.SizeMode.CUSTOM;
- self.node.width = sizeNode.width;
- self.node.height = sizeNode.height;
- }
- Global.print("loadAvatarImage self.node.width0 = " + self.node.width );
- Global.print("loadAvatarImage self.node.height0 = " + self.node.height );
- Global.print("loadAvatarImage sizeMode0 = " + self.node.getComponent(cc.Sprite).sizeMode);
- self.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
- Global.print("loadAvatarImage self.node.width1 = " + self.node.width );
- Global.print("loadAvatarImage self.node.height1 = " + self.node.height );
- Global.print("loadAvatarImage sizeMode1 = " + self.node.getComponent(cc.Sprite).sizeMode);
- if (sizeNode) {
- self.node.width = sizeNode.width;
- self.node.height = sizeNode.height;
- }
- Global.print("loadAvatarImage self.node.width2 = " + self.node.width );
- Global.print("loadAvatarImage self.node.height2 = " + self.node.height );
- if(self.callBack){
- self.callBack();
- self.callBack = null;
- }
- }
- });
- }
- }.bind(this));
- },
- updateAccountHttpIP: function (userID) {
- if (!this._kReqPlayerInfo) {
- this._kReqPlayerInfo = new CGPIndividualMission({ URL: cc.vv.config.Address, Port: cc.vv.config.Port });
- this._kReqPlayerInfo.setMissionSink(this);
- }
- this._kReqPlayerInfo.queryIndividual(userID);
- },
- onGPAccountInfoHttpIP: function (kNetInfo) {
- // cc.vv.globalUserInfo.setUserHead(kNetInfo.HeadHttp);
- this.loadAvatar(kNetInfo.HeadHttp,this.callBack, this._sizeNode);
- },
- setUserId: function(userId,callBack, sizeNode) {
- this.updateAccountHttpIP(userId);
- this.callBack = callBack;
- this._sizeNode = sizeNode;
- },
- loadAvatarImage(url, callback){
- var dirpath = jsb.fileUtils.getWritablePath() + 'Avatar/';
- let md5URL = MyMd5(url);
- var filepath = dirpath + md5URL + '.png';
- // console.log(`loadAvator:: filepath=${filepath}`);
- function loadEnd(path) {
- cc.assetManager.loadRemote(path, function(err, tex){
- if( err ){
- cc.error(err);
- }else{
- var spriteFrame = new cc.SpriteFrame(tex);
- if( spriteFrame ){
- //spriteFrame.retain();
- callback(spriteFrame);
- }
- }
- });
- }
- if( jsb.fileUtils.isFileExist(filepath) ){
- Global.print('urlSpriteFrame imageLoadTool filepath isFileExist = ' + filepath);
- // console.log(`loadAvator:: filepath is exist`);
- loadEnd(filepath);
- return;
- }
- var saveFile = function(data){
- if (!data) {
- callback(null);
- return;
- }
- if( typeof data !== 'undefined' ){
- if(!jsb.fileUtils.isDirectoryExist(dirpath) ){
- jsb.fileUtils.createDirectory(dirpath);
- }
- // new Uint8Array(data) writeDataToFile writeStringToFile
- if( jsb.fileUtils.writeDataToFile(new Uint8Array(data) , filepath)){
- Global.print('urlSpriteFrame imageLoadTool writeDataToFile succeed.');
- loadEnd(filepath);
- }else{
- Global.print('urlSpriteFrame imageLoadTool writeDataToFile failed.');
- }
- }else{
- Global.print('urlSpriteFrame imageLoadTool writeDataToFile failed.');
- }
- };
-
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function () {
- // console.log(`loadAvator:: state:${xhr.readyState}\tstatus:${xhr.status}\tresponse:${xhr.response}`);
- if (xhr.readyState === 4 ) {
- if(xhr.status === 200){
- saveFile(xhr.response);
- }else{
- //saveFile(null);
- callback(null);
- }
- }
- }.bind(this);
- xhr.responseType = 'arraybuffer';
- xhr.open("GET", url, true);
- xhr.send();
- },
- });
|