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`
[
JS SDK
](
https://github.com/lanrion/weixin_authorize/wiki/js-sdk
)
## 支持自助实现API
详情见:https://github.com/lanrion/weixin_authorize/wiki/diy-your-api
## 已经完成API
*
客服消息
...
...
lib/weixin_authorize.rb
View file @
cc783b25
...
...
@@ -30,24 +30,23 @@ module WeixinAuthorize
OK_MSG
=
"ok"
.
freeze
OK_CODE
=
0
.
freeze
GRANT_TYPE
=
"client_credential"
.
freeze
# 用于标记endpoint可以直接使用url作为完整请求API
CUSTOM_ENDPOINT
=
"custom_endpoint"
.
freeze
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
)
load_json
(
resource
(
get_api_url
).
get
(
params:
header
s
))
load_json
(
resource
(
get_api_url
).
get
(
params:
url_param
s
))
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
)
# to json if invoke "plain"
if
endpoint
==
"plain"
||
endpoint
==
CUSTOM_ENDPOINT
p
ayload
=
JSON
.
dump
(
payload
)
p
ost_body
=
JSON
.
dump
(
post_body
)
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
def
resource
(
url
)
...
...
lib/weixin_authorize/api/mass.rb
View file @
cc783b25
...
...
@@ -3,12 +3,12 @@ module WeixinAuthorize
module
Api
module
Mass
MSG_TYPE
=
[
"mpnews"
,
"image"
,
"text"
,
"voice"
,
"mpvideo"
]
MSG_TYPE
=
[
"mpnews"
,
"image"
,
"text"
,
"voice"
,
"mpvideo"
]
.
freeze
# media_info= {"media_id" media_id}
# 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
)
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
)
mass_url
=
"
#{
mass_base_url
}
/sendall"
...
...
@@ -19,7 +19,7 @@ module WeixinAuthorize
# if mpvideo,
# media_info= {"media_id" => media_id, "title" => "title", "description" => "description"}
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
)
mass_url
=
"
#{
mass_base_url
}
/send"
http_post
(
mass_url
,
media
)
...
...
@@ -29,13 +29,13 @@ module WeixinAuthorize
# 另外,删除群发消息只能删除图文消息和视频消息,其他类型的消息一经发送,无法删除。
def
mass_delete_with_msgid
(
msg_id
)
mass_url
=
"
#{
mass_base_url
}
/delete"
http_post
(
mass_url
,
{
"msg_id"
=>
msg_id
})
http_post
(
mass_url
,
{
msg_id:
msg_id
})
end
# 预览接口【订阅号与服务号认证后均可用】
def
mass_preview
(
openid
,
media_info
,
msgtype
=
"mpnews"
)
openid_option
=
{
"touser"
=>
openid
}
media
=
generate_media
(
msgtype
,
media_info
,
openid_option
)
def
mass_preview
(
openid
,
media_info
,
msg
_
type
=
"mpnews"
)
openid_option
=
{
touser:
openid
}
media
=
generate_media
(
msg
_
type
,
media_info
,
openid_option
)
mass_url
=
"
#{
mass_base_url
}
/preview"
http_post
(
mass_url
,
media
)
end
...
...
@@ -52,22 +52,24 @@ module WeixinAuthorize
"/message/mass"
end
def
generate_media
(
msgtype
,
media_info
,
option
)
msgtype
=
msgtype
.
to_s
raise
"
#{
msgtype
}
is a valid msgtype"
if
not
MSG_TYPE
.
include?
(
msgtype
)
def
generate_media
(
msg_type
,
media_info
,
option
)
msg_type
=
msg_type
.
to_s
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
),
"msgtype"
=>
msgtype
msg
_type
=>
convert_media_info
(
msg_
type
,
media_info
),
"msgtype"
=>
msg
_
type
}.
merge
(
option
)
end
# 如果用户填写的media信息,是字符串,则转换来符合的数据结构,如果 是hash,则直接使用用户的结构。
def
convert_media_info
(
msgtype
,
media_info
)
def
convert_media_info
(
msg
_
type
,
media_info
)
if
media_info
.
is_a?
(
String
)
if
msgtype
==
"text"
return
{
"content"
=>
media_info
}
if
msg
_
type
==
"text"
return
{
content:
media_info
}
else
return
{
"media_id"
=>
media_info
}
return
{
media_id:
media_info
}
end
end
media_info
...
...
lib/weixin_authorize/api/user.rb
View file @
cc783b25
...
...
@@ -29,11 +29,11 @@ module WeixinAuthorize
# }
def
update_remark
(
openid
,
remark
)
update_url
=
"/user/info/updateremark"
p
ayload
=
{
p
ost_body
=
{
openid:
openid
,
remark:
remark
}
http_post
(
update_url
,
p
ayload
)
http_post
(
update_url
,
p
ost_body
)
end
private
...
...
lib/weixin_authorize/client.rb
View file @
cc783b25
...
...
@@ -66,14 +66,14 @@ module WeixinAuthorize
end
# 暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。
def
http_get
(
url
,
header
s
=
{},
endpoint
=
"plain"
)
headers
=
header
s
.
merge
(
access_token_param
)
WeixinAuthorize
.
http_get_without_token
(
url
,
header
s
,
endpoint
)
def
http_get
(
url
,
url_param
s
=
{},
endpoint
=
"plain"
)
url_params
=
url_param
s
.
merge
(
access_token_param
)
WeixinAuthorize
.
http_get_without_token
(
url
,
url_param
s
,
endpoint
)
end
def
http_post
(
url
,
p
ayload
=
{},
header
s
=
{},
endpoint
=
"plain"
)
headers
=
access_token_param
.
merge
(
header
s
)
WeixinAuthorize
.
http_post_without_token
(
url
,
p
ayload
,
header
s
,
endpoint
)
def
http_post
(
url
,
p
ost_body
=
{},
url_param
s
=
{},
endpoint
=
"plain"
)
url_params
=
access_token_param
.
merge
(
url_param
s
)
WeixinAuthorize
.
http_post_without_token
(
url
,
p
ost_body
,
url_param
s
,
endpoint
)
end
private
...
...
lib/weixin_authorize/handler/exceptions.rb
View file @
cc783b25
# encoding: utf-8
module
WeixinAuthorize
class
ValidAccessTokenException
<
RuntimeError
end
class
ValidAccessTokenException
<
RuntimeError
;
end
class
MediaTypeException
<
RuntimeError
;
end
end
lib/weixin_authorize/token/redis_store.rb
View file @
cc783b25
...
...
@@ -14,8 +14,11 @@ module WeixinAuthorize
def
refresh_token
super
weixin_redis
.
hmset
(
client
.
redis_key
,
"access_token"
,
client
.
access_token
,
"expired_at"
,
client
.
expired_at
)
weixin_redis
.
hmset
(
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秒超时
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