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

677 lines
16 KiB

3 years ago
  1. import { LocalData, getStorage, setStorage, setClass, serialNum, initBackPics } from '@/utils/global'
  2. //
  3. const url_sync = getApp().globalData.request_url+'/portrait-api/sync' //获取所有资源接口
  4. const url_confirm = getApp().globalData.request_url+'/portrait-api/syncConfirm' //同步结果回送
  5. const url_updateSwap = getApp().globalData.request_url+'/portrait-api/swapUpdate' //更新轮播图
  6. var currentSyncNum = 0
  7. var currentDelNum = 0
  8. var bookNum = 0
  9. var docNum = 0
  10. var videoNum = 0
  11. var themeNum = 0
  12. var vrNum = 0
  13. var listBook = []
  14. var listDoc = []
  15. var listVideo = []
  16. var listTheme = []
  17. var listVr = []
  18. //分类
  19. var bookClass = {}
  20. var docClass = {}
  21. var videoClass = {}
  22. var themeClass = {}
  23. var vrClass = {}
  24. //下载后的数组
  25. var fBook = []
  26. var fDoc = []
  27. var fVideo = []
  28. var fTheme = []
  29. var fVr = []
  30. var delResList = []
  31. var delBook = []
  32. var delDoc = []
  33. var delVideo = []
  34. var delTheme = []
  35. var delVr = []
  36. var curVideoDir = 0
  37. var backPics = []
  38. var classMap = {}
  39. //获取所有资源
  40. const Sync = {
  41. start: function(){
  42. console.log(777, serialNum)
  43. // let serialNum='VSFRNMH3'
  44. showLoading('正在加载')
  45. try{
  46. uni.request({
  47. url:url_sync,
  48. data:{sn: serialNum},
  49. success: (res) => {
  50. console.log(res)
  51. uni.hideLoading()
  52. let {errCode, data} = res.data
  53. if(errCode == 0){
  54. currentSyncNum = data.currentSyncNum
  55. currentDelNum = data.currentDelNum
  56. listBook = data.listBook
  57. bookNum = listBook.length
  58. listDoc = data.listDoc
  59. docNum = listDoc.length
  60. listVideo = data.listVideo
  61. videoNum = listVideo.length
  62. listTheme = data.listTheme
  63. themeNum = listTheme.length
  64. listVr = data.listVr
  65. vrNum = listVr.length
  66. delBook = data.delEbook
  67. delDoc = data.delDoc
  68. delTheme = data.delTheme
  69. delVr = data.delVr
  70. delVideo = data.delVideo
  71. classMap = data.classOrderMap
  72. fBook = []
  73. fDoc = []
  74. fVideo = []
  75. fTheme = []
  76. fVr = []
  77. // console.log('2222', currentSyncNum, currentDelNum, JSON.stringify(classMap))
  78. setClassMap()
  79. }
  80. },
  81. fail: (e) => {
  82. console.log('fail: ', e)
  83. uni.hideLoading()
  84. uni.showToast({
  85. title:"更新失败"
  86. })
  87. queueFail()
  88. }
  89. })
  90. }catch(e){
  91. uni.showToast({
  92. title:"更新失败" + e.error,
  93. icon: none
  94. })
  95. console.log(e)
  96. queueFail()
  97. }
  98. // plus.device.beep(1)
  99. }
  100. }
  101. //得到列表分类
  102. function setClassMap(){
  103. bookClass = classMap[0] ? classMap[0] : [];
  104. docClass = classMap[1] ? classMap[1] : [];
  105. videoClass = classMap[2] ? classMap[2] : [];
  106. themeClass = classMap[3] ? classMap[3] : [];
  107. vrClass = classMap[4] ? classMap[4] : [];
  108. // setClass(bookClass, docClass, videoClass, themeClass, vrClass)
  109. // console.log('LocalData--',LocalData)
  110. //正式存储
  111. syncQueue()
  112. //测试存储
  113. // syncQueueDone()
  114. }
  115. function syncQueue() {
  116. // console.log(listBook.length, listDoc.length)
  117. if(listBook.length > 0){
  118. showLoading('获取图书:' + (bookNum - listBook.length + 1) + ' / ' + bookNum)
  119. // getResource(listBook, fBook)
  120. getResource(listBook, 1)
  121. }else if(listDoc.length > 0){
  122. showLoading('获取阅文:' + (docNum - listDoc.length + 1) + ' / ' + docNum)
  123. // getResource(listDoc, fDoc)
  124. getResource(listDoc, 2)
  125. }else if(listTheme.length > 0){
  126. showLoading('获取阅刊: ' + (themeNum - listTheme.length + 1) + ' / ' + themeNum)
  127. // getResource(listTheme, fTheme)
  128. getResource(listTheme, 3)
  129. }else if(listVr.length > 0){
  130. showLoading('获取VR: ' + (vrNum - listVr.length + 1) + ' / ' + vrNum)
  131. // getResource(listVr, fVr)
  132. getResource(listVr, 4)
  133. }else if(listVideo.length > 0){
  134. showLoading('获取视频:' + (videoNum - listVideo.length + 1) + ' / ' + videoNum)
  135. // getResource(listVideo, fVideo)
  136. getResource(listVideo, 5)
  137. }
  138. else{
  139. uni.hideLoading()
  140. // delRes()
  141. //更新轮播图
  142. updateSwap()
  143. }
  144. }
  145. //下载封面到本地
  146. function getResource(resList, savetype) {
  147. let entity = resList.shift()
  148. if(entity && entity.cover){
  149. uni.downloadFile({
  150. url:encodeURI(entity.cover),
  151. header: {'content-type':'application/x-www-form-urlencoded',
  152. 'changeOrigin':true},
  153. success: (res) => {
  154. if(res && (res.statusCode == 200)){
  155. uni.saveFile({
  156. tempFilePath:res.tempFilePath,
  157. success: (saveRes) => {
  158. entity.localCover = saveRes.savedFilePath
  159. if(resList == listVideo){
  160. curVideoDir = 0
  161. getVideoDirectory(entity)
  162. }else{
  163. syncPush(entity,savetype)
  164. // console.log(entity, savetype)
  165. // switch(savetype){
  166. // case 1:
  167. // LocalData.book=categoryFiles(LocalData.book, entity, bookClass)
  168. // console.log('存进分类书列表:', LocalData.book)
  169. // break;
  170. // case 2:
  171. // LocalData.doc=categoryFiles(LocalData.doc, entity, docClass)
  172. // break;
  173. // case 3:
  174. // LocalData.theme=categoryFiles(LocalData.theme, entity, themeClass)
  175. // break;
  176. // case 4:
  177. // LocalData.vr=categoryFiles(LocalData.vr, entity, vrClass)
  178. // break;
  179. // case 5:
  180. // LocalData.video=categoryFiles(LocalData.video, entity, videoClass)
  181. // break;
  182. // }
  183. // setStorage(LocalData)
  184. // console.log(LocalData)
  185. syncQueue()
  186. }
  187. },
  188. fail: (e) => {
  189. console.log('saveFile err: ', e)
  190. }
  191. })
  192. }else{
  193. uni.hideLoading()
  194. syncQueue()
  195. }
  196. },
  197. fail: (e) => {
  198. uni.hideLoading()
  199. uni.showToast({
  200. title: 'err: ', e,
  201. duration: 2000,
  202. complete: () => {
  203. syncQueue()
  204. }
  205. })
  206. },
  207. })
  208. }else{
  209. syncQueue()
  210. }
  211. }
  212. //同步推送下载
  213. function syncPush(entity,savetype) {
  214. let syncedIds = [];
  215. if(entity.baseId){
  216. syncedIds.push({
  217. resourceId:entity.baseId,
  218. type:entity.resourceType})
  219. }
  220. let param = {
  221. sn: serialNum,
  222. syncedIds: syncedIds,
  223. delIds: []
  224. }
  225. syncRequest(param,entity,savetype)
  226. }
  227. //同步推送删除
  228. function syncPushdel(entity) {
  229. let delIds = [];
  230. if(entity.baseId){
  231. delIds.push(entity.baseId)
  232. }
  233. let param = {
  234. sn: serialNum,
  235. syncedIds: [],
  236. delIds: delIds
  237. }
  238. syncRequest(param,entity)
  239. }
  240. //同步接口请求
  241. function syncRequest(param,entity,savetype) {
  242. uni.request({
  243. url:url_confirm,
  244. data:param,
  245. method: 'POST',
  246. success: (res) => {
  247. // saveList.push(entity)
  248. if(res.data.errCode==0){
  249. switch(savetype){
  250. case 1:
  251. LocalData.book=categoryFiles(LocalData.book, entity, bookClass)
  252. // console.log('存进分类书列表:', LocalData.book)
  253. break;
  254. case 2:
  255. LocalData.doc=categoryFiles(LocalData.doc, entity, docClass)
  256. break;
  257. case 3:
  258. LocalData.theme=categoryFiles(LocalData.theme, entity, themeClass)
  259. break;
  260. case 4:
  261. LocalData.vr=categoryFiles(LocalData.vr, entity, vrClass)
  262. break;
  263. case 5:
  264. LocalData.video=categoryFiles(LocalData.video, entity, videoClass)
  265. break;
  266. }
  267. setStorage(LocalData)
  268. // console.log(savetype, fBook)
  269. console.log('向后台返回成功',res)
  270. }else{
  271. console.log(res.data.errMsg)
  272. }
  273. },
  274. fail: (err) => {
  275. uni.hideLoading()
  276. uni.showToast({
  277. title: '推送失败: ' + err,
  278. icon:none,
  279. duration: 2000
  280. })
  281. }
  282. })
  283. }
  284. //存进分类列表
  285. function categoryFiles(locData, curData, classArray) {
  286. // console.log(curData)
  287. if(locData.length < 1||locData==null){
  288. locData.push({id:0, name: '全部', order: -1, list:[]})
  289. classArray.forEach((classItem) => {
  290. classItem.list=[];
  291. locData.push(classItem)
  292. })
  293. console.log(locData)
  294. categoryFiles(locData, curData, classArray)
  295. }else{
  296. locData.forEach((classItem) => {
  297. if(curData.classId==classItem.id){
  298. classItem.list.push(curData)
  299. // console.log(classItem.list)
  300. }
  301. if(classItem.id==0){
  302. classItem.list.push(curData)
  303. // console.log(classItem)
  304. }
  305. })
  306. }
  307. locData = locData.sort((a,b) => {
  308. return a.order < b.order
  309. })
  310. // console.log('存进分类列表:', locData)
  311. return locData
  312. }
  313. //存进分类列表
  314. function cunfenlei(locData, curData, classArray) {
  315. console.log(locData,curData)
  316. if(locData.length < 1||locData==null){
  317. locData.push({id:0, name: '全部', order: -1, list:curData})
  318. classArray.forEach((classItem) => {
  319. let fil = locData.filter(item => item.id == classItem.id)
  320. // console.log(912, JSON.stringify(fil))
  321. let curAddResList = curData.filter(item => classItem.id == item.classId)
  322. // console.log('push class:', classItem.name, curAddResList)
  323. classItem.list = curAddResList
  324. locData.push(classItem)
  325. })
  326. }else{
  327. locData.forEach((classItem) => {
  328. let curAddResList = curData.filter(item => classItem.id == item.classId)
  329. classItem.list=classItem.list.concat(curAddResList)
  330. // console.log(curAddResList,classItem.list)
  331. console.log(classItem.list)
  332. if(classItem.id==0){
  333. classItem.list=classItem.list.concat(curData)
  334. console.log(classItem.id)
  335. }
  336. })
  337. }
  338. locData = locData.sort((a,b) => {
  339. return a.order < b.order
  340. })
  341. console.log('存进分类列表:', locData)
  342. return locData
  343. }
  344. function syncQueueDone() {
  345. // showLoading("同步推送中...")
  346. // let tb = categoryFiles(LocalData.book, listBook, bookClass)
  347. // let td = categoryFiles(LocalData.doc, listDoc, docClass)
  348. // let tv = categoryFiles(LocalData.video, listVideo, videoClass)
  349. // let tt = categoryFiles(LocalData.theme, listTheme, themeClass)
  350. // let tvr = categoryFiles(LocalData.vr, listVr, vrClass)
  351. let tb = categoryFiles(LocalData.book, fBook, bookClass)
  352. let td = categoryFiles(LocalData.doc, fDoc, docClass)
  353. let tv = categoryFiles(LocalData.video, fVideo, videoClass)
  354. let tt = categoryFiles(LocalData.theme, fTheme, themeClass)
  355. let tvr = categoryFiles(LocalData.vr, fVr, vrClass)
  356. setStorage(tb, td, tv, tt, tvr)
  357. console.log(tb)
  358. uni.hideLoading()
  359. uni.showModal({
  360. title:'更新已完成!',
  361. content:'回到首页',
  362. showCancel:false,
  363. success: function (res) {
  364. if (res.confirm) {
  365. console.log('用户点击确定');
  366. uni.navigateTo({
  367. url:'../index/index?downenter=true'
  368. })
  369. } else if (res.cancel) {
  370. console.log('用户点击取消');
  371. }
  372. }
  373. })
  374. }
  375. //删除
  376. function delRes() {
  377. if(delBook && delBook.length > 0){
  378. delCoverFile(LocalData.book, delBook)
  379. delResObject(LocalData.book, delBook)
  380. }
  381. if(delDoc && delDoc.length > 0){
  382. delCoverFile(LocalData.doc, delDoc)
  383. delResObject(LocalData.doc, delDoc)
  384. }
  385. if(delTheme && delTheme.length > 0){
  386. delCoverFile(LocalData.theme, delTheme)
  387. delResObject(LocalData.theme, delTheme)
  388. }
  389. if(delVr && delVr.length > 0){
  390. delCoverFile(LocalData.vr, delVr)
  391. delResObject(LocalData.vr, delVr)
  392. }
  393. if(delVideo && delVideo.length > 0){
  394. delCoverFile(LocalData.video, delVideo, true)
  395. delResObject(LocalData.video, delVideo)
  396. }
  397. }
  398. function delCoverFile(biglist, itemIds, isVid){
  399. let list = biglist[0]
  400. if(list && list.length > 0){
  401. let fl = list.filters(item => itemIds.indexOf(item.baseId) > -1)
  402. for(let i = 0; i < fl.length; i ++){
  403. let item = fl[i]
  404. uni.removeSavedFile({
  405. filePath: item.localCover,
  406. complete: (ret) => {
  407. console.log('删除封面', item.localCover, ret)
  408. let idx = list.indexOf(item)
  409. if(idx > -1){
  410. list.splice(item, 1)
  411. }
  412. }
  413. })
  414. if(isVid){
  415. let dir = item.directory
  416. if(dir && dir.length){
  417. for(let j = 0; j < dir.length; j ++){
  418. let sub = dir[j]
  419. uni.removeSavedFile({
  420. filePath: sub.localCover,
  421. complete: (ret) => {
  422. console.log('删除视频集封面', sub.localCover, ret)
  423. uni.removeSavedFile({
  424. filePath: sub.localUrl,
  425. complete: (ret) => { console.log('删除视频文件', sub.localUrl, ret) }
  426. })
  427. }
  428. })
  429. }
  430. }
  431. }
  432. }
  433. }
  434. }
  435. function delResObject(list, itemIds){
  436. for(let i = 1; i < list.length; i ++){
  437. let tmp = list[i].list;
  438. for(let j = 0; j < tmp.length; j++){
  439. if(delBook.indexOf(tmp[j].baseId) > -1){
  440. tmp.splice(tmp[j], 1)
  441. j --
  442. }
  443. }
  444. }
  445. }
  446. function getVideoDirectory (video) {
  447. let {directory} = video
  448. // console.log(875, video)
  449. if(curVideoDir < directory.length){
  450. getFile(video, directory[curVideoDir])
  451. return
  452. }
  453. syncPush(video,fVideo)
  454. // fVideo.push(video)
  455. uni.hideLoading()
  456. syncQueue()
  457. }
  458. function getFile (video, dir) {
  459. curVideoDir += 1
  460. if(dir){
  461. if(dir.cover){
  462. uni.downloadFile({
  463. url:encodeURI(dir.cover),
  464. header: {'content-type':'application/x-www-form-urlencoded',
  465. 'changeOrigin':true},
  466. success: (res) => {
  467. if(res && (res.statusCode == 200)){
  468. uni.saveFile({
  469. tempFilePath:res.tempFilePath,
  470. success: (saveRes) => {
  471. dir.localCover = saveRes.savedFilePath
  472. }
  473. })
  474. }
  475. }
  476. })
  477. }
  478. if(dir.url){
  479. console.log('dlVideo', dir.url)
  480. uni.downloadFile({
  481. url:encodeURI(dir.url),
  482. header: {'content-type':'application/x-www-form-urlencoded',
  483. 'changeOrigin':true},
  484. success: (res) => {
  485. if(res && (res.statusCode == 200)){
  486. // console.log(JSON.stringify(res))
  487. uni.saveFile({
  488. tempFilePath:res.tempFilePath,
  489. success: (saveRes) => {
  490. dir.localUrl = saveRes.savedFilePath
  491. getVideoDirectory(video)
  492. },
  493. fail: (e) => {
  494. console.log('saveFile err: ', e)
  495. getVideoDirectory(video)
  496. }
  497. })
  498. }else{
  499. getVideoDirectory(video)
  500. }
  501. },
  502. fail: (e) => {
  503. uni.showToast({
  504. title: 'err: ', e,
  505. duration: 2000,
  506. complete: () => {
  507. getVideoDirectory(video)
  508. }
  509. })
  510. },
  511. })
  512. }else{
  513. getVideoDirectory(video)
  514. }
  515. }else{
  516. getVideoDirectory(video)
  517. }
  518. }
  519. function updateSwap() {
  520. // showLoading('同步轮播图...')
  521. uni.request({
  522. url: url_updateSwap,
  523. data: {sn: serialNum},
  524. success: (res) => {
  525. if(res.data.errCode == 0){
  526. let {data, h5ReaderUrl} = res.data
  527. console.log(res.data)
  528. uni.setStorageSync('deviceTitle', data.title)
  529. uni.setStorageSync('h5ReaderUrl', h5ReaderUrl)
  530. curVideoDir = 0
  531. backPics = []
  532. getSwapPic(data.bgUrls)
  533. }
  534. },
  535. fail: (err) => {
  536. uni.showToast({
  537. title:'轮播图同步失败',
  538. icon:'none'
  539. })
  540. // syncQueueDone()
  541. }
  542. })
  543. }
  544. function getSwapPic(pics) {
  545. if(curVideoDir < pics.length){
  546. showLoading('更新轮播图' + (curVideoDir + 1) + ' / ' + pics.length)
  547. let url = pics[curVideoDir]
  548. uni.downloadFile({
  549. url:encodeURI(url),
  550. header: {'content-type':'application/x-www-form-urlencoded',
  551. 'changeOrigin':true},
  552. success: (res) => {
  553. if(res && (res.statusCode == 200)){
  554. uni.saveFile({
  555. tempFilePath:res.tempFilePath,
  556. success: (saveRes) => {
  557. backPics.push(saveRes.savedFilePath)
  558. curVideoDir += 1
  559. getSwapPic(pics)
  560. uni.hideLoading()
  561. uni.showModal({
  562. title:'更新已完成!',
  563. content:'回到首页',
  564. showCancel:false,
  565. success: function (res) {
  566. if (res.confirm) {
  567. console.log('用户点击确定');
  568. uni.navigateTo({
  569. url:'../index/index?downenter=true'
  570. })
  571. } else if (res.cancel) {
  572. console.log('用户点击取消');
  573. }
  574. }
  575. })
  576. },
  577. fail: (e) => {
  578. console.log('saveFile err: ', e)
  579. showLoading('轮播图' + (curVideoDir + 1) + ' 获取失败')
  580. curVideoDir += 1
  581. getSwapPic(pics)
  582. },
  583. })
  584. }else{
  585. showLoading('轮播图' + (curVideoDir + 1) + ' 获取失败')
  586. curVideoDir += 1
  587. getSwapPic(pics)
  588. }
  589. },
  590. fail: (err) => {
  591. showLoading('轮播图' + (curVideoDir + 1) + ' 获取失败')
  592. curVideoDir += 1
  593. getSwapPic(pics)
  594. }
  595. })
  596. }else{
  597. // console.log('9999', backPics)
  598. uni.setStorageSync('backPics', backPics)
  599. initBackPics()
  600. // syncQueueDone()
  601. uni.showModal({
  602. title:'更新已完成!',
  603. content:'回到首页',
  604. showCancel:false,
  605. success: function (res) {
  606. if (res.confirm) {
  607. console.log('用户点击确定');
  608. uni.navigateTo({
  609. url:'../index/index?downenter=true'
  610. })
  611. } else if (res.cancel) {
  612. console.log('用户点击取消');
  613. }
  614. }
  615. })
  616. }
  617. }
  618. const queueFail = function () {
  619. let list = fBook.concat(fDoc).concat(fVideo).concat(fTheme).concat(fVr)
  620. list.forEach((res) => {
  621. if(res.localCover)
  622. uni.removeSavedFile({
  623. filePath:res.localCover
  624. })
  625. if(res.directory){
  626. res.directory.forEach((sub) => {
  627. if(sub.localCover){
  628. uni.removeSavedFile({
  629. filePath:sub.localCover
  630. })
  631. }
  632. if(sub.localUrl){
  633. uni.removeSavedFile({
  634. filePath:sub.localUrl
  635. })
  636. }
  637. })
  638. }
  639. })
  640. }
  641. const showLoading = function(str) {
  642. uni.showLoading({
  643. title:str,
  644. mask:true
  645. })
  646. }
  647. export {
  648. Sync,
  649. queueFail
  650. }