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.
678 lines
16 KiB
678 lines
16 KiB
|
|
import { LocalData, getStorage, setStorage, setClass, serialNum, initBackPics } from '@/utils/global'
|
|
//
|
|
const url_sync = getApp().globalData.request_url+'/portrait-api/sync' //获取所有资源接口
|
|
const url_confirm = getApp().globalData.request_url+'/portrait-api/syncConfirm' //同步结果回送
|
|
const url_updateSwap = getApp().globalData.request_url+'/portrait-api/swapUpdate' //更新轮播图
|
|
var currentSyncNum = 0
|
|
var currentDelNum = 0
|
|
var bookNum = 0
|
|
var docNum = 0
|
|
var videoNum = 0
|
|
var themeNum = 0
|
|
var vrNum = 0
|
|
var listBook = []
|
|
var listDoc = []
|
|
var listVideo = []
|
|
var listTheme = []
|
|
var listVr = []
|
|
//分类
|
|
var bookClass = {}
|
|
var docClass = {}
|
|
var videoClass = {}
|
|
var themeClass = {}
|
|
var vrClass = {}
|
|
//下载后的数组
|
|
var fBook = []
|
|
var fDoc = []
|
|
var fVideo = []
|
|
var fTheme = []
|
|
var fVr = []
|
|
|
|
var delResList = []
|
|
var delBook = []
|
|
var delDoc = []
|
|
var delVideo = []
|
|
var delTheme = []
|
|
var delVr = []
|
|
var curVideoDir = 0
|
|
var backPics = []
|
|
var classMap = {}
|
|
|
|
//获取所有资源
|
|
const Sync = {
|
|
start: function(){
|
|
console.log(777, serialNum)
|
|
// let serialNum='VSFRNMH3'
|
|
showLoading('正在加载')
|
|
try{
|
|
uni.request({
|
|
url:url_sync,
|
|
data:{sn: serialNum},
|
|
success: (res) => {
|
|
console.log(res)
|
|
uni.hideLoading()
|
|
let {errCode, data} = res.data
|
|
if(errCode == 0){
|
|
currentSyncNum = data.currentSyncNum
|
|
currentDelNum = data.currentDelNum
|
|
listBook = data.listBook
|
|
bookNum = listBook.length
|
|
listDoc = data.listDoc
|
|
docNum = listDoc.length
|
|
listVideo = data.listVideo
|
|
videoNum = listVideo.length
|
|
listTheme = data.listTheme
|
|
themeNum = listTheme.length
|
|
listVr = data.listVr
|
|
vrNum = listVr.length
|
|
|
|
delBook = data.delEbook
|
|
delDoc = data.delDoc
|
|
delTheme = data.delTheme
|
|
delVr = data.delVr
|
|
delVideo = data.delVideo
|
|
|
|
classMap = data.classOrderMap
|
|
|
|
fBook = []
|
|
fDoc = []
|
|
fVideo = []
|
|
fTheme = []
|
|
fVr = []
|
|
|
|
// console.log('2222', currentSyncNum, currentDelNum, JSON.stringify(classMap))
|
|
setClassMap()
|
|
}
|
|
},
|
|
fail: (e) => {
|
|
console.log('fail: ', e)
|
|
uni.hideLoading()
|
|
uni.showToast({
|
|
title:"更新失败"
|
|
})
|
|
queueFail()
|
|
}
|
|
})
|
|
}catch(e){
|
|
uni.showToast({
|
|
title:"更新失败" + e.error,
|
|
icon: none
|
|
})
|
|
console.log(e)
|
|
queueFail()
|
|
}
|
|
// plus.device.beep(1)
|
|
}
|
|
}
|
|
//得到列表分类
|
|
function setClassMap(){
|
|
bookClass = classMap[0] ? classMap[0] : [];
|
|
docClass = classMap[1] ? classMap[1] : [];
|
|
videoClass = classMap[2] ? classMap[2] : [];
|
|
themeClass = classMap[3] ? classMap[3] : [];
|
|
vrClass = classMap[4] ? classMap[4] : [];
|
|
|
|
// setClass(bookClass, docClass, videoClass, themeClass, vrClass)
|
|
// console.log('LocalData--',LocalData)
|
|
//正式存储
|
|
syncQueue()
|
|
//测试存储
|
|
// syncQueueDone()
|
|
}
|
|
function syncQueue() {
|
|
// console.log(listBook.length, listDoc.length)
|
|
if(listBook.length > 0){
|
|
showLoading('获取图书:' + (bookNum - listBook.length + 1) + ' / ' + bookNum)
|
|
// getResource(listBook, fBook)
|
|
getResource(listBook, 1)
|
|
}else if(listDoc.length > 0){
|
|
showLoading('获取阅文:' + (docNum - listDoc.length + 1) + ' / ' + docNum)
|
|
// getResource(listDoc, fDoc)
|
|
getResource(listDoc, 2)
|
|
}else if(listTheme.length > 0){
|
|
showLoading('获取阅刊: ' + (themeNum - listTheme.length + 1) + ' / ' + themeNum)
|
|
// getResource(listTheme, fTheme)
|
|
getResource(listTheme, 3)
|
|
}else if(listVr.length > 0){
|
|
showLoading('获取VR: ' + (vrNum - listVr.length + 1) + ' / ' + vrNum)
|
|
// getResource(listVr, fVr)
|
|
getResource(listVr, 4)
|
|
}else if(listVideo.length > 0){
|
|
showLoading('获取视频:' + (videoNum - listVideo.length + 1) + ' / ' + videoNum)
|
|
// getResource(listVideo, fVideo)
|
|
getResource(listVideo, 5)
|
|
}
|
|
else{
|
|
uni.hideLoading()
|
|
// delRes()
|
|
//更新轮播图
|
|
updateSwap()
|
|
}
|
|
}
|
|
//下载封面到本地
|
|
function getResource(resList, savetype) {
|
|
let entity = resList.shift()
|
|
if(entity && entity.cover){
|
|
uni.downloadFile({
|
|
url:encodeURI(entity.cover),
|
|
header: {'content-type':'application/x-www-form-urlencoded',
|
|
'changeOrigin':true},
|
|
success: (res) => {
|
|
if(res && (res.statusCode == 200)){
|
|
uni.saveFile({
|
|
tempFilePath:res.tempFilePath,
|
|
success: (saveRes) => {
|
|
entity.localCover = saveRes.savedFilePath
|
|
if(resList == listVideo){
|
|
curVideoDir = 0
|
|
getVideoDirectory(entity)
|
|
}else{
|
|
syncPush(entity,savetype)
|
|
// console.log(entity, savetype)
|
|
// switch(savetype){
|
|
// case 1:
|
|
// LocalData.book=categoryFiles(LocalData.book, entity, bookClass)
|
|
// console.log('存进分类书列表:', LocalData.book)
|
|
// break;
|
|
// case 2:
|
|
// LocalData.doc=categoryFiles(LocalData.doc, entity, docClass)
|
|
// break;
|
|
// case 3:
|
|
// LocalData.theme=categoryFiles(LocalData.theme, entity, themeClass)
|
|
// break;
|
|
// case 4:
|
|
// LocalData.vr=categoryFiles(LocalData.vr, entity, vrClass)
|
|
// break;
|
|
// case 5:
|
|
// LocalData.video=categoryFiles(LocalData.video, entity, videoClass)
|
|
// break;
|
|
// }
|
|
// setStorage(LocalData)
|
|
// console.log(LocalData)
|
|
syncQueue()
|
|
}
|
|
},
|
|
fail: (e) => {
|
|
console.log('saveFile err: ', e)
|
|
}
|
|
})
|
|
}else{
|
|
uni.hideLoading()
|
|
syncQueue()
|
|
}
|
|
},
|
|
fail: (e) => {
|
|
uni.hideLoading()
|
|
uni.showToast({
|
|
title: 'err: ', e,
|
|
duration: 2000,
|
|
complete: () => {
|
|
syncQueue()
|
|
}
|
|
})
|
|
},
|
|
})
|
|
}else{
|
|
syncQueue()
|
|
}
|
|
}
|
|
//同步推送下载
|
|
function syncPush(entity,savetype) {
|
|
let syncedIds = [];
|
|
if(entity.baseId){
|
|
syncedIds.push({
|
|
resourceId:entity.baseId,
|
|
type:entity.resourceType})
|
|
}
|
|
let param = {
|
|
sn: serialNum,
|
|
syncedIds: syncedIds,
|
|
delIds: []
|
|
}
|
|
syncRequest(param,entity,savetype)
|
|
}
|
|
//同步推送删除
|
|
function syncPushdel(entity) {
|
|
let delIds = [];
|
|
if(entity.baseId){
|
|
delIds.push(entity.baseId)
|
|
}
|
|
let param = {
|
|
sn: serialNum,
|
|
syncedIds: [],
|
|
delIds: delIds
|
|
}
|
|
syncRequest(param,entity)
|
|
}
|
|
//同步接口请求
|
|
function syncRequest(param,entity,savetype) {
|
|
|
|
uni.request({
|
|
url:url_confirm,
|
|
data:param,
|
|
method: 'POST',
|
|
success: (res) => {
|
|
// saveList.push(entity)
|
|
if(res.data.errCode==0){
|
|
switch(savetype){
|
|
case 1:
|
|
LocalData.book=categoryFiles(LocalData.book, entity, bookClass)
|
|
// console.log('存进分类书列表:', LocalData.book)
|
|
break;
|
|
case 2:
|
|
LocalData.doc=categoryFiles(LocalData.doc, entity, docClass)
|
|
break;
|
|
case 3:
|
|
LocalData.theme=categoryFiles(LocalData.theme, entity, themeClass)
|
|
break;
|
|
case 4:
|
|
LocalData.vr=categoryFiles(LocalData.vr, entity, vrClass)
|
|
break;
|
|
case 5:
|
|
LocalData.video=categoryFiles(LocalData.video, entity, videoClass)
|
|
break;
|
|
}
|
|
setStorage(LocalData)
|
|
// console.log(savetype, fBook)
|
|
console.log('向后台返回成功',res)
|
|
}else{
|
|
console.log(res.data.errMsg)
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
uni.hideLoading()
|
|
uni.showToast({
|
|
title: '推送失败: ' + err,
|
|
icon:none,
|
|
duration: 2000
|
|
})
|
|
}
|
|
})
|
|
}
|
|
//存进分类列表
|
|
function categoryFiles(locData, curData, classArray) {
|
|
// console.log(curData)
|
|
if(locData.length < 1||locData==null){
|
|
locData.push({id:0, name: '全部', order: -1, list:[]})
|
|
classArray.forEach((classItem) => {
|
|
classItem.list=[];
|
|
locData.push(classItem)
|
|
})
|
|
console.log(locData)
|
|
categoryFiles(locData, curData, classArray)
|
|
}else{
|
|
locData.forEach((classItem) => {
|
|
if(curData.classId==classItem.id){
|
|
classItem.list.push(curData)
|
|
// console.log(classItem.list)
|
|
}
|
|
|
|
if(classItem.id==0){
|
|
classItem.list.push(curData)
|
|
// console.log(classItem)
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
locData = locData.sort((a,b) => {
|
|
return a.order < b.order
|
|
})
|
|
// console.log('存进分类列表:', locData)
|
|
return locData
|
|
}
|
|
//存进分类列表
|
|
function cunfenlei(locData, curData, classArray) {
|
|
console.log(locData,curData)
|
|
if(locData.length < 1||locData==null){
|
|
locData.push({id:0, name: '全部', order: -1, list:curData})
|
|
classArray.forEach((classItem) => {
|
|
let fil = locData.filter(item => item.id == classItem.id)
|
|
// console.log(912, JSON.stringify(fil))
|
|
let curAddResList = curData.filter(item => classItem.id == item.classId)
|
|
// console.log('push class:', classItem.name, curAddResList)
|
|
classItem.list = curAddResList
|
|
locData.push(classItem)
|
|
})
|
|
}else{
|
|
locData.forEach((classItem) => {
|
|
let curAddResList = curData.filter(item => classItem.id == item.classId)
|
|
classItem.list=classItem.list.concat(curAddResList)
|
|
// console.log(curAddResList,classItem.list)
|
|
console.log(classItem.list)
|
|
|
|
if(classItem.id==0){
|
|
classItem.list=classItem.list.concat(curData)
|
|
console.log(classItem.id)
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
locData = locData.sort((a,b) => {
|
|
return a.order < b.order
|
|
})
|
|
console.log('存进分类列表:', locData)
|
|
return locData
|
|
}
|
|
function syncQueueDone() {
|
|
// showLoading("同步推送中...")
|
|
// let tb = categoryFiles(LocalData.book, listBook, bookClass)
|
|
// let td = categoryFiles(LocalData.doc, listDoc, docClass)
|
|
// let tv = categoryFiles(LocalData.video, listVideo, videoClass)
|
|
// let tt = categoryFiles(LocalData.theme, listTheme, themeClass)
|
|
// let tvr = categoryFiles(LocalData.vr, listVr, vrClass)
|
|
|
|
let tb = categoryFiles(LocalData.book, fBook, bookClass)
|
|
let td = categoryFiles(LocalData.doc, fDoc, docClass)
|
|
let tv = categoryFiles(LocalData.video, fVideo, videoClass)
|
|
let tt = categoryFiles(LocalData.theme, fTheme, themeClass)
|
|
let tvr = categoryFiles(LocalData.vr, fVr, vrClass)
|
|
|
|
setStorage(tb, td, tv, tt, tvr)
|
|
console.log(tb)
|
|
|
|
uni.hideLoading()
|
|
uni.showModal({
|
|
title:'更新已完成!',
|
|
content:'回到首页',
|
|
showCancel:false,
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
console.log('用户点击确定');
|
|
uni.navigateTo({
|
|
url:'../index/index?downenter=true'
|
|
})
|
|
} else if (res.cancel) {
|
|
console.log('用户点击取消');
|
|
}
|
|
}
|
|
})
|
|
}
|
|
//删除
|
|
function delRes() {
|
|
if(delBook && delBook.length > 0){
|
|
delCoverFile(LocalData.book, delBook)
|
|
delResObject(LocalData.book, delBook)
|
|
}
|
|
if(delDoc && delDoc.length > 0){
|
|
delCoverFile(LocalData.doc, delDoc)
|
|
delResObject(LocalData.doc, delDoc)
|
|
}
|
|
if(delTheme && delTheme.length > 0){
|
|
delCoverFile(LocalData.theme, delTheme)
|
|
delResObject(LocalData.theme, delTheme)
|
|
}
|
|
if(delVr && delVr.length > 0){
|
|
delCoverFile(LocalData.vr, delVr)
|
|
delResObject(LocalData.vr, delVr)
|
|
}
|
|
if(delVideo && delVideo.length > 0){
|
|
delCoverFile(LocalData.video, delVideo, true)
|
|
delResObject(LocalData.video, delVideo)
|
|
}
|
|
}
|
|
function delCoverFile(biglist, itemIds, isVid){
|
|
let list = biglist[0]
|
|
if(list && list.length > 0){
|
|
let fl = list.filters(item => itemIds.indexOf(item.baseId) > -1)
|
|
for(let i = 0; i < fl.length; i ++){
|
|
let item = fl[i]
|
|
uni.removeSavedFile({
|
|
filePath: item.localCover,
|
|
complete: (ret) => {
|
|
console.log('删除封面', item.localCover, ret)
|
|
let idx = list.indexOf(item)
|
|
if(idx > -1){
|
|
list.splice(item, 1)
|
|
}
|
|
}
|
|
})
|
|
if(isVid){
|
|
let dir = item.directory
|
|
if(dir && dir.length){
|
|
for(let j = 0; j < dir.length; j ++){
|
|
let sub = dir[j]
|
|
uni.removeSavedFile({
|
|
filePath: sub.localCover,
|
|
complete: (ret) => {
|
|
console.log('删除视频集封面', sub.localCover, ret)
|
|
uni.removeSavedFile({
|
|
filePath: sub.localUrl,
|
|
complete: (ret) => { console.log('删除视频文件', sub.localUrl, ret) }
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
function delResObject(list, itemIds){
|
|
for(let i = 1; i < list.length; i ++){
|
|
let tmp = list[i].list;
|
|
for(let j = 0; j < tmp.length; j++){
|
|
if(delBook.indexOf(tmp[j].baseId) > -1){
|
|
tmp.splice(tmp[j], 1)
|
|
j --
|
|
}
|
|
}
|
|
}
|
|
}
|
|
function getVideoDirectory (video) {
|
|
let {directory} = video
|
|
// console.log(875, video)
|
|
if(curVideoDir < directory.length){
|
|
getFile(video, directory[curVideoDir])
|
|
return
|
|
}
|
|
syncPush(video,fVideo)
|
|
// fVideo.push(video)
|
|
uni.hideLoading()
|
|
syncQueue()
|
|
}
|
|
function getFile (video, dir) {
|
|
curVideoDir += 1
|
|
if(dir){
|
|
if(dir.cover){
|
|
uni.downloadFile({
|
|
url:encodeURI(dir.cover),
|
|
header: {'content-type':'application/x-www-form-urlencoded',
|
|
'changeOrigin':true},
|
|
success: (res) => {
|
|
if(res && (res.statusCode == 200)){
|
|
uni.saveFile({
|
|
tempFilePath:res.tempFilePath,
|
|
success: (saveRes) => {
|
|
dir.localCover = saveRes.savedFilePath
|
|
|
|
}
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
if(dir.url){
|
|
console.log('dlVideo', dir.url)
|
|
uni.downloadFile({
|
|
url:encodeURI(dir.url),
|
|
header: {'content-type':'application/x-www-form-urlencoded',
|
|
'changeOrigin':true},
|
|
success: (res) => {
|
|
if(res && (res.statusCode == 200)){
|
|
// console.log(JSON.stringify(res))
|
|
uni.saveFile({
|
|
tempFilePath:res.tempFilePath,
|
|
success: (saveRes) => {
|
|
dir.localUrl = saveRes.savedFilePath
|
|
getVideoDirectory(video)
|
|
},
|
|
fail: (e) => {
|
|
console.log('saveFile err: ', e)
|
|
getVideoDirectory(video)
|
|
}
|
|
})
|
|
}else{
|
|
getVideoDirectory(video)
|
|
}
|
|
},
|
|
fail: (e) => {
|
|
uni.showToast({
|
|
title: 'err: ', e,
|
|
duration: 2000,
|
|
complete: () => {
|
|
getVideoDirectory(video)
|
|
}
|
|
})
|
|
},
|
|
})
|
|
}else{
|
|
getVideoDirectory(video)
|
|
}
|
|
}else{
|
|
getVideoDirectory(video)
|
|
}
|
|
}
|
|
function updateSwap() {
|
|
// showLoading('同步轮播图...')
|
|
uni.request({
|
|
url: url_updateSwap,
|
|
data: {sn: serialNum},
|
|
success: (res) => {
|
|
if(res.data.errCode == 0){
|
|
let {data, h5ReaderUrl} = res.data
|
|
console.log(res.data)
|
|
uni.setStorageSync('deviceTitle', data.title)
|
|
uni.setStorageSync('h5ReaderUrl', h5ReaderUrl)
|
|
curVideoDir = 0
|
|
backPics = []
|
|
getSwapPic(data.bgUrls)
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
uni.showToast({
|
|
title:'轮播图同步失败',
|
|
icon:'none'
|
|
})
|
|
// syncQueueDone()
|
|
|
|
}
|
|
})
|
|
}
|
|
function getSwapPic(pics) {
|
|
if(curVideoDir < pics.length){
|
|
showLoading('更新轮播图' + (curVideoDir + 1) + ' / ' + pics.length)
|
|
let url = pics[curVideoDir]
|
|
uni.downloadFile({
|
|
url:encodeURI(url),
|
|
header: {'content-type':'application/x-www-form-urlencoded',
|
|
'changeOrigin':true},
|
|
success: (res) => {
|
|
if(res && (res.statusCode == 200)){
|
|
uni.saveFile({
|
|
tempFilePath:res.tempFilePath,
|
|
success: (saveRes) => {
|
|
backPics.push(saveRes.savedFilePath)
|
|
curVideoDir += 1
|
|
getSwapPic(pics)
|
|
uni.hideLoading()
|
|
|
|
uni.showModal({
|
|
title:'更新已完成!',
|
|
content:'回到首页',
|
|
showCancel:false,
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
console.log('用户点击确定');
|
|
uni.navigateTo({
|
|
url:'../index/index?downenter=true'
|
|
})
|
|
} else if (res.cancel) {
|
|
console.log('用户点击取消');
|
|
}
|
|
}
|
|
})
|
|
|
|
},
|
|
fail: (e) => {
|
|
console.log('saveFile err: ', e)
|
|
showLoading('轮播图' + (curVideoDir + 1) + ' 获取失败')
|
|
curVideoDir += 1
|
|
getSwapPic(pics)
|
|
},
|
|
})
|
|
}else{
|
|
showLoading('轮播图' + (curVideoDir + 1) + ' 获取失败')
|
|
curVideoDir += 1
|
|
getSwapPic(pics)
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
showLoading('轮播图' + (curVideoDir + 1) + ' 获取失败')
|
|
curVideoDir += 1
|
|
getSwapPic(pics)
|
|
}
|
|
})
|
|
}else{
|
|
// console.log('9999', backPics)
|
|
uni.setStorageSync('backPics', backPics)
|
|
initBackPics()
|
|
// syncQueueDone()
|
|
uni.showModal({
|
|
title:'更新已完成!',
|
|
content:'回到首页',
|
|
showCancel:false,
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
console.log('用户点击确定');
|
|
uni.navigateTo({
|
|
url:'../index/index?downenter=true'
|
|
})
|
|
} else if (res.cancel) {
|
|
console.log('用户点击取消');
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
const queueFail = function () {
|
|
let list = fBook.concat(fDoc).concat(fVideo).concat(fTheme).concat(fVr)
|
|
list.forEach((res) => {
|
|
if(res.localCover)
|
|
uni.removeSavedFile({
|
|
filePath:res.localCover
|
|
})
|
|
if(res.directory){
|
|
res.directory.forEach((sub) => {
|
|
if(sub.localCover){
|
|
uni.removeSavedFile({
|
|
filePath:sub.localCover
|
|
})
|
|
}
|
|
if(sub.localUrl){
|
|
uni.removeSavedFile({
|
|
filePath:sub.localUrl
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
const showLoading = function(str) {
|
|
uni.showLoading({
|
|
title:str,
|
|
mask:true
|
|
})
|
|
}
|
|
|
|
export {
|
|
Sync,
|
|
queueFail
|
|
}
|