Commit cc783b25 by lanrion

部分参数命名重构

parent 1afe8f3b
...@@ -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
* 客服消息 * 客服消息
......
...@@ -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, headers={}, endpoint="plain") def http_get_without_token(url, url_params={}, endpoint="plain")
get_api_url = endpoint_url(endpoint, url) get_api_url = endpoint_url(endpoint, url)
load_json(resource(get_api_url).get(params: headers)) load_json(resource(get_api_url).get(params: url_params))
end end
def http_post_without_token(url, payload={}, headers={}, endpoint="plain") def http_post_without_token(url, post_body={}, url_params={}, 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
payload = JSON.dump(payload) post_body = JSON.dump(post_body)
end end
load_json(resource(post_api_url).post(payload, params: headers)) load_json(resource(post_api_url).post(post_body, params: url_params))
end end
def resource(url) def resource(url)
......
...@@ -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
{ {
msgtype => convert_media_info(msgtype, 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
......
...@@ -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"
payload = { post_body = {
openid: openid, openid: openid,
remark: remark remark: remark
} }
http_post(update_url, payload) http_post(update_url, post_body)
end end
private private
......
...@@ -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, headers={}, endpoint="plain") def http_get(url, url_params={}, endpoint="plain")
headers = headers.merge(access_token_param) url_params = url_params.merge(access_token_param)
WeixinAuthorize.http_get_without_token(url, headers, endpoint) WeixinAuthorize.http_get_without_token(url, url_params, endpoint)
end end
def http_post(url, payload={}, headers={}, endpoint="plain") def http_post(url, post_body={}, url_params={}, endpoint="plain")
headers = access_token_param.merge(headers) url_params = access_token_param.merge(url_params)
WeixinAuthorize.http_post_without_token(url, payload, headers, endpoint) WeixinAuthorize.http_post_without_token(url, post_body, url_params, endpoint)
end end
private private
......
# encoding: utf-8 # encoding: utf-8
module WeixinAuthorize module WeixinAuthorize
class ValidAccessTokenException < RuntimeError;end
class ValidAccessTokenException < RuntimeError class MediaTypeException < RuntimeError;end
end
end end
...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment