Module: QqMapHelper

Extended by:
QqMapHelper
Included in:
QqMapHelper
Defined in:
app/models/concerns/qq_map_helper.rb

Constant Summary collapse

KEY =
Rails.application.credentials.dig(:qq_map, :ws)

Instance Method Summary collapse

Instance Method Details

#districtsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/concerns/qq_map_helper.rb', line 37

def districts
  url = 'https://apis.map.qq.com/ws/district/v1/list'
  body = {
    key: KEY
  }

  r = HTTPX.get(url, params: body)
  result = JSON.parse(r.to_s)
  if result['status'] == 0
    result['result']
  else
    result
  end
end

#geocoder(lat:, lng:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/concerns/qq_map_helper.rb', line 5

def geocoder(lat:, lng:)
  url = 'https://apis.map.qq.com/ws/geocoder/v1'
  body = {
    key: KEY,
    location: [lat, lng].join(',')
  }

  r = HTTPX.get(url, params: body)
  result = JSON.parse(r.to_s)
  if result['status'] == 0
    result['result']
  else
    result
  end
end

#ip(ip) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/concerns/qq_map_helper.rb', line 21

def ip(ip)
  url = 'https://apis.map.qq.com/ws/location/v1/ip'
  body = {
    key: KEY,
    ip: ip
  }

  r = HTTPX.get(url, params: body)
  result = JSON.parse(r.to_s)
  if result['status'] == 0
    result['result']
  else
    result
  end
end

#sync_to_areasObject



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
# File 'app/models/concerns/qq_map_helper.rb', line 52

def sync_to_areas
  results = districts
  results[0].each do |result|
    area = Profiled::Area.find_or_initialize_by(name: result['name'])
    area.full = result['fullname']
    area.code = result['id']
    area.save
  end

  results[1].each do |result|
    area = Profiled::Area.find_or_initialize_by(name: result['name'])
    parent = Profiled::Area.find_by(code: "#{result['id'][0..1]}0000")
    area.parent = parent
    area.full = result['fullname']
    area.code = result['id']
    area.save
  end

  results[2].each do |result|
    area = Profiled::Area.find_or_initialize_by(name: result['fullname'])
    parent = Profiled::Area.find_by(code: "#{result['id'][0..3]}00")
    area.parent = parent
    area.full = result['fullname']
    area.code = result['id']
    area.save
  end
end