mapHelper.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // 地图文件处理
  2. import JSZip from 'jszip'
  3. import {
  4. fileUtils
  5. } from '@/utils/util.js'
  6. let that = null
  7. let dbHelper = null
  8. export default {
  9. unzipFiles: [],
  10. mapData: null, // 地图图片的二进制BLOB数据
  11. mapUrl: "", // 地图图片的URL
  12. mapConfig: "", // 地图图片的地理参考信息
  13. async handleMapInfo(context, mapInfo) {
  14. // console.log('[handleMapInfo] mapInfo', mapInfo)
  15. that = context
  16. dbHelper = that.$dbHelper
  17. try {
  18. await dbHelper.openDB()
  19. const res = await this.dbGetMapInfo(mapInfo.mapId)
  20. let download = false
  21. let action = null
  22. if (res == null || res.length == 0) {
  23. console.warn('[handleMapInfo] 本地无匹配记录,需要下载地图')
  24. download = true
  25. action = 'insert'
  26. } else {
  27. const localMd5 = res.mapMd5
  28. if (localMd5 != mapInfo.zipImage.md5) {
  29. console.warn('[handleMapInfo] MD5不同,需再次下载地图')
  30. download = true
  31. action = 'update'
  32. } else {
  33. console.log('[handleMapInfo] MD5相同,无需再次下载地图')
  34. download = false
  35. // download = true // debug, will del
  36. // action = 'update' // debug, will del
  37. }
  38. }
  39. if (download) {
  40. const zipData = await this.getZipFiles(mapInfo.zipImage.url)
  41. const unzipFiles = await this.unzipMap(zipData)
  42. await this.dbSaveMapInfo(mapInfo, zipData, unzipFiles, action)
  43. }
  44. await dbHelper.closeDB()
  45. await this.createMapURL(this.mapData)
  46. } catch (err) {
  47. console.error('[handleMapInfo]', err)
  48. }
  49. },
  50. // 从指定的URL下载地图压缩包
  51. async getZipFiles(zipUrl) {
  52. console.log("[getZipFiles] zipUrl:", zipUrl)
  53. if (!(zipUrl.length > 0)) {
  54. console.log("[getZipFiles] err: zipUrl 为空")
  55. return
  56. }
  57. let res = await uni.request({
  58. url: zipUrl,
  59. timeout: 60000, // 超时时间,单位 ms
  60. responseType: 'arraybuffer', // 设置响应的数据类型。合法值:text、arraybuffer
  61. });
  62. console.log("[getZipFiles]", res)
  63. return res.data
  64. },
  65. // 解包并获取包内的各文件内容数据 (arraybuffer格式)
  66. async unzipMap(zipData) {
  67. // console.log("[unzipMap] zipData: ", zipData)
  68. if (zipData == undefined || (!(zipData.byteLength > 0))) {
  69. console.log("[unzipMap] err: zipData 为空")
  70. return
  71. }
  72. console.log("[unzipMap] 压缩包大小(字节) zipData.byteLength:", zipData.byteLength)
  73. try {
  74. let jsZip = new JSZip()
  75. let zip = await jsZip.loadAsync(zipData)
  76. // console.log('zip.files', zip.files)
  77. let unzipFiles = []
  78. let i = 0
  79. for (let fileName in zip.files) { // filename为压缩包中的文件名
  80. console.log('[unzipMap] fileName', fileName)
  81. let isImage = fileUtils.isTypeImage(fileName)
  82. let isTypeGISConfig = fileUtils.isTypeGISConfig(fileName)
  83. let fileData = ''
  84. let dataType = ''
  85. // 获取文件内容,可以使用string、arraybuffer等格式
  86. if (isImage) {
  87. fileData = await zip.file(fileName).async('arraybuffer')
  88. dataType = 'arraybuffer'
  89. this.mapData = fileData
  90. console.log('[unzipMap] mapData', this.mapData)
  91. } else {
  92. fileData = await zip.file(fileName).async('string')
  93. dataType = 'string'
  94. if (isTypeGISConfig) {
  95. this.mapConfig = fileData
  96. console.log('[unzipMap] mapConfig', this.mapConfig)
  97. }
  98. }
  99. // console.log('[unzipMap] content', content)
  100. unzipFiles[i] = {
  101. fileName: fileName,
  102. fileData: fileData,
  103. dataType: dataType
  104. }
  105. i++
  106. }
  107. this.unzipFiles = unzipFiles
  108. // console.log('[unzipMap] unzipFiles', unzipFiles)
  109. return unzipFiles
  110. } catch (err) {
  111. console.error('[unzipMap]', err)
  112. }
  113. },
  114. // 将地图信息保存到本地数据库中
  115. async dbSaveMapInfo(mapInfo, zipData, unzipFiles = '', action = 'insert') {
  116. // #ifdef H5
  117. await this.dbSaveMapInfo_H5(mapInfo, zipData, unzipFiles, action)
  118. // #endif
  119. // #ifdef APP-PLUS
  120. await this.dbSaveMapInfo_APP(mapInfo, zipData, unzipFiles, action)
  121. // #endif
  122. },
  123. // [H5端] 将地图信息保存到本地浏览器 dbHelper 数据库中
  124. async dbSaveMapInfo_H5(mapInfo, zipData, unzipFiles, action) {
  125. console.log("[dbSaveMapInfo_H5]", mapInfo, unzipFiles)
  126. let storeName = 'mapInfo'
  127. let data = {
  128. mapId: mapInfo.mapId,
  129. mapName: mapInfo.mapName,
  130. mapMd5: mapInfo.zipImage.md5,
  131. mapData: this.mapData,
  132. mapConfig: this.mapConfig,
  133. // zipImage: {
  134. // url: mapInfo.zipImage.url,
  135. // md5: mapInfo.zipImage.md5,
  136. // },
  137. // zipData: zipData,
  138. // unzipFiles: unzipFiles
  139. }
  140. try {
  141. if (action == 'insert') {
  142. await dbHelper.insertData(storeName, data)
  143. } else if (action == 'update') {
  144. await dbHelper.updateData(storeName, data)
  145. } else {
  146. console.warn('[dbSaveMapInfo_H5] 参数 action 非法: ' + action)
  147. }
  148. } catch (err) {
  149. console.error('[dbSaveMapInfo_H5]', err)
  150. }
  151. },
  152. // [APP端] 将地图信息保存到手机中的 SQLITE 数据库中
  153. async dbSaveMapInfo_APP(mapInfo, zipData, unzipFiles, action) {
  154. // console.log("[dbSaveMapInfo_APP]", mapInfo, unzipFiles)
  155. let tableName = 'mapInfo'
  156. let zipFileKey = 'map_' + mapInfo.mapId
  157. let zipFileName = ''
  158. if (unzipFiles.length > 0) {
  159. zipFileName = unzipFiles[0].fileName
  160. // let zipFilePath = mapInfo.mapId + '_' + unzipFiles[0].fileName
  161. let zipFileData = uni.arrayBufferToBase64(unzipFiles[0].fileData)
  162. // await io.write(zipFilePath, zipFileData)
  163. uni.setStorageSync(zipFileKey, zipFileData);
  164. console.log("[dbSaveMapInfo_APP] setStorageSync Key:", zipFileKey)
  165. console.log("[dbSaveMapInfo_APP] setStorageSync data.fileData length:", unzipFiles[0].fileData
  166. .byteLength)
  167. // let zipFile = uni.base64ToArrayBuffer(uni.getStorageSync(zipFileKey));
  168. // console.log("-----> getStorageSync Key", zipFileKey)
  169. // console.log("-----> getStorageSync data.fileData length", zipFile.byteLength)
  170. }
  171. let data = {
  172. mapId: mapInfo.mapId,
  173. mapName: mapInfo.mapName,
  174. zipImageUrl: mapInfo.zipImage.url,
  175. zipImageMd5: mapInfo.zipImage.md5,
  176. zipFileName: zipFileName,
  177. zipFileKey: zipFileKey,
  178. // zipFilePath: zipFilePath,
  179. }
  180. try {
  181. if (action == 'insert') {
  182. await dbHelper.insertData(tableName, data)
  183. } else if (action == 'update') {
  184. let wsql = 'mapId = ' + data.mapId
  185. await dbHelper.updateData(tableName, data, wsql)
  186. } else {
  187. console.warn('[dbSaveMapInfo_APP] 参数 action 非法: ' + action)
  188. }
  189. } catch (err) {
  190. console.error('[dbSaveMapInfo_APP]', err)
  191. }
  192. },
  193. async dbGetMapInfo(mapId) {
  194. let res = null
  195. // #ifdef H5
  196. res = await this.dbGetMapInfo_H5(mapId)
  197. // #endif
  198. // #ifdef APP-PLUS
  199. res = await this.dbGetMapInfo_APP(mapId)
  200. // #endif
  201. return res
  202. },
  203. // [H5端]
  204. async dbGetMapInfo_H5(mapId) {
  205. // console.log("[dbGetMapInfo_H5] mapId", mapId)
  206. let storeName = 'mapInfo'
  207. let key = mapId
  208. try {
  209. let data = await dbHelper.getDataByKey(storeName, key)
  210. // console.log("[dbGetMapInfo_H5] data", data)
  211. if (data != null) {
  212. this.mapData = data.mapData
  213. this.mapConfig = data.mapConfig
  214. }
  215. return data
  216. } catch (err) {
  217. console.error('[dbGetMapInfo_H5]', err)
  218. }
  219. },
  220. // [APP端]
  221. async dbGetMapInfo_APP(mapId) {
  222. console.log("[dbGetMapInfo_APP] mapId", mapId)
  223. let tableName = 'mapInfo'
  224. let key = mapId
  225. try {
  226. let data = await dbHelper.getDataByKey(tableName, key)
  227. if (data != '' && data.zipFileKey.length > 0) {
  228. // let zipFileData = uni.base64ToArrayBuffer(await io.read(data.zipFilePath))
  229. let zipFileData = uni.base64ToArrayBuffer(uni.getStorageSync(data.zipFileKey))
  230. console.log("[dbGetMapInfo_APP] getStorageSync Key", data.zipFileKey)
  231. console.log("[dbGetMapInfo_APP] getStorageSync data length", zipFileData.byteLength)
  232. data.zipFileData = zipFileData
  233. }
  234. // console.log("[dbGetMapInfo_APP] data", data)
  235. return data
  236. } catch (err) {
  237. console.error('[dbGetMapInfo_APP]', err)
  238. }
  239. },
  240. /* getMapDataFromDBres(dbres) {
  241. let arrayBuffer = null
  242. // #ifdef H5
  243. // arrayBuffer = dbres.unzipFiles[0].fileData
  244. arrayBuffer = dbres.zipData
  245. // console.log('getMapDataFromDBres', dbres.unzipFiles[0].fileName, dbres.unzipFiles[0].fileData)
  246. // #endif
  247. // #ifdef APP-PLUS
  248. arrayBuffer = dbres.zipFileData
  249. // console.log('getMapDataFromDBres', dbres.zipFileName, dbres.zipFileData)
  250. // #endif
  251. return arrayBuffer
  252. }, */
  253. async createMapURL(mapData) {
  254. // console.log("[createMapURL] mapData", mapData)
  255. let binaryData = [];
  256. binaryData.push(mapData);
  257. this.mapUrl = URL.createObjectURL(new Blob(binaryData));
  258. // console.log("[createMapURL] mapUrl", this.mapUrl)
  259. },
  260. }