uni_android_plugin_project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.0 KiB

3 years ago
  1. import downloadImg from "./download.js"
  2. /*
  3. fileName: 需要查询的目录名
  4. */
  5. async function getFileChildrenList(fileName){
  6. let fs = await downloadImg.requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
  7. let faceSyncEntry = await downloadImg.getDirectory({
  8. //在PUBLIC_DOWNLOADS下定义人脸库同步文件夹"Face-Sync" Success-Import
  9. OriginPathEntry: fs.root,
  10. newFileName: fileName
  11. });
  12. return downloadImg.getFileChildrenList(faceSyncEntry).then((res)=>{
  13. return res
  14. }).catch((e)=>{
  15. console.log("获取文件列表失败:",e)
  16. })
  17. }
  18. /*
  19. fileName: 需要删除的文件名
  20. */
  21. async function deleteAndroidFaceImage(fileName){
  22. let item = {
  23. "deleteFile" : true,
  24. "name": fileName
  25. }
  26. let fs = await downloadImg.requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
  27. let url = item.url; //测试url---------
  28. let newName = item.name;
  29. let faceSyncEntry = await downloadImg.getDirectory({
  30. //在PUBLIC_DOWNLOADS下定义人脸库同步文件夹"Face-Sync" Success-Import
  31. OriginPathEntry: fs.root,
  32. newFileName: 'Face-Sync'
  33. });
  34. let newNameFileEntry = await downloadImg.resolveLocalFileSystemURL(faceSyncEntry.fullPath + newName).catch(e => {
  35. console.log('目标文件可以操作', e);
  36. });
  37. if (newNameFileEntry) {
  38. console.log('存在缓存的同名文件:', newNameFileEntry.fullPath);
  39. if(item.deleteFile){
  40. let delResult = await downloadImg.removeFile(newNameFileEntry); //删除已存在的文件
  41. console.log("删除同名文件结果:",delResult)
  42. return delResult
  43. }
  44. if(item.forceDownload){//当本地存在缓存时,是否强制删除后重新下载
  45. let delResult = await downloadImg.removeFile(newNameFileEntry); //删除已存在的文件
  46. }else{
  47. return newNameFileEntry.fullPath //返回已缓存文件路径
  48. }
  49. }else{
  50. console.log("不存在同名文件:"+fileName)
  51. }
  52. }
  53. /*
  54. 直接处理接口数据,下载列表并返回下载结果
  55. 并同时处理request_time,检测确保当完成列表下载后,本地再存储request_time
  56. */
  57. async function downloadFileList(res){
  58. // await asyncSetTimeOut(3000) //延时,手动断网,模拟下载失败
  59. return new Promise(async(rs,rj)=>{
  60. let {list,request_time,total} = res
  61. console.log(request_time,total);
  62. let downloadSuccessList = []
  63. for (var i=0; i < list.length; i++) {
  64. try{
  65. await checkNetworkUsable()
  66. console.log("当前第"+(i+1)+"张, 总需下载:"+(list.length))
  67. let local_path = await downloadFileImg(list[i])
  68. downloadSuccessList.push(local_path);
  69. list[i]['local_path'] = local_path
  70. }catch(e){
  71. //TODO handle the exception
  72. // util.showNone(e)
  73. console.error("下载失败:",e);
  74. list[i]['error'] = e
  75. continue
  76. }
  77. }
  78. res.list = list
  79. checkRequestTime(res)
  80. rs(list)
  81. })
  82. }
  83. /*,,
  84. */
  85. function checkRequestTime(res){
  86. if(res.list.every(e=>e.local_path)){
  87. setRequestTime(res)
  88. }
  89. }
  90. /*downloadFileList ,pluginuser_idstring
  91. : 15,17
  92. */
  93. function getDataForPlugin(list){
  94. return list.map(e=>{
  95. if(e.local_path)return e.user_id
  96. }).join(",")
  97. }
  98. /*
  99. */
  100. function checkNetworkUsable(){
  101. return new Promise((rs,rj)=>{
  102. uni.getNetworkType({
  103. success: function (res) {
  104. console.log("checkNetworkUsable 当前可用网络:",res.networkType);
  105. rs(true)
  106. },
  107. fail: function (err) {
  108. console.log(err);
  109. rj(false)
  110. }
  111. });
  112. })
  113. }
  114. /*
  115. 后台约定,人脸更新机制为带时间请求
  116. */
  117. function setRequestTime(res){
  118. if(res.request_time){
  119. uni.setStorageSync("request_time",res.request_time)
  120. }
  121. }
  122. /*
  123. */
  124. function getRequestTime(){
  125. return new Promise((rs,rj)=>{
  126. let _time = ""
  127. try{
  128. _time = uni.getStorageSync("request_time")
  129. }catch(e){
  130. console.warn("请求时间戳异常:",e);
  131. }
  132. rs(_time)
  133. })
  134. }
  135. async function asyncSetTimeOut(time){
  136. const res = await new Promise(resolve => {
  137. setTimeout(() => resolve("asyncSetTimeOut"), time||1000);
  138. });
  139. return res
  140. }
  141. /*
  142. //下载文件,转存后,按user_id重命名,并返回最终文件本地路径.
  143. //当有重复文件时,覆盖
  144. item:{
  145. pic_url:"",user_id:"",update_time:""
  146. }
  147. */
  148. async function downloadFileImg(item) {
  149. let fs = await downloadImg.requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
  150. let url = item.pic_url; //测试url---------
  151. let onProgresCallBack = function(e) {
  152. //预留下载进度封装拓展
  153. console.log('下载中...user_id:'+item.user_id, e);
  154. };
  155. let tempDownloadFilePath = await downloadImg.getDownloadFile({
  156. url,
  157. onProgresCallBack
  158. }); //获取下载文件临时路径
  159. let savedFilePath = await downloadImg.getSaveFile(tempDownloadFilePath); //将文件临时路径长久存储并清除
  160. let newName = item.user_id + '.' + (savedFilePath.split('.')[1] ||
  161. 'jpg'); //命名与android约定的文件名: [user_id].jpg------------
  162. newName = item.user_id + getName(url);
  163. // console.log(tempDownloadFilePath, savedFilePath.split('.')[1], savedFilePath);
  164. let savedFilePathEntry = await downloadImg.resolveLocalFileSystemURL(savedFilePath); //获取下载后存储的文件Entry
  165. let faceSyncEntry = await downloadImg.getDirectory({
  166. //在PUBLIC_DOWNLOADS下定义人脸库同步文件夹"Face-Sync"
  167. OriginPathEntry: fs.root,
  168. newFileName: 'Face-Sync'
  169. });
  170. let newNameFileEntry = await downloadImg.resolveLocalFileSystemURL(faceSyncEntry.fullPath + newName).catch(
  171. e => {
  172. console.log('目标文件可以操作', e);
  173. });
  174. if (newNameFileEntry) {
  175. console.log('删除存在的同名文件:', newNameFileEntry.fullPath);
  176. let delResult = await downloadImg.removeFile(newNameFileEntry); //删除已存在的文件
  177. }
  178. let reNamePathEntry = await downloadImg.moveFileTo(savedFilePathEntry, faceSyncEntry, newName); //重命名文件到人脸库同步文件夹
  179. // console.log(savedFilePath,newName,reNamePathEntry.fullPath)
  180. return reNamePathEntry.fullPath;
  181. }
  182. function getName(url) {
  183. let end = '.';
  184. let urlarr = url.split('.');
  185. let endname = urlarr[urlarr.length];
  186. if (endname != 'jpg' || endname != 'png' || endname != 'jpeg' || endname != 'PNG' || endname != 'JPG' || endname != 'JPEG') endname = 'jpg';
  187. return (end += endname);
  188. }
  189. //递归清空本地同步文件目录及子目录
  190. async function clearDownloadFace(fileName){
  191. let fs = await downloadImg.requestFileSystem(plus.io.PUBLIC_DOWNLOADS); //获取所要操作根目录File System
  192. let faceSyncEntry = await downloadImg.getDirectory({
  193. //在PUBLIC_DOWNLOADS下定义人脸库同步文件夹[fileName]
  194. OriginPathEntry: fs.root,
  195. newFileName: fileName
  196. });
  197. let newNameFileEntry = await downloadImg.resolveLocalFileSystemURL(faceSyncEntry.fullPath).catch(
  198. e => {
  199. console.log('操作目标文件', e);
  200. });
  201. await downloadImg.removeFileAll(newNameFileEntry).then(e=>{
  202. console.log(fileName,"删除:",e);
  203. })
  204. }
  205. module.exports = {
  206. downloadFileImg,
  207. clearDownloadFace,
  208. downloadFileList,
  209. getRequestTime,
  210. getDataForPlugin,
  211. asyncSetTimeOut,
  212. deleteAndroidFaceImage,
  213. getFileChildrenList
  214. }