mini_program 1.4.3 → 1.4.4

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: f5043ecc58767ee31b8ccd7833282a6dcea311b5e8918da20b42388df020a208
4
- data.tar.gz: 1e6d9b5726bfdfc7f77a1edd6608f1a4e6557791855d2d4eada35a4e85678599
3
+ metadata.gz: 989d3f042ef00e840a26f6e051a1fdbf37c0a95b360debc1b7fe6684da39915b
4
+ data.tar.gz: 673745489d798f92a3f47be040f8b1c14c9a899755066e3243db0b314dd49be2
5
5
  SHA512:
6
- metadata.gz: 6740168c1a5ecf8ce2d91785e04d529741e4653699a98b2f8aae4b7269ae7faa3d94800641098f472e643e01edb772de6fcbec28a381e3269d85587a2b3dc310
7
- data.tar.gz: 94fcb924c05b597957f20685f2b8f6bb1c0c50739d866c957b0a4870cbca21c4018298bcb55dc2ef59293dd25c47258b45596a0d3d3505df0c4302d06050fcaa
6
+ metadata.gz: 2d70b971ecfc8428055cb22e2741ec7eb8f9358d55d6a5e23c8e04b219fec194009a04d6b95dc346261346937789d20a914da692634fb5dbb3bfc00d09a45864
7
+ data.tar.gz: 4bc9e581830bd4554f980fcc29823c6273b060759d59dfe71a5d1bf0388946bd0f8c5a789ecf7fc59a3badde57b82025a70b3c7d6338faaaaaa40720113cd24b
@@ -23,7 +23,8 @@ module MiniProgram
23
23
  access_token = Rails.cache.read(access_token_store_key)
24
24
 
25
25
  if access_token.present? && !fresh
26
- return MiniProgram::SuccessResult.new(data: { access_token: access_token },
26
+ return MiniProgram::ServiceResult.new(success: true,
27
+ data: { access_token: access_token },
27
28
  message: "获取 access token 成功",
28
29
  message_kind: :get_access_token_success)
29
30
  end
@@ -61,15 +62,21 @@ module MiniProgram
61
62
  result = JSON.parse(response)
62
63
 
63
64
  if result["errcode"] && result["errcode"].to_s != "0"
64
- MiniProgram::Logger.error do
65
- { action: :request_access_token, params: params, response: result }.to_json
65
+ logger.tagged "request access token" do
66
+ logger.error <<~ERROR
67
+ Get access token failed.
68
+ api: #{api}
69
+ error: #{result}
70
+ ERROR
66
71
  end
67
72
 
68
- MiniProgram::FailureResult.new(error: result,
73
+ MiniProgram::ServiceResult.new(success: false,
74
+ error: result,
69
75
  message: result["errmsg"],
70
76
  message_kind: :request_access_token_failed)
71
77
  else
72
- MiniProgram::SuccessResult.new(data: { access_token: result["access_token"] },
78
+ MiniProgram::ServiceResult.new(success: true,
79
+ data: { access_token: result["access_token"] },
73
80
  message: "请求 access token 成功",
74
81
  message_kind: :request_access_token_success)
75
82
  end
@@ -92,20 +99,25 @@ module MiniProgram
92
99
  result = JSON.parse(response)
93
100
 
94
101
  if result["errcode"] && result["errcode"].to_s != "0"
95
- MiniProgram::Logger.error do
96
- { action: :login, params: params, response: result }.to_json
97
- end
102
+ logger.tagged "login mini program"
103
+ logger.error <<~ERROR
104
+ Get session key failed.
105
+ api: #{api}
106
+ result: #{result}
107
+ ERROR
98
108
 
99
- MiniProgram::FailureResult.new(error: result,
109
+ MiniProgram::ServiceResult.new(success: false,
110
+ error: result,
100
111
  message: result["errmsg"],
101
112
  message_kind: :login_failed)
102
113
 
103
114
  else
104
- MiniProgram::SuccessResult.new(data: result,
115
+ MiniProgram::ServiceResult.new(success: true,
116
+ data: result,
105
117
  message: "小程序登录成功",
106
118
  message_kind: :login_success)
107
-
108
119
  end
120
+
109
121
  end
110
122
 
111
123
  # 发送订阅消息
@@ -125,22 +137,26 @@ module MiniProgram
125
137
  api = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=#{get_token_result["access_token"]}"
126
138
  result = post(api, payload)
127
139
 
128
- if result["errcode"].to_s == "0"
129
- MiniProgram::Logger.info do
130
- { action: :send_subscribed_message, params: payload, response: result }.to_json
140
+ if result["errcode"].to_s != "0"
141
+ logger.tagged "Subscribe Message" do
142
+ logger.error { "{params: #{payload}, response: #{result}}" }
131
143
  end
132
144
 
133
- MiniProgram::SuccessResult.new(data: { msgid: result.data["msgid"] },
134
- message: "发送订阅消息成功",
135
- message_kind: :send_subscribed_message_success)
145
+ MiniProgram::ServiceResult.new(success: false,
146
+ error: result,
147
+ message: "发送订阅消息失败",
148
+ message_kind: :send_subscribed_message_failed)
136
149
  else
137
- MiniProgram::Logger.error do
138
- { action: :send_subscribed_message, params: payload, response: result }.to_json
150
+ logger.tagged "Subscribe Message" do
151
+ logger.info { "{params: #{payload}, response: #{result}}" }
139
152
  end
140
153
 
141
- MiniProgram::FailureResult.new(error: result,
142
- message: "发送订阅消息失败",
143
- message_kind: :send_subscribed_message_failed)
154
+ MiniProgram::ServiceResult.new(success: true,
155
+ data: {
156
+ msgid: result.data["msgid"]
157
+ },
158
+ message: "发送订阅消息成功",
159
+ message_kind: :send_subscribed_message_success)
144
160
  end
145
161
 
146
162
  end
@@ -165,28 +181,18 @@ module MiniProgram
165
181
  mp_template_msg: payload
166
182
  })
167
183
 
168
- if result["errcode"].to_s == "0"
169
- message = "发送统一服务消息成功"
170
-
171
- MiniProgram::Logger.info do
172
- { action: :send_uniform_message, message: message, params: payload, response: result }.to_json
173
- end
174
-
175
- MiniProgram::SuccessResult.new(data: result,
176
- message: message,
177
- message_kind: :send_uniform_message_success)
178
-
179
- else
180
- message = "发送统一服务消息失败"
181
-
182
- MiniProgram::Logger.error do
183
- { action: :send_uniform_message, message: message, params: payload, response: result }.to_json
184
- end
185
-
186
- MiniProgram::FailureResult.new(error: result["errmsg"],
187
- message: message,
184
+ if result["errcode"].to_s != "0"
185
+ MiniProgram::ServiceResult.new(success: false,
186
+ error: result,
187
+ message: "发送统一服务消息失败",
188
188
  message_kind: :send_uniform_message_failed)
189
+ else
190
+ MiniProgram::ServiceResult.new(success: true,
191
+ data: result,
192
+ message: "发送统一服务消息成功",
193
+ message_kind: :send_uniform_message_success)
189
194
  end
195
+
190
196
  end
191
197
 
192
198
  # 获取用户手机号
@@ -201,24 +207,19 @@ module MiniProgram
201
207
 
202
208
  phone_num = JSON.parse(data)["phoneNumber"]
203
209
 
204
- MiniProgram::SuccessResult.new(
210
+ MiniProgram::ServiceResult.new(
211
+ success: true,
205
212
  message: "解密手机号成功",
206
213
  message_kind: :decrypt_phone_number_success,
207
214
  data: {
208
215
  openid: openid,
209
216
  phone_num: phone_num
210
- }
211
- )
217
+ })
212
218
  rescue OpenSSL::Cipher::CipherError, ArgumentError => e
213
- message = "解密手机号失败"
214
-
215
- MiniProgram::Logger.error do
216
- { action: "decrypt_phone_num", message: message, error: e }.to_json
217
- end
218
-
219
- MiniProgram::FailureResult.new(
220
- message: message,
221
- message_kind: :decrypt_phone_number_failed,
219
+ MiniProgram::ServiceResult.new(
220
+ success: false,
221
+ message: "解密手机号失败",
222
+ message_kind: :decrypt_phone_data_failed,
222
223
  error: e
223
224
  )
224
225
  end
@@ -244,25 +245,18 @@ module MiniProgram
244
245
  end
245
246
 
246
247
  api = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=#{get_token_result["access_token"]}"
247
-
248
248
  params = { scene:, page:, width:, check_path:, env_version: }
249
249
 
250
250
  result = post(api, params)
251
251
 
252
252
  if result["errcode"]
253
- MiniProgram::Logger.error do
254
- { action: 'generate_code', params: params, response: result }.to_json
255
- end
256
-
253
+ logger.error { "{ action: 'generate qrcode', params: #{params}, response: #{result}}" }
257
254
  MiniProgram::ServiceResult.new(success: false,
258
255
  error: result,
259
256
  message: "获取二维码失败",
260
257
  message_kind: :get_unlimited_qrcode_failed)
261
258
  else
262
- MiniProgram::Logger.info do
263
- { action: 'generate_code', params: params, response: result }.to_json
264
- end
265
-
259
+ logger.info { "{ action: 'generate qrcode', params: #{params}, response: #{result} }" }
266
260
  MiniProgram::ServiceResult.new(success: true,
267
261
  data: { image: result },
268
262
  message: "获取二维码成功",
@@ -310,9 +304,17 @@ module MiniProgram
310
304
  aes.update(Base64::decode64(encrypted_data)) + aes.final
311
305
  end
312
306
 
307
+ def logger
308
+ @logger ||= MiniProgram::RLogger.make("mini_program")
309
+ end
310
+
313
311
  def access_token_store_key
314
312
  @access_token_store_key ||= "mp-#{appid}-access-token"
315
313
  end
316
314
 
315
+ def msg_logger
316
+ @msg_logger ||= MiniProgram::RLogger.make("wx_msg")
317
+ end
318
+
317
319
  end
318
320
  end
@@ -0,0 +1,34 @@
1
+ module MiniProgram
2
+ class RLogger
3
+ def initialize
4
+ raise Exceptions::InitializeDenied.new("please use 'RLogger.make' instead of 'RLogger.new'")
5
+ end
6
+
7
+ class << self
8
+ def make(given_filename)
9
+ filename = log_filename(given_filename)
10
+
11
+ # 如果已经存在日志对象,则返回已有的日志对象
12
+ logger = Logger.new(filename)
13
+ ActiveSupport::TaggedLogging.new(logger)
14
+ end
15
+
16
+ def log_filename(given_filename)
17
+ return STDOUT if ENV["RAILS_LOG_TO_STDOUT"].present?
18
+
19
+ if given_filename.class.in? [String, Symbol]
20
+ unless given_filename.end_with? ".log"
21
+ given_filename = "#{given_filename}.log"
22
+ end
23
+
24
+ Rails.root.join("log/#{given_filename}")
25
+ elsif given_filename.respond_to? :to_path
26
+ given_filename.to_path
27
+ else
28
+ raise Exception::UnsupportedParamType.new("\"log filename parameter must be a String type or a Symbol\"")
29
+ end
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -10,11 +10,11 @@ module MiniProgram
10
10
  delegate :[], :[]=, to: :data
11
11
 
12
12
  def initialize(success: false,
13
- data: {},
14
13
  error: nil,
15
14
  message: nil,
16
15
  message_type: nil,
17
- message_kind: nil)
16
+ message_kind: nil,
17
+ data: {})
18
18
 
19
19
  self.success = success
20
20
  self.data = (data.presence || {}).with_indifferent_access
@@ -40,32 +40,24 @@ module MiniProgram
40
40
  self
41
41
  end
42
42
 
43
- def message_type
44
- if @message_type.present?
45
- @message_type.to_sym
43
+ def get_message_type
44
+ if message_type.present?
45
+ message_type.to_sym
46
46
  elsif success?
47
- :info
47
+ :notice
48
48
  else
49
49
  :error
50
50
  end
51
51
  end
52
52
 
53
- def message_kind_prefix
54
- "mini_program_"
55
- end
56
-
57
- def message_kind
58
- "#{message_kind_prefix}#{@message_kind}"
59
- end
60
-
61
53
  def as_json(options = nil)
62
54
  {
63
- success: success,
64
- data: data,
65
- message: message,
66
- message_type: message_type,
67
- message_kind: message_kind,
68
- error: error,
55
+ success: ,
56
+ data: ,
57
+ message:,
58
+ message_type: get_message_type,
59
+ message_kind:,
60
+ error:,
69
61
  }
70
62
  end
71
63
  end
@@ -1,3 +1,3 @@
1
1
  module MiniProgram
2
- VERSION = '1.4.3'
2
+ VERSION = '1.4.4'
3
3
  end
data/lib/mini_program.rb CHANGED
@@ -4,10 +4,8 @@ require "mini_program/client"
4
4
  require "mini_program/msg"
5
5
  require "mini_program/user"
6
6
  require "application_controller_ext"
7
+ require "mini_program/r_logger"
7
8
  require "mini_program/service_result"
8
- require "mini_program/success_result"
9
- require "mini_program/failure_result"
10
- require "mini_program/logger"
11
9
  require "mocha"
12
10
 
13
11
  module MiniProgram
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_program
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.3
4
+ version: 1.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-17 00:00:00.000000000 Z
11
+ date: 2022-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -74,12 +74,10 @@ files:
74
74
  - lib/mini_program.rb
75
75
  - lib/mini_program/client.rb
76
76
  - lib/mini_program/engine.rb
77
- - lib/mini_program/failure_result.rb
78
- - lib/mini_program/logger.rb
79
77
  - lib/mini_program/msg.rb
78
+ - lib/mini_program/r_logger.rb
80
79
  - lib/mini_program/request_result.rb
81
80
  - lib/mini_program/service_result.rb
82
- - lib/mini_program/success_result.rb
83
81
  - lib/mini_program/user.rb
84
82
  - lib/mini_program/version.rb
85
83
  - lib/tasks/mini_program_tasks.rake
@@ -98,14 +96,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
96
  requirements:
99
97
  - - ">="
100
98
  - !ruby/object:Gem::Version
101
- version: '0'
99
+ version: 3.0.0
102
100
  required_rubygems_version: !ruby/object:Gem::Requirement
103
101
  requirements:
104
102
  - - ">="
105
103
  - !ruby/object:Gem::Version
106
104
  version: '0'
107
105
  requirements: []
108
- rubygems_version: 3.3.7
106
+ rubygems_version: 3.3.13
109
107
  signing_key:
110
108
  specification_version: 4
111
109
  summary: a engine for develop mini program
@@ -1,11 +0,0 @@
1
-
2
- class MiniProgram::FailureResult < MiniProgram::ServiceResult
3
- def initialize(data: {}, error: nil, message: nil, message_type: nil, message_kind: nil)
4
- super(success: false,
5
- data: data,
6
- error: error,
7
- message: message,
8
- message_type: message_type,
9
- message_kind: message_kind)
10
- end
11
- end
@@ -1,18 +0,0 @@
1
- class MiniProgram::Logger
2
-
3
- def self.tags
4
- ["MiniProgram"]
5
- end
6
-
7
- %w{ info warn error fatal }.each do |level|
8
- define_singleton_method level do |content = "", &block|
9
- Rails.logger.tagged *tags do
10
- if block.present?
11
- Rails.logger.send(level, &block)
12
- else
13
- Rails.logger.send(level, content)
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,11 +0,0 @@
1
-
2
- class MiniProgram::SuccessResult < MiniProgram::ServiceResult
3
- def initialize(data: {}, error: nil, message: nil, message_type: nil, message_kind: nil)
4
- super(success: true,
5
- data: data,
6
- error: error,
7
- message: message,
8
- message_type: message_type,
9
- message_kind: message_kind)
10
- end
11
- end