lhj-tools 0.1.72 → 0.1.74

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1414ebd1811cbc9f773ed21506171acd5aa5399a5b9a9d3a74634ff8ef93e882
4
- data.tar.gz: e0b5c1dab6b128c8e8a8518f8b38c4d0e3c3b4de8cd4c61746591fc602878c5d
3
+ metadata.gz: f86764f14d88348e59221a4e339765a6ec4488a815d179a710d15e7c415471a1
4
+ data.tar.gz: 9f5635a3e149ce527aea1c298fa7a39316c446e75aaed941d23c6010ce0896de
5
5
  SHA512:
6
- metadata.gz: d0c41abe42ed5812635dc7c9a235efb9c2bce06054aba7c977ab82f7693d04e38151e91f68b8e78f22efb1d7d69ba502cca7c5024a74ca1f3364e089d4e1cf0d
7
- data.tar.gz: 989dd03e74941a8b10623179c785367db74d8b615bac7e84d564718c0cacffb5b545ceb8d565a63687c440153e5a5493c0f8e6e1821b19e959f97da0b1f8f65e
6
+ metadata.gz: 144bfb6d35d9190b81ed1715dd518c2b707971815a3412f279f673faffb4c6dd36768c05c86da1249d9eed7328620a0588fe62a3753b249fd9fd8625726bb0d3
7
+ data.tar.gz: 6d875c726247374420c5b2856614283a817078e073517a3dc5849b7a9258567eefffc9df76f3d54ec58836f647ef922ea6169c5fe9211d53630ab39a2fe78570
@@ -13,6 +13,9 @@ module Lhj
13
13
  self.summary = '通过yapi接口生成请求'
14
14
  self.description = '执行`lhj api`生成接口模型'
15
15
 
16
+ API_INTERFACE_URL = 'api/interface/get?id='.freeze
17
+ API_PROJECT_URL = 'api/project/get?id='.freeze
18
+
16
19
  def self.options
17
20
  [
18
21
  %w[--id api的id],
@@ -39,7 +42,7 @@ module Lhj
39
42
  @config_model_name = 'Result'
40
43
  @model_default_suffix = 'Model'
41
44
  @type_trans = {}
42
- @http_url = ''
45
+ @base_url = 'http://yapi.xx.com'
43
46
  super
44
47
  end
45
48
 
@@ -49,13 +52,16 @@ module Lhj
49
52
 
50
53
  def process(config_file)
51
54
  load_config(config_file)
52
- res_body = req_api_model
55
+ res_body = get_interface_api_model
53
56
  unless res_body['errcode'].to_i.zero?
54
57
  puts '请执行 lhj yapi-init 初始化配置'.red
55
58
  return
56
59
  end
57
60
  return unless res_body && !res_body.empty?
58
61
 
62
+ # get project info
63
+ project_id = res_body['data']['project_id']
64
+ project_info = get_project_info(project_id)
59
65
  # 1.print request result
60
66
  result_model_name = print_res_body_model(res_body)
61
67
  # 2.print request json body
@@ -63,10 +69,9 @@ module Lhj
63
69
  # 3.print request param
64
70
  print_req_query(res_body['data'])
65
71
  # 4.print request method
66
- service_code = print_http_method(res_body['data'], result_model_name, param_model_name)
72
+ service_code = print_http_method(res_body['data'], project_info, result_model_name, param_model_name)
67
73
  # 5.save to file
68
74
  file_map = save_to_file(service_code) if @save
69
- puts file_map
70
75
  # 6.push to git
71
76
  push_to_git if @sync
72
77
  # 7.notify robot
@@ -101,7 +106,7 @@ module Lhj
101
106
 
102
107
  def save_to_file(service_code)
103
108
  name = model_name
104
- file_name = gen_model_name('')
109
+ file_name = gen_model_name
105
110
  unless File.exist?(File.expand_path(sub_folder_name, '.'))
106
111
  FileUtils.mkdir_p(File.expand_path(sub_folder_name, '.'))
107
112
  end
@@ -129,8 +134,12 @@ module Lhj
129
134
  Lhj::Dingtalk.post_message_robot(robot_url, '生成代码', output)
130
135
  end
131
136
 
132
- def url_str
133
- "#{@http_url}#{api_id}"
137
+ def interface_url_str
138
+ "#{@base_url}/#{API_INTERFACE_URL}#{api_id}"
139
+ end
140
+
141
+ def project_url_str(project_id)
142
+ "#{@base_url}/#{API_PROJECT_URL}#{project_id}"
134
143
  end
135
144
 
136
145
  def yml_file
@@ -142,7 +151,7 @@ module Lhj
142
151
  config.each do |k, v|
143
152
  @http_headers << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
144
153
  end
145
- @http_url = config['url']
154
+ @base_url = config['base_url']
146
155
  @config_id = config['id']
147
156
  @config_model_pre = config['model_pre']
148
157
  @config_model_suffix = config['model_suffix']
@@ -162,12 +171,16 @@ module Lhj
162
171
  @model_result_name || @config_model_name
163
172
  end
164
173
 
174
+ def req_model_name
175
+ 'Param'
176
+ end
177
+
165
178
  def model_suffix
166
179
  @config_model_suffix || @model_default_suffix
167
180
  end
168
181
 
169
- def req_api_model
170
- uri = URI.parse(url_str)
182
+ def get_interface_api_model
183
+ uri = URI.parse(interface_url_str)
171
184
  req = Net::HTTP::Get.new(uri)
172
185
  req['Cookie'] = @http_headers.join('; ')
173
186
  res = Net::HTTP.start(uri.hostname, uri.port) do |http|
@@ -179,6 +192,17 @@ module Lhj
179
192
  res_json
180
193
  end
181
194
 
195
+ def get_project_info(project_id)
196
+ url = project_url_str(project_id)
197
+ uri = URI.parse(url)
198
+ req = Net::HTTP::Get.new(uri)
199
+ req['Cookie'] = @http_headers.join('; ')
200
+ res = Net::HTTP.start(uri.hostname, uri.port) do |http|
201
+ http.request(req)
202
+ end
203
+ JSON.parse(res.body)
204
+ end
205
+
182
206
  def print_res_body_model(res_json)
183
207
  res_body = fetch_res_boy(res_json)
184
208
  return unless res_body
@@ -228,7 +252,7 @@ module Lhj
228
252
  result = res_body['properties']['detailMsg']
229
253
  return unless result['type'] == 'object' || result['type'] == 'array'
230
254
 
231
- result['name'] = gen_model_name('')
255
+ result['name'] = gen_model_name
232
256
  result
233
257
  end
234
258
 
@@ -236,14 +260,18 @@ module Lhj
236
260
  return if !res_json || !res_json['data'] || !res_json['data']['req_body_other']
237
261
 
238
262
  result = JSON.parse(res_json['data']['req_body_other'])
239
- result['name'] = gen_model_name('')
263
+ result['name'] = gen_model_name(nil, :req)
240
264
  result
241
265
  end
242
266
 
243
- def gen_model_name(name)
244
- n = name.gsub(/vo|model|list/i, '').gsub(/(.*)s$/, '\1').gsub(/^\w/) { Regexp.last_match(0).upcase }
245
- n = model_name if n.length <= 0
246
- "#{model_pre}#{n}#{model_suffix}"
267
+ def gen_model_name(property_name = nil, type = :res)
268
+ name = model_name
269
+ name = req_model_name if type == :req
270
+ unless property_name.nil?
271
+ name = property_name.gsub(/vo|model|list/i, '').gsub(/(.*)s$/, '\1').gsub(/^\w/) { Regexp.last_match(0).upcase }
272
+ name = property_name.gsub(/^\w/) { Regexp.last_match(0).upcase } if name.length <= 0
273
+ end
274
+ "#{model_pre}#{name}#{model_suffix}"
247
275
  end
248
276
 
249
277
  def handle_model(model, &block)
@@ -394,11 +422,13 @@ module Lhj
394
422
  end
395
423
  end
396
424
 
397
- def print_http_method(data, result_model_name, param_model_name)
425
+ def print_http_method(data, project_info, result_model_name, param_model_name)
398
426
  return unless data
399
427
 
400
428
  path = data['path']
401
- path_name = path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { Regexp.last_match(0).upcase } }.join('') if path
429
+ if path
430
+ path_name = path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { Regexp.last_match(0).upcase } }.join('')
431
+ end
402
432
  path_key = "k#{path_name}URL"
403
433
  mth = data['method']
404
434
  mth = 'JSON' if data['req_body_is_json_schema']
@@ -412,7 +442,7 @@ module Lhj
412
442
  yapi_vars = { title: data['title'],
413
443
  desc: data['desc'],
414
444
  username: data['username'],
415
- path: path,
445
+ path: "#{project_info['data']['basepath']}#{path}",
416
446
  path_key: path_key,
417
447
  path_name: path_name,
418
448
  result_model_name: result_model_name,
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lhj
4
4
  module Tools
5
- VERSION = "0.1.72"
5
+ VERSION = "0.1.74"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhj-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.72
4
+ version: 0.1.74
5
5
  platform: ruby
6
6
  authors:
7
7
  - lihaijian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-09 00:00:00.000000000 Z
11
+ date: 2022-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xcodeproj