From ef95c59e5962027dadf26299672817872342c5ef Mon Sep 17 00:00:00 2001 From: jingquan huang Date: Tue, 16 Jul 2019 18:00:54 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E8=AE=B0=E5=BD=95=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=B5=81=E9=87=8F=E8=A1=8C=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/memos.rb | 8 ++++++++ app/models/user_agent.rb | 3 +++ db/migrate/20190716073605_create_user_agents.rb | 12 ++++++++++++ spec/factories/user_agents.rb | 7 +++++++ spec/models/user_agent_spec.rb | 5 +++++ 5 files changed, 35 insertions(+) create mode 100644 app/models/user_agent.rb create mode 100644 db/migrate/20190716073605_create_user_agents.rb create mode 100644 spec/factories/user_agents.rb create mode 100644 spec/models/user_agent_spec.rb diff --git a/app/api/mobile/apis/memos.rb b/app/api/mobile/apis/memos.rb index c8c0041a..04191f53 100644 --- a/app/api/mobile/apis/memos.rb +++ b/app/api/mobile/apis/memos.rb @@ -29,6 +29,14 @@ module Mobile requires :id, type: Integer, desc: "帖子ID" end get ':id' do + # 记录用户流量源 + if params[:edu] + ip = request.host + ua = UserAgent.find_by_ip(ip) + unless ua + UserAgent.create(:key => params[:edu].strip, :ip => request.host) + end + end MemosService.new.show params, current_user end diff --git a/app/models/user_agent.rb b/app/models/user_agent.rb new file mode 100644 index 00000000..b71c8449 --- /dev/null +++ b/app/models/user_agent.rb @@ -0,0 +1,3 @@ +class UserAgent < ActiveRecord::Base + +end diff --git a/db/migrate/20190716073605_create_user_agents.rb b/db/migrate/20190716073605_create_user_agents.rb new file mode 100644 index 00000000..22bc181e --- /dev/null +++ b/db/migrate/20190716073605_create_user_agents.rb @@ -0,0 +1,12 @@ +class CreateUserAgents < ActiveRecord::Migration + def change + create_table :user_agents do |t| + t.string :type + t.string :key + t.string :ip + + t.timestamps + end + add_index(:user_agents, :ip, :unique => true) + end +end diff --git a/spec/factories/user_agents.rb b/spec/factories/user_agents.rb new file mode 100644 index 00000000..c8afcb41 --- /dev/null +++ b/spec/factories/user_agents.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :user_agent do + type 1 + key "MyString" + ip "MyString" + end +end diff --git a/spec/models/user_agent_spec.rb b/spec/models/user_agent_spec.rb new file mode 100644 index 00000000..20fd5cf3 --- /dev/null +++ b/spec/models/user_agent_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe UserAgent, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 400de970192c71eadda8b59083d6ac86f948a3da Mon Sep 17 00:00:00 2001 From: jingquan huang Date: Tue, 16 Jul 2019 18:15:02 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B5=81=E9=87=8F=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E6=B7=BB=E5=8A=A0remote=20ip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/memos.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/api/mobile/apis/memos.rb b/app/api/mobile/apis/memos.rb index 04191f53..22382a51 100644 --- a/app/api/mobile/apis/memos.rb +++ b/app/api/mobile/apis/memos.rb @@ -31,10 +31,10 @@ module Mobile get ':id' do # 记录用户流量源 if params[:edu] - ip = request.host + ip = @env['REMOTE_HOST'] ua = UserAgent.find_by_ip(ip) unless ua - UserAgent.create(:key => params[:edu].strip, :ip => request.host) + UserAgent.create(:key => params[:edu].strip, :ip => ip) end end MemosService.new.show params, current_user From 8971da893c1bfb6bd6104bd05d00e4100cb0beed Mon Sep 17 00:00:00 2001 From: hjm <63528605@qq.com> Date: Tue, 16 Jul 2019 18:17:50 +0800 Subject: [PATCH 3/6] window.location.search --- public/react/src/modules/forums/MemoDetail.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/react/src/modules/forums/MemoDetail.js b/public/react/src/modules/forums/MemoDetail.js index 075b2cd9..264f9be8 100644 --- a/public/react/src/modules/forums/MemoDetail.js +++ b/public/react/src/modules/forums/MemoDetail.js @@ -53,7 +53,7 @@ class MemoDetail extends Component { const { match } = this.props - const memoUrl = `/api/v1/memos/${match.params.memoId}`; + const memoUrl = `/api/v1/memos/${match.params.memoId}${window.location.search}`; this.setState({ memoLoading: true }) From 4a6a032b058d54f35bcbaa2f5579826ad65a14aa Mon Sep 17 00:00:00 2001 From: jingquan huang Date: Tue, 16 Jul 2019 20:17:37 +0800 Subject: [PATCH 4/6] log --- app/api/mobile/apis/memos.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/api/mobile/apis/memos.rb b/app/api/mobile/apis/memos.rb index 22382a51..7bb06c33 100644 --- a/app/api/mobile/apis/memos.rb +++ b/app/api/mobile/apis/memos.rb @@ -31,6 +31,9 @@ module Mobile get ':id' do # 记录用户流量源 if params[:edu] + Rails.info("####11111222##{@env}") + Rails.info("####1111122255##{env}") + Rails.info("####11111222666##{request}") ip = @env['REMOTE_HOST'] ua = UserAgent.find_by_ip(ip) unless ua From d8e09b70800972a7fdb8f904031715329c1acfb5 Mon Sep 17 00:00:00 2001 From: jingquan huang Date: Tue, 16 Jul 2019 20:22:03 +0800 Subject: [PATCH 5/6] log --- app/api/mobile/apis/memos.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/api/mobile/apis/memos.rb b/app/api/mobile/apis/memos.rb index 7bb06c33..3c7796ed 100644 --- a/app/api/mobile/apis/memos.rb +++ b/app/api/mobile/apis/memos.rb @@ -31,9 +31,9 @@ module Mobile get ':id' do # 记录用户流量源 if params[:edu] - Rails.info("####11111222##{@env}") - Rails.info("####1111122255##{env}") - Rails.info("####11111222666##{request}") + Rails.logger.info("####11111222##{@env}") + Rails.logger.info("####1111122255##{env}") + Rails.logger.info("####11111222666##{request}") ip = @env['REMOTE_HOST'] ua = UserAgent.find_by_ip(ip) unless ua From 87b884786cd6569dd99c945486172d64a29ecb71 Mon Sep 17 00:00:00 2001 From: jingquan huang Date: Tue, 16 Jul 2019 20:31:28 +0800 Subject: [PATCH 6/6] add remote ip --- app/api/mobile/apis/memos.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/api/mobile/apis/memos.rb b/app/api/mobile/apis/memos.rb index 3c7796ed..14654d10 100644 --- a/app/api/mobile/apis/memos.rb +++ b/app/api/mobile/apis/memos.rb @@ -31,10 +31,8 @@ module Mobile get ':id' do # 记录用户流量源 if params[:edu] - Rails.logger.info("####11111222##{@env}") - Rails.logger.info("####1111122255##{env}") - Rails.logger.info("####11111222666##{request}") - ip = @env['REMOTE_HOST'] + Rails.logger.info("####11111222##{@env['HTTP_X_REAL_IP']}") + ip = @env['HTTP_X_REAL_IP'] ua = UserAgent.find_by_ip(ip) unless ua UserAgent.create(:key => params[:edu].strip, :ip => ip)