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.

253 lines
8.0 KiB

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