Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
weixin_authorize
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
weixin_authorize
Commits
26728259
Commit
26728259
authored
Mar 20, 2014
by
lanrion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor client.rb
parent
bfc6a7a0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
13 deletions
+41
-13
lib/weixin_authorize/client.rb
+41
-13
No files found.
lib/weixin_authorize/client.rb
View file @
26728259
...
...
@@ -10,40 +10,63 @@ module WeixinAuthorize
include
Api
::
Groups
attr_accessor
:app_id
,
:app_secret
,
:expired_at
# Time.now + expires_in
attr_accessor
:access_token
attr_accessor
:access_token
,
:redis_key
# 对于多用户微信营销平台的对接,需要把每次的expired_at, access_token保存在Redis中
# 每次使用,则可以从Redis中获取expired_at和access_token,即
# @client = WeixinAuthorize::Client.new(appid, appsecret, expired_at, access_token)
# @client = WeixinAuthorize::Client.new(appid, appsecret)
# 如果使用存在多个公众账号,请使用 务必传递: redis_key
# 获取access_token,则仍然是:@client.get_access_token 来获取
def
initialize
(
app_id
=
""
,
app_secret
=
""
,
expired_at
=
nil
,
access_token
=
nil
)
@app_id
=
app_id
@app_secret
=
app_secret
@expired_at
=
(
expired_at
.
to_i
||
Time
.
now
.
to_i
)
@access_token
=
access_token
yield
self
if
block_given?
def
initialize
(
app_id
,
app_secret
,
redis_key
=
nil
)
@app_id
=
app_id
@app_secret
=
app_secret
@expired_at
=
(
expired_at
.
to_i
||
Time
.
now
.
to_i
)
@redis_key
=
(
redis_key
||
"weixin_"
+
app_id
)
end
# return token
def
get_access_token
# 如果当前token过期时间小于现在的时间,则重新获取一次
authenticate
if
token_expired?
@access_token
end
# authenticate access_token
def
authenticate
hash_infos
=
http_get_without_token
(
"/token"
,
authenticate_options
)
self
.
access_token
=
hash_infos
[
"access_token"
]
self
.
expired_at
=
Time
.
now
.
to_i
+
hash_infos
[
"expires_in"
]
if
weixin_redis
.
nil?
http_get_access_token
else
authenticate_with_redis
end
end
def
token_expired?
@expired_at
<=
Time
.
now
.
to_i
if
weixin_redis
.
nil?
# 如果当前token过期时间小于现在的时间,则重新获取一次
@expired_at
<=
Time
.
now
.
to_i
else
weixin_redis
.
hvals
(
redis_key
).
empty?
end
end
private
def
authenticate_with_redis
if
token_expired?
http_get_access_token
weixin_redis
.
hmset
(
redis_key
,
:access_token
,
access_token
,
:expired_at
,
expired_at
)
weixin_redis
.
expireat
(
redis_key
,
expired_at
.
to_i
-
10
)
# 提前10秒超时
else
self
.
access_token
=
weixin_redis
.
hget
(
redis_key
,
"access_token"
)
self
.
expired_at
=
weixin_redis
.
hget
(
redis_key
,
"expired_at"
)
end
end
def
http_get_access_token
hash_infos
=
http_get_without_token
(
"/token"
,
authenticate_options
)
self
.
access_token
=
hash_infos
[
"access_token"
]
self
.
expired_at
=
Time
.
now
.
to_i
+
hash_infos
[
"expires_in"
]
end
def
authenticate_options
{
grant_type:
"client_credential"
,
appid:
app_id
,
secret:
app_secret
}
end
...
...
@@ -81,5 +104,10 @@ module WeixinAuthorize
"http://file.api.weixin.qq.com/cgi-bin"
end
def
weixin_redis
return
nil
if
WeixinAuthorize
.
config
.
nil?
@redis
||=
WeixinAuthorize
.
config
.
redis
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment