Class: SimpleWechat::JsApi

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_wechat/jsapi.rb

Defined Under Namespace

Classes: Ticket

Instance Method Summary collapse

Instance Method Details

#connectionObject



44
45
46
47
48
49
50
# File 'lib/simple_wechat/jsapi.rb', line 44

def connection
  @connection ||= begin
                    conn = Faraday.new do |faraday|
                      faraday.adapter  Faraday.default_adapter
                    end
                  end
end

#get_config(jsapi_ticket, url, appid, api_list, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/simple_wechat/jsapi.rb', line 32

def get_config(jsapi_ticket, url, appid, api_list, options = {})
  timestamp = (options[:timestamp] || Time.now).to_i
  noncestr = options[:noncestr] || SecureRandom.hex(10)
  signature = sign(jsapi_ticket, noncestr, timestamp, url)
  {
    appId: appid,
    timestamp: timestamp,
    signature: signature,
    jsApiList: api_list
  }
end

#get_ticket(access_token) ⇒ Object



14
15
16
17
# File 'lib/simple_wechat/jsapi.rb', line 14

def get_ticket(access_token)
  response = connection.get("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=#{access_token}&type=jsapi")
  Ticket.new MultiJson.load(response.body)
end

#sign(jsapi_ticket, noncestr, timestamp, url) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_wechat/jsapi.rb', line 19

def sign(jsapi_ticket, noncestr, timestamp, url)
  params = {
    noncestr: noncestr,
    timestamp: timestamp,
    jsapi_ticket: jsapi_ticket,
    url: url
  }

  text = params.keys.sort.map {|key| "#{key}=#{params[key]}"}.join('&')
  
  Digest::SHA1.hexdigest(text)
end