微信小游戏子域中请求微信头像,cocos create会自动添加前缀的问题

在微信小游戏中获取到了头像url之后,打算使用cc.loader.load进行加载

但是请求出去的时候,会在url前面添加子域目录:

像这样

    https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83epNsiabVVcQSduPLHdBicZJMIn4ogY46KrOP2rZwJngjYeP7YdYaeuaAKa0ibq3OjCunYOH3SjWzPcQQ/0

    变成了

http://127.0.0.1:50439/game/wx-sub-test/https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83epNsiabVVcQSduPLHdBicZJMIn4ogY46KrOP2rZwJngjYeP7YdYaeuaAKa0ibq3OjCunYOH3SjWzPcQQ/0

然后就报这个错误:

    0:1 GET http://127.0.0.1:50439/game/wx-sub-test/https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83epNsiabVVcQSduPLHdBicZJMIn4ogY46KrOP2rZwJngjYeP7YdYaeuaAKa0ibq3OjCunYOH3SjWzPcQQ/0?aaa=aa.jpg 404 (Not Found)
Image (async)
w @ WAGameSubContext.js:4
(anonymous) @ cocos2d-js.c822e.js:26272
downloadImage @ cocos2d-js.c822e.js:26273
errorCallback @ cocos2d-js.c822e.js:26268
(anonymous) @ WAGameSubContext.js:4
VM1253:1 gameThirdScriptError
fs.unlink is not a function
TypeError: fs.unlink is not a function
    at http://127.0.0.1:50439/game/wx-sub-test/libs/wx-downloader.js:162:16
    at LoadingItems.190.CallbacksInvoker.invoke (http://127.0.0.1:50439/game/wx-sub-test/cocos2d-js.c822e.js:33230:66)
    at LoadingItems.139.proto.itemComplete (http://127.0.0.1:50439/game/wx-sub-test/cocos2d-js.c822e.js:26792:12)
    at Function.139.LoadingItems.itemComplete (http://127.0.0.1:50439/game/wx-sub-test/cocos2d-js.c822e.js:26661:22)
    at CCLoader.142.proto.flowOut (http://127.0.0.1:50439/game/wx-sub-test/cocos2d-js.c822e.js:27061:20)
    at http://127.0.0.1:50439/game/wx-sub-test/cocos2d-js.c822e.js:26978:22
    at http://127.0.0.1:50439/game/wx-sub-test/cocos2d-js.c822e.js:26395:23
    at errorCallback (http://127.0.0.1:50439/game/wx-sub-test/cocos2d-js.c822e.js:26268:163)

    at Image.<anonymous> (http://127.0.0.1:50439/game/__subdev__/WAGameSubContext.js:4:8234)



问题源:

        在子域libs文件夹下面的wx-downloader.js这个文件,里面有这么一段,会自动跟上前缀。

WXDownloader.prototype.handle = function (item, callback){
    ...
          if (isSubdomain) {
            item.url = this.SUBCONTEXT_ROOT + '/' + item.url;

            if (item.type && non_text_format.indexOf(item.type) !== -1) {
                nextPipe(item, callback);
                return;
            }
          }

    ...
}

解决方案:

    判断url如果是http打头的,就当成网络资源,就不需要进行url重写了。

    都是针对刚刚那个文件进行修改,2处地方修改的地方都是添加 if-else判断,可以和源文件对比下。

        步骤一:修改WXDownloader.prototype.handle的url判断方式

        步骤二:修改downloadRemoteFile中的代码

WXDownloader.prototype.handle = function (item, callback) {

    if (item.type === 'js') {
        callback(null, null);
        return;
    }
    if (item.type === 'uuid') {
        var result = cc.Pipeline.Downloader.PackDownloader.load(item, callback);
        // handled by PackDownloader
        if (result !== undefined) {
            // null result
            if (!!result) {
                return result;
            }
            else {
                return;
            }
        }
    }



    var filePath = item.url;
  
  if (item.url.indexOf('http') === 0) {
    //网络图片,直接download
    downloadRemoteFile(item, callback);
}else{
  
    if (isSubdomain) {
      item.url = this.SUBCONTEXT_ROOT + '/' + item.url;

      if (item.type && non_text_format.indexOf(item.type) !== -1) {
        nextPipe(item, callback);
        return;
      }
    }
      
    // Read from package
    fs.access({
        path: filePath,
        success: function () {
            if (item.type && non_text_format.indexOf(item.type) !== -1) {
                nextPipe(item, callback);
            }
            else {
                readText(item, callback);
            }
        },
        fail: function (res) {
            readFromLocal(item, callback);
        }
    }
  );
}
};


function downloadRemoteFile (item, callback) {
    // Download from remote server
    var relatUrl = item.url;

    // filter protocol url (E.g: https:// or http:// or ftp://)
    if (REGEX.test(relatUrl)) {
        callback(null, null);
        return
    }
	var remoteUrl ='';
	if (relatUrl.indexOf('http') === 0) {
        remoteUrl = relatUrl;
    }else{
        remoteUrl = wxDownloader.REMOTE_SERVER_ROOT + '/' + relatUrl;
    }

    item.url = remoteUrl;
    ...
}


这样直接修改打包出来的项目文件,当然也可以直接修改模板文件,将下面这个文件夹的文件替换掉

 "cocos create安装目录"\resources\static\build-templates\wx\libs
如:D:\APPDate\CocosCreator_v1.9.1-rc.1\resources\static\build-templates\wx\libs 

    

猜你喜欢

转载自blog.csdn.net/cmqwan/article/details/80155607