From 7835ed034eb29d45f71f6a68d9826082a521d755 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Tue, 3 Sep 2019 09:56:54 +0800 Subject: [PATCH] wechat js sdk signature api --- .../wechats/js_sdk_signatures_controller.rb | 8 ++ app/libs/util/wechat.rb | 74 +++++++++++++++++++ config/configuration.yml.example | 3 + config/initializers/aliyun_vod_init.rb | 2 +- config/initializers/util_wechat_init.rb | 16 ++++ config/routes.rb | 4 + 6 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 app/controllers/wechats/js_sdk_signatures_controller.rb create mode 100644 app/libs/util/wechat.rb create mode 100644 config/initializers/util_wechat_init.rb diff --git a/app/controllers/wechats/js_sdk_signatures_controller.rb b/app/controllers/wechats/js_sdk_signatures_controller.rb new file mode 100644 index 000000000..e4ee0da27 --- /dev/null +++ b/app/controllers/wechats/js_sdk_signatures_controller.rb @@ -0,0 +1,8 @@ +class Wechats::JsSdkSignaturesController < ApplicationController + def create + signature = Util::Wechat.js_sdk_signature(params[:url], params[:noncestr], params[:timestamp]) + render_ok(signature: signature) + rescue Util::Wechat::Error => ex + render_error(ex.message) + end +end \ No newline at end of file diff --git a/app/libs/util/wechat.rb b/app/libs/util/wechat.rb new file mode 100644 index 000000000..069322f18 --- /dev/null +++ b/app/libs/util/wechat.rb @@ -0,0 +1,74 @@ +module Util::Wechat + BASE_SITE = 'https://api.weixin.qq.com'.freeze + + Error = Class.new(StandardError) + + class << self + attr_accessor :appid, :secret + + def js_sdk_signature(url, noncestr, timestamp) + str = { jsapi_ticket: jsapi_ticket, noncestr: noncestr, timestamp: timestamp, url: url }.to_query + Digest::SHA1.hexdigest(str) + end + + def access_token + # 7200s 有效时间 + Rails.cache.fetch(access_token_cache_key, expires_in: 100.minutes) do + result = request(:get, '/cgi-bin/token', appid: appid, secret: secret, grant_type: 'client_credential') + result['access_token'] + end + end + + def refresh_access_token + Rails.cache.delete(access_token_cache_key) + access_token + end + + def jsapi_ticket + # 7200s 有效时间 + Rails.cache.fetch(jsapi_ticket_cache_key, expires_in: 100.minutes) do + result = request(:get, '/cgi-bin/ticket/getticket', access_token: access_token, type: 'jsapi') + result['ticket'] + end + end + + def refresh_jsapi_ticket + Rails.cache.delete(jsapi_ticket_cache_key) + jsapi_ticket + end + + def access_token_cache_key + "#{base_cache_key}/access_token" + end + + def jsapi_ticket_cache_key + "#{base_cache_key}/jsapi_ticket" + end + + def base_cache_key + "wechat/#{appid}" + end + + private + + def request(method, url, **params) + Rails.logger.error("[wechat] request: #{method} #{url} #{params.inspect}") + + client = Faraday.new(url: BASE_SITE) + response = client.public_send(method, url, params) + result = JSON.parse(response.body) + + Rails.logger.error("[wechat] response:#{response.status} #{result.inspect}") + + if response.status != 200 + raise Error, result.inspect + end + + if result['errcode'].present? && result['errcode'].to_i.nonzero? + raise Error, result.inspect + end + + result + end + end +end \ No newline at end of file diff --git a/config/configuration.yml.example b/config/configuration.yml.example index 2a4f4f017..6feee28d9 100644 --- a/config/configuration.yml.example +++ b/config/configuration.yml.example @@ -6,6 +6,9 @@ defaults: &defaults cate_id: '-1' callback_url: 'http://47.96.87.25:48080/api/callbacks/aliyun_vod.json' signature_key: 'test12345678' + wechat: + appid: 'test' + secret: 'test' development: <<: *defaults diff --git a/config/initializers/aliyun_vod_init.rb b/config/initializers/aliyun_vod_init.rb index 47b1dc6a3..9185eac20 100644 --- a/config/initializers/aliyun_vod_init.rb +++ b/config/initializers/aliyun_vod_init.rb @@ -4,7 +4,7 @@ aliyun_vod_config = {} begin config = Rails.application.config_for(:configuration) aliyun_vod_config = config['aliyun_vod'] - raise 'oauth wechat config missing' if aliyun_vod_config.blank? + raise 'aliyun vod config missing' if aliyun_vod_config.blank? rescue => ex raise ex if Rails.env.production? diff --git a/config/initializers/util_wechat_init.rb b/config/initializers/util_wechat_init.rb new file mode 100644 index 000000000..cb39afcaf --- /dev/null +++ b/config/initializers/util_wechat_init.rb @@ -0,0 +1,16 @@ +wechat_config = {} + +begin + config = Rails.application.config_for(:configuration) + wechat_config = config['wechat'] + raise 'wechat config missing' if wechat_config.blank? +rescue => ex + raise ex if Rails.env.production? + + puts %Q{\033[33m [warning] wechat config or configuration.yml missing, + please add it or execute 'cp config/configuration.yml.example config/configuration.yml' \033[0m} + wechat_config = {} +end + +Util::Wechat.appid = wechat_config['appid'] +Util::Wechat.secret = wechat_config['secret'] diff --git a/config/routes.rb b/config/routes.rb index 22808ba10..2b0cd3030 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -880,6 +880,10 @@ Rails.application.routes.draw do end end + namespace :wechats do + resource :js_sdk_signature, only: [:create] + end + #git 认证回调 match 'gitauth/*url', to: 'gits#auth', via: :all