wxjsapi 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +6 -0
- data/README.md +42 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/wechat-config.rb +1 -0
- data/lib/wechat_config.rb +95 -0
- data/lib/wxjsapi.rb +1 -0
- data/lib/wxjsapi/version.rb +3 -0
- data/wxjsapi-config.gemspec +38 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c0111c827649326e6dffc4981b3a3d79d3a5860e
|
4
|
+
data.tar.gz: e77c9394c17c387916d2ecbf2d86de3e3b5aa55a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3731633f51c84b6ebb8d59e062db3c8f908c0ace8387ee486a2d563018cc5751a376946734d742227753f31b86e30b00675d714f99505cf3e1da45bfd6b2026c
|
7
|
+
data.tar.gz: 2aef201bf313e5fa467dadc1b8d0a21de5e085aa7c41a0bcfd903283fa77e935c263b5360d93f41840088e050d9874cd9138ff55e1312db2dd5f7a38a2952469
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Wxjsapi::Config
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/wxjsapi/config`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'wxjsapi-config'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install wxjsapi-config
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'wechat_config'
|
27
|
+
wx_config = WechatConfig([appId], [appSecret], [jsapilist]) # jsapilist 需要使用微信jsapi的功能,如 "onMenuShareTimeline,onMenuShareAppMessage"
|
28
|
+
wx_config.wxJsSDKSign([url]) # 生成微信jsapi需要的参数。使用微信jsapi页面的url
|
29
|
+
wx_config 可以使用的微信jsapi参数有 :appId, :timestamp, :nonceStr, :signature, :jsApiList
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
## Development
|
34
|
+
|
35
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
36
|
+
|
37
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/wxjsapi-config. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
42
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wxjsapi/config"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/wechat_config'
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require "wxjsapi/version"
|
2
|
+
|
3
|
+
class WechatConfig
|
4
|
+
attr_accessor :debug, :appId, :appSecret, :timestamp, :nonceStr, :signature, :jsApiList
|
5
|
+
def initialize appId, appSecret, jsApiList
|
6
|
+
#全局缓存access_token
|
7
|
+
$access_token = Hash.new
|
8
|
+
$access_token['access_token'] = ''
|
9
|
+
$access_token['expires_in'] = Time.new
|
10
|
+
#全局缓存jsapi_ticket
|
11
|
+
$jsapi_ticket = Hash.new
|
12
|
+
$jsapi_ticket['jsapi_ticket'] = ''
|
13
|
+
$jsapi_ticket['expires_in'] = Time.new
|
14
|
+
|
15
|
+
@appId = appId
|
16
|
+
@appSecret = appSecret
|
17
|
+
if jsApiList.present?
|
18
|
+
@jsApiList = jsApiList
|
19
|
+
else
|
20
|
+
@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"
|
21
|
+
end
|
22
|
+
self.debug = 'false'
|
23
|
+
end
|
24
|
+
|
25
|
+
def wxJsSDKSign url
|
26
|
+
uuid = UUID.new
|
27
|
+
self.timestamp = Time.new.to_i.to_s
|
28
|
+
self.nonceStr = uuid.generate
|
29
|
+
ticket = self.wxGetJsAPITicket
|
30
|
+
puts 'ticket:' + ticket.to_s
|
31
|
+
if ticket.blank?
|
32
|
+
return false
|
33
|
+
end
|
34
|
+
|
35
|
+
str = 'jsapi_ticket=' + ticket.to_s + '&noncestr=' + self.nonceStr.to_s + '×tamp=' + self.timestamp + '&url=' + url.to_s
|
36
|
+
puts str
|
37
|
+
self.signature = Digest::SHA1.hexdigest(str)
|
38
|
+
end
|
39
|
+
|
40
|
+
#获取调用微信JS接口的临时票据 jsapi_ticket
|
41
|
+
def wxGetJsAPITicket
|
42
|
+
today = Time.new
|
43
|
+
ticket_ex = $jsapi_ticket['expires_in']
|
44
|
+
if today < ticket_ex
|
45
|
+
$jsapi_ticket['jsapi_ticket']
|
46
|
+
else
|
47
|
+
token_ex = $access_token['expires_in']
|
48
|
+
if today > token_ex
|
49
|
+
token = self.wxGetAccessToken
|
50
|
+
if token.blank?
|
51
|
+
return nil
|
52
|
+
end
|
53
|
+
else
|
54
|
+
token = $access_token['access_token']
|
55
|
+
end
|
56
|
+
puts 'token:' + token.to_s
|
57
|
+
url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&' + 'access_token=' + token.to_s
|
58
|
+
response = RestClient.get url
|
59
|
+
if response.code == 200
|
60
|
+
result = ActiveSupport::JSON.decode(response)
|
61
|
+
puts 'ticket result:' + result.to_s
|
62
|
+
ticket = result['ticket']
|
63
|
+
$jsapi_ticket['jsapi_ticket'] = ticket
|
64
|
+
$jsapi_ticket['expires_in'] = Time.new + result['expires_in'].to_i
|
65
|
+
return ticket
|
66
|
+
else
|
67
|
+
return nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
#获取公众号的全局唯一票据access_token
|
73
|
+
def wxGetAccessToken
|
74
|
+
today = Time.new
|
75
|
+
token_ex = $access_token['expires_in']
|
76
|
+
puts today<token_ex
|
77
|
+
if today < token_ex
|
78
|
+
$access_token['access_token']
|
79
|
+
else
|
80
|
+
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+ self.appId.to_s + '&secret=' + self.appSecret.to_s
|
81
|
+
puts 'url:' + url.to_s
|
82
|
+
response = RestClient.get url
|
83
|
+
if response.code == 200
|
84
|
+
result = ActiveSupport::JSON.decode(response)
|
85
|
+
puts 'token result:' + result.to_s
|
86
|
+
token = result['access_token']
|
87
|
+
$access_token['access_token'] = token
|
88
|
+
$access_token['expires_in'] = Time.new + result['expires_in'].to_i
|
89
|
+
return token
|
90
|
+
else
|
91
|
+
return nil
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/wxjsapi.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/wechat_config'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'wxjsapi/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "wxjsapi"
|
8
|
+
spec.version = Wxjsapi::VERSION
|
9
|
+
spec.authors = ["Nick.zhu"]
|
10
|
+
spec.email = ["nick.zhu@icitymoble.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{微信JS-SDK wx.config 配置参数}
|
13
|
+
spec.description = %q{所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用(同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用,目前Android微信客户端不支持pushState的H5新特性,所以使用pushState来实现web app的页面会导致签名失败,此问题会在Android6.2中修复)。wx.config({
|
14
|
+
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
15
|
+
appId: '', // 必填,公众号的唯一标识
|
16
|
+
timestamp: , // 必填,生成签名的时间戳
|
17
|
+
nonceStr: '', // 必填,生成签名的随机串
|
18
|
+
signature: '',// 必填,签名,见附录1
|
19
|
+
jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
|
20
|
+
});}
|
21
|
+
spec.homepage = "https://github.com/VitalYoung/wxjsapi-config"
|
22
|
+
|
23
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
24
|
+
# delete this section to allow pushing this gem to any host.
|
25
|
+
if spec.respond_to?(:metadata)
|
26
|
+
spec.metadata['allowed_push_host'] = "Set to 'http://mygemserver.com'"
|
27
|
+
else
|
28
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
29
|
+
end
|
30
|
+
|
31
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
32
|
+
spec.bindir = "exe"
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ["lib"]
|
35
|
+
|
36
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wxjsapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick.zhu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: |-
|
42
|
+
所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用(同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用,目前Android微信客户端不支持pushState的H5新特性,所以使用pushState来实现web app的页面会导致签名失败,此问题会在Android6.2中修复)。wx.config({
|
43
|
+
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
|
44
|
+
appId: '', // 必填,公众号的唯一标识
|
45
|
+
timestamp: , // 必填,生成签名的时间戳
|
46
|
+
nonceStr: '', // 必填,生成签名的随机串
|
47
|
+
signature: '',// 必填,签名,见附录1
|
48
|
+
jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
|
49
|
+
});
|
50
|
+
email:
|
51
|
+
- nick.zhu@icitymoble.com
|
52
|
+
executables: []
|
53
|
+
extensions: []
|
54
|
+
extra_rdoc_files: []
|
55
|
+
files:
|
56
|
+
- .gitignore
|
57
|
+
- CODE_OF_CONDUCT.md
|
58
|
+
- Gemfile
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- bin/console
|
62
|
+
- bin/setup
|
63
|
+
- lib/wechat-config.rb
|
64
|
+
- lib/wechat_config.rb
|
65
|
+
- lib/wxjsapi.rb
|
66
|
+
- lib/wxjsapi/version.rb
|
67
|
+
- wxjsapi-config.gemspec
|
68
|
+
homepage: https://github.com/VitalYoung/wxjsapi-config
|
69
|
+
licenses: []
|
70
|
+
metadata:
|
71
|
+
allowed_push_host: Set to 'http://mygemserver.com'
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.0.14
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: 微信JS-SDK wx.config 配置参数
|
92
|
+
test_files: []
|