blsm-mp-wx 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTc4MjQwYTUzZjk3ZTNkMjQ5MGMzMGVhZGY1NzJiZDFhNWZjMGNlMg==
4
+ NmU4MDJhZTFhYzg5MGRkZjY1Y2I0MWI0YWZhZTE5NzcwM2Y5MDdlMw==
5
5
  data.tar.gz: !binary |-
6
- MTY4Zjc4ZjAwMTQ4NWE0ODY2ODQ3ODI4MTMwYzc4NmEzNmVhMmMxNA==
6
+ N2YwYzM3OGFiZDNlNDJhMTcyMjk2ZWYxMjQ4NTM0N2E2ZTdmOTNhZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTM5YWQ5MWEwMDE4MDllZjY0Y2U3YTMyYjllOGE2NWFhZWFhMGY3MGRkMzdk
10
- NzhlMzNlYjZkMTdjYTMxOWRlM2EwMjdjNmQ1NWY1OWRiYmFjNjY2Y2NjNjI0
11
- ZDk1NDU0YjVhNGIyNDBiYzk0NzBlYWFmZjZiMzI1ZjQwZWRmZTQ=
9
+ YWQ0MDU1Yzk0YTQ1ZTJhNWYwNjk5OWNiZWZmYTVmZGJmMjZmM2UzZjIwODll
10
+ NTYyODU2MmM0ZGMzOTA1M2RmNmFkOTE1ZjM4NzhmYjA3ZjAxNjBlYTUzZjky
11
+ YjgyZjY5NTM3ZjFjMTY5NGNmYjQ5Mjk2ZTUwYjc5YzQ3MGZmODM=
12
12
  data.tar.gz: !binary |-
13
- Njk0OWRiMzAxY2E3NWIzOTdkNWE5NGEzNzVhNjFlY2YwYWM3YmYxNDVhOTEy
14
- YTJjM2Y1YjlkMWQzOGU0YTU1YWU3MDE3NDZkMDEyNmVmZTljMzdjOWQ1Yzhk
15
- ODJiZjE2MjEyZjAyMDYwNTE0MWNjMTY3ODM2ZTZjYzQ0NDc5ZDU=
13
+ ZmQ2NTFjY2Y4OTBjMWQ0Mjk4OTZlN2I5Yzc0MTU4MzI3ZTMwZjZmMmM3Nzkx
14
+ ZjM2M2RhYTJlYmFjMzlmMzUzMDYxYWU4NGY3ZmI1MjhhMzkxNDUwMDI0YjBj
15
+ NWIwZWNmNTkzNGU5YWZkNTNiYjU0Mzc1ZGY3NWNlOTgzOTMwOTA=
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+ /.idea/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://ruby.taobao.org'
2
+
3
+ # Specify your gem's dependencies in blsm-mp-wx.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,120 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ blsm-mp-wx (0.1.1)
5
+
6
+ GEM
7
+ remote: https://ruby.taobao.org/
8
+ specs:
9
+ actionmailer (3.2.21)
10
+ actionpack (= 3.2.21)
11
+ mail (~> 2.5.4)
12
+ actionpack (3.2.21)
13
+ activemodel (= 3.2.21)
14
+ activesupport (= 3.2.21)
15
+ builder (~> 3.0.0)
16
+ erubis (~> 2.7.0)
17
+ journey (~> 1.0.4)
18
+ rack (~> 1.4.5)
19
+ rack-cache (~> 1.2)
20
+ rack-test (~> 0.6.1)
21
+ sprockets (~> 2.2.1)
22
+ activemodel (3.2.21)
23
+ activesupport (= 3.2.21)
24
+ builder (~> 3.0.0)
25
+ activerecord (3.2.21)
26
+ activemodel (= 3.2.21)
27
+ activesupport (= 3.2.21)
28
+ arel (~> 3.0.2)
29
+ tzinfo (~> 0.3.29)
30
+ activeresource (3.2.21)
31
+ activemodel (= 3.2.21)
32
+ activesupport (= 3.2.21)
33
+ activesupport (3.2.21)
34
+ i18n (~> 0.6, >= 0.6.4)
35
+ multi_json (~> 1.0)
36
+ arel (3.0.3)
37
+ builder (3.0.4)
38
+ diff-lcs (1.2.5)
39
+ erubis (2.7.0)
40
+ faraday (0.9.1)
41
+ multipart-post (>= 1.2, < 3)
42
+ hike (1.2.3)
43
+ i18n (0.7.0)
44
+ journey (1.0.4)
45
+ json (1.8.3)
46
+ mail (2.5.4)
47
+ mime-types (~> 1.16)
48
+ treetop (~> 1.4.8)
49
+ mime-types (1.25.1)
50
+ multi_json (1.11.1)
51
+ multipart-post (2.0.0)
52
+ mysql2 (0.3.18)
53
+ polyglot (0.3.5)
54
+ rack (1.4.5)
55
+ rack-cache (1.2)
56
+ rack (>= 0.4)
57
+ rack-ssl (1.3.4)
58
+ rack
59
+ rack-test (0.6.3)
60
+ rack (>= 1.0)
61
+ rails (3.2.21)
62
+ actionmailer (= 3.2.21)
63
+ actionpack (= 3.2.21)
64
+ activerecord (= 3.2.21)
65
+ activeresource (= 3.2.21)
66
+ activesupport (= 3.2.21)
67
+ bundler (~> 1.0)
68
+ railties (= 3.2.21)
69
+ railties (3.2.21)
70
+ actionpack (= 3.2.21)
71
+ activesupport (= 3.2.21)
72
+ rack-ssl (~> 1.3.2)
73
+ rake (>= 0.8.7)
74
+ rdoc (~> 3.4)
75
+ thor (>= 0.14.6, < 2.0)
76
+ rake (10.4.2)
77
+ rdoc (3.12.2)
78
+ json (~> 1.4)
79
+ rspec (3.3.0)
80
+ rspec-core (~> 3.3.0)
81
+ rspec-expectations (~> 3.3.0)
82
+ rspec-mocks (~> 3.3.0)
83
+ rspec-core (3.3.0)
84
+ rspec-support (~> 3.3.0)
85
+ rspec-expectations (3.3.0)
86
+ diff-lcs (>= 1.2.0, < 2.0)
87
+ rspec-support (~> 3.3.0)
88
+ rspec-mocks (3.3.0)
89
+ diff-lcs (>= 1.2.0, < 2.0)
90
+ rspec-support (~> 3.3.0)
91
+ rspec-support (3.3.0)
92
+ sprockets (2.2.3)
93
+ hike (~> 1.2)
94
+ multi_json (~> 1.0)
95
+ rack (~> 1.0)
96
+ tilt (~> 1.1, != 1.3.0)
97
+ thor (0.19.1)
98
+ tilt (1.4.1)
99
+ treetop (1.4.15)
100
+ polyglot
101
+ polyglot (>= 0.3.1)
102
+ tzinfo (0.3.44)
103
+
104
+ PLATFORMS
105
+ ruby
106
+
107
+ DEPENDENCIES
108
+ activemodel (~> 3.2)
109
+ activerecord (~> 3.2)
110
+ activesupport (~> 3.2)
111
+ blsm-mp-wx!
112
+ bundler (~> 1.9)
113
+ faraday (~> 0.9)
114
+ mysql2 (~> 0.3.16)
115
+ rails (~> 3.2)
116
+ rake (~> 10.0)
117
+ rspec (~> 3.2)
118
+
119
+ BUNDLED WITH
120
+ 1.10.3
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Blsm::Mp::Wx
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/blsm/mp/wx`. 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 'blsm-mp-wx'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install blsm-mp-wx
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/blsm-mp-wx/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
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 "blsm_mp_wx"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
Binary file
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blsm_mp_wx/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blsm-mp-wx"
8
+ spec.version = BlsmMpWx::VERSION
9
+ spec.authors = ["saxer"]
10
+ spec.licenses = ["MIT"]
11
+ spec.email = ["15201280641@qq.com"]
12
+
13
+ spec.summary = %q{首趣微店微信公众号基本模块}
14
+ spec.description = %q{包括access_token、js_api_ticket的获取,发送消息,模板消息,生成二维码,获取用户信息等。}
15
+ spec.homepage = "https://bitbucket.org/liuxi/blsm-mp-wx/"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|config)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "rspec", "~> 3.2"
31
+ spec.add_development_dependency "bundler", "~> 1.9"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rails", "~> 3.2"
34
+ spec.add_development_dependency "mysql2", "~> 0.3.16"
35
+ spec.add_development_dependency "activemodel", "~> 3.2"
36
+ spec.add_development_dependency "activerecord", "~> 3.2"
37
+ spec.add_development_dependency "activesupport", "~> 3.2"
38
+ spec.add_development_dependency "faraday", "~> 0.9"
39
+ end
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+ module BlsmMpWx
3
+
4
+ class BaseRecord < ActiveRecord::Base
5
+ self.configurations = YAML::load(ERB.new(BlsmMpWx::DB_CONFIG).result)
6
+ establish_connection Rails::env.to_s.to_sym
7
+ self.table_name='vd_mp_wxes'
8
+ end
9
+
10
+ end
@@ -11,6 +11,13 @@ module BlsmMpWx
11
11
  DateTime.now.to_time > self.access_token_expire.to_time
12
12
  end
13
13
 
14
+ #判断js_api_ticket是否过期了
15
+ def js_api_ticket_expired?
16
+ return true unless self.js_api_ticket
17
+ return true unless self.js_api_ticket_expire
18
+ DateTime.now.to_time > self.js_api_ticket_expire.to_time
19
+ end
20
+
14
21
  #保存token的有效期
15
22
  def save_access_token(access_token, expires_in)
16
23
  update_attributes({
@@ -19,5 +26,14 @@ module BlsmMpWx
19
26
  })
20
27
  end
21
28
 
29
+ #保存js_api_ticket
30
+ def save_js_api_ticket(js_api_ticket, expires_in)
31
+ update_attributes({
32
+ js_api_ticket: js_api_ticket,
33
+ js_api_ticket_expire: Time.at(DateTime.now.to_time.to_i + expires_in.to_i/4)
34
+ })
35
+ end
36
+
37
+
22
38
  end
23
39
  end
@@ -0,0 +1,3 @@
1
+ module BlsmMpWx
2
+ VERSION = "0.1.1"
3
+ end
data/lib/blsm_mp_wx.rb ADDED
@@ -0,0 +1,192 @@
1
+ # coding: utf-8
2
+ require 'cgi'
3
+ require 'rails'
4
+ require 'active_model'
5
+ require 'active_record'
6
+ require 'active_support'
7
+ require 'blsm_mp_wx/version'
8
+ require 'blsm_mp_wx/db_config'
9
+ require 'blsm_mp_wx/model/active_record'
10
+ require 'blsm_mp_wx/model/vd_mp_wx'
11
+ require 'json'
12
+ require 'faraday'
13
+
14
+ module BlsmMpWx
15
+ ERROR_CODES = {
16
+ 404 => {code: 404, msg: '未知的公众号,如有疑问,请联系xi.liu@abcomb.com'}
17
+ }
18
+ class << self
19
+ attr_accessor :app_id
20
+ attr_accessor :app_id_2
21
+
22
+ #根据app_id获取access_token
23
+ #系统会根据当前的access_token即过期时间,自动更新并返回
24
+ #开发者无需关心这里的access_token怎么更新
25
+ #===Parameters
26
+ # * +app_id+ -公众号的appid
27
+ def access_token(app_id)
28
+ app = VdMpWx.where(app_id: app_id).first
29
+ return nil unless app
30
+ return app.access_token unless app.access_token_expired? #没有过期,直接返回
31
+
32
+ response = Faraday.new(:url => 'https://api.weixin.qq.com').get do |req|
33
+ req.url '/cgi-bin/token'
34
+ req.params[:appid] = app.app_id
35
+ req.params[:secret] = app.app_secret
36
+ req.params[:grant_type] = 'client_credential'
37
+ end
38
+
39
+ json_obj = parse_json(response.body)
40
+ return nil unless json_obj
41
+ return nil unless json_obj.has_key?('access_token')
42
+
43
+ access_token = json_obj['access_token']
44
+ app.save_access_token(access_token, json_obj['expires_in'])
45
+
46
+ access_token
47
+ end
48
+
49
+ #获取jsapi全局唯一票据
50
+ def js_api_ticket(app_id)
51
+
52
+ access_token = access_token(app_id)
53
+ app = VdMpWx.where(app_id: app_id).first
54
+
55
+ return nil unless access_token
56
+ return nil unless app
57
+ return app.js_api_ticket unless app.js_api_ticket_expired? #没有过期,直接返回
58
+
59
+ response = Faraday.new(:url => 'https://api.weixin.qq.com').get do |req|
60
+ req.url '/cgi-bin/ticket/getticket'
61
+ req.params[:access_token] = access_token
62
+ req.params[:type] = 'jsapi'
63
+ end
64
+
65
+ json_obj = parse_json(response.body)
66
+ return nil unless json_obj
67
+ return nil unless json_obj.has_key?('ticket')
68
+
69
+ ticket = json_obj['ticket']
70
+ app.save_js_api_ticket(ticket, json_obj['expires_in'])
71
+
72
+ ticket
73
+ end
74
+
75
+ #发送普通消息
76
+ #文本消息:
77
+ # {
78
+ # "touser":"OPENID",
79
+ # "msgtype":"text",
80
+ # "text":
81
+ # {
82
+ # "content":"Hello World"
83
+ # }
84
+ # }
85
+ #图文消息:
86
+ # {
87
+ # "touser":"OPENID",
88
+ # "msgtype":"news",
89
+ # "news":
90
+ # {
91
+ # "articles": [
92
+ # {
93
+ # "title":"Happy Day",
94
+ # "description":"Is Really A Happy Day",
95
+ # "url":"URL",
96
+ # "picurl":"PIC_URL"
97
+ # },
98
+ # {
99
+ # "title":"Happy Day",
100
+ # "description":"Is Really A Happy Day",
101
+ # "url":"URL",
102
+ # "picurl":"PIC_URL"
103
+ # }
104
+ # ]
105
+ # }
106
+ # }
107
+ def send_msg(app_id, openid, msg)
108
+ access_token = access_token(app_id)
109
+ return nil unless access_token
110
+ return nil unless openid
111
+ return nil unless msg[:touser]==openid
112
+
113
+ response = Faraday.new(:url => 'https://api.weixin.qq.com').post do |req|
114
+ req.url '/cgi-bin/message/custom/send'
115
+ req.params[:access_token] = access_token
116
+ req.headers['Content-Type'] = 'application/json'
117
+ req.body = msg.to_json
118
+ req
119
+ end
120
+
121
+ json_obj = parse_json(response.body)
122
+ return nil unless json_obj
123
+ json_obj['errcode']==0
124
+ end
125
+
126
+ #JSAPI签名package
127
+ def js_api_sign_package(app_id, url)
128
+ ticket = js_api_ticket(app_id)
129
+ timestamp = Time.new.to_i
130
+ nonce_str = Digest::MD5.hexdigest(Time.new.to_i.to_s + Random.rand(99999).to_s)
131
+ string = "jsapi_ticket=#{ticket}&noncestr=#{nonce_str}&timestamp=#{timestamp}&url=#{url}"
132
+ signature = Digest::SHA1.hexdigest(string)
133
+ {
134
+ appId: app_id,
135
+ nonceStr: nonce_str,
136
+ timestamp: timestamp,
137
+ url: url,
138
+ signature: signature,
139
+ rawString: string
140
+ }
141
+ end
142
+
143
+
144
+ #获取用户的基本信息(包括UnionID机制)
145
+ def gen_wx_user_info(app_id, openid)
146
+ access_token = access_token(app_id)
147
+ return nil unless access_token
148
+
149
+ response = Faraday.new(:url => 'https://api.weixin.qq.com').get do |req|
150
+ req.url '/cgi-bin/user/info'
151
+ req.params[:openid] = openid
152
+ req.params[:access_token] = access_token
153
+ end
154
+
155
+ json_obj = parse_json(response.body)
156
+ puts "#{json_obj}"
157
+ return nil unless json_obj
158
+ return json_obj.inject({}) { |memo, (key, v)| memo[key.to_s.to_sym]=v; memo } if json_obj.has_key?('openid')
159
+
160
+ nil
161
+ end
162
+
163
+ #生成永久二维码ticket
164
+ def gen_forever_qr_ticket(app_id, scene_id)
165
+ access_token = access_token(app_id)
166
+ return nil unless access_token
167
+
168
+ response = Faraday.new(:url => 'https://api.weixin.qq.com').post do |req|
169
+ req.url '/cgi-bin/qrcode/create'
170
+ req.params[:access_token] = access_token
171
+ req.body="{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": #{scene_id}}}}"
172
+ end
173
+
174
+ json_obj = parse_json(response.body)
175
+ return nil unless json_obj
176
+ return nil unless json_obj.has_key?('ticket')
177
+
178
+ json_obj['ticket']
179
+ end
180
+
181
+ private
182
+ #解析json类型的数据,并将最后的数据转为hash或array
183
+ # +json_data_str+ 要转换的json数据
184
+ def parse_json(json_data_str)
185
+ begin
186
+ return JSON.parse(json_data_str)
187
+ rescue
188
+ end
189
+ nil
190
+ end
191
+ end
192
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blsm-mp-wx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - saxer
@@ -143,8 +143,21 @@ executables: []
143
143
  extensions: []
144
144
  extra_rdoc_files: []
145
145
  files:
146
+ - .gitignore
147
+ - .travis.yml
148
+ - Gemfile
149
+ - Gemfile.lock
150
+ - README.md
151
+ - Rakefile
152
+ - bin/console
153
+ - bin/setup
154
+ - blsm-mp-wx-0.1.0.gem
155
+ - blsm-mp-wx.gemspec
156
+ - lib/blsm_mp_wx.rb
146
157
  - lib/blsm_mp_wx/db_config.rb
158
+ - lib/blsm_mp_wx/model/active_record.rb
147
159
  - lib/blsm_mp_wx/model/vd_mp_wx.rb
160
+ - lib/blsm_mp_wx/version.rb
148
161
  homepage: https://bitbucket.org/liuxi/blsm-mp-wx/
149
162
  licenses:
150
163
  - MIT