智慧画屏客户端
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.

690 lines
17 KiB

3 years ago
  1. import { LocalData, getStorage, setStorage, setClass } from '@/utils/global'
  2. import store from '../store/store.js';
  3. const url_sync = getApp().globalData.request_url+'/portrait-api/getSync' //获取所有资源接口
  4. const url_confirm = getApp().globalData.request_url+'/portrait-api/getSyncConfirm' //同步结果回送
  5. const url_updateSwap = getApp().globalData.request_url+'/portrait-api/swapUpdate' //更新轮播图
  6. //下载列表
  7. let listBook = []
  8. let listDoc = []
  9. let listVideo = []
  10. let listTheme = []
  11. let listVr = []
  12. //当前下载的个数
  13. let videoIndex = 0
  14. let videochildIndex= 0
  15. let bookIndex = 0
  16. let docIndex = 0
  17. let themeIndex = 0
  18. let vrIndex = 0
  19. //当前下载 成功 的个数
  20. let videodown = 0
  21. let bookdown = 0
  22. let docdown = 0
  23. let themedown = 0
  24. let vrdown = 0
  25. let aiyxliburl="https://qiniu.aiyxlib.com/";
  26. let targetPath ; //存储本地文件路径
  27. let overtime=0; //超时次数
  28. let curVideoDir = 0; //视频子集下载
  29. let backPics = [] //轮播图
  30. let currentSyncNum=0; //总共更新下载资源数
  31. let serialNum=uni.getStorageSync('serialNum')||'';
  32. //分类列表
  33. let bookClass
  34. let docClass
  35. let videoClass
  36. let themeClass
  37. let vrClass
  38. //删除列表
  39. let delBook = []
  40. let delDoc = []
  41. let delVideo = []
  42. let delTheme = []
  43. let delVr = []
  44. //获取所有资源
  45. const Sync = {
  46. start: function(){
  47. serialNum=uni.getStorageSync('serialNum')||'';
  48. // console.log(777, serialNum)
  49. showLoading('正在加载')
  50. try{
  51. uni.request({
  52. url:`${url_sync}?serialNumber=${serialNum}`,
  53. method: 'POST',
  54. success: (res) => {
  55. console.log(res)
  56. uni.hideLoading()
  57. let {errCode, data} = res.data
  58. if(errCode == 0){
  59. listBook = data.listBook
  60. listDoc = data.listDoc
  61. listVideo = data.listVideo
  62. listTheme = data.listTheme
  63. listVr = data.listVr
  64. delBook = data.delEbook
  65. delDoc = data.delDoc
  66. delTheme = data.delTheme
  67. delVr = data.delVr
  68. delVideo = data.delVideo
  69. let classMap = data.classOrderMap;
  70. bookClass = classMap[0] ? classMap[0] : [];
  71. docClass = classMap[1] ? classMap[1] : [];
  72. videoClass = classMap[2] ? classMap[2] : [];
  73. themeClass = classMap[3] ? classMap[3] : [];
  74. vrClass = classMap[4] ? classMap[4] : [];
  75. //当前下载 成功 的个数
  76. videodown = 0
  77. bookdown = 0
  78. docdown = 0
  79. themedown = 0
  80. vrdown = 0
  81. //当前下载到第几个
  82. videoIndex = 0
  83. videochildIndex= 0
  84. bookIndex = 0
  85. docIndex = 0
  86. themeIndex = 0
  87. vrIndex = 0
  88. aiyxliburl=data.url
  89. currentSyncNum=data.currentSyncNum
  90. console.log(aiyxliburl)
  91. // console.log('2222', JSON.stringify(classMap))
  92. //删除
  93. delRes()
  94. //正式存储
  95. syncQueue()
  96. }else{
  97. uni.showToast({
  98. title: '资源请求失败',
  99. icon:'none',
  100. duration: 2500
  101. });
  102. }
  103. },
  104. fail: (e) => {
  105. console.log('fail: ', e)
  106. uni.hideLoading()
  107. uni.showToast({
  108. title: '接口请求失败',
  109. icon:'none',
  110. duration: 2000
  111. });
  112. }
  113. })
  114. }catch(e){
  115. console.log(e)
  116. }
  117. }
  118. }
  119. function syncQueue() {
  120. // console.log(bookIndex, docIndex,themeIndex,vrIndex,videoIndex)
  121. if(bookIndex < listBook.length ){
  122. showLoading('获取图书:' + (bookIndex) + ' / ' + listBook.length)
  123. targetPath = "_doc/book/";
  124. getResource(listBook[bookIndex],0)
  125. }else if(docIndex < listDoc.length ){
  126. showLoading('获取阅文:' + (docIndex) + ' / ' + listDoc.length)
  127. targetPath = "_doc/doc/";
  128. getResource(listDoc[docIndex],1)
  129. }else if(themeIndex < listTheme.length){
  130. showLoading('获取阅刊: ' + (themeIndex) + ' / ' + listTheme.length)
  131. targetPath = "_doc/theme/";
  132. getResource(listTheme[themeIndex],3)
  133. }else if(vrIndex < listVr.length){
  134. showLoading('获取VR: ' + (vrIndex) + ' / ' + listVr.length)
  135. targetPath = "_doc/theme/";
  136. getResource(listVr[vrIndex],4)
  137. }else if(videoIndex < listVideo.length){
  138. targetPath = "_doc/video/";
  139. getResource(listVideo[videoIndex],2)
  140. }
  141. else{
  142. uni.hideLoading()
  143. //更新轮播图
  144. updateSwap()
  145. }
  146. }
  147. //下载封面
  148. function getResource(resList,savetype) {
  149. const that = this;
  150. let coverState=resList.coverState||resList.coverCollectionState;
  151. console.log("coverState: " + coverState);
  152. if(!coverState && resList.cover){
  153. let onurl=aiyxliburl+resList.cover;
  154. // console.log("onurl: " + onurl);
  155. try {
  156. let dtask = plus.downloader.createDownload(
  157. onurl,
  158. { filename: targetPath },
  159. function (d, status) {
  160. if (status==200) {
  161. resList.cover = resList.cover.split(".")[0]+".jpg";
  162. console.log("Download success: " + resList.cover);
  163. switch(savetype){
  164. case 0://读书
  165. bookIndex++;
  166. bookdown++;
  167. store.commit("bookData", bookdown);
  168. store.commit("bookFalse", listBook.length-bookdown);
  169. break;
  170. case 1://阅文
  171. docIndex++;
  172. docdown++;
  173. store.commit("docData", docdown);
  174. store.commit("docFalse", listDoc.length-docdown);
  175. break;
  176. case 3://阅刊
  177. themeIndex++;
  178. themedown++;
  179. store.commit("themeData", themedown);
  180. store.commit("themeFalse", listTheme.length-themedown);
  181. break;
  182. case 4://vr
  183. vrIndex++;
  184. vrdown++;
  185. store.commit("vrData", vrdown);
  186. store.commit("vrFalse", listVr.length-vrdown);
  187. break;
  188. case 2://视频
  189. videoIndex++;
  190. break;
  191. }
  192. if(savetype == 2){
  193. curVideoDir = 0
  194. resList.coverCollectionState = true;
  195. // console.log(resList);
  196. getVideoDirectory(resList)
  197. }else{
  198. resList.coverState = true;
  199. syncPush(resList,savetype)
  200. syncQueue()
  201. }
  202. } else {
  203. syncQueue()
  204. if(savetype == 2){
  205. let videofalse=store.state.videofalse+resList.directory.length;
  206. store.commit("videoFalse", videofalse);
  207. }
  208. console.log("Download failed: " + status);
  209. }
  210. }
  211. );
  212. dtask.start();
  213. } catch (error) {
  214. syncQueue()
  215. console.log("Download failed: " + error);
  216. }
  217. }else{
  218. switch(savetype){
  219. case 0://读书
  220. bookIndex++;
  221. break;
  222. case 1://阅文
  223. docIndex++;
  224. break;
  225. case 3://阅刊
  226. themeIndex++;
  227. break;
  228. case 4://vr
  229. vrIndex++;
  230. break;
  231. case 2://视频
  232. videoIndex++;
  233. break;
  234. }
  235. console.log(coverState );
  236. syncQueue()
  237. // resList.localCover =targetPath+resList.cover;
  238. }
  239. }
  240. //同步推送下载
  241. function syncPush(entity,savetype) {
  242. // console.log(entity)
  243. let param = {
  244. belongVideoName: entity.name, //(父)资源名称
  245. coverState: entity.coverState||entity.coverCollectionState, //封面下载状况
  246. failType: 0, //失败类别 0成功 1失败 2超时
  247. ok: true, //(是否操作成功)
  248. fileName:entity.cover, //封面地址
  249. resourceId: entity.baseId, //资源id
  250. sn: serialNum, //设备序列号
  251. state: 1, //1.编辑 2.删除
  252. classId:entity.classId, //资源类型
  253. typeId: savetype,//资源类别 0图书 1阅文 2视频 3阅刊 4vr (子视频不填)
  254. videoState: entity.videoCollectionState||""//视频是否下载(子视频是否全部下载)
  255. }
  256. // console.log(param)
  257. syncRequest(param,entity,savetype)
  258. }
  259. //同步推送下载子视频
  260. function syncPushvideo(entity,viedochild,type) {
  261. // console.log(entity,viedochild)
  262. if(type==0){
  263. videodown++;
  264. store.commit("videoData", videodown);
  265. }else{
  266. store.state.videofalse++;
  267. }
  268. let reason
  269. type==0 ? reason="" : type==1 ? reason="失败" : reason="超时"
  270. let param = {
  271. belongVideoName: entity.name, //(父)资源名称
  272. coverState: viedochild.coverState, //封面下载状况
  273. failType: type, //失败类别 0成功 1失败 2超时
  274. fileName:viedochild.cover, //封面地址
  275. lastWrongReason: reason,
  276. ok: true, //(是否操作成功)
  277. resourceId: entity.baseId, //资源id
  278. sn: serialNum, //设备序列号
  279. state: 1, //1.编辑 2.删除
  280. videoId: viedochild.id,//子视频资源id
  281. videoName: viedochild.name, //子视频名称
  282. belongId: viedochild.belongId,//父集合资源id
  283. videoState: viedochild.videoState//视频是否下载(子视频是否全部下载)
  284. }
  285. console.log(param)
  286. syncRequest(param,entity,6)
  287. }
  288. //同步接口请求
  289. function syncRequest(param,entity,savetype) {
  290. uni.request({
  291. url:url_confirm,
  292. data:param,
  293. method: 'POST',
  294. success: (res) => {
  295. console.log(res)
  296. if(res.data.errCode==0){
  297. switch(savetype){
  298. case 0:
  299. LocalData.book=categoryFiles(LocalData.book, entity, bookClass)
  300. // console.log('存进分类书列表:', LocalData.book)
  301. break;
  302. case 1:
  303. LocalData.doc=categoryFiles(LocalData.doc, entity, docClass)
  304. break;
  305. case 3:
  306. LocalData.theme=categoryFiles(LocalData.theme, entity, themeClass)
  307. break;
  308. case 4:
  309. LocalData.vr=categoryFiles(LocalData.vr, entity, vrClass)
  310. break;
  311. case 2:
  312. LocalData.video=categoryFiles(LocalData.video, entity, videoClass)
  313. break;
  314. }
  315. setStorage(LocalData)
  316. // console.log(LocalData)
  317. console.log('向后台返回成功')
  318. }else{
  319. console.log(res.data.errMsg)
  320. }
  321. },
  322. fail: (err) => {
  323. uni.hideLoading()
  324. uni.showToast({
  325. title: '推送失败: ' + err,
  326. icon:none,
  327. duration: 2000
  328. })
  329. }
  330. })
  331. }
  332. //存进分类列表
  333. function categoryFiles(locData, curData, classArray) {
  334. // console.log("存进分类",locData)
  335. if(locData.length < 1||locData==null){
  336. locData.push({id:0, name: '全部', order: -1, list:[]})
  337. classArray.forEach((classItem) => {
  338. classItem.list=[];
  339. locData.push(classItem)
  340. })
  341. console.log(locData)
  342. categoryFiles(locData, curData, classArray)
  343. }else{
  344. locData.forEach((classItem) => {
  345. if(curData.classId==classItem.id){
  346. classItem.list.push(curData)
  347. // console.log(classItem.list)
  348. }
  349. if(classItem.id==0){
  350. classItem.list.push(curData)
  351. // console.log(classItem)
  352. }
  353. })
  354. }
  355. locData = locData.sort((a,b) => {
  356. return a.order < b.order
  357. })
  358. return locData
  359. }
  360. function getVideoDirectory (video) {
  361. let directory = video.directory
  362. // console.log("directory", directory)
  363. if(video.videoCollectionState){
  364. console.log(videoIndex)
  365. syncQueue()
  366. return ;
  367. }
  368. curVideoDir++;
  369. if(curVideoDir <= directory.length){
  370. console.log(386)
  371. showLoading('获取视频:' + ((videoIndex-1)*1+((curVideoDir/directory.length).toFixed(1))*1) +'/ ' + listVideo.length)
  372. getFile(video, directory[curVideoDir-1])
  373. return
  374. }
  375. uni.hideLoading()
  376. video.videoCollectionState=video.directory.every((x)=> {
  377. return x.videoState;
  378. });
  379. console.log("402--", video.videoCollectionState)
  380. syncPush(video,2)
  381. syncQueue()
  382. }
  383. //下载视频子集封面
  384. function getFile (video, dir) {
  385. if(dir.cover && !dir.coverState){
  386. let onurl=aiyxliburl+dir.cover;
  387. try {
  388. let dtask = plus.downloader.createDownload(
  389. onurl,
  390. { filename: targetPath },
  391. function (d, status) {
  392. // console.log(d.filename, status);
  393. // 下载完成
  394. if (status==200) {
  395. dir.cover = dir.cover.split(".")[0]+".jpg";
  396. dir.coverState = true;
  397. // console.log("Download success: " + listVideo[videoIndex].directory[curVideoDir].localCover);
  398. } else {dir.localCover ="";}
  399. }
  400. );
  401. dtask.start();
  402. } catch (error) {
  403. dir.localCover ="";
  404. console.log("Download failed: " + error);
  405. }
  406. }
  407. if(dir.url && !dir.videoState){
  408. // console.log('dlVideo', dir.url)
  409. let p1 = new Promise((resolve, reject) => {
  410. getFilevideo (video, dir,resolve)
  411. });
  412. let p2 = new Promise((resolve, reject) => {
  413. setTimeout(function () {
  414. resolve(2);
  415. }, 600000);
  416. });
  417. Promise.race([p1, p2]).then(
  418. (data) => {
  419. if (data == 2) {
  420. //超时计次,跳过
  421. that.overtime++;
  422. if (that.overtime > 2) {
  423. // 超时3次,退出更新?
  424. store.commit("falsepop", true);
  425. }else{
  426. getVideoDirectory(video)
  427. syncPushvideo(video,dir,2)
  428. }
  429. }
  430. // console.log(data);
  431. },
  432. (err) => {
  433. console.log(err);
  434. }
  435. );
  436. }else{
  437. getVideoDirectory(video)
  438. }
  439. }
  440. //下载视频子集
  441. function getFilevideo (video, dir,resolve) {
  442. console.log("下载视频子集",curVideoDir)
  443. let onurl=aiyxliburl+dir.url;
  444. try {
  445. let dtask = plus.downloader.createDownload(
  446. onurl,
  447. { filename: targetPath },
  448. function (d, status) {
  449. console.log(d.filename, status);
  450. resolve(1); //下载成功返回Promise状态
  451. // 下载完成
  452. if (status==200) {
  453. dir.localUrl = d.filename;
  454. dir.videoState = true;
  455. // console.log("Download: " ,video.directory[curVideoDir]);
  456. getVideoDirectory(video)
  457. syncPushvideo(video,dir,0)
  458. } else {
  459. dir.localUrl =""
  460. getVideoDirectory(video)
  461. syncPushvideo(video,dir,1)
  462. }
  463. }
  464. );
  465. dtask.start();
  466. } catch (error) {
  467. dir.localUrl =""
  468. getVideoDirectory(video)
  469. console.log("Download failed: " + error);
  470. }
  471. }
  472. function updateSwap() {
  473. // showLoading('同步轮播图...')
  474. uni.request({
  475. url:`${url_updateSwap}?sn=${serialNum}`,
  476. method: 'POST',
  477. success: (res) => {
  478. // console.log(res)
  479. if(res.data.errCode == 0){
  480. let {data, h5ReaderUrl} = res.data
  481. console.log(res.data)
  482. uni.setStorageSync('deviceTitle', data.title)
  483. uni.setStorageSync('h5ReaderUrl', h5ReaderUrl)
  484. curVideoDir = 0;
  485. if(data.bgUrls!=null){
  486. backPics = []
  487. getSwapPic(data.bgUrls)
  488. }else{
  489. if(currentSyncNum){
  490. store.commit("downpop", true);
  491. }else{
  492. uni.showToast({
  493. title: '已有全部最新资源~',
  494. icon:'none',
  495. duration: 3000
  496. });
  497. }
  498. }
  499. }
  500. },
  501. fail: (err) => {
  502. uni.showToast({
  503. title:'轮播图同步失败',
  504. icon:'none'
  505. })
  506. }
  507. })
  508. }
  509. function getSwapPic(pics) {
  510. if(curVideoDir < pics.length){
  511. showLoading('更新轮播图' + (curVideoDir + 1) + ' / ' + pics.length)
  512. let onurl = pics[curVideoDir]
  513. let bantPath="_doc/theme/";
  514. try {
  515. let dtask = plus.downloader.createDownload(
  516. onurl,
  517. { filename: bantPath },
  518. function (d, status) {
  519. if (status==200) {
  520. backPics.push(d.filename)
  521. curVideoDir ++
  522. getSwapPic(pics)
  523. uni.hideLoading()
  524. } else {
  525. getSwapPic(pics)
  526. console.log("Download failed: " + status);
  527. }
  528. }
  529. );
  530. dtask.start();
  531. } catch (error) {
  532. getSwapPic(pics)
  533. console.log("Download failed: " + error);
  534. }
  535. }else{
  536. // console.log('9999', backPics)
  537. uni.setStorageSync('backPics', backPics)
  538. if(currentSyncNum){
  539. store.commit("downpop", true);
  540. }else{
  541. uni.showToast({
  542. title: '轮播图已更新',
  543. icon:'none',
  544. duration: 3000
  545. });
  546. }
  547. }
  548. }
  549. //删除
  550. function delRes() {
  551. console.log('删除')
  552. if(delBook && delBook.length > 0){
  553. console.log(delBook)
  554. delResObject(LocalData.book, delBook,0)
  555. }
  556. if(delDoc && delDoc.length > 0){
  557. // console.log(delDoc)
  558. delResObject(LocalData.doc, delDoc,1)
  559. }
  560. if(delTheme && delTheme.length > 0){
  561. delResObject(LocalData.theme, delTheme,3)
  562. }
  563. if(delVr && delVr.length > 0){
  564. delResObject(LocalData.vr, delVr,4)
  565. }
  566. if(delVideo && delVideo.length > 0){
  567. delResObject(LocalData.video, delVideo,2)
  568. }
  569. }
  570. function delResObject(locallist, items,savetype){
  571. let classindex;
  572. let listindex;
  573. console.log(items)
  574. items.forEach((arr, num)=>{
  575. locallist.forEach((x, index)=>{
  576. if(x.id==arr.classId){
  577. classindex=index;
  578. x.list.forEach((y, ind, b)=>{
  579. if(y.baseId==arr.resourceId){
  580. listindex=ind;
  581. locallist[classindex].list.splice(listindex,1)
  582. // console.log(locallist)
  583. // setStorage(LocalData)
  584. syncPushdel(items,savetype)
  585. }
  586. })
  587. }
  588. })
  589. locallist[0].list.forEach((x, index)=>{
  590. if(x.baseId==arr.resourceId){
  591. locallist[0].list.splice(index,1)
  592. // debugger
  593. // console.log(index,LocalData.book)
  594. setStorage(LocalData)
  595. }
  596. })
  597. })
  598. }
  599. //同步推送删除
  600. function syncPushdel(entity,savetype) {
  601. let param = {
  602. belongVideoName: entity.name||"", //(父)资源名称
  603. coverState: entity.coverState||entity.coverCollectionState, //封面下载状况
  604. failType: 0, //失败类别 0成功 1失败 2超时
  605. fileName:entity.fileName, //封面地址
  606. ok: true, //(是否操作成功)
  607. resourceId: entity.resourceId, //资源id
  608. sn: serialNum, //设备序列号
  609. state: 2, //1.编辑 2.删除
  610. classId:entity.classId, //资源类型
  611. typeId: savetype,
  612. videoState: true,//视频是否下载(子视频是否全部下载)
  613. }
  614. // console.log(param)
  615. uni.request({
  616. url:url_confirm,
  617. data:param,
  618. method: 'POST',
  619. success: (res) => {
  620. console.log(res)
  621. if(res.data.errCode==0){
  622. setStorage(LocalData)
  623. }else{
  624. console.log(res.data.errMsg)
  625. }
  626. },
  627. fail: (err) => {
  628. console.log(err)
  629. }
  630. })
  631. }
  632. const showLoading = function(str) {
  633. uni.showLoading({
  634. title:str,
  635. mask:true
  636. })
  637. }
  638. export {
  639. Sync,
  640. delRes
  641. }