Module: Haoyaoshi

Defined in:
lib/haoyaoshi.rb,
lib/haoyaoshi/client.rb,
lib/haoyaoshi/config.rb,
lib/haoyaoshi/version.rb,
lib/haoyaoshi/api/drug.rb,
lib/haoyaoshi/api/image.rb,
lib/haoyaoshi/api/order.rb,
lib/haoyaoshi/api/stock.rb,
lib/haoyaoshi/token/store.rb,
lib/haoyaoshi/api/e_invoice.rb,
lib/haoyaoshi/api/logistics.rb,
lib/haoyaoshi/api/order_center.rb,
lib/haoyaoshi/token/redis_store.rb,
lib/haoyaoshi/handler/exceptions.rb,
lib/haoyaoshi/token/object_store.rb,
lib/haoyaoshi/handler/global_code.rb,
lib/haoyaoshi/handler/result_handler.rb

Defined Under Namespace

Modules: Api, Token Classes: Client, Config, MediaTypeException, ResultHandler, ValidAccessTokenException

Constant Summary collapse

OK_MSG =
"ok".freeze
OK_CODE =
0.freeze
GRANT_TYPE =
"client_credentials".freeze
CUSTOM_ENDPOINT =

用于标记endpoint可以直接使用url作为完整请求API

"custom_endpoint".freeze
VERSION =
"0.1.0"
GLOBAL_CODES =
{
  -1    => "系统繁忙",
  0     => "请求成功",
  40001 => "获取access_token时AppSecret错误,或者access_token无效",
  40002 => "不合法的凭证类型",
  40003 => "不合法的OpenID",
  40004 => "不合法的媒体文件类型",
  40005 => "不合法的文件类型",
  40006 => "不合法的文件大小",
  40007 => "不合法的媒体文件id",
  40008 => "不合法的消息类型",
  40009 => "不合法的图片文件大小",
  40010 => "不合法的语音文件大小",
  40011 => "不合法的视频文件大小",
  40012 => "不合法的缩略图文件大小",
  40013 => "不合法的APPID",
  40014 => "不合法的access_token",
  40015 => "不合法的菜单类型",
  40016 => "不合法的按钮个数",
  40017 => "不合法的按钮个数",
  40018 => "不合法的按钮名字长度",
  40019 => "不合法的按钮KEY长度",
  40020 => "不合法的按钮URL长度",
  40021 => "不合法的菜单版本号",
  40022 => "不合法的子菜单级数",
  40023 => "不合法的子菜单按钮个数",
  40024 => "不合法的子菜单按钮类型",
  40025 => "不合法的子菜单按钮名字长度",
  40026 => "不合法的子菜单按钮KEY长度",
  40027 => "不合法的子菜单按钮URL长度",
  40028 => "不合法的自定义菜单使用用户",
  40029 => "不合法的oauth_code",
  40030 => "不合法的refresh_token",
  40031 => "不合法的openid列表",
  40032 => "不合法的openid列表长度",
  40033 => "不合法的请求字符,不能包含xxxx格式的字符",
  40035 => "不合法的参数",
  40038 => "不合法的请求格式",
  40039 => "不合法的URL长度",
  40050 => "不合法的分组id",
  40051 => "分组名字不合法",
  41001 => "缺少access_token参数",
  41002 => "缺少appid参数",
  41003 => "缺少refresh_token参数",
  41004 => "缺少secret参数",
  41005 => "缺少多媒体文件数据",
  41006 => "缺少media_id参数",
  41007 => "缺少子菜单数据",
  41008 => "缺少oauth code",
  41009 => "缺少openid",
  42001 => "access_token超时",
  42002 => "refresh_token超时",
  42003 => "oauth_code超时",
  43001 => "需要GET请求",
  43002 => "需要POST请求",
  43003 => "需要HTTPS请求",
  43004 => "需要接收者关注",
  43005 => "需要好友关系",
  44001 => "多媒体文件为空",
  44002 => "POST的数据包为空",
  44003 => "图文消息内容为空",
  44004 => "文本消息内容为空",
  45001 => "多媒体文件大小超过限制",
  45002 => "消息内容超过限制",
  45003 => "标题字段超过限制",
  45004 => "描述字段超过限制",
  45005 => "链接字段超过限制",
  45006 => "图片链接字段超过限制",
  45007 => "语音播放时间超过限制",
  45008 => "图文消息超过限制",
  45009 => "接口调用超过限制",
  45010 => "创建菜单个数超过限制",
  45015 => "回复时间超过限制",
  45016 => "系统分组,不允许修改",
  45017 => "分组名字过长",
  45018 => "分组数量超过上限",
  46001 => "不存在媒体数据",
  46002 => "不存在的菜单版本",
  46003 => "不存在的菜单数据",
  46004 => "不存在的用户",
  47001 => "解析JSON/XML内容错误",
  48001 => "api功能未授权",
  50001 => "用户未授权该api",
  50002 => "用户受限,可能是违规后接口被封禁",
  61451 => "参数错误(invalid parameter)",
  61452 => "无效客服账号(invalid kf_account)",
  61453 => "客服帐号已存在(kf_account exsited)",
  61454 => "客服帐号名长度超过限制(仅允许10个英文字符,不包括@及@后的公众号的微信号)(invalid kf_acount length)",
  61455 => "客服帐号名包含非法字符(仅允许英文+数字)(illegal character in kf_account)",
  61456 => "客服帐号个数超过限制(10个客服账号)(kf_account count exceeded)",
  61457 => "无效头像文件类型(invalid file type)",
  61450 => "系统错误(system error)",
  61500 => "日期格式错误",
  61501 => "日期范围错误",
  9001001 => "POST数据参数不合法",
  9001002 => "远端服务不可用",
  9001003 => "Ticket不合法",
  9001004 => "获取摇周边用户信息失败",
  9001005 => "获取商户信息失败",
  9001006 => "获取OpenID失败",
  9001007 => "上传文件缺失",
  9001008 => "上传素材的文件类型不合法",
  9001009 => "上传素材的文件尺寸不合法",
  9001010 => "上传失败",
  9001020 => "帐号不合法",
  9001021 => "已有设备激活率低于50%,不能新增设备",
  9001022 => "设备申请数不合法,必须为大于0的数字",
  9001023 => "已存在审核中的设备ID申请",
  9001024 => "一次查询设备ID数量不能超过50",
  9001025 => "设备ID不合法",
  9001026 => "页面ID不合法",
  9001027 => "页面参数不合法",
  9001028 => "一次删除页面ID数量不能超过10",
  9001029 => "页面已应用在设备中,请先解除应用关系再删除",
  9001030 => "一次查询页面ID数量不能超过50",
  9001031 => "时间区间不合法",
  9001032 => "保存设备与页面的绑定关系参数错误",
  9001033 => "门店ID不合法",
  9001034 => "设备备注信息过长",
  9001035 => "设备申请参数不合法",
  9001036 => "查询起始值begin不合法",
  1010001025 => "缺少请求参数:开始时间(startTime)或结束时间(endTime)!"
}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



5
6
7
# File 'lib/haoyaoshi/config.rb', line 5

def config
  @config
end

Class Method Details

.api_base_urlObject



37
38
39
40
41
42
# File 'lib/haoyaoshi/config.rb', line 37

def api_base_url
  if config.nil?
    return "https://api.ehaoyao.com"
  end
  @api_base_url ||=  config.api_base_url
end

.api_endpointObject



91
92
93
# File 'lib/haoyaoshi.rb', line 91

def api_endpoint
  api_base_url
end

.calculate_expire(expires_in) ⇒ Object



115
116
117
# File 'lib/haoyaoshi.rb', line 115

def calculate_expire(expires_in)
  Time.now.to_i + expires_in.to_i - key_expired.to_i
end

.configure {|self.config ||= Config.new| ... } ⇒ Object

Yields:



7
8
9
# File 'lib/haoyaoshi/config.rb', line 7

def configure
  yield self.config ||= Config.new
end

.endpoint_url(endpoint, url) ⇒ Object



80
81
82
83
84
85
# File 'lib/haoyaoshi.rb', line 80

def endpoint_url(endpoint, url)
  # 此处为了应对第三方开发者如果自助对接接口时,URL不规范的情况下,可以直接使用URL当为endpoint
  return url if endpoint == CUSTOM_ENDPOINT
  Rails.logger.info("#{endpoint}_endpoint")
  send("#{endpoint}_endpoint") + url
end

.haoyaoshi_redisObject



11
12
13
14
# File 'lib/haoyaoshi/config.rb', line 11

def haoyaoshi_redis
  return nil if config.nil?
  @redis ||= config.redis
end

.http_delete_without_token(url, delete_body = {}, url_params = {}, endpoint = "plain") ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/haoyaoshi.rb', line 51

def http_delete_without_token(url, delete_body={}, url_params={}, endpoint="plain")
  delete_api_url = endpoint_url(endpoint, url)
  if endpoint == "plain" || endpoint == CUSTOM_ENDPOINT
    delete_body = JSON.dump(delete_body)
  else
    delete_body = JSON.dump(delete_body)
  end
  Rails.logger.info("====好药师order请求参数===>#{delete_api_url}===#{delete_body}=======#{url_params}=#{endpoint}")
  load_json(resource(delete_api_url).delete(params: url_params,raw_response: true,:content_type => :json))
end

.http_get_without_token(url, url_params = {}, endpoint = "plain") ⇒ Object



34
35
36
37
38
# File 'lib/haoyaoshi.rb', line 34

def http_get_without_token(url, url_params={}, endpoint="plain")
  get_api_url = endpoint_url(endpoint, url)
  Rails.logger.info("====好药师order请求参数===>#{get_api_url}===#{url_params}===#{endpoint}")
  load_json(resource(get_api_url).get(params: url_params))
end

.http_post_without_token(url, post_body = {}, url_params = {}, endpoint = "plain") ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/haoyaoshi.rb', line 40

def http_post_without_token(url, post_body={}, url_params={}, endpoint="plain")
  post_api_url = endpoint_url(endpoint, url)
  if endpoint == "plain" || endpoint == CUSTOM_ENDPOINT
    post_body = JSON.dump(post_body)
  else
    post_body = JSON.dump(post_body)
  end
  Rails.logger.info("====好药师order请求参数===>#{post_api_url}===#{post_body}=======#{url_params}=#{endpoint}")
  load_json(resource(post_api_url).post(post_body, params: url_params,raw_response: true,:content_type => :json))
end

.img_base_urlObject



23
24
25
26
27
28
# File 'lib/haoyaoshi/config.rb', line 23

def img_base_url
  if config.nil?
    return "http://img01.img.ehaoyao.com"
  end
  @img_base_url ||= config.img_base_url
end

.img_endpoint(url) ⇒ Object



111
112
113
# File 'lib/haoyaoshi.rb', line 111

def img_endpoint(url)
  "#{img_base_url}#{url}"
end

.key_expiredObject



16
17
18
19
20
21
# File 'lib/haoyaoshi/config.rb', line 16

def key_expired
  if config.nil?
    return 100
  end
  @key_expired ||= config.key_expired
end

.load_json(string) ⇒ Object

return hash



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/haoyaoshi.rb', line 67

def load_json(string)
  result_hash = JSON.parse(string.force_encoding("UTF-8").gsub(/[\u0011-\u001F]/, ""))
  if result_hash.present?
    code   = result_hash.delete("errcode")
    en_msg = result_hash.delete("errmsg")
  else
    result_hash = {}
    code   = 0
    en_msg = "请求成功"
  end
  ResultHandler.new(code, en_msg, result_hash)
end

.open_base_urlObject



30
31
32
33
34
35
# File 'lib/haoyaoshi/config.rb', line 30

def open_base_url
  if config.nil?
    return "http://api.goodscenter.ehaoyao.com"
  end
  @open_base_url ||=  config.open_base_url
end

.open_endpointObject



107
108
109
# File 'lib/haoyaoshi.rb', line 107

def open_endpoint
  open_base_url
end

.order_base_urlObject



44
45
46
47
48
49
# File 'lib/haoyaoshi/config.rb', line 44

def order_base_url
  if config.nil?
    return "https://api.ehaoyao.com"
  end
  @order_base_url ||= config.order_base_url
end

.order_center_base_urlObject



51
52
53
54
55
56
# File 'lib/haoyaoshi/config.rb', line 51

def order_center_base_url
  if config.nil?
    return "https://internal.api.ehaoyao.com"
  end
  @order_center_base_url ||= config.order_center_base_url
end

.order_center_endpointObject



99
100
101
# File 'lib/haoyaoshi.rb', line 99

def order_center_endpoint
  order_center_base_url
end

.order_endpointObject



95
96
97
# File 'lib/haoyaoshi.rb', line 95

def order_endpoint
  order_base_url
end

.plain_endpointObject



87
88
89
# File 'lib/haoyaoshi.rb', line 87

def plain_endpoint
  "#{api_endpoint}"
end

.resource(url) ⇒ Object



62
63
64
# File 'lib/haoyaoshi.rb', line 62

def resource(url)
  RestClient::Resource.new(url, rest_client_options)
end

.rest_client_optionsObject

可选配置: RestClient timeout, etc. key 必须是符号 如果出现 RestClient::SSLCertificateNotVerified Exception: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed 这个错,除了改 verify_ssl: true,请参考:www.extendi.it/blog/2015/5/23/47-sslv3-read-server-certificate-b-certificate-verify-failed



62
63
64
65
66
67
# File 'lib/haoyaoshi/config.rb', line 62

def rest_client_options
  if config.nil?
    return {timeout: 5, open_timeout: 5, verify_ssl: true}
  end
  @rest_client_options ||= config.rest_client_options
end

.token_endpoint(url) ⇒ Object



103
104
105
# File 'lib/haoyaoshi.rb', line 103

def token_endpoint(url)
  "#{api_endpoint}#{url}"
end