Commit cc783b25 by lanrion

部分参数命名重构

parent 1afe8f3b
......@@ -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
* 客服消息
......
......@@ -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, headers={}, endpoint="plain")
def http_get_without_token(url, url_params={}, endpoint="plain")
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
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)
# to json if invoke "plain"
if endpoint == "plain" || endpoint == CUSTOM_ENDPOINT
payload = JSON.dump(payload)
post_body = JSON.dump(post_body)
end
load_json(resource(post_api_url).post(payload, params: headers))
load_json(resource(post_api_url).post(post_body, params: url_params))
end
def resource(url)
......
......@@ -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
{
msgtype => convert_media_info(msgtype, 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
......
......@@ -29,11 +29,11 @@ module WeixinAuthorize
# }
def update_remark(openid, remark)
update_url = "/user/info/updateremark"
payload = {
post_body = {
openid: openid,
remark: remark
}
http_post(update_url, payload)
http_post(update_url, post_body)
end
private
......
......@@ -66,14 +66,14 @@ module WeixinAuthorize
end
# 暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。
def http_get(url, headers={}, endpoint="plain")
headers = headers.merge(access_token_param)
WeixinAuthorize.http_get_without_token(url, headers, endpoint)
def http_get(url, url_params={}, endpoint="plain")
url_params = url_params.merge(access_token_param)
WeixinAuthorize.http_get_without_token(url, url_params, endpoint)
end
def http_post(url, payload={}, headers={}, endpoint="plain")
headers = access_token_param.merge(headers)
WeixinAuthorize.http_post_without_token(url, payload, headers, endpoint)
def http_post(url, post_body={}, url_params={}, endpoint="plain")
url_params = access_token_param.merge(url_params)
WeixinAuthorize.http_post_without_token(url, post_body, url_params, endpoint)
end
private
......
# encoding: utf-8
module WeixinAuthorize
class ValidAccessTokenException < RuntimeError
end
class ValidAccessTokenException < RuntimeError;end
class MediaTypeException < RuntimeError;end
end
......@@ -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
......
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