Repository navigation
Expand file tree
/
Copy pathmap.js
More file actions
519 lines (502 loc) · 14.2 KB
/
Copy pathmap.js
File metadata and controls
519 lines (502 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import { fromJS } from 'immutable'
import { REHYDRATE } from 'redux-persist'
import { handleActions } from 'redux-actions'
import { SMap, SScene } from 'imobile_for_reactnative'
import { FileTools } from '../native'
import { Toast } from '../utils'
import { ConstPath, ConstInfo } from '../constants'
import fs from 'react-native-fs'
import UserType from '../constants/UserType'
import ConstOnline from '../constants/ConstOnline'
import { getLanguage } from '../language'
// Constants
// --------------------------------------------------
export const OPEN_WORKSPACE = 'OPEN_WORKSPACE'
export const GET_MAPS = 'GET_MAPS'
export const SET_LATEST_MAP = 'SET_LATEST_MAP'
export const SET_MAP_VIEW = 'SET_MAP_VIEW'
export const SET_CURRENT_MAP = 'SET_CURRENT_MAP'
export const SET_BASEMAP = 'SET_BASEMAP'
let isExporting = false
// Actions
// --------------------------------------------------
// 打开工作空间
export const openWorkspace = (params, cb = () => {}) => async dispatch => {
try {
let result = await SMap.openWorkspace(params)
await dispatch({
type: OPEN_WORKSPACE,
payload: params || {},
})
cb && cb(result)
return result
} catch (e) {
await dispatch({
type: OPEN_WORKSPACE,
payload: {},
})
cb && cb(false)
return false
}
}
// 关闭工作空间
export const closeWorkspace = (cb = () => {}) => async dispatch => {
try {
// await SMap.closeDatasource()
let result = await SMap.closeWorkspace()
await dispatch({
type: OPEN_WORKSPACE,
payload: {},
})
cb && cb(result)
return result
} catch (e) {
await dispatch({
type: OPEN_WORKSPACE,
payload: {},
})
cb && cb(false)
return false
}
}
// 打开地图
export const openMap = (params, cb = () => {}) => async (
dispatch,
getState,
) => {
try {
if (
params === null ||
params === undefined ||
(!params.title && !params.name) ||
!params.path
)
return
let absolutePath = await FileTools.appendingHomeDirectory(params.path)
let userName = getState().user.toJS().currentUser.userName || 'Customer'
let moduleName = GLOBAL.Type
let module = params.module || ''
let fileName = params.name || params.title
let isCustomerPath = params.path.indexOf(ConstPath.CustomerPath) >= 0
let importResult = await SMap.openMapName(fileName, {
Module: module,
IsPrivate: !isCustomerPath,
})
let expFilePath =
absolutePath.substr(0, absolutePath.lastIndexOf('.')) + '.exp'
let openMapResult = importResult && (await SMap.openMap(fileName))
let mapInfo = await SMap.getMapInfo()
if (openMapResult) {
let expIsExist = await FileTools.fileIsExist(expFilePath)
if (expIsExist) {
let data = await fs.readFile(expFilePath)
Object.assign(mapInfo, JSON.parse(data), { path: params.path })
await SMap.resetMapFixColorsModeValue(true)
await dispatch({
type: SET_CURRENT_MAP,
payload: mapInfo || {},
extData: {
userName,
moduleName,
},
})
cb && cb(mapInfo)
return mapInfo
}
} else {
return openMapResult
}
} catch (e) {
cb && cb(false)
return false
}
}
// 保存地图地图
export const saveMap = (params = {}, cb = () => {}) => async (
dispatch,
getState,
) => {
try {
let mapName = params.mapName
let currentMap = getState().map.toJS().currentMap
let curMapName = currentMap.name
let userName = getState().user.toJS().currentUser.userName
let path = ''
if (!params.notSaveToXML) {
mapName = await SMap.saveMapName(
params.mapName,
params.nModule || '',
params.addition,
params.isNew,
)
path =
ConstPath.UserPath +
userName +
'/' +
ConstPath.RelativePath.Map +
mapName +
'.xml'
}
// 另存为 和 未打开一幅已命名的地图,则需要重新设置当前地图
// 另存不需要重新设置当前地图
if (!params.isNew /*|| curMapName !== mapName*/) {
let mapInfo = {}
if (curMapName === mapName) {
Object.assign(mapInfo, currentMap, { path, name: mapName })
} else {
mapInfo = { path, name: mapName }
}
await dispatch({
type: SET_CURRENT_MAP,
payload: mapInfo,
extData: {
userName,
moduleName: GLOBAL.Type,
},
})
}
cb && cb()
return mapName
} catch (e) {
cb && cb()
return null
}
}
// 关闭地图
export const closeMap = (cb = () => {}) => async dispatch => {
try {
await SMap.resetMapFixColorsModeValue(false)
await SMap.closeMap()
await SMap.removeMap(-1) // 移除所有地图
await SMap.closeDatasource()
await dispatch({
type: SET_CURRENT_MAP,
payload: {},
})
cb && cb()
} catch (e) {
cb && cb()
}
}
// 获取当前工作空间的地图列表
export const getMaps = (cb = () => {}) => async dispatch => {
try {
let maps = await SMap.getMaps()
await dispatch({
type: GET_MAPS,
payload: maps || [],
})
cb && cb(maps)
} catch (e) {
await dispatch({
type: GET_MAPS,
payload: [],
})
cb && cb(false)
}
}
export const setLatestMap = (params, cb = () => {}) => async dispatch => {
await dispatch({
type: SET_LATEST_MAP,
payload: params || {},
})
cb && cb()
}
export const setMapView = (params, cb = () => {}) => async dispatch => {
await dispatch({
type: SET_MAP_VIEW,
payload: params || {},
})
cb && cb()
}
export const setCurrentMap = (params, cb = () => {}) => async dispatch => {
// let result = params && await SMap.importWorkspace(params)
await dispatch({
type: SET_CURRENT_MAP,
payload: params || {},
})
cb && cb()
}
// 导出模版
export const exportWorkspace = (params, cb = () => {}) => async (
dispatch,
getState,
) => {
if (isExporting) {
Toast.show(getLanguage(global.language).Prompt.TYR_AGAIN_LATER)
//''请稍后再试')
return false
}
isExporting = true
let exportResult = false
let zipResult = false
try {
let userName = getState().user.toJS().currentUser.userName || 'Customer'
let workspace = getState().map.toJS().workspace
let path = params.outPath,
fileName = '',
fileNameWithoutExtension = '',
parentPath = '',
zipPath = params.zipPath
let exportResult = false
if (!path) {
fileName = workspace.server.substr(workspace.server.lastIndexOf('/') + 1)
fileNameWithoutExtension = fileName.substr(0, fileName.lastIndexOf('.'))
parentPath = await FileTools.appendingHomeDirectory(
ConstPath.UserPath +
userName +
'/' +
ConstPath.RelativePath.Temp +
fileNameWithoutExtension,
)
path = parentPath + '/' + fileName
} else {
parentPath = path.substr(0, path.lastIndexOf('/'))
}
// 导出工作空间
if (params.maps && params.maps.length > 0) {
let fileReplace =
params.fileReplace === undefined ? true : params.fileReplace
if (params.isOpenMap) {
let isLogin =
getState().user.toJS().currentUser.userType !==
UserType.PROBATION_USER
exportResult = await SMap.exportWorkspaceByMap(params.maps[0], path, {
Module: '',
IsPrivate: isLogin,
})
} else {
exportResult = await SMap.exportWorkspace(
params.maps,
path,
fileReplace,
params.extra,
)
}
}
// console.warn(exportResult)
// 压缩工作空间
if (exportResult) {
zipPath = zipPath ? zipPath : parentPath + '.zip'
zipResult = await FileTools.zipFile(parentPath, zipPath)
}
// 删除导出的工作空间
await FileTools.deleteFile(parentPath)
isExporting = false
cb && cb(exportResult && zipResult, zipPath)
} catch (e) {
isExporting = false
if (!exportResult) {
Toast.show(getLanguage(global.language).Prompt.EXPORT_FAILED)
//ConstInfo.EXPORT_WORKSPACE_FAILED)
} else if (!zipResult) {
Toast.show(ConstInfo.ZIP_FAILED)
}
cb && cb(exportResult && zipResult, '')
}
}
//导出工作空间
export const exportmap3DWorkspace = (params, cb = () => {}) => async (
dispatch,
getState,
) => {
// return
let userName = getState().user.toJS().currentUser.userName || 'Customer'
if (params.name) {
if (isExporting) {
Toast.show(getLanguage(global.language).Prompt.TYR_AGAIN_LATER)
//'请稍后再试')
return false
}
isExporting = true
let path = await FileTools.appendingHomeDirectory(
ConstPath.UserPath +
userName +
'/' +
ConstPath.RelativePath.Temp +
params.name,
)
let result = await SScene.export3DScenceName(params.name, path)
if (result) {
let zipPath = path + '.zip'
result = await FileTools.zipFile(path, zipPath)
if (result) {
await FileTools.deleteFile(path)
Toast.show(getLanguage(global.language).Prompt.SHARE_START)
//'导出成功,开始分享')
isExporting = false
cb && cb(result, zipPath)
}
} else {
Toast.show(getLanguage(global.language).Prompt.EXPORT_FAILED)
//'导出失败')
}
// let result = await SScene.is3DWorkspace({ server: params.server })
// if (result) {
// let result2 = await SScene.import3DWorkspace({ server: params.server })
// if (result2) {
// Toast.show('倒入成功')
// } else {
// Toast.show('倒入失败')
// }
// } else {
// Toast.show('倒入失败')
// }
}
}
//导入三维工作空间
export const importSceneWorkspace = params => async (dispatch, getState) => {
let userName = getState().user.toJS().currentUser.userName || 'Customer'
// return
if (userName !== 'Customer') {
let path = await FileTools.appendingHomeDirectory(
ConstPath.UserPath + userName,
)
await SScene.setCustomerDirectory(path)
}
if (params.server) {
let result = await SScene.is3DWorkspace({ server: params.server })
if (result) {
let result2 = await SScene.import3DWorkspace({ server: params.server })
if (result2) {
// Toast.show('导入成功')
return result2
} else {
// Toast.show('导入失败')
return result2
}
} else {
// Toast.show('导入失败')
// return
return result
}
}
}
export const setBaseMap = (params, cb = () => {}) => async dispatch => {
await dispatch({
type: SET_BASEMAP,
payload: params || {},
})
cb && cb()
}
const initialState = fromJS({
latestMap: {},
map: {},
maps: [],
currentMap: {},
workspace: {},
baseMaps: {
default: [
ConstOnline.Baidu,
ConstOnline.Google,
ConstOnline.OSM,
ConstOnline.SuperMapCloud,
],
},
})
export default handleActions(
{
[`${OPEN_WORKSPACE}`]: (state, { payload }) => {
return state.setIn(['workspace'], fromJS(payload))
},
[`${SET_LATEST_MAP}`]: (state, { payload }) => {
let newData = state.toJS().latestMap || []
let isExist = false
for (let i = 0; i < newData.length; i++) {
if (newData[i].path === payload.path) {
newData[i] = payload
let temp = newData[0]
newData[0] = newData[i]
newData[i] = temp
isExist = true
break
}
}
if (!isExist && payload) {
newData.unshift(payload)
}
return state.setIn(['latestMap'], fromJS(newData))
},
[`${SET_MAP_VIEW}`]: (state, { payload }) => {
if (payload.workspace) {
state.setIn(['workspace'], fromJS(payload.workspace))
}
if (payload.map) {
state.setIn(['map'], fromJS(payload.map))
}
if (payload.mapControl) {
state.setIn(['mapControl'], fromJS(payload.mapControl))
}
return state
},
[`${GET_MAPS}`]: (state, { payload }) => {
return state.setIn(['maps'], fromJS(payload))
},
[`${SET_CURRENT_MAP}`]: (state, { payload, extData }) => {
let newData = state.toJS().latestMap || {}
if (extData) {
if (!newData[extData.userName]) {
newData[extData.userName] = {}
}
if (!newData[extData.userName][extData.moduleName]) {
newData[extData.userName][extData.moduleName] = []
}
let isExist = false
for (
let i = 0;
i < newData[extData.userName][extData.moduleName].length;
i++
) {
if (!payload.path) break
if (
newData[extData.userName][extData.moduleName][i].path ===
payload.path
) {
newData[extData.userName][extData.moduleName][i] = payload
let temp = newData[extData.userName][extData.moduleName][0]
newData[extData.userName][extData.moduleName][0] =
newData[extData.userName][extData.moduleName][i]
newData[extData.userName][extData.moduleName][i] = temp
isExist = true
break
}
}
if (!isExist && payload && payload.path) {
newData[extData.userName][extData.moduleName].unshift(payload)
}
return state
.setIn(['currentMap'], fromJS(payload))
.setIn(['latestMap'], fromJS(newData))
} else {
return state.setIn(['currentMap'], fromJS(payload))
}
},
//payload {userId:id,baseMaps:[]}
[`${SET_BASEMAP}`]: (state, { payload }) => {
let allBaseMap = state.toJS()
if (allBaseMap.baseMaps.hasOwnProperty(payload.userId) === false) {
allBaseMap.baseMaps[payload.userId] = []
}
// let newData
if (payload.baseMaps.length > 0) {
allBaseMap.baseMaps[payload.userId] = payload.baseMaps
}
return state.setIn(['baseMaps'], fromJS(allBaseMap.baseMaps))
},
[REHYDRATE]: (state, { payload }) => {
if (payload && payload.map) {
let data = payload.map
data.maps = []
data.currentMap = {}
data.workspace = {}
if (data.baseMaps === undefined) {
data.baseMaps = initialState.toJS().baseMaps
}
return fromJS(data)
} else {
return state
}
},
},
initialState,
)