class WxOpendata::Service

Attributes

content_type[RW]

Public Instance Methods

create_activity_id(token) click to toggle source

创建被分享动态消息的 activity_id

# File lib/wx_opendata/service.rb, line 48
def create_activity_id(token)
  url = "cgi-bin/message/wxopen/activityid/create?access_token=#{token}"
  jsondata = get(url)
  raise ServiceNotAvailableError if jsondata['errcode'] != 0
  jsondata['activity_id']
end
create_wx_aqrcode(token, params = {}) click to toggle source

获取小程序二维码,适用于需要的码数量较少的业务场景

# File lib/wx_opendata/service.rb, line 56
def create_wx_aqrcode(token, params = {})
  url = "cgi-bin/wxaapp/createwxaqrcode?access_token=#{token}"
  path = params[:path] || 'pages/index/main'
  width = params[:width] || 430
  p = { path: path, width: width }
  data = post(url, p)
  raise InvalidCredentialError if data['errcode'] == 45029
  tmpfile(data)
end
get_wx_acode(token, params = {}) click to toggle source

获取小程序码,适用于需要的码数量较少的业务场景

# File lib/wx_opendata/service.rb, line 67
def get_wx_acode(token, params = {})
  url = "wxa/getwxacode?access_token=#{token}"
  path = params[:path] || 'pages/index/main'
  width = params[:width] || 430
  auto_color = params[:auto_color] || false
  line_color = params[:line_color] || {"r": 0,"g": 0,"b": 0}
  is_hyaline = params[:is_hyaline] || true
  p = { path: path, width: width, auto_color: auto_color, line_color: line_color, is_hyaline: is_hyaline }
  data = post(url, p)
  raise InvalidCredentialError if data['errcode'] == 45029
  tmpfile(data)
end
get_wx_acode_unlimit(token, params = {}) click to toggle source

获取小程序码,适用于需要的码数量极多的业务场景。通过该接口生成的小程序码,永久有效,数量暂无限制。

# File lib/wx_opendata/service.rb, line 81
def get_wx_acode_unlimit(token, params = {})
  url = "wxa/getwxacodeunlimit?access_token=#{token}"
  scene = params[:scene] || 'api'
  page = params[:page] || 'pages/index/main'
  width = params[:width] || 430
  auto_color = params[:auto_color] || false
  line_color = params[:line_color] || {"r": 0,"g": 0,"b": 0}
  is_hyaline = params[:is_hyaline] || true
  p = { scene: scene, page: page, width: width, auto_color: auto_color, line_color: line_color, is_hyaline: is_hyaline }
  data = post(url, p)
  raise InvalidCredentialError if data['errcode'] == 45009 || data['errcode'] == 41030
  tmpfile(data)
end
img_sec_check() click to toggle source

校验一张图片是否含有违法违规内容

# File lib/wx_opendata/service.rb, line 44
def img_sec_check
end
msg_sec_check(token, content) click to toggle source

检查一段文本是否含有违法违规内容

# File lib/wx_opendata/service.rb, line 35
def msg_sec_check(token, content)
  url = "wxa/msg_sec_check?access_token=#{token}"
  p = { content: content }
  jsondata = post(url, p)
  return true if jsondata['errcode'] == 0
  return false if jsondata['errcode'] == 87014
end
send_template_message(token, params) click to toggle source

发送模板消息

# File lib/wx_opendata/service.rb, line 11
def send_template_message(token, params)
  raise InvalidParametersError unless params[:touser] && params[:template_id] && params[:form_id]
  url = "cgi-bin/message/wxopen/template/send?access_token=#{token}"
  touser = params[:touser]
  template_id = params[:template_id]
  form_id = params[:form_id]
  page = params[:page] || 'pages/index/main'
  data = params[:data] || { keyword1: { value: 'Test Message Template' } }
  emphasis_keyword = params[:emphasis_keyword] || ""
  p = { touser: touser, template_id: template_id, form_id: form_id, page: page, data: data, emphasis_keyword: emphasis_keyword }
  jsondata = post(url, p)
  begin
    raise ErrCodeError, 'template_id不正确' if jsondata['errcode'] == 40037
    raise ErrCodeError, 'form_id不正确,或者过期' if jsondata['errcode'] == 41028
    raise ErrCodeError, 'form_id已被使用' if jsondata['errcode'] == 41029
    raise ErrCodeError, 'page不正确' if jsondata['errcode'] == 41030
    raise ErrCodeError, '接口调用超过限额(目前默认每个帐号日调用限额为100万)' if jsondata['errcode'] == 45009
    jsondata['errcode'] == 0
  rescue ErrCodeError => e
    p "#{e.class}: #{e.message}"
  end
end
send_uniform_message() click to toggle source

下发小程序和公众号统一的服务消息

# File lib/wx_opendata/service.rb, line 96
def send_uniform_message
end

Protected Instance Methods

current_req_type() click to toggle source
# File lib/wx_opendata/service.rb, line 122
def current_req_type
  @content_type
end
format_result(resp) click to toggle source
# File lib/wx_opendata/service.rb, line 113
def format_result(resp)
  @content_type = resp.headers[:content_type]
  if resp.headers[:content_type].include? "application/json".freeze
    JSON.parse resp.body
  else
    resp.body
  end
end
get(url) click to toggle source
# File lib/wx_opendata/service.rb, line 101
def get(url)
  url = BASE_URL + url
  resp = RestClient.get url
  format_result(resp)
end
post(url, params) click to toggle source
# File lib/wx_opendata/service.rb, line 107
def post(url, params)
  url = BASE_URL + url
  resp = RestClient.post url, params.to_json, {accept: :json}
  format_result(resp)
end
tmpfile(data) click to toggle source
# File lib/wx_opendata/service.rb, line 126
def tmpfile(data)
  tmpname = (Time.now.to_i + rand(1000...9999)).to_s
  ext = '.' + MIME::Types[current_req_type].first.extensions.first 
  f = Tempfile.new([tmpname, ext], Dir.tmpdir)
  # f.binmode
  f.write(data)
  f.flush
  f.path
end