// 地图文件处理 import JSZip from 'jszip' import { fileUtils } from '@/utils/util.js' let that = null let dbHelper = null export default { unzipFiles: [], mapData: null, // 地图图片的二进制BLOB数据 mapUrl: "", // 地图图片的URL mapConfig: "", // 地图图片的地理参考信息 async handleMapInfo(context, mapInfo) { // console.log('[handleMapInfo] mapInfo', mapInfo) that = context dbHelper = that.$dbHelper try { await dbHelper.openDB() const res = await this.dbGetMapInfo(mapInfo.mapId) let download = false let action = null if (res == null || res.length == 0) { console.warn('[handleMapInfo] 本地无匹配记录,需要下载地图') download = true action = 'insert' } else { const localMd5 = res.mapMd5 if (localMd5 != mapInfo.zipImage.md5) { console.warn('[handleMapInfo] MD5不同,需再次下载地图') download = true action = 'update' } else { console.log('[handleMapInfo] MD5相同,无需再次下载地图') download = false // download = true // debug, will del // action = 'update' // debug, will del } } if (download) { const zipData = await this.getZipFiles(mapInfo.zipImage.url) const unzipFiles = await this.unzipMap(zipData) await this.dbSaveMapInfo(mapInfo, zipData, unzipFiles, action) } await dbHelper.closeDB() await this.createMapURL(this.mapData) } catch (err) { console.error('[handleMapInfo]', err) } }, // 从指定的URL下载地图压缩包 async getZipFiles(zipUrl) { console.log("[getZipFiles] zipUrl:", zipUrl) if (!(zipUrl.length > 0)) { console.log("[getZipFiles] err: zipUrl 为空") return } let res = await uni.request({ url: zipUrl, timeout: 60000, // 超时时间,单位 ms responseType: 'arraybuffer', // 设置响应的数据类型。合法值:text、arraybuffer }); console.log("[getZipFiles]", res) return res.data }, // 解包并获取包内的各文件内容数据 (arraybuffer格式) async unzipMap(zipData) { // console.log("[unzipMap] zipData: ", zipData) if (zipData == undefined || (!(zipData.byteLength > 0))) { console.log("[unzipMap] err: zipData 为空") return } console.log("[unzipMap] 压缩包大小(字节) zipData.byteLength:", zipData.byteLength) try { let jsZip = new JSZip() let zip = await jsZip.loadAsync(zipData) // console.log('zip.files', zip.files) let unzipFiles = [] let i = 0 for (let fileName in zip.files) { // filename为压缩包中的文件名 console.log('[unzipMap] fileName', fileName) let isImage = fileUtils.isTypeImage(fileName) let isTypeGISConfig = fileUtils.isTypeGISConfig(fileName) let fileData = '' let dataType = '' // 获取文件内容,可以使用string、arraybuffer等格式 if (isImage) { fileData = await zip.file(fileName).async('arraybuffer') dataType = 'arraybuffer' this.mapData = fileData console.log('[unzipMap] mapData', this.mapData) } else { fileData = await zip.file(fileName).async('string') dataType = 'string' if (isTypeGISConfig) { this.mapConfig = fileData console.log('[unzipMap] mapConfig', this.mapConfig) } } // console.log('[unzipMap] content', content) unzipFiles[i] = { fileName: fileName, fileData: fileData, dataType: dataType } i++ } this.unzipFiles = unzipFiles // console.log('[unzipMap] unzipFiles', unzipFiles) return unzipFiles } catch (err) { console.error('[unzipMap]', err) } }, // 将地图信息保存到本地数据库中 async dbSaveMapInfo(mapInfo, zipData, unzipFiles = '', action = 'insert') { // #ifdef H5 await this.dbSaveMapInfo_H5(mapInfo, zipData, unzipFiles, action) // #endif // #ifdef APP-PLUS await this.dbSaveMapInfo_APP(mapInfo, zipData, unzipFiles, action) // #endif }, // [H5端] 将地图信息保存到本地浏览器 dbHelper 数据库中 async dbSaveMapInfo_H5(mapInfo, zipData, unzipFiles, action) { console.log("[dbSaveMapInfo_H5]", mapInfo, unzipFiles) let storeName = 'mapInfo' let data = { mapId: mapInfo.mapId, mapName: mapInfo.mapName, mapMd5: mapInfo.zipImage.md5, mapData: this.mapData, mapConfig: this.mapConfig, // zipImage: { // url: mapInfo.zipImage.url, // md5: mapInfo.zipImage.md5, // }, // zipData: zipData, // unzipFiles: unzipFiles } try { if (action == 'insert') { await dbHelper.insertData(storeName, data) } else if (action == 'update') { await dbHelper.updateData(storeName, data) } else { console.warn('[dbSaveMapInfo_H5] 参数 action 非法: ' + action) } } catch (err) { console.error('[dbSaveMapInfo_H5]', err) } }, // [APP端] 将地图信息保存到手机中的 SQLITE 数据库中 async dbSaveMapInfo_APP(mapInfo, zipData, unzipFiles, action) { // console.log("[dbSaveMapInfo_APP]", mapInfo, unzipFiles) let tableName = 'mapInfo' let zipFileKey = 'map_' + mapInfo.mapId let zipFileName = '' if (unzipFiles.length > 0) { zipFileName = unzipFiles[0].fileName // let zipFilePath = mapInfo.mapId + '_' + unzipFiles[0].fileName let zipFileData = uni.arrayBufferToBase64(unzipFiles[0].fileData) // await io.write(zipFilePath, zipFileData) uni.setStorageSync(zipFileKey, zipFileData); console.log("[dbSaveMapInfo_APP] setStorageSync Key:", zipFileKey) console.log("[dbSaveMapInfo_APP] setStorageSync data.fileData length:", unzipFiles[0].fileData .byteLength) // let zipFile = uni.base64ToArrayBuffer(uni.getStorageSync(zipFileKey)); // console.log("-----> getStorageSync Key", zipFileKey) // console.log("-----> getStorageSync data.fileData length", zipFile.byteLength) } let data = { mapId: mapInfo.mapId, mapName: mapInfo.mapName, zipImageUrl: mapInfo.zipImage.url, zipImageMd5: mapInfo.zipImage.md5, zipFileName: zipFileName, zipFileKey: zipFileKey, // zipFilePath: zipFilePath, } try { if (action == 'insert') { await dbHelper.insertData(tableName, data) } else if (action == 'update') { let wsql = 'mapId = ' + data.mapId await dbHelper.updateData(tableName, data, wsql) } else { console.warn('[dbSaveMapInfo_APP] 参数 action 非法: ' + action) } } catch (err) { console.error('[dbSaveMapInfo_APP]', err) } }, async dbGetMapInfo(mapId) { let res = null // #ifdef H5 res = await this.dbGetMapInfo_H5(mapId) // #endif // #ifdef APP-PLUS res = await this.dbGetMapInfo_APP(mapId) // #endif return res }, // [H5端] async dbGetMapInfo_H5(mapId) { // console.log("[dbGetMapInfo_H5] mapId", mapId) let storeName = 'mapInfo' let key = mapId try { let data = await dbHelper.getDataByKey(storeName, key) // console.log("[dbGetMapInfo_H5] data", data) if (data != null) { this.mapData = data.mapData this.mapConfig = data.mapConfig } return data } catch (err) { console.error('[dbGetMapInfo_H5]', err) } }, // [APP端] async dbGetMapInfo_APP(mapId) { console.log("[dbGetMapInfo_APP] mapId", mapId) let tableName = 'mapInfo' let key = mapId try { let data = await dbHelper.getDataByKey(tableName, key) if (data != '' && data.zipFileKey.length > 0) { // let zipFileData = uni.base64ToArrayBuffer(await io.read(data.zipFilePath)) let zipFileData = uni.base64ToArrayBuffer(uni.getStorageSync(data.zipFileKey)) console.log("[dbGetMapInfo_APP] getStorageSync Key", data.zipFileKey) console.log("[dbGetMapInfo_APP] getStorageSync data length", zipFileData.byteLength) data.zipFileData = zipFileData } // console.log("[dbGetMapInfo_APP] data", data) return data } catch (err) { console.error('[dbGetMapInfo_APP]', err) } }, /* getMapDataFromDBres(dbres) { let arrayBuffer = null // #ifdef H5 // arrayBuffer = dbres.unzipFiles[0].fileData arrayBuffer = dbres.zipData // console.log('getMapDataFromDBres', dbres.unzipFiles[0].fileName, dbres.unzipFiles[0].fileData) // #endif // #ifdef APP-PLUS arrayBuffer = dbres.zipFileData // console.log('getMapDataFromDBres', dbres.zipFileName, dbres.zipFileData) // #endif return arrayBuffer }, */ async createMapURL(mapData) { // console.log("[createMapURL] mapData", mapData) let binaryData = []; binaryData.push(mapData); this.mapUrl = URL.createObjectURL(new Blob(binaryData)); // console.log("[createMapURL] mapUrl", this.mapUrl) }, }