123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- /**
- * 网络图片管理器
- * 仅原生平台支持下载
- */
- let NetImageMgr = {
- /**
- * 初始化
- *
- * @author Pyden
- * @date 2019-03-21
- */
- init () {
- if (!CC_JSB) {
- this._isDownloadingMap = {};
- this._storagePath = '';
- this._inited = false;
- return;
- }
- this._isDownloadingMap = {};
- this._storagePath = cc.path.join(jsb.fileUtils.getWritablePath(), 'image');
- this._inited = jsb.fileUtils.createDirectory(this._storagePath);
- if (!this._inited) {
- G.LogUtils.log('Failed to create storage path, NetImageMgr won\'t work correctly');
- return;
- }
- let searchPaths = jsb.fileUtils.getSearchPaths();
- searchPaths.unshift(this._storagePath);
- searchPaths.unshift('image');
- jsb.fileUtils.setSearchPaths(searchPaths);
- this._downloader = new jsb.Downloader();
- this._downloader.setOnFileTaskSuccess(this._onSucceed.bind(this));
- this._downloader.setOnTaskError(this._onError.bind(this));
- },
- /**
- * 获取存储图片的文件夹路径
- *
- * @author Pyden
- * @date 2019-03-21
- * @returns {string} 存储图片的文件夹路径
- */
- getImageDirPath () {
- return this._storagePath;
- },
- /**
- * 获取图片文件的全路径。优先级:图片下载路径 > 热更新路径 > 包内
- *
- * @author Pyden
- * @date 2019-03-21
- * @param {string} filename 文件名称
- * @returns {string} 图片文件的全路径。文件不存在时,返回undefined
- */
- getImageFullPath (filename) {
- if (!CC_JSB) {
- return undefined;
- }
- // 优先搜索路径里面的(即图片下载路径)
- let fullPath = jsb.fileUtils.fullPathForFilename(filename);
- return fullPath;
- },
- /**
- * 获取指定url的图片文件的全路径
- *
- * @author Pyden
- * @date 2019-03-21
- * @param {string} url 文件下载url
- * @returns {string} 图片文件的全路径。文件不存在时,返回undefined
- */
- getImageFullPathWithUrl (url) {
- let filename = this._getFilename(url);
- return this.getImageFullPath(filename);
- },
- /**
- * 清除图片缓存
- *
- * @author Pyden
- * @date 2019-03-21
- * @returns {boolean} 是否成功
- */
- clearImageDir () {
- if (!CC_JSB) {
- return false;
- }
- let ok = jsb.fileUtils.removeDirectory(this._storagePath);
- if (ok) {
- this._inited = jsb.fileUtils.createDirectory(this._storagePath);
- }
- return ok;
- },
- /**
- * 下载图片文件
- *
- * @author Pyden
- * @date 2019-03-21
- * @param {string} url 图片文件url
- * @returns {boolean} 是否正常下载
- */
- downloadImage (url) {
- if (!this._inited) {
- G.LogUtils.log('Failed to downloadImage, init failed');
- return false;
- }
- let filename = this._getFilename(url);
- if (!filename || filename.length == 0) {
- G.LogUtils.log('Failed to downloadImage, url error');
- return false;
- }
- let isDownloading = this._getIsDownloading(filename);
- if (isDownloading) {
- G.LogUtils.log('Did downloadImage');
- return true;
- }
- this._setIsDownloading(filename, true);
- G.LogUtils.log('开始下载图片 (' + url + ')');
- let savePath = this._getImageSavePath(filename);
- this._downloader.createDownloadFileTask(url, savePath);
- return true;
- },
- /**
- * 获取图片网络下载地址
- *
- * @author libo
- * @date 2019-05-21
- * @param {string} imageName 图片文件名
- * @returns
- */
- getImageFullUrl (imageName) {
- if (!imageName || imageName.length == 0) {
- return imageName;
- }
- return cc.path.join(window.JMC.RES_ROOT_URL, 'ssmj/res_h5/icon_img/' + imageName);
- },
- /**
- * 获取图片文件存储路径
- *
- * @author Pyden
- * @date 2019-03-21
- * @param {string} filename 文件名称
- * @returns {string} 图片文件存储路径
- */
- _getImageSavePath (filename) {
- return cc.path.join(this._storagePath, filename);
- },
- /**
- * 获取文件名称
- *
- * @author Pyden
- * @date 2019-03-21
- * @param {string} url 文件路径
- * @returns {string} 文件名称(带后缀)
- */
- _getFilename (url) {
- return cc.path.basename(url);
- },
- /**
- * 判断文件是否正在下载
- *
- * @author Pyden
- * @date 2019-03-21
- * @param {string} filename 文件名称
- * @returns {boolean} 是否正在下载
- */
- _getIsDownloading (filename) {
- return this._isDownloadingMap[filename];
- },
- /**
- * 设置文件是否正在下载
- *
- * @author Pyden
- * @date 2019-03-21
- * @param {string} filename 文件名称
- * @param {boolean} value 是否正在下载
- */
- _setIsDownloading (filename, value) {
- this._isDownloadingMap[filename] = value;
- },
- /**
- * 下载任务成功的回调
- *
- * @author Pyden
- * @date 2019-03-21
- * @param {DownloadTask} task 下载任务
- */
- _onSucceed (task) {
- G.LogUtils.log('下载图片成功 (' + task.requestURL + ')');
- let url = task.requestURL;
- let filename = this._getFilename(url);
- this._setIsDownloading(filename, false);
- cc.game.emit('e_mgr_net_image_download_ok', url);
- },
- /**
- * 下载任务失败的回调
- *
- * @author Pyden
- * @date 2019-03-21
- * @param {DownloadTask} task 下载任务
- * @param {int} errorCode 错误码
- * @param {int} errorCodeInternal 内部错误码
- * @param {string} errorStr 错误信息
- */
- _onError (task, errorCode, errorCodeInternal, errorStr) {
- G.LogUtils.log('下载图片失败 (' + task.requestURL + '): ' + errorStr + '(' + errorCode + ')');
- let url = task.requestURL;
- let filename = this._getFilename(url);
- this._setIsDownloading(filename, false);
- cc.game.emit('e_net_image_download_error', url, errorCode, errorCodeInternal, errorStr);
- }
- };
- module.exports = NetImageMgr;
|