qy_weixin 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2a1f3474151475c43e48b1cd326df18f4a3b3a014fe92aa4f57ef534fa202ee3
4
+ data.tar.gz: b05a390d877323dbebc75daaf75c6c5d9f05ea9602cb47ee931fe14395466a20
5
+ SHA512:
6
+ metadata.gz: ae2932cf630a12888af91e60851305b0d1e77515df4c6eb8425c1e862b0a81bf30de93aeb3101217332c5e71dde5c8fbadbdef6ae2e1a70926341f8122c9ce87
7
+ data.tar.gz: 6ccdc9490a20879e84e8ad85ce11b162aee03266c1c6569aeaa6ee6073b1b5edec60238e0c07d45b882a8df486158bebf0e604efd80c30574f73af32a3b518e1
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.rdb
2
+ *.swp
3
+ Gemfile.lock
4
+ *.gem
5
+ /tmp/
6
+ /.idea
7
+ /.yardoc
8
+ /.bundle
9
+ /coverage/*
10
+ /doc/
11
+ /examples/sentinel/sentinel.conf
12
+ /nohup.out
13
+ /pkg/*
14
+ /rdsrv
15
+ /redis/*
16
+ /test/db
17
+ /test/test.conf
18
+ appendonly.aof
19
+ temp-rewriteaof-*.aof
20
+ .history
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yol_qy_weixin.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # qy_weixin
2
+ Shop middleware for QyWeixin
3
+
4
+ https://rubygems.org/gems/qy_weixin
5
+
6
+ [![Gem Version](https://badge.fury.io/rb/yol_qy_weixin.svg)](http://badge.fury.io/rb/qy_weixin)
7
+
8
+ **有问题请及时提issue**
9
+
10
+ https://github.com/luojie92/qy_weixin/issues
11
+
12
+ ## Getting started
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'qy_weixin'
18
+ ```
19
+
20
+ You can connect to QyWexinClient by instantiating the Redis class:
21
+
22
+ ```ruby
23
+ QyWexinClient = YolQyWeixin::Client.new(
24
+ corpid : qy_weixin_config["corpid"],
25
+ secret : qy_weixin_config["secret"],
26
+ redis : RedisClient
27
+ )
28
+ ```
29
+ configuration:
30
+
31
+ `corpid`:[https://developer.work.weixin.qq.com/document/path/90665#corpid](https://developer.work.weixin.qq.com/document/path/90665#corpid)
32
+
33
+ `secret`:[https://developer.work.weixin.qq.com/document/path/90665#secret](https://developer.work.weixin.qq.com/document/path/90665#secret)
34
+
35
+ ****RedisClient为redis实例,如果没有配置可传nil,建议使用redis,考虑到access_token获取次数限制;****
36
+
37
+ ## Support methods
38
+
39
+ get access_token:
40
+ ```ruby
41
+ QyWexinClient.get_access_token
42
+
43
+ # => "IFYJSh2oZ4MdWCZsuV-GF_zt...vXbfmgOkkM4ZkJLCb19MuLY5pMFKj4_w"
44
+ ```
45
+
46
+ get user_id:
47
+ ```ruby
48
+ QyWexinClient.get_user_id(code)
49
+
50
+ # => { "errcode": 0, "errmsg": "ok", "UserId":"USERID"}
51
+ ```
52
+ **params:**
53
+
54
+ `code`:https://developer.work.weixin.qq.com/document/path/91019
55
+
56
+
57
+ get user_info:
58
+ ```ruby
59
+ QyWexinClient.get_user_info(userid)
60
+
61
+ # => {"errcode"=>0, "errmsg"=>"ok", "userid"=>"333", "name"=>"罗xx", "department"=>[233], "position"=>"系统开发工程师", "mobile"=>"185xxxx0248", "gender"=>"1", "email"=>"", "avatar"=>"", "status"=>1, "isleader"=>0, "extattr"=>{}}
62
+ ```
63
+
64
+ ---
65
+
66
+ ## 支持
67
+
68
+ 如果你觉得我的gem对你有帮助,可以动动小手右上角点个`starred`:star::star::star:,:star:感谢你的关注:star:
69
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'monitor'
4
+ require 'redis'
5
+ require 'digest/md5'
6
+
7
+ module QyWeixin
8
+ # Client
9
+ class Client
10
+ include Connection::Base
11
+ include Connection::Qrcode
12
+ include Connection::Template
13
+ include Connection::User
14
+ include Connection::Department
15
+ include Connection::Message
16
+ attr_accessor :corpid, :secret, :redis
17
+
18
+ def initialize(options = {})
19
+ @corpid = options[:corpid] || QyWeixin.configuration.corpid
20
+ @secret = options[:secret] || QyWeixin.configuration.secret
21
+ @redis = options[:redis] || QyWeixin.configuration.redis
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dir["#{File.dirname(__FILE__)}/connections/*.rb"].each do |path|
4
+ require path
5
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QyWeixin
4
+ module Connection
5
+ # Base
6
+ module Base
7
+ def http_post(url, params)
8
+ uri = URI(url)
9
+ req = Net::HTTP.new(uri.host, uri.port)
10
+ req.use_ssl = true
11
+ res = req.post("#{uri.path}?#{uri.query}", params.to_json)
12
+ handle_res(res)
13
+ end
14
+
15
+ def http_get(url)
16
+ uri = URI(url)
17
+ req = Net::HTTP.new(uri.host, uri.port)
18
+ req.use_ssl = true
19
+ res = req.get("#{uri.path}?#{uri.query}")
20
+ handle_res(res)
21
+ end
22
+
23
+ def get_access_token
24
+ if redis.nil?
25
+ access_token_res = get_token(corpid, secret)
26
+ access_token = access_token_res['access_token']
27
+ else
28
+ access_token = redis.get('qywx_access_token')
29
+ if access_token.nil?
30
+ access_token_res = get_token(corpid, secret)
31
+ access_token = access_token_res['access_token']
32
+ raise StandardError, "QyWeixin access token authorize false, corpid: #{corpid}" if access_token.nil?
33
+
34
+ redis.set('qywx_access_token', access_token)
35
+ redis.expire('qywx_access_token', 7200)
36
+ end
37
+ end
38
+ access_token
39
+ end
40
+
41
+ private
42
+
43
+ def get_token(app_id, app_secret)
44
+ http_get(token_url(app_id, app_secret))
45
+ end
46
+
47
+ def token_url(corpid, secret)
48
+ "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=#{corpid}&corpsecret=#{secret}"
49
+ end
50
+
51
+ def handle_res(res)
52
+ return JSON.parse(res.body) if res.code == '200'
53
+
54
+ { code: res.code }.to_json
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QyWeixin
4
+ module Connection
5
+ # Department
6
+ module Department
7
+ def department_list(department_id = 0)
8
+ http_get(department_list_url(department_id))
9
+ end
10
+
11
+ private
12
+
13
+ def department_list_url(department_id)
14
+ "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=#{get_access_token}&id=#{department_id}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QyWeixin
4
+ module Connection
5
+ # Message
6
+ module Message
7
+ def send_message(body)
8
+ http_post(send_url, body)
9
+ end
10
+
11
+ private
12
+
13
+ def send_url
14
+ "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=#{get_access_token}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QyWeixin
4
+ module Connection
5
+ # Qrcode
6
+ module Qrcode
7
+ def get_limited_qr(scene_id)
8
+ post_params = { 'expire_seconds': 3600, 'action_name': 'QR_SCENE', 'action_info': { 'scene': { 'scene_id': scene_id } } }
9
+ http_post(qrcode_create_url, post_params)
10
+ end
11
+
12
+ private
13
+
14
+ def qrcode_create_url
15
+ "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=#{get_access_token}"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QyWeixin
4
+ module Connection
5
+ # Template
6
+ module Template
7
+ def send_template_message(template_params)
8
+ http_post(message_template_url, template_params)
9
+ end
10
+
11
+ private
12
+
13
+ def message_template_url
14
+ "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=#{get_access_token}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QyWeixin
4
+ module Connection
5
+ # User
6
+ module User
7
+ def get_users_by_department(department_id, fetch_child = 0)
8
+ http_get(get_users_by_department_url(department_id, fetch_child))
9
+ end
10
+
11
+ def get_user_id(code)
12
+ http_get(user_id_url(code))
13
+ end
14
+
15
+ def get_user_info(open_id)
16
+ http_get(user_info_url(open_id))
17
+ end
18
+
19
+ private
20
+
21
+ def get_users_by_department_url(department_id, fetch_child)
22
+ "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=#{get_access_token}&department_id=#{department_id}&fetch_child=#{fetch_child}"
23
+ end
24
+
25
+ def user_id_url(code)
26
+ "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=#{get_access_token}&code=#{code}"
27
+ end
28
+
29
+ def user_info_url(open_id)
30
+ "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=#{get_access_token}&userid=#{open_id}"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QyWeixin
4
+ # Configuration
5
+ class Configuration
6
+ OPTIONS = %i[corpid secret redis].freeze
7
+ attr_accessor :corpid, :secret, :redis
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QyWeixin
4
+ VERSION = '0.0.1'
5
+ end
data/lib/qy_weixin.rb ADDED
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'qy_weixin/version'
4
+ require 'qy_weixin/models/configuration'
5
+ require 'qy_weixin/connection'
6
+ require 'qy_weixin/client'
7
+
8
+ # QyWeixin
9
+ module QyWeixin
10
+ class << self
11
+ attr_writer :configuration
12
+
13
+ def configure
14
+ yield(configuration)
15
+ end
16
+
17
+ def configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+ end
21
+ end
data/qy_weixin.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './lib/qy_weixin/version'
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'qy_weixin'
6
+ spec.version = QyWeixin::VERSION
7
+ spec.homepage = 'https://github.com/luojie92/qy_weixin'
8
+ spec.authors = ['luojie92']
9
+ spec.email = ['444530861@qq.com']
10
+ spec.summary = 'Shop middleware for QyWeixin.'
11
+ spec.description = ''
12
+ spec.license = 'MIT'
13
+ spec.files = `git ls-files -z`.split("\x0")
14
+ # spec.files = Dir['CHANGELOG.md', 'LICENSE', 'README.md', 'lib/**/*'']
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+ spec.required_ruby_version = '>= 2.4.0'
19
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qy_weixin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - luojie92
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-04-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email:
15
+ - 444530861@qq.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/qy_weixin.rb
26
+ - lib/qy_weixin/client.rb
27
+ - lib/qy_weixin/connection.rb
28
+ - lib/qy_weixin/connections/base.rb
29
+ - lib/qy_weixin/connections/department.rb
30
+ - lib/qy_weixin/connections/message.rb
31
+ - lib/qy_weixin/connections/qrcode.rb
32
+ - lib/qy_weixin/connections/template.rb
33
+ - lib/qy_weixin/connections/user.rb
34
+ - lib/qy_weixin/models/configuration.rb
35
+ - lib/qy_weixin/version.rb
36
+ - qy_weixin.gemspec
37
+ homepage: https://github.com/luojie92/qy_weixin
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.4.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.7.9
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Shop middleware for QyWeixin.
61
+ test_files: []