Class: WechatConfig

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appId, appSecret, jsApiList) ⇒ WechatConfig

Returns a new instance of WechatConfig.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wechat_config.rb', line 5

def initialize appId, appSecret, jsApiList
  #全局缓存access_token
  $access_token = Hash.new
  $access_token['access_token'] = ''
  $access_token['expires_in'] = Time.new
  #全局缓存jsapi_ticket
  $jsapi_ticket = Hash.new
  $jsapi_ticket['jsapi_ticket'] = ''
  $jsapi_ticket['expires_in'] = Time.new

  @appId = appId
  @appSecret = appSecret
  if jsApiList.present?
    @jsApiList = jsApiList
  else
    @jsApiList = "onMenuShareTimeline,onMenuShareAppMessage,onMenuShareQQ,onMenuShareWeibo,startRecord,stopRecord,onVoiceRecordEnd,playVoice,pauseVoice,stopVoice,onVoicePlayEnd,uploadVoice,downloadVoice,chooseImage,previewImage,uploadImage,downloadImage,translateVoice,getNetworkType,openLocation,getLocation,hideOptionMenu,showOptionMenu,hideMenuItems,showMenuItems,hideAllNonBaseMenuItem,showAllNonBaseMenuItem,closeWindow,scanQRCode,chooseWXPay,openProductSpecificView,addCard,chooseCard,openCard"
  end
  self.debug = 'false'
end

Instance Attribute Details

#appIdObject

Returns the value of attribute appId.



4
5
6
# File 'lib/wechat_config.rb', line 4

def appId
  @appId
end

#appSecretObject

Returns the value of attribute appSecret.



4
5
6
# File 'lib/wechat_config.rb', line 4

def appSecret
  @appSecret
end

#debugObject

Returns the value of attribute debug.



4
5
6
# File 'lib/wechat_config.rb', line 4

def debug
  @debug
end

#jsApiListObject

Returns the value of attribute jsApiList.



4
5
6
# File 'lib/wechat_config.rb', line 4

def jsApiList
  @jsApiList
end

#nonceStrObject

Returns the value of attribute nonceStr.



4
5
6
# File 'lib/wechat_config.rb', line 4

def nonceStr
  @nonceStr
end

#signatureObject

Returns the value of attribute signature.



4
5
6
# File 'lib/wechat_config.rb', line 4

def signature
  @signature
end

#timestampObject

Returns the value of attribute timestamp.



4
5
6
# File 'lib/wechat_config.rb', line 4

def timestamp
  @timestamp
end

Instance Method Details

#wxGetAccessTokenObject

获取公众号的全局唯一票据access_token



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/wechat_config.rb', line 73

def wxGetAccessToken
  today = Time.new
  token_ex = $access_token['expires_in']
  puts today<token_ex
  if today < token_ex
    $access_token['access_token']
  else
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+ self.appId.to_s + '&secret=' + self.appSecret.to_s
    puts 'url:' + url.to_s
    response = RestClient.get url
    if response.code == 200
      result = ActiveSupport::JSON.decode(response)
      puts 'token result:' + result.to_s
      token = result['access_token']
      $access_token['access_token'] = token
      $access_token['expires_in'] = Time.new + result['expires_in'].to_i
      return token
    else
      return nil
    end
  end
end

#wxGetJsAPITicketObject

获取调用微信JS接口的临时票据 jsapi_ticket



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wechat_config.rb', line 41

def wxGetJsAPITicket
  today = Time.new
  ticket_ex = $jsapi_ticket['expires_in']
  if today < ticket_ex
    $jsapi_ticket['jsapi_ticket']
  else
    token_ex = $access_token['expires_in']
    if today > token_ex
      token = self.wxGetAccessToken
      if token.blank?
        return nil
      end
    else
      token = $access_token['access_token']
    end
    puts 'token:' + token.to_s
    url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&' + 'access_token=' + token.to_s
    response = RestClient.get url
    if response.code == 200
      result = ActiveSupport::JSON.decode(response)
      puts 'ticket result:' + result.to_s
      ticket = result['ticket']
      $jsapi_ticket['jsapi_ticket'] = ticket
      $jsapi_ticket['expires_in'] = Time.new + result['expires_in'].to_i
      return ticket
    else
      return nil
    end
  end
end

#wxJsSDKSign(url) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wechat_config.rb', line 25

def wxJsSDKSign url
  uuid = UUID.new
  self.timestamp = Time.new.to_i.to_s
  self.nonceStr = uuid.generate
  ticket = self.wxGetJsAPITicket
  puts 'ticket:' + ticket.to_s
  if ticket.blank?
    return false
  end

  str = 'jsapi_ticket=' + ticket.to_s + '&noncestr=' + self.nonceStr.to_s + '&timestamp=' + self.timestamp + '&url=' + url.to_s
  puts str
  self.signature = Digest::SHA1.hexdigest(str)
end