Class: Lhj::Command::SyncDingdingUser

Inherits:
Lhj::Command
  • Object
show all
Defined in:
lib/lhj/command/sync_dingding_user.rb

Overview

sync code to pod

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #begin_title, #initialize, #run, #stop

Constructor Details

This class inherits a constructor from Lhj::Command

Instance Method Details

#fetch_department(token, dept_id) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lhj/command/sync_dingding_user.rb', line 46

def fetch_department(token, dept_id)
  url = URI("https://oapi.dingtalk.com/topapi/v2/department/listsub?access_token=#{token}")
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = Net::HTTP::Post.new(url)
  request['Content-Type'] = 'application/json'
  request.body = { 'dept_id' => dept_id }.to_json if dept_id
  response = https.request(request)
  res_json = JSON.parse(response.body)
  res_json['result']
end

#fetch_user_ids(token, dept_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lhj/command/sync_dingding_user.rb', line 58

def fetch_user_ids(token, dept_id)
  url = URI("https://oapi.dingtalk.com/topapi/user/listid?access_token=#{token}")
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = Net::HTTP::Post.new(url)
  request['Content-Type'] = 'application/json'
  request.body = { 'dept_id' => dept_id }.to_json if dept_id
  response = https.request(request)
  res_json = JSON.parse(response.body)
  res_json['result']['userid_list']
end

#fetch_user_info(token, user_id) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lhj/command/sync_dingding_user.rb', line 70

def (token, user_id)
  url = URI("https://oapi.dingtalk.com/topapi/v2/user/get?access_token=#{token}")
  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = Net::HTTP::Post.new(url)
  request['Content-Type'] = 'application/json'
  request.body = { 'userid' => user_id }.to_json
  response = https.request(request)
  res_json = JSON.parse(response.body)
  res_json['result']
end

#handleObject



10
11
12
# File 'lib/lhj/command/sync_dingding_user.rb', line 10

def handle
  sync_data
end

#handle_dept(token, dept_id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lhj/command/sync_dingding_user.rb', line 21

def handle_dept(token, dept_id)
  dept_array = fetch_department(token, dept_id)
  dept_array.each do |d|
    dept_json = { 'dept_id' => d['dept_id'], 'name' => d['name'], 'parent_id' => d['parent_id'] }.to_json
    save_dept(dept_json)
    user_ids = fetch_user_ids(token, d['dept_id'])
    user_ids.each do |id|
      user = (token, id)
      user_json = { 'avatar' => user['avatar'],
                    'dept_id' => d['dept_id'],
                    'email' => user['email'],
                    'job_number' => user['job_number'],
                    'mobile' => user['mobile'],
                    'name' => user['name'],
                    'telephone' => user['telephone'],
                    'title' => user['title'],
                    'unionid' => user['unionid'],
                    'userid' => user['userid'],
                    'work_place' => user['work_place'] }.to_json
      save_user(user_json)
    end
    handle_dept(token, d['dept_id'])
  end
end

#save_dept(json) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/lhj/command/sync_dingding_user.rb', line 82

def save_dept(json)
  url = URI("#{Lhj::IOSRobotConfig.base_url}/dept/create")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)
  request['Content-Type'] = 'application/json'
  request.body = json
  http.request(request)
end

#save_user(json) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/lhj/command/sync_dingding_user.rb', line 91

def save_user(json)
  url = URI("#{Lhj::IOSRobotConfig.base_url}/user/create")
  http = Net::HTTP.new(url.host, url.port)
  request = Net::HTTP::Post.new(url)
  request['Content-Type'] = 'application/json'
  request.body = json
  http.request(request)
end

#sync_dataObject



14
15
16
17
18
19
# File 'lib/lhj/command/sync_dingding_user.rb', line 14

def sync_data
  key = Lhj::IOSRobotConfig.app_key
  secret = Lhj::IOSRobotConfig.app_secret
  token = Lhj::Dingtalk.get_token(key, secret)
  handle_dept(token, nil)
end