NetImageMgr.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * 网络图片管理器
  3. * 仅原生平台支持下载
  4. */
  5. let NetImageMgr = {
  6. /**
  7. * 初始化
  8. *
  9. * @author Pyden
  10. * @date 2019-03-21
  11. */
  12. init () {
  13. if (!CC_JSB) {
  14. this._isDownloadingMap = {};
  15. this._storagePath = '';
  16. this._inited = false;
  17. return;
  18. }
  19. this._isDownloadingMap = {};
  20. this._storagePath = cc.path.join(jsb.fileUtils.getWritablePath(), 'image');
  21. this._inited = jsb.fileUtils.createDirectory(this._storagePath);
  22. if (!this._inited) {
  23. G.LogUtils.log('Failed to create storage path, NetImageMgr won\'t work correctly');
  24. return;
  25. }
  26. let searchPaths = jsb.fileUtils.getSearchPaths();
  27. searchPaths.unshift(this._storagePath);
  28. searchPaths.unshift('image');
  29. jsb.fileUtils.setSearchPaths(searchPaths);
  30. this._downloader = new jsb.Downloader();
  31. this._downloader.setOnFileTaskSuccess(this._onSucceed.bind(this));
  32. this._downloader.setOnTaskError(this._onError.bind(this));
  33. },
  34. /**
  35. * 获取存储图片的文件夹路径
  36. *
  37. * @author Pyden
  38. * @date 2019-03-21
  39. * @returns {string} 存储图片的文件夹路径
  40. */
  41. getImageDirPath () {
  42. return this._storagePath;
  43. },
  44. /**
  45. * 获取图片文件的全路径。优先级:图片下载路径 > 热更新路径 > 包内
  46. *
  47. * @author Pyden
  48. * @date 2019-03-21
  49. * @param {string} filename 文件名称
  50. * @returns {string} 图片文件的全路径。文件不存在时,返回undefined
  51. */
  52. getImageFullPath (filename) {
  53. if (!CC_JSB) {
  54. return undefined;
  55. }
  56. // 优先搜索路径里面的(即图片下载路径)
  57. let fullPath = jsb.fileUtils.fullPathForFilename(filename);
  58. return fullPath;
  59. },
  60. /**
  61. * 获取指定url的图片文件的全路径
  62. *
  63. * @author Pyden
  64. * @date 2019-03-21
  65. * @param {string} url 文件下载url
  66. * @returns {string} 图片文件的全路径。文件不存在时,返回undefined
  67. */
  68. getImageFullPathWithUrl (url) {
  69. let filename = this._getFilename(url);
  70. return this.getImageFullPath(filename);
  71. },
  72. /**
  73. * 清除图片缓存
  74. *
  75. * @author Pyden
  76. * @date 2019-03-21
  77. * @returns {boolean} 是否成功
  78. */
  79. clearImageDir () {
  80. if (!CC_JSB) {
  81. return false;
  82. }
  83. let ok = jsb.fileUtils.removeDirectory(this._storagePath);
  84. if (ok) {
  85. this._inited = jsb.fileUtils.createDirectory(this._storagePath);
  86. }
  87. return ok;
  88. },
  89. /**
  90. * 下载图片文件
  91. *
  92. * @author Pyden
  93. * @date 2019-03-21
  94. * @param {string} url 图片文件url
  95. * @returns {boolean} 是否正常下载
  96. */
  97. downloadImage (url) {
  98. if (!this._inited) {
  99. G.LogUtils.log('Failed to downloadImage, init failed');
  100. return false;
  101. }
  102. let filename = this._getFilename(url);
  103. if (!filename || filename.length == 0) {
  104. G.LogUtils.log('Failed to downloadImage, url error');
  105. return false;
  106. }
  107. let isDownloading = this._getIsDownloading(filename);
  108. if (isDownloading) {
  109. G.LogUtils.log('Did downloadImage');
  110. return true;
  111. }
  112. this._setIsDownloading(filename, true);
  113. G.LogUtils.log('开始下载图片 (' + url + ')');
  114. let savePath = this._getImageSavePath(filename);
  115. this._downloader.createDownloadFileTask(url, savePath);
  116. return true;
  117. },
  118. /**
  119. * 获取图片网络下载地址
  120. *
  121. * @author libo
  122. * @date 2019-05-21
  123. * @param {string} imageName 图片文件名
  124. * @returns
  125. */
  126. getImageFullUrl (imageName) {
  127. if (!imageName || imageName.length == 0) {
  128. return imageName;
  129. }
  130. return cc.path.join(window.JMC.RES_ROOT_URL, 'ssmj/res_h5/icon_img/' + imageName);
  131. },
  132. /**
  133. * 获取图片文件存储路径
  134. *
  135. * @author Pyden
  136. * @date 2019-03-21
  137. * @param {string} filename 文件名称
  138. * @returns {string} 图片文件存储路径
  139. */
  140. _getImageSavePath (filename) {
  141. return cc.path.join(this._storagePath, filename);
  142. },
  143. /**
  144. * 获取文件名称
  145. *
  146. * @author Pyden
  147. * @date 2019-03-21
  148. * @param {string} url 文件路径
  149. * @returns {string} 文件名称(带后缀)
  150. */
  151. _getFilename (url) {
  152. return cc.path.basename(url);
  153. },
  154. /**
  155. * 判断文件是否正在下载
  156. *
  157. * @author Pyden
  158. * @date 2019-03-21
  159. * @param {string} filename 文件名称
  160. * @returns {boolean} 是否正在下载
  161. */
  162. _getIsDownloading (filename) {
  163. return this._isDownloadingMap[filename];
  164. },
  165. /**
  166. * 设置文件是否正在下载
  167. *
  168. * @author Pyden
  169. * @date 2019-03-21
  170. * @param {string} filename 文件名称
  171. * @param {boolean} value 是否正在下载
  172. */
  173. _setIsDownloading (filename, value) {
  174. this._isDownloadingMap[filename] = value;
  175. },
  176. /**
  177. * 下载任务成功的回调
  178. *
  179. * @author Pyden
  180. * @date 2019-03-21
  181. * @param {DownloadTask} task 下载任务
  182. */
  183. _onSucceed (task) {
  184. G.LogUtils.log('下载图片成功 (' + task.requestURL + ')');
  185. let url = task.requestURL;
  186. let filename = this._getFilename(url);
  187. this._setIsDownloading(filename, false);
  188. cc.game.emit('e_mgr_net_image_download_ok', url);
  189. },
  190. /**
  191. * 下载任务失败的回调
  192. *
  193. * @author Pyden
  194. * @date 2019-03-21
  195. * @param {DownloadTask} task 下载任务
  196. * @param {int} errorCode 错误码
  197. * @param {int} errorCodeInternal 内部错误码
  198. * @param {string} errorStr 错误信息
  199. */
  200. _onError (task, errorCode, errorCodeInternal, errorStr) {
  201. G.LogUtils.log('下载图片失败 (' + task.requestURL + '): ' + errorStr + '(' + errorCode + ')');
  202. let url = task.requestURL;
  203. let filename = this._getFilename(url);
  204. this._setIsDownloading(filename, false);
  205. cc.game.emit('e_net_image_download_error', url, errorCode, errorCodeInternal, errorStr);
  206. }
  207. };
  208. module.exports = NetImageMgr;