@smart100/spu-web-plugin 1.0.11 → 1.0.13

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.11",
3
+ "version": "1.0.13",
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,
@@ -22,9 +22,7 @@ const load = (options?: {
22
22
  }
23
23
  }
24
24
 
25
- // 不开放地址查询 改用ipaas
26
- const plugin = ['AMap.Geolocation']
27
- // plugin.push('AMap.Geocoder') // 不开放地址查询 改用ipaas
25
+ const plugin = ['AMap.Geolocation', 'AMap.Geocoder']
28
26
 
29
27
  return AMapLoader.load({
30
28
  key: AMapKey.key,
@@ -127,46 +127,51 @@ class MapService {
127
127
  }
128
128
 
129
129
  private async initAmap() {
130
- // if (this.secretkey) {
131
- // window._AMapSecurityConfig = {
132
- // securityJsCode: this.secretkey
133
- // }
134
- // }
135
-
136
- const plugin = ['AMap.Geolocation']
137
- // plugin.push('AMap.Geocoder') // 不开放地址查询 改用ipaas
138
-
139
- const AMap = await AMapLoader.load({
140
- key: this.key,
141
- version: '2.0',
142
- plugins: plugin,
143
- AMapUI: {
144
- version: '1.1',
145
- plugins: []
130
+ return new Promise(async (resolve, reject) => {
131
+ // 高德地图api初始化失败 没有返回reject 因此用超时机制检测
132
+ const time = setTimeout(() => {
133
+ console.error('initAmap fail: 请检查 AMap key 是否正确配置')
134
+ resolve(null)
135
+ }, 3000)
136
+
137
+ if (this.secretkey) {
138
+ window._AMapSecurityConfig = {
139
+ securityJsCode: this.secretkey
140
+ }
146
141
  }
142
+
143
+ const plugin = ['AMap.Geolocation', 'AMap.Geocoder']
144
+ // debugger
145
+
146
+ const AMap = await AMapLoader.load({
147
+ key: this.key,
148
+ version: '2.0',
149
+ plugins: plugin,
150
+ AMapUI: {
151
+ version: '1.1',
152
+ plugins: []
153
+ }
154
+ })
155
+ // debugger
156
+ clearTimeout(time)
157
+
158
+ window.AMap = AMap
159
+ this.AMap = window.AMap
160
+ this.MapCore = window.AMap
161
+ // console.log(window)
162
+ // console.log(window.AMap)
163
+ // console.log(window.AMapUI)
164
+ // console.log(window.AMapLoader)
165
+ // console.log(window.AMap === aaaa)
166
+ resolve(window.AMap)
147
167
  })
148
- window.AMap = AMap
149
- this.AMap = AMap
150
- this.MapCore = window.AMap
151
- // console.log(window)
152
- // console.log(window.AMap)
153
- // console.log(window.AMapUI)
154
- // console.log(window.AMapLoader)
155
- // console.log(window.AMap === aaaa)
156
168
  }
157
169
 
158
170
  private async initBaidu() {
159
- // await importJS(
160
- // `https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=${this.key}`,
161
- // 'BMap'
162
- // )
163
- // await delay(300)
164
- // function initialize() {
165
- // const mp = new BMap.Map('map')
166
- // mp.centerAndZoom(new BMap.Point(121.491, 31.233), 11)
167
- // }
168
171
  return new Promise((resolve, reject) => {
169
- window.BMAP_INITIAL_CALLBACK = () => {
172
+ window.BMAP_INITIAL_CALLBACK = (e: any) => {
173
+ // console.log(e)
174
+ // debugger
170
175
  this.BMap = window.BMap
171
176
  this.MapCore = window.BMap
172
177
  // 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,37 +238,42 @@ const getCityLocationByAmap = async (): Promise<Location> => {
230
238
  }
231
239
 
232
240
  // 高德逆地址解析
233
- const getAddressByAmap = async (position: Location): Promise<string> => {
234
- console.log('getAddressByAmap start...')
235
- console.log('getAddressByAmap changeto getAddressByIpaas')
236
- // return new Promise((resolve, reject) => {
237
- // if (position) {
238
- // new window.AMap.Geocoder({
239
- // city: '',
240
- // radius: 500
241
- // }).getAddress([position.longitude, position.latitude], (status: string, result: any) => {
242
- // if (status === 'complete' && result.info === 'OK' && result.regeocode) {
243
- // resolve(result?.regeocode?.formattedAddress || '')
244
- // } else {
245
- // console.error('getAddressByAmap fail')
246
- // resolve('')
247
- // }
248
- // })
249
- // }
250
- // })
251
- // 如果不设置安全秘钥的话 js-api的逆地址查询不成功 返回 INVALID_USER_SCODE 改成用ipaas服务查询
252
- const address = await getAddressByIpaas(position)
253
- return address
241
+ const getAddressByAMap = async (position: Location): Promise<string> => {
242
+ console.log('getAddressByAMap start...')
243
+ return new Promise((resolve, reject) => {
244
+ if (position) {
245
+ if (!window?.AMap) {
246
+ console.error('getAddressByAMap fail: AMap is undefinded')
247
+ resolve('')
248
+ return
249
+ }
250
+ new window.AMap.Geocoder({
251
+ city: '',
252
+ radius: 500
253
+ }).getAddress([position.longitude, position.latitude], (status: string, result: any) => {
254
+ // console.log(status)
255
+ // console.log(result)
256
+ // debugger
257
+ if (status === 'complete' && result.info === 'OK' && result?.regeocode?.formattedAddress) {
258
+ const address = result.regeocode.formattedAddress || ''
259
+ console.log(`getAddressByAMap success: ${address}`)
260
+ resolve(address)
261
+ } else {
262
+ console.error(`getAddressByAMap fail: status = ${status}, result = ${result}`)
263
+ resolve('')
264
+ }
265
+ })
266
+ }
267
+ })
254
268
  }
255
269
 
256
270
  // 腾讯ip定位:通过终端设备IP地址获取其当前所在地理位置,精确到市级,常用于显示当地城市天气预报、初始化用户城市等非精确定位场景。
257
271
  const getIPLocationByTMap = async (ip?: string): Promise<Location> => {
258
272
  console.log('getIPLocationByTMap start...')
259
273
  return new Promise((resolve, reject) => {
260
- const ipLocation = new window.TMap.service.IPLocation()
261
274
  const params = ip ? { ip } : {}
262
- ipLocation
263
- .locate(params)
275
+ new window.TMap.service.IPLocation()
276
+ .locate({ ...params, servicesk: mapService.secretkey })
264
277
  .then((res: any) => {
265
278
  const result = {
266
279
  longitude: res.result.location.lng.toString(),
@@ -282,14 +295,12 @@ const getAddressByTMap = async (position: Location): Promise<string> => {
282
295
  console.log('getAddressByTMap start...')
283
296
  return new Promise((resolve, reject) => {
284
297
  if (position) {
285
- const location = new window.TMap.LatLng(position.latitude, position.longitude)
286
- // console.log(position)
287
- // console.log(location)
288
298
  // debugger
289
299
  new window.TMap.service.Geocoder()
290
300
  .getAddress({
291
- location,
292
- getPoi: false
301
+ location: new window.TMap.LatLng(position.latitude, position.longitude),
302
+ getPoi: false,
303
+ servicesk: mapService.secretkey
293
304
  })
294
305
  .then((res: any) => {
295
306
  // console.log(res)
@@ -431,14 +442,14 @@ const getAddressByBmap = async (position: Location): Promise<string> => {
431
442
  resolve(address)
432
443
  } else {
433
444
  console.error('getAddressByBmap fail')
445
+ // console.error(result)
434
446
  resolve('')
435
447
  }
436
448
  })
437
449
  })
438
450
  }
439
451
 
440
- // 定位流程: 缓存 > 判断环境(APP,小程序,企微)基于环境获取定位 > 地图商高精度定位 > 地图商城市ip定位
441
- const getLocationPromise = async (): Promise<Location> => {
452
+ const getLocationPromise = async (isuseiplocarion = false): Promise<Location> => {
442
453
  let location: Location = null
443
454
 
444
455
  // 在 SPU 容器里使用 Native-API 的定位
@@ -449,29 +460,28 @@ const getLocationPromise = async (): Promise<Location> => {
449
460
  if (!location) {
450
461
  location = await getLocationByNavigator()
451
462
  }
463
+ // location = null
452
464
 
453
465
  if (!location) {
454
466
  if (mapService.type === 'amap') {
455
- location = await getLocationByAmap()
456
- // ip城市定位结果不精确 但总比定不到位好
457
- if (!location) {
458
- location = await getCityLocationByAmap()
467
+ location = await getLocationByAMap()
468
+ if (!location && isuseiplocarion) {
469
+ location = await getCityLocationByAMap()
459
470
  }
460
- } else if (mapService.type === 'tencent') {
471
+ // 改成不使用ipaas了
472
+ // if (!location && isuseiplocarion) {
473
+ // location = await getIPLocationByIpaas()
474
+ // }
475
+ } else if (mapService.type === 'tencent' && isuseiplocarion) {
461
476
  location = await getIPLocationByTMap()
462
477
  } else if (mapService.type === 'baidu') {
463
478
  location = await getLocationByBMap()
464
- // ip城市定位结果不精确 但总比定不到位好
465
- if (!location) {
479
+ if (!location && isuseiplocarion) {
466
480
  location = await getCityLocationByBMap()
467
481
  }
468
482
  }
469
483
  }
470
484
 
471
- if (!location) {
472
- location = await getIPLocationByIpaas()
473
- }
474
-
475
485
  // 开发模式下为了方便测试提供虚拟定位
476
486
  if (!location && urlquery.isvirtuallocation) {
477
487
  location = {
@@ -480,6 +490,20 @@ const getLocationPromise = async (): Promise<Location> => {
480
490
  }
481
491
  }
482
492
 
493
+ // if (location) {
494
+ // for (let i = 0, len = 5000; i < len; i++) {
495
+ // // location.latitude = (Number(location.latitude) + 0.02).toString()
496
+ // location.longitude = (Number(location.longitude) + 0.002).toString()
497
+ // await getAddress(location)
498
+ // .then((res: any) => {
499
+ // console.log(res)
500
+ // }).catch((err: any) => {
501
+ // console.error(err)
502
+ // })
503
+ // await delay(300)
504
+ // }
505
+ // }
506
+
483
507
  if (location && !location.address) {
484
508
  location.address = (await getAddress(location)) || '经纬度获取成功,但地址获取失败。'
485
509
  }
@@ -489,7 +513,8 @@ const getLocationPromise = async (): Promise<Location> => {
489
513
 
490
514
  // WGS84 GCJ-02 BD-09 坐标系
491
515
  // https://www.jianshu.com/p/559029832a67
492
- async function getLocation() {
516
+ // 不能精确定位的情况下是否启用ip城市定位,ip定位用于不需要精确定位的场景
517
+ async function getLocation(isuseiplocarion = false) {
493
518
  await mapService.init()
494
519
  // debugger
495
520
  // 缓存30秒
@@ -502,7 +527,7 @@ async function getLocation() {
502
527
  }
503
528
  // console.log('runing')
504
529
  runing = true
505
- locationPromise = getLocationPromise()
530
+ locationPromise = getLocationPromise(isuseiplocarion)
506
531
  const locationRes = await locationPromise
507
532
  runing = false
508
533
  if (locationRes) {
@@ -518,13 +543,13 @@ const getAddress = async (position: Location): Promise<string> => {
518
543
 
519
544
  let address = ''
520
545
 
521
- // 先统一用ipaas解析 因为需要储存一致的地址格式(各地图商逆地址查询的地址格式不统一)
522
- // 如果不行再按照各地图商解析
523
- address = await getAddressByIpaas(position)
524
-
525
546
  if (!address) {
526
547
  if (mapService.type === 'amap') {
527
- address = await getAddressByAmap(position)
548
+ address = await getAddressByAMap(position)
549
+ // if (!address) {
550
+ // // 如果不设置安全秘钥的话 js-api的逆地址查询不成功 返回 INVALID_USER_SCODE 改成用ipaas服务查询
551
+ // address = await getAddressByIpaas(position)
552
+ // }
528
553
  } else if (mapService.type === 'tencent') {
529
554
  address = await getAddressByTMap(position)
530
555
  } else if (mapService.type === 'baidu') {
@@ -39,38 +39,61 @@ class TenantSetting {
39
39
  }
40
40
 
41
41
  get(key: string) {
42
- const tenantsettingStr = lsProxy.getItem('tenantsetting')
43
- if (tenantsettingStr) {
44
- const tenantsetting = JSON.parse(tenantsettingStr)
42
+ // const tenantsetting = {
43
+ // lbssetting: {
44
+ // enable: '1',
45
+ // setting: {
46
+ // type: '',
47
+ // key: {
48
+ // web: '',
49
+ // android: '',
50
+ // ios: '',
51
+ // harmony: ''
52
+ // },
53
+ // secretkey: {
54
+ // web: '',
55
+ // android: '',
56
+ // ios: '',
57
+ // harmony: ''
58
+ // }
59
+ // }
60
+ // }
61
+ // }
62
+
63
+ // // 高德地图key配置错误
64
+ // // 1 如果是key格式不对 那么没返回promise 连window.AMap都拿不到 地图和都不能用
65
+ // // 1 如果是key对式对 但key不对 有返回 window.AMap 但api定位调用失败 地图可以渲染空白地图
66
+
67
+ // // // 高德地图公司key
68
+ // // tenantsetting.lbssetting.setting.type = 'amap'
69
+ // // tenantsetting.lbssetting.setting.key.web = '1993ac213d2f4675ac1bffb1b03ef1f0'
70
+ // // tenantsetting.lbssetting.setting.secretkey.web = '816fe46b7b7bce145940b93c1e4818fa'
45
71
 
46
- // tenantsetting.lbssetting = {
47
- // enable: '1',
48
- // setting: {
49
- // type: '',
50
- // key: {
51
- // web: '',
52
- // android: '',
53
- // ios: '',
54
- // harmony: ''
55
- // },
56
- // secretkey: {
57
- // web: '',
58
- // android: '',
59
- // ios: '',
60
- // harmony: ''
61
- // }
62
- // }
63
- // }
64
- // tenantsetting.lbssetting.setting.type = 'amap'
65
- // tenantsetting.lbssetting.setting.key.web = '1993ac213d2f4675ac1bffb1b03ef1f0'
66
- // tenantsetting.lbssetting.setting.secretkey.web = '816fe46b7b7bce145940b93c1e4818fa'
72
+ // // // 高德地图个人key
73
+ // // tenantsetting.lbssetting.setting.type = 'amap'
74
+ // // tenantsetting.lbssetting.setting.key.web = 'e4d25fe4661a34198c4e6f79abe9afac'
75
+ // // tenantsetting.lbssetting.setting.secretkey.web = 'a6b674affd9a3278c68602cf7ba02fcb'
67
76
 
68
- // tenantsetting.lbssetting.setting.type = 'tencent'
69
- // tenantsetting.lbssetting.setting.key.web = 'NHBBZ-K5LCQ-LF35M-2CTDP-E4OO7-AIBFT'
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'
70
85
 
71
- // tenantsetting.lbssetting.setting.type = 'baidu'
72
- // tenantsetting.lbssetting.setting.key.web = '7r3bsPeQqJ74vsxf3EOXg7C1AM4lOWA1'
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'
73
90
 
91
+ // lsProxy.setItem('tenantsetting', JSON.stringify(tenantsetting))
92
+
93
+
94
+ let tenantsettingStr = lsProxy.getItem('tenantsetting')
95
+ if (tenantsettingStr) {
96
+ const tenantsetting = JSON.parse(tenantsettingStr)
74
97
  if (key) {
75
98
  return tenantsetting[key]
76
99
  }
@@ -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