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.

264 lines
8.4 KiB

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