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
cc783b25
Commit
cc783b25
authored
Aug 15, 2015
by
lanrion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
部分参数命名重构
parent
1afe8f3b
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
42 additions
and
36 deletions
+42
-36
README.md
+4
-0
lib/weixin_authorize.rb
+5
-6
lib/weixin_authorize/api/mass.rb
+18
-16
lib/weixin_authorize/api/user.rb
+2
-2
lib/weixin_authorize/client.rb
+6
-6
lib/weixin_authorize/handler/exceptions.rb
+2
-4
lib/weixin_authorize/token/redis_store.rb
+5
-2
No files found.
README.md
View file @
cc783b25
...
@@ -13,6 +13,10 @@ Support using [Redis](http://redis.io) to store `access_token`
...
@@ -13,6 +13,10 @@ Support using [Redis](http://redis.io) to store `access_token`
[
JS SDK
](
https://github.com/lanrion/weixin_authorize/wiki/js-sdk
)
[
JS SDK
](
https://github.com/lanrion/weixin_authorize/wiki/js-sdk
)
## 支持自助实现API
详情见:https://github.com/lanrion/weixin_authorize/wiki/diy-your-api
## 已经完成API
## 已经完成API
*
客服消息
*
客服消息
...
...
lib/weixin_authorize.rb
View file @
cc783b25
...
@@ -30,24 +30,23 @@ module WeixinAuthorize
...
@@ -30,24 +30,23 @@ module WeixinAuthorize
OK_MSG
=
"ok"
.
freeze
OK_MSG
=
"ok"
.
freeze
OK_CODE
=
0
.
freeze
OK_CODE
=
0
.
freeze
GRANT_TYPE
=
"client_credential"
.
freeze
GRANT_TYPE
=
"client_credential"
.
freeze
# 用于标记endpoint可以直接使用url作为完整请求API
# 用于标记endpoint可以直接使用url作为完整请求API
CUSTOM_ENDPOINT
=
"custom_endpoint"
.
freeze
CUSTOM_ENDPOINT
=
"custom_endpoint"
.
freeze
class
<<
self
class
<<
self
def
http_get_without_token
(
url
,
header
s
=
{},
endpoint
=
"plain"
)
def
http_get_without_token
(
url
,
url_param
s
=
{},
endpoint
=
"plain"
)
get_api_url
=
endpoint_url
(
endpoint
,
url
)
get_api_url
=
endpoint_url
(
endpoint
,
url
)
load_json
(
resource
(
get_api_url
).
get
(
params:
header
s
))
load_json
(
resource
(
get_api_url
).
get
(
params:
url_param
s
))
end
end
def
http_post_without_token
(
url
,
p
ayload
=
{},
header
s
=
{},
endpoint
=
"plain"
)
def
http_post_without_token
(
url
,
p
ost_body
=
{},
url_param
s
=
{},
endpoint
=
"plain"
)
post_api_url
=
endpoint_url
(
endpoint
,
url
)
post_api_url
=
endpoint_url
(
endpoint
,
url
)
# to json if invoke "plain"
# to json if invoke "plain"
if
endpoint
==
"plain"
||
endpoint
==
CUSTOM_ENDPOINT
if
endpoint
==
"plain"
||
endpoint
==
CUSTOM_ENDPOINT
p
ayload
=
JSON
.
dump
(
payload
)
p
ost_body
=
JSON
.
dump
(
post_body
)
end
end
load_json
(
resource
(
post_api_url
).
post
(
p
ayload
,
params:
header
s
))
load_json
(
resource
(
post_api_url
).
post
(
p
ost_body
,
params:
url_param
s
))
end
end
def
resource
(
url
)
def
resource
(
url
)
...
...
lib/weixin_authorize/api/mass.rb
View file @
cc783b25
...
@@ -3,12 +3,12 @@ module WeixinAuthorize
...
@@ -3,12 +3,12 @@ module WeixinAuthorize
module
Api
module
Api
module
Mass
module
Mass
MSG_TYPE
=
[
"mpnews"
,
"image"
,
"text"
,
"voice"
,
"mpvideo"
]
MSG_TYPE
=
[
"mpnews"
,
"image"
,
"text"
,
"voice"
,
"mpvideo"
]
.
freeze
# media_info= {"media_id" media_id}
# media_info= {"media_id" media_id}
# https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN
# https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN
def
mass_with_group
(
group_id
,
media_info
,
msgtype
=
"mpnews"
,
is_to_all
=
false
)
def
mass_with_group
(
group_id
,
media_info
,
msgtype
=
"mpnews"
,
is_to_all
=
false
)
group_option
=
{
"filter"
=>
{
"group_id"
=>
group_id
.
to_s
,
"is_to_all"
=>
is_to_all
}}
group_option
=
{
filter:
{
group_id:
group_id
,
is_to_all:
is_to_all
}}
media
=
generate_media
(
msgtype
,
media_info
,
group_option
)
media
=
generate_media
(
msgtype
,
media_info
,
group_option
)
mass_url
=
"
#{
mass_base_url
}
/sendall"
mass_url
=
"
#{
mass_base_url
}
/sendall"
...
@@ -19,7 +19,7 @@ module WeixinAuthorize
...
@@ -19,7 +19,7 @@ module WeixinAuthorize
# if mpvideo,
# if mpvideo,
# media_info= {"media_id" => media_id, "title" => "title", "description" => "description"}
# media_info= {"media_id" => media_id, "title" => "title", "description" => "description"}
def
mass_with_openids
(
openids
,
media_info
,
msgtype
=
"mpnews"
)
def
mass_with_openids
(
openids
,
media_info
,
msgtype
=
"mpnews"
)
openid_option
=
{
"touser"
=>
openids
}
openid_option
=
{
touser:
openids
}
media
=
generate_media
(
msgtype
,
media_info
,
openid_option
)
media
=
generate_media
(
msgtype
,
media_info
,
openid_option
)
mass_url
=
"
#{
mass_base_url
}
/send"
mass_url
=
"
#{
mass_base_url
}
/send"
http_post
(
mass_url
,
media
)
http_post
(
mass_url
,
media
)
...
@@ -29,13 +29,13 @@ module WeixinAuthorize
...
@@ -29,13 +29,13 @@ module WeixinAuthorize
# 另外,删除群发消息只能删除图文消息和视频消息,其他类型的消息一经发送,无法删除。
# 另外,删除群发消息只能删除图文消息和视频消息,其他类型的消息一经发送,无法删除。
def
mass_delete_with_msgid
(
msg_id
)
def
mass_delete_with_msgid
(
msg_id
)
mass_url
=
"
#{
mass_base_url
}
/delete"
mass_url
=
"
#{
mass_base_url
}
/delete"
http_post
(
mass_url
,
{
"msg_id"
=>
msg_id
})
http_post
(
mass_url
,
{
msg_id:
msg_id
})
end
end
# 预览接口【订阅号与服务号认证后均可用】
# 预览接口【订阅号与服务号认证后均可用】
def
mass_preview
(
openid
,
media_info
,
msgtype
=
"mpnews"
)
def
mass_preview
(
openid
,
media_info
,
msg
_
type
=
"mpnews"
)
openid_option
=
{
"touser"
=>
openid
}
openid_option
=
{
touser:
openid
}
media
=
generate_media
(
msgtype
,
media_info
,
openid_option
)
media
=
generate_media
(
msg
_
type
,
media_info
,
openid_option
)
mass_url
=
"
#{
mass_base_url
}
/preview"
mass_url
=
"
#{
mass_base_url
}
/preview"
http_post
(
mass_url
,
media
)
http_post
(
mass_url
,
media
)
end
end
...
@@ -52,22 +52,24 @@ module WeixinAuthorize
...
@@ -52,22 +52,24 @@ module WeixinAuthorize
"/message/mass"
"/message/mass"
end
end
def
generate_media
(
msgtype
,
media_info
,
option
)
def
generate_media
(
msg_type
,
media_info
,
option
)
msgtype
=
msgtype
.
to_s
msg_type
=
msg_type
.
to_s
raise
"
#{
msgtype
}
is a valid msgtype"
if
not
MSG_TYPE
.
include?
(
msgtype
)
if
not
MSG_TYPE
.
include?
(
msg_type
)
raise
MediaTypeException
,
"
#{
msg_type
}
is a valid msg_type"
end
{
{
msg
type
=>
convert_media_info
(
msg
type
,
media_info
),
msg
_type
=>
convert_media_info
(
msg_
type
,
media_info
),
"msgtype"
=>
msgtype
"msgtype"
=>
msg
_
type
}.
merge
(
option
)
}.
merge
(
option
)
end
end
# 如果用户填写的media信息,是字符串,则转换来符合的数据结构,如果 是hash,则直接使用用户的结构。
# 如果用户填写的media信息,是字符串,则转换来符合的数据结构,如果 是hash,则直接使用用户的结构。
def
convert_media_info
(
msgtype
,
media_info
)
def
convert_media_info
(
msg
_
type
,
media_info
)
if
media_info
.
is_a?
(
String
)
if
media_info
.
is_a?
(
String
)
if
msgtype
==
"text"
if
msg
_
type
==
"text"
return
{
"content"
=>
media_info
}
return
{
content:
media_info
}
else
else
return
{
"media_id"
=>
media_info
}
return
{
media_id:
media_info
}
end
end
end
end
media_info
media_info
...
...
lib/weixin_authorize/api/user.rb
View file @
cc783b25
...
@@ -29,11 +29,11 @@ module WeixinAuthorize
...
@@ -29,11 +29,11 @@ module WeixinAuthorize
# }
# }
def
update_remark
(
openid
,
remark
)
def
update_remark
(
openid
,
remark
)
update_url
=
"/user/info/updateremark"
update_url
=
"/user/info/updateremark"
p
ayload
=
{
p
ost_body
=
{
openid:
openid
,
openid:
openid
,
remark:
remark
remark:
remark
}
}
http_post
(
update_url
,
p
ayload
)
http_post
(
update_url
,
p
ost_body
)
end
end
private
private
...
...
lib/weixin_authorize/client.rb
View file @
cc783b25
...
@@ -66,14 +66,14 @@ module WeixinAuthorize
...
@@ -66,14 +66,14 @@ module WeixinAuthorize
end
end
# 暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。
# 暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。
def
http_get
(
url
,
header
s
=
{},
endpoint
=
"plain"
)
def
http_get
(
url
,
url_param
s
=
{},
endpoint
=
"plain"
)
headers
=
header
s
.
merge
(
access_token_param
)
url_params
=
url_param
s
.
merge
(
access_token_param
)
WeixinAuthorize
.
http_get_without_token
(
url
,
header
s
,
endpoint
)
WeixinAuthorize
.
http_get_without_token
(
url
,
url_param
s
,
endpoint
)
end
end
def
http_post
(
url
,
p
ayload
=
{},
header
s
=
{},
endpoint
=
"plain"
)
def
http_post
(
url
,
p
ost_body
=
{},
url_param
s
=
{},
endpoint
=
"plain"
)
headers
=
access_token_param
.
merge
(
header
s
)
url_params
=
access_token_param
.
merge
(
url_param
s
)
WeixinAuthorize
.
http_post_without_token
(
url
,
p
ayload
,
header
s
,
endpoint
)
WeixinAuthorize
.
http_post_without_token
(
url
,
p
ost_body
,
url_param
s
,
endpoint
)
end
end
private
private
...
...
lib/weixin_authorize/handler/exceptions.rb
View file @
cc783b25
# encoding: utf-8
# encoding: utf-8
module
WeixinAuthorize
module
WeixinAuthorize
class
ValidAccessTokenException
<
RuntimeError
;
end
class
ValidAccessTokenException
<
RuntimeError
class
MediaTypeException
<
RuntimeError
;
end
end
end
end
lib/weixin_authorize/token/redis_store.rb
View file @
cc783b25
...
@@ -14,8 +14,11 @@ module WeixinAuthorize
...
@@ -14,8 +14,11 @@ module WeixinAuthorize
def
refresh_token
def
refresh_token
super
super
weixin_redis
.
hmset
(
client
.
redis_key
,
"access_token"
,
client
.
access_token
,
weixin_redis
.
hmset
(
"expired_at"
,
client
.
expired_at
)
client
.
redis_key
,
"access_token"
,
client
.
access_token
,
"expired_at"
,
client
.
expired_at
)
weixin_redis
.
expireat
(
client
.
redis_key
,
client
.
expired_at
.
to_i
-
10
)
# 提前10秒超时
weixin_redis
.
expireat
(
client
.
redis_key
,
client
.
expired_at
.
to_i
-
10
)
# 提前10秒超时
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