@smart100/spu-web-plugin 1.0.12 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smart100/spu-web-plugin",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "dev": "npm run build:types && rollup -c -w",
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ import { version } from '../package.json'
2
2
  import { merge } from 'lodash-es'
3
3
  import { v4 as getUuid } from 'uuid'
4
4
  import { installStorageProxy, lsProxy, ssProxy } from './storageProxy'
5
- import { getLocation, getDistance } from './map/index'
5
+ import { getLocation, getDistance, getAddress } from './map/index'
6
6
  import { installAxios, spuAxios, axios } from './axios'
7
7
  import { installSpuConfig, spuConfig } from './spuConfig'
8
8
  import { globalConfig } from './globalConfig'
@@ -101,6 +101,7 @@ export {
101
101
  ssProxy,
102
102
  getLocation,
103
103
  getDistance,
104
+ getAddress,
104
105
  spuAxios,
105
106
  axios,
106
107
  axios as apaasAxios,
@@ -101,11 +101,25 @@ class MapService {
101
101
 
102
102
  private async _init() {
103
103
  const type = this.type
104
+ const key = this.key
105
+ const secretkey = this.secretkey
104
106
  if (type === 'tencent') {
107
+ if (!key || !secretkey) {
108
+ console.error('请填写腾讯地图 Web App Key 和 Web Secret Key')
109
+ return
110
+ }
105
111
  await this.initTecent()
106
112
  } else if (type === 'amap') {
113
+ if (!key || !secretkey) {
114
+ console.error('请填写高德地图 Web App Key 和 Web Secret Key')
115
+ return
116
+ }
107
117
  await this.initAmap()
108
118
  } else if (type === 'baidu') {
119
+ if (!key) {
120
+ console.error('请填写百度地图 Web App Key')
121
+ return
122
+ }
109
123
  await this.initBaidu()
110
124
  }
111
125
  this.isInit = true
@@ -127,45 +141,51 @@ class MapService {
127
141
  }
128
142
 
129
143
  private async initAmap() {
130
- if (this.secretkey) {
131
- window._AMapSecurityConfig = {
132
- securityJsCode: this.secretkey
133
- }
134
- }
135
-
136
- const plugin = ['AMap.Geolocation', 'AMap.Geocoder']
144
+ return new Promise(async (resolve, reject) => {
145
+ // 高德地图api初始化失败 没有返回reject 因此用超时机制检测
146
+ const time = setTimeout(() => {
147
+ console.error('initAmap fail: 请检查高德地图 Web App Key 和 Web Secret Key 是否正确配置')
148
+ resolve(null)
149
+ }, 3000)
137
150
 
138
- const AMap = await AMapLoader.load({
139
- key: this.key,
140
- version: '2.0',
141
- plugins: plugin,
142
- AMapUI: {
143
- version: '1.1',
144
- plugins: []
151
+ if (this.secretkey) {
152
+ window._AMapSecurityConfig = {
153
+ securityJsCode: this.secretkey
154
+ }
145
155
  }
156
+
157
+ const plugin = ['AMap.Geolocation', 'AMap.Geocoder']
158
+ // debugger
159
+
160
+ const AMap = await AMapLoader.load({
161
+ key: this.key,
162
+ version: '2.0',
163
+ plugins: plugin,
164
+ AMapUI: {
165
+ version: '1.1',
166
+ plugins: []
167
+ }
168
+ })
169
+ // debugger
170
+ clearTimeout(time)
171
+
172
+ window.AMap = AMap
173
+ this.AMap = window.AMap
174
+ this.MapCore = window.AMap
175
+ // console.log(window)
176
+ // console.log(window.AMap)
177
+ // console.log(window.AMapUI)
178
+ // console.log(window.AMapLoader)
179
+ // console.log(window.AMap === aaaa)
180
+ resolve(window.AMap)
146
181
  })
147
- window.AMap = AMap
148
- this.AMap = AMap
149
- this.MapCore = window.AMap
150
- // console.log(window)
151
- // console.log(window.AMap)
152
- // console.log(window.AMapUI)
153
- // console.log(window.AMapLoader)
154
- // console.log(window.AMap === aaaa)
155
182
  }
156
183
 
157
184
  private async initBaidu() {
158
- // await importJS(
159
- // `https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=${this.key}`,
160
- // 'BMap'
161
- // )
162
- // await delay(300)
163
- // function initialize() {
164
- // const mp = new BMap.Map('map')
165
- // mp.centerAndZoom(new BMap.Point(121.491, 31.233), 11)
166
- // }
167
185
  return new Promise((resolve, reject) => {
168
- window.BMAP_INITIAL_CALLBACK = () => {
186
+ window.BMAP_INITIAL_CALLBACK = (e: any) => {
187
+ // console.log(e)
188
+ // debugger
169
189
  this.BMap = window.BMap
170
190
  this.MapCore = window.BMap
171
191
  // debugger
package/src/map/index.ts CHANGED
@@ -3,6 +3,7 @@ import { cloneDeep } from 'lodash-es'
3
3
  import { mapService } from './MapService'
4
4
  import { getAMapKey } from './AMapKey'
5
5
  import { axios } from '../axios'
6
+ import { delay } from '../utils'
6
7
  import { wgs84ToGcj02, BMapTransformBD09ToGCJ02Points } from './utils'
7
8
 
8
9
  type Location = {
@@ -30,9 +31,9 @@ const getLocationByNative = async (): Promise<Location> => {
30
31
  }
31
32
  }, 30000)
32
33
  window.Native.getLocation((res: any, error: any, status: any) => {
33
- // console.log('getLocation res', res)
34
- // console.log('getLocation error', error)
35
- // console.log('getLocation status', status)
34
+ // console.log('window.Native.getLocation res', res)
35
+ // console.log('window.Native.getLocation error', error)
36
+ // console.log('window.Native.getLocation status', status)
36
37
  isload = true
37
38
  if (res && res?.longitude && res?.latitude) {
38
39
  const result = {
@@ -174,15 +175,18 @@ const getAddressByIpaas = async (position: Location): Promise<string> => {
174
175
  }
175
176
 
176
177
  // 高德定位
177
- const getLocationByAmap = async (): Promise<Location> => {
178
- console.log('getLocationByAmap start...')
178
+ const getLocationByAMap = async (): Promise<Location> => {
179
+ console.log('getLocationByAMap start...')
179
180
  return new Promise((resolve, reject) => {
180
- const geolocation = new window.AMap.Geolocation({
181
+ if (!window?.AMap) {
182
+ console.error('getLocationByAMap fail: AMap is undefinded')
183
+ resolve(null)
184
+ return
185
+ }
186
+ new window.AMap.Geolocation({
181
187
  enableHighAccuracy: true,
182
188
  timeout: 15000
183
- })
184
-
185
- geolocation.getCurrentPosition((status: string, res: any) => {
189
+ }).getCurrentPosition((status: string, res: any) => {
186
190
  // console.log(status, result)
187
191
  // debugger
188
192
  if (status === 'complete') {
@@ -191,10 +195,10 @@ const getLocationByAmap = async (): Promise<Location> => {
191
195
  longitude: lng.toString(),
192
196
  latitude: lat.toString()
193
197
  }
194
- console.log(`getLocationByAmap success: ${JSON.stringify(result)}`)
198
+ console.log(`getLocationByAMap success: ${JSON.stringify(result)}`)
195
199
  resolve(result)
196
200
  } else {
197
- console.error('getLocationByAmap fail')
201
+ console.error('getLocationByAMap fail')
198
202
  resolve(null)
199
203
  }
200
204
  })
@@ -202,14 +206,18 @@ const getLocationByAmap = async (): Promise<Location> => {
202
206
  }
203
207
 
204
208
  // 高德城市定位
205
- const getCityLocationByAmap = async (): Promise<Location> => {
206
- console.log('getCityLocationByAmap start...')
209
+ const getCityLocationByAMap = async (): Promise<Location> => {
210
+ console.log('getCityLocationByAMap start...')
207
211
  return new Promise((resolve, reject) => {
208
- const geolocation = new window.AMap.Geolocation({
212
+ if (!window?.AMap) {
213
+ console.error('getCityLocationByAMap fail: AMap is undefinded')
214
+ resolve(null)
215
+ return
216
+ }
217
+ new window.AMap.Geolocation({
209
218
  enableHighAccuracy: true,
210
219
  timeout: 15000
211
- })
212
- geolocation.getCityInfo((status: string, res: any) => {
220
+ }).getCityInfo((status: string, res: any) => {
213
221
  // console.log(res)
214
222
  // debugger
215
223
  if (status === 'complete') {
@@ -219,10 +227,10 @@ const getCityLocationByAmap = async (): Promise<Location> => {
219
227
  longitude: lng.toString(),
220
228
  latitude: lat.toString()
221
229
  }
222
- console.log(`getCityLocationByAmap success: ${JSON.stringify(result)}`)
230
+ console.log(`getCityLocationByAMap success: ${JSON.stringify(result)}`)
223
231
  resolve(result)
224
232
  } else {
225
- console.error('getCityLocationByAmap fail')
233
+ console.error('getCityLocationByAMap fail')
226
234
  resolve(null)
227
235
  }
228
236
  })
@@ -230,9 +238,14 @@ const getCityLocationByAmap = async (): Promise<Location> => {
230
238
  }
231
239
 
232
240
  // 高德逆地址解析
233
- const getAddressByAmap = async (position: Location): Promise<string> => {
234
- console.log('getAddressByAmap start...')
241
+ const getAddressByAMap = async (position: Location): Promise<string> => {
242
+ console.log('getAddressByAMap start...')
235
243
  return new Promise((resolve, reject) => {
244
+ if (!window?.AMap) {
245
+ console.error('getAddressByAMap fail: AMap is undefinded')
246
+ resolve('')
247
+ return
248
+ }
236
249
  if (position) {
237
250
  new window.AMap.Geocoder({
238
251
  city: '',
@@ -243,10 +256,10 @@ const getAddressByAmap = async (position: Location): Promise<string> => {
243
256
  // debugger
244
257
  if (status === 'complete' && result.info === 'OK' && result?.regeocode?.formattedAddress) {
245
258
  const address = result.regeocode.formattedAddress || ''
246
- console.log(`getAddressByAmap success: ${address}`)
259
+ console.log(`getAddressByAMap success: ${address}`)
247
260
  resolve(address)
248
261
  } else {
249
- console.error(`getAddressByAmap fail: status = ${status}, result = ${result}`)
262
+ console.error(`getAddressByAMap fail: status = ${status}, result = ${result}`)
250
263
  resolve('')
251
264
  }
252
265
  })
@@ -258,10 +271,15 @@ const getAddressByAmap = async (position: Location): Promise<string> => {
258
271
  const getIPLocationByTMap = async (ip?: string): Promise<Location> => {
259
272
  console.log('getIPLocationByTMap start...')
260
273
  return new Promise((resolve, reject) => {
261
- const ipLocation = new window.TMap.service.IPLocation()
274
+ if (!window?.TMap) {
275
+ console.error('getIPLocationByTMap fail: TMap is undefinded')
276
+ resolve(null)
277
+ return
278
+ }
279
+
262
280
  const params = ip ? { ip } : {}
263
- ipLocation
264
- .locate(params)
281
+ new window.TMap.service.IPLocation()
282
+ .locate({ ...params, servicesk: mapService.secretkey })
265
283
  .then((res: any) => {
266
284
  const result = {
267
285
  longitude: res.result.location.lng.toString(),
@@ -282,15 +300,19 @@ const getIPLocationByTMap = async (ip?: string): Promise<Location> => {
282
300
  const getAddressByTMap = async (position: Location): Promise<string> => {
283
301
  console.log('getAddressByTMap start...')
284
302
  return new Promise((resolve, reject) => {
303
+ if (!window?.TMap) {
304
+ console.error('getAddressByTMap fail: TMap is undefinded')
305
+ resolve('')
306
+ return
307
+ }
308
+
285
309
  if (position) {
286
- const location = new window.TMap.LatLng(position.latitude, position.longitude)
287
- // console.log(position)
288
- // console.log(location)
289
310
  // debugger
290
311
  new window.TMap.service.Geocoder()
291
312
  .getAddress({
292
- location,
293
- getPoi: false
313
+ location: new window.TMap.LatLng(position.latitude, position.longitude),
314
+ getPoi: false,
315
+ servicesk: mapService.secretkey
294
316
  })
295
317
  .then((res: any) => {
296
318
  // console.log(res)
@@ -312,6 +334,11 @@ const getAddressByTMap = async (position: Location): Promise<string> => {
312
334
  const getLocationByBMap = async (): Promise<Location> => {
313
335
  console.log('getLocationByBMap start...')
314
336
  return new Promise((resolve, reject) => {
337
+ if (!window?.BMap) {
338
+ console.error('getLocationByBMap fail: BMap is undefinded')
339
+ resolve(null)
340
+ return
341
+ }
315
342
  new window.BMap.Geolocation().getCurrentPosition(
316
343
  async (res: any) => {
317
344
  // console.log(res)
@@ -374,6 +401,11 @@ const getLocationByBMap = async (): Promise<Location> => {
374
401
  const getCityLocationByBMap = async (): Promise<Location> => {
375
402
  console.log('getCityLocationByBMap start...')
376
403
  return new Promise((resolve, reject) => {
404
+ if (!window?.BMap) {
405
+ console.error('getCityLocationByBMap fail: BMap is undefinded')
406
+ resolve(null)
407
+ return
408
+ }
377
409
  new window.BMap.LocalCity().get(async (res: any) => {
378
410
  // console.log(res)
379
411
  // debugger
@@ -407,7 +439,12 @@ const getCityLocationByBMap = async (): Promise<Location> => {
407
439
  const getAddressByBmap = async (position: Location): Promise<string> => {
408
440
  console.log('getAddressByBmap start...')
409
441
  return new Promise(async (resolve, reject) => {
410
- // resolve('')
442
+ if (!window?.BMap) {
443
+ console.error('getAddressByBmap fail: BMap is undefinded')
444
+ resolve('')
445
+ return
446
+ }
447
+
411
448
  if (!position) {
412
449
  console.error('getAddressByBmap fail')
413
450
  resolve('')
@@ -432,6 +469,7 @@ const getAddressByBmap = async (position: Location): Promise<string> => {
432
469
  resolve(address)
433
470
  } else {
434
471
  console.error('getAddressByBmap fail')
472
+ // console.error(result)
435
473
  resolve('')
436
474
  }
437
475
  })
@@ -451,15 +489,18 @@ const getLocationPromise = async (isuseiplocarion = false): Promise<Location> =>
451
489
  }
452
490
  // location = null
453
491
 
492
+ await mapService.init()
493
+
454
494
  if (!location) {
455
495
  if (mapService.type === 'amap') {
456
- location = await getLocationByAmap()
457
- if (!location && isuseiplocarion) {
458
- location = await getCityLocationByAmap()
459
- }
496
+ location = await getLocationByAMap()
460
497
  if (!location && isuseiplocarion) {
461
- location = await getIPLocationByIpaas()
498
+ location = await getCityLocationByAMap()
462
499
  }
500
+ // 改成不使用ipaas了
501
+ // if (!location && isuseiplocarion) {
502
+ // location = await getIPLocationByIpaas()
503
+ // }
463
504
  } else if (mapService.type === 'tencent' && isuseiplocarion) {
464
505
  location = await getIPLocationByTMap()
465
506
  } else if (mapService.type === 'baidu') {
@@ -478,6 +519,20 @@ const getLocationPromise = async (isuseiplocarion = false): Promise<Location> =>
478
519
  }
479
520
  }
480
521
 
522
+ // if (location) {
523
+ // for (let i = 0, len = 5000; i < len; i++) {
524
+ // // location.latitude = (Number(location.latitude) + 0.02).toString()
525
+ // location.longitude = (Number(location.longitude) + 0.002).toString()
526
+ // await getAddress(location)
527
+ // .then((res: any) => {
528
+ // console.log(res)
529
+ // }).catch((err: any) => {
530
+ // console.error(err)
531
+ // })
532
+ // await delay(300)
533
+ // }
534
+ // }
535
+
481
536
  if (location && !location.address) {
482
537
  location.address = (await getAddress(location)) || '经纬度获取成功,但地址获取失败。'
483
538
  }
@@ -489,7 +544,6 @@ const getLocationPromise = async (isuseiplocarion = false): Promise<Location> =>
489
544
  // https://www.jianshu.com/p/559029832a67
490
545
  // 不能精确定位的情况下是否启用ip城市定位,ip定位用于不需要精确定位的场景
491
546
  async function getLocation(isuseiplocarion = false) {
492
- await mapService.init()
493
547
  // debugger
494
548
  // 缓存30秒
495
549
  if (datetime && Date.now() - datetime <= cachetime && lastLocation && !runing) {
@@ -519,12 +573,11 @@ const getAddress = async (position: Location): Promise<string> => {
519
573
 
520
574
  if (!address) {
521
575
  if (mapService.type === 'amap') {
522
- address = await getAddressByAmap(position)
523
-
524
- if (!address) {
525
- // 如果不设置安全秘钥的话 js-api的逆地址查询不成功 返回 INVALID_USER_SCODE 改成用ipaas服务查询
526
- address = await getAddressByIpaas(position)
527
- }
576
+ address = await getAddressByAMap(position)
577
+ // if (!address) {
578
+ // // 如果不设置安全秘钥的话 js-api的逆地址查询不成功 返回 INVALID_USER_SCODE 改成用ipaas服务查询
579
+ // address = await getAddressByIpaas(position)
580
+ // }
528
581
  } else if (mapService.type === 'tencent') {
529
582
  address = await getAddressByTMap(position)
530
583
  } else if (mapService.type === 'baidu') {
@@ -60,15 +60,39 @@ class TenantSetting {
60
60
  // }
61
61
  // }
62
62
 
63
+ // 高德地图key配置错误
64
+ // 1 如果是key格式不对 那么没返回promise 连window.AMap都拿不到 地图和都不能用
65
+ // 1 如果是key对式对 但key不对 有返回 window.AMap 但api定位调用失败 地图可以渲染空白地图
66
+
67
+ // // 高德地图公司key
63
68
  // tenantsetting.lbssetting.setting.type = 'amap'
64
69
  // tenantsetting.lbssetting.setting.key.web = '1993ac213d2f4675ac1bffb1b03ef1f0'
65
70
  // tenantsetting.lbssetting.setting.secretkey.web = '816fe46b7b7bce145940b93c1e4818fa'
66
71
 
67
- // // tenantsetting.lbssetting.setting.type = 'tencent'
68
- // // tenantsetting.lbssetting.setting.key.web = 'NHBBZ-K5LCQ-LF35M-2CTDP-E4OO7-AIBFT'
72
+ // // 高德地图个人key
73
+ // tenantsetting.lbssetting.setting.type = 'amap'
74
+ // tenantsetting.lbssetting.setting.key.web = 'e4d25fe4661a34198c4e6f79abe9afac'
75
+ // tenantsetting.lbssetting.setting.secretkey.web = 'a6b674affd9a3278c68602cf7ba02fcb'
76
+
77
+ // tencent地图key配置错误
78
+ // 1 如果是地图 会在地图上明确文字提示 鉴权失败,请传入正确的key
79
+ // 2 如果是调用定位等api 会返回 catch 和详细的错误信息
80
+ // getIPLocationByTMap fail: {"status":311,"message":"key格式错误","request_id":"ccedb04fd95e4f3f9cd45cfbad729d10","id":"cbm919vjdj0"}
81
+ // getIPLocationByTMap fail: {"status":190,"message":"无效的key","request_id":"b6ca9d0749eb4c91a47db9412b1253ca","id":"cbm919wxo40"}
82
+ // tenantsetting.lbssetting.setting.type = 'tencent'
83
+ // tenantsetting.lbssetting.setting.key.web = 'NHBBZ-K5LCQ-LF35M-2CTDP-E4OO7-AIBFT'
84
+ // tenantsetting.lbssetting.setting.secretkey.web = 'zowvV5I2pSxqgGb2Sgr1x62HGXbqdxT0'
85
+
86
+ // 百度地图key配置错误alert以下信息
87
+ // 您提供的密钥不是有效的百度LBS开放平台密钥,或此密钥未对本应用的百度地图JavaScriptAPI授权。您可以访问如下网址了解如何获取有效的密钥:http://lbsyun.baidu.com/apiconsole/key#。
88
+ // tenantsetting.lbssetting.setting.type = 'baidu'
89
+ // tenantsetting.lbssetting.setting.key.web = '7r3bsPeQqJ74vsxf3EOXg7C1AM4lOWA1'
90
+
91
+
69
92
 
70
- // // tenantsetting.lbssetting.setting.type = 'baidu'
71
- // // tenantsetting.lbssetting.setting.key.web = '7r3bsPeQqJ74vsxf3EOXg7C1AM4lOWA1'
93
+ // tenantsetting.lbssetting.setting.type = 'tencent'
94
+ // tenantsetting.lbssetting.setting.key.web = 'NHBBZ-K5LCQ-LF35M-2CTDP-E4OO7-AIBFT是的是的'
95
+ // tenantsetting.lbssetting.setting.secretkey.web = 'zowvV5I2pSxqgGb2Sgr1x62HGXbqdxT0对对对'
72
96
 
73
97
  // lsProxy.setItem('tenantsetting', JSON.stringify(tenantsetting))
74
98
 
@@ -99,6 +99,11 @@ export const getLocation: () => Promise<{
99
99
  [propName: string]: any
100
100
  } | null>
101
101
  export const getDistance: (p1: [number, number], p2: [number, number]) => Promise<any>
102
+ export const getAddress: (location: {
103
+ longitude: string
104
+ latitude: string
105
+ [propName: string]: any
106
+ }) => Promise<string>
102
107
  export const spuAxios: any
103
108
  export const apaasAxios: any
104
109
  export const axios: any