From 4e6c58ae371e276b3c2b954f7d54a5215ba51ede Mon Sep 17 00:00:00 2001 From: yuanke <249218296@qq.com> Date: Thu, 11 Aug 2016 17:22:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=82=B9=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E6=B2=A1=E7=BB=91=E5=AE=9A=E5=88=99=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=9B=E5=BB=BA=E4=B8=80=E4=B8=AA=E5=B8=90=E5=8F=B7?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=EF=BC=8C=E7=84=B6=E5=90=8E=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E5=B0=86=E5=B7=B2=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E6=B3=A8=E5=86=8C=E7=9A=84=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/new_comment.rb | 117 +++++++++++------- app/api/mobile/apis/users.rb | 31 +++-- app/controllers/wechats_controller.rb | 3 +- app/models/user_wechat.rb | 2 +- app/services/wechat_service.rb | 24 ++-- app/views/wechats/user_activities.html.erb | 1 + config/wechat.yml.template | 4 + config/wechat.yml.test | 4 + ...0811084401_add_bindtype_to_user_wechats.rb | 5 + .../wechat/controllers/invite_code.js | 4 +- .../wechat/controllers/project_invite_code.js | 5 +- public/javascripts/wechat/others/factory.js | 26 ++-- 12 files changed, 141 insertions(+), 85 deletions(-) create mode 100644 db/migrate/20160811084401_add_bindtype_to_user_wechats.rb diff --git a/app/api/mobile/apis/new_comment.rb b/app/api/mobile/apis/new_comment.rb index 10308774f..4228a5c7d 100644 --- a/app/api/mobile/apis/new_comment.rb +++ b/app/api/mobile/apis/new_comment.rb @@ -15,6 +15,24 @@ module Mobile end post ':id' do # authenticate! + + unless current_user + #如果当前用户不存在 + openid = session[:wechat_openid] + raise "请在微信中关注Trustie创新实践平台后再打开本页面" unless openid + + us = UsersService.new + #login mail password + user = us.register ({:login=>openid, :mail=>openid+"@163.com", + :password=>"12345678", :password_confirmation=>"12345678", :should_confirmation_password => true}) + raise user.errors.full_messages.first if user.new_record? + UserWechat.create!( + openid: openid, + user: user, + bindtype: 1 + ) + end + status = 0 tip = 0 #0班级1项目 type = params[:type] @@ -25,32 +43,37 @@ module Mobile homework_common = HomeworkCommon.find(params[:id]) #如果是私有的 并且不是成员则不能回复 - is_public = homework_common.course.is_public - if is_public == 0 && !current_user.member_of_course?(homework_common.course) - status = -1 - tip = 0 - else - feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id]) - if (feedback.errors.empty?) - homework_common.update_column(:updated_at, Time.now) - result = 2 - end + # is_public = homework_common.course.is_public + # if is_public == 0 && !current_user.member_of_course?(homework_common.course) + # status = -1 + # tip = 0 + # else + # feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id]) + # if (feedback.errors.empty?) + # homework_common.update_column(:updated_at, Time.now) + # result = 2 + # end + # end + feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id]) + if (feedback.errors.empty?) + homework_common.update_column(:updated_at, Time.now) + result = 2 end when "News" news = News.find(params[:id]) - if news.project - if news.project.is_public == false && !current_user.member_of?(news.project) - status = -1 - tip = 1 - end - elsif news.course - if news.course.is_public == 0 && !current_user.member_of_course?(news.course) - status = -1 - tip = 0 - end - end + # if news.project + # if news.project.is_public == false && !current_user.member_of?(news.project) + # status = -1 + # tip = 1 + # end + # elsif news.course + # if news.course.is_public == 0 && !current_user.member_of_course?(news.course) + # status = -1 + # tip = 0 + # end + # end if status == 0 comment = Comment.new @@ -64,17 +87,17 @@ module Mobile message = Message.find(params[:id]) board = Board.find(message.board_id) - if message.project - if message.project.is_public == false && !current_user.member_of?(message.project) - status = -1 - tip = 1 - end - elsif message.course - if message.course.is_public == 0 && !current_user.member_of_course?(message.course) - status = -1 - tip = 0 - end - end + # if message.project + # if message.project.is_public == false && !current_user.member_of?(message.project) + # status = -1 + # tip = 1 + # end + # elsif message.course + # if message.course.is_public == 0 && !current_user.member_of_course?(message.course) + # status = -1 + # tip = 0 + # end + # end if status == 0 topic = message.root @@ -91,17 +114,17 @@ module Mobile when "JournalsForMessage" jour = JournalsForMessage.find params[:id] - if jour.jour_type == "Project" - if jour.project.is_public == false && !current_user.member_of?(jour.project) - status = -1 - tip = 1 - end - elsif jour.jour_type == "Course" - if jour.course.is_public == 0 && !current_user.member_of_course?(jour.course) - status = -1 - tip = 0 - end - end + # if jour.jour_type == "Project" + # if jour.project.is_public == false && !current_user.member_of?(jour.project) + # status = -1 + # tip = 1 + # end + # elsif jour.jour_type == "Course" + # if jour.course.is_public == 0 && !current_user.member_of_course?(jour.course) + # status = -1 + # tip = 0 + # end + # end if status == 0 parent_id = params[:id] @@ -125,10 +148,10 @@ module Mobile when 'Issue' issue = Issue.find params[:id] - if issue.project.is_public == false && !current_user.member_of?(issue.project) - status = -1 - tip = 1 - end + # if issue.project.is_public == false && !current_user.member_of?(issue.project) + # status = -1 + # tip = 1 + # end if status == 0 is_jour = Journal.new diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index 34558563d..3a0526a52 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -54,21 +54,34 @@ module Mobile requires :password, type: String, desc: 'password' end post do - openid = session[:wechat_openid] logger.debug "openid ============== #{openid}" # raise "无法获取到openid,请在微信中打开本页面" unless openid raise "请在微信中关注Trustie创新实践平台后再打开本页面" unless openid + uw = UserWechat.where(openid: openid).first + if uw + uw.bindtype = 0 + uw.save + user = uw.user + user[:login] = params[:login] + user[:mail] = params[:mail] + user[:password] = params[:password] + user[:password_confirmation] = params[:password] + user[:should_confirmation_password] = true + user.save! + else + us = UsersService.new + user = us.register params.merge(:password_confirmation => params[:password], + :should_confirmation_password => true) + raise user.errors.full_messages.first if user.new_record? + + UserWechat.create!( + openid: openid, + user: user + ) + end - us = UsersService.new - user = us.register params.merge(:password_confirmation => params[:password], - :should_confirmation_password => true) - raise user.errors.full_messages.first if user.new_record? - UserWechat.create!( - openid: openid, - user: user - ) ws = WechatService.new ws.binding_succ_notice(user.id, "您已成功绑定Trustie平台。", user.login, Time.now.strftime("%Y-%m-%d")) present :data, user, with: Mobile::Entities::User diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index 24103ede3..5c2b0bade 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -424,7 +424,7 @@ class WechatsController < ActionController::Base session[:wechat_openid] = open_id if params[:code] - redirect_to "/wechat/user_activities##{@path}?id=#{params[:id]}" and return + redirect_to "/wechat/user_activities##{@path}?id=#{params[:id]}&userid=#{params[:userid]}" and return end # end render 'wechats/user_activities', layout: nil @@ -451,6 +451,7 @@ class WechatsController < ActionController::Base def user_binded?(openid) uw = UserWechat.where(openid: openid).first + uw && uw.bindtype == 0 end def current_url diff --git a/app/models/user_wechat.rb b/app/models/user_wechat.rb index 49053dbf0..370ab8fd1 100644 --- a/app/models/user_wechat.rb +++ b/app/models/user_wechat.rb @@ -1,6 +1,6 @@ class UserWechat < ActiveRecord::Base attr_accessible :subscribe, :openid, :nickname, :sex, :language, :city, :province, :country, - :headimgurl, :subscribe_time, :unionid, :remark, :groupid, :user, :user_id + :headimgurl, :subscribe_time, :unionid, :remark, :groupid, :user, :user_id, :bindtype belongs_to :user end diff --git a/app/services/wechat_service.rb b/app/services/wechat_service.rb index c7eb3491a..cd06759fc 100644 --- a/app/services/wechat_service.rb +++ b/app/services/wechat_service.rb @@ -112,9 +112,11 @@ class WechatService end def two_keys_template(openid, template_id, type, id, first, key1, key2, remark="",uid) - tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}" + # tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}" + tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+type+Wechat.config.auto_openid_url_3 if uid && uid != 0 - tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s + # tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s + tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+ "&user_id="+uid.to_s+Wechat.config.auto_openid_url_2+type+Wechat.config.auto_openid_url_3 end data = { touser:openid, @@ -144,9 +146,11 @@ class WechatService end def three_keys_template(openid, template_id, type, id, first, key1, key2, key3, remark="",uid) - tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}" + # tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}" + tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+type+Wechat.config.auto_openid_url_3 if uid && uid != 0 - tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s + # tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s + tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+ "&user_id="+uid.to_s+Wechat.config.auto_openid_url_2+type+Wechat.config.auto_openid_url_3 end data = { @@ -181,9 +185,11 @@ class WechatService end def four_keys_template(openid, template_id, type, id, first, key1, key2, key3, key4, remark="",uid) - tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}" + # tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}" + tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+type+Wechat.config.auto_openid_url_3 if uid && uid != 0 - tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s + # tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s + tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+ "&user_id="+uid.to_s+Wechat.config.auto_openid_url_2+type+Wechat.config.auto_openid_url_3 end data = { @@ -227,7 +233,7 @@ class WechatService data = { touser:uw.openid, template_id:Wechat.config.binding_succ_notice, - url:"#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities", + url:Wechat.config.auto_openid_url_1+Wechat.config.auto_openid_url_2+"activites"+Wechat.config.auto_openid_url_3, topcolor:"#FF0000", data:{ first: { @@ -315,7 +321,7 @@ class WechatService data = { touser:uw.openid, template_id:Wechat.config.create_class_notice, - url:"#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/class?id="+id.to_s, + url:Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+"class"+Wechat.config.auto_openid_url_3, topcolor:"#FF0000", data:{ first: { @@ -356,7 +362,7 @@ class WechatService data = { touser:uw.openid, template_id:Wechat.config.create_project_notice, - url:"#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/project?id="+id.to_s, + url:Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+"project"+Wechat.config.auto_openid_url_3, topcolor:"#FF0000", data:{ first: { diff --git a/app/views/wechats/user_activities.html.erb b/app/views/wechats/user_activities.html.erb index 8c31c2106..6fc29aba1 100644 --- a/app/views/wechats/user_activities.html.erb +++ b/app/views/wechats/user_activities.html.erb @@ -18,6 +18,7 @@ window.apiUrl = '/api/v1/'; window.g_redirect_path = '<%= @path %>'; window.g_localhost = "<%= Setting.protocol%>://"+"<%= Setting.host_name%>"; + window.g_appid = <%= @appid %> <% if @course_id %> window.g_courseid = <%= @course_id %>; <% elsif @project_id %> diff --git a/config/wechat.yml.template b/config/wechat.yml.template index ce3008729..e6b942f09 100644 --- a/config/wechat.yml.template +++ b/config/wechat.yml.template @@ -24,6 +24,10 @@ default: &default create_project_notice: "jYu0iimbDpgWYZaTLXioZe2lvqoWTdKnUPyphTJ1mxs" project_review_notice: "kdb-8UlMjTc3z51Qcf8g2vY4i_nE4OGKZAucdQma_2E" join_project_notice: "TtXvy0XMIQyCgpnXHhoB8t-x0QIfy-78gAJXsGf9afg" + + auto_openid_url_1: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8e1ab05163a28e37&redirect_uri=https://www.trustie.net/wechat/user_activities" + auto_openid_url_2: "&response_type=code&scope=snsapi_base&state=" + auto_openid_url_3: "&connect_redirect=1#wechat_redirect" production: <<: *default diff --git a/config/wechat.yml.test b/config/wechat.yml.test index a5339d398..c0676cc26 100644 --- a/config/wechat.yml.test +++ b/config/wechat.yml.test @@ -25,6 +25,10 @@ default: &default project_review_notice: "ip192wVXTav3qchgUn9_7B6lFfTlCZjwL7A1tncTOuc" join_project_notice: "3KnMQEMUCmQWkB5JvzrpmguEwnN8bvUHUdpOTudxv_M" + auto_openid_url_1: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09454f171153c2d&redirect_uri=https://test.forge.trustie.net/wechat/user_activities" + auto_openid_url_2: "&response_type=code&scope=snsapi_base&state=" + auto_openid_url_3: "&connect_redirect=1#wechat_redirect" + production: <<: *default diff --git a/db/migrate/20160811084401_add_bindtype_to_user_wechats.rb b/db/migrate/20160811084401_add_bindtype_to_user_wechats.rb new file mode 100644 index 000000000..6cba9e675 --- /dev/null +++ b/db/migrate/20160811084401_add_bindtype_to_user_wechats.rb @@ -0,0 +1,5 @@ +class AddBindtypeToUserWechats < ActiveRecord::Migration + def change + add_column :user_wechats, :bindtype, :integer, :default => 0 + end +end diff --git a/public/javascripts/wechat/controllers/invite_code.js b/public/javascripts/wechat/controllers/invite_code.js index 71826d54d..512e506e8 100644 --- a/public/javascripts/wechat/controllers/invite_code.js +++ b/public/javascripts/wechat/controllers/invite_code.js @@ -14,8 +14,8 @@ app.controller('InviteCodeController', ['$scope','$http', '$routeParams','config vm.course = response.data.data; var desc = "您的好友邀请您加入班级:\n"+vm.course.name+"\n和小伙伴一起踏上便捷的学习之旅吧!"; - var link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09454f171153c2d" + - "&redirect_uri=https://test.forge.trustie.net/wechat/user_activities?id="+vm.course.id+ + var link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+window.g_appid+ + "&redirect_uri="+window.g_localhost+"/wechat/user_activities?id="+vm.course.id+ "&response_type=code&scope=snsapi_base&state=invite_code&connect_redirect=1#wechat_redirect"; common.wxConfig("",desc,"",link); diff --git a/public/javascripts/wechat/controllers/project_invite_code.js b/public/javascripts/wechat/controllers/project_invite_code.js index 0729e882e..8058797f1 100644 --- a/public/javascripts/wechat/controllers/project_invite_code.js +++ b/public/javascripts/wechat/controllers/project_invite_code.js @@ -14,9 +14,8 @@ app.controller('ProjectInviteCodeController', ['$scope','$http', '$routeParams', vm.project = response.data.data; var desc = "您的好友邀请您加入项目:\n"+vm.project.name+"\n和小伙伴一起踏上便捷的研发之旅吧!"; - - var link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09454f171153c2d" + - "&redirect_uri=https://test.forge.trustie.net/wechat/user_activities?id="+vm.project.id+ + var link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+window.g_appid+ + "&redirect_uri="+window.g_localhost+"/wechat/user_activities?id="+vm.project.id+ "&response_type=code&scope=snsapi_base&state=project_invite_code&connect_redirect=1#wechat_redirect"; common.wxConfig("",desc,"",link); diff --git a/public/javascripts/wechat/others/factory.js b/public/javascripts/wechat/others/factory.js index 3ad11fe66..f0d48dda9 100644 --- a/public/javascripts/wechat/others/factory.js +++ b/public/javascripts/wechat/others/factory.js @@ -96,15 +96,15 @@ app.factory('rms', function(){ app.factory('common', ['$http', 'auth', '$routeParams','rms','config','wx','$location', function($http, auth, $routeParams,rms,config,wx,$location){ var addCommonReply = function(id, type, data,args, cb){ //先判断有没有绑定 - $http.post( - '/wechat/is_bind', - {} ///不用传code了,都由服务器来处理 - ).then(function(response){ - console.log(response.data); - if(response.data.status != 0){ - $location.path("/login_tip"); - } - }); +// $http.post( +// '/wechat/is_bind', +// {} ///不用传code了,都由服务器来处理 +// ).then(function(response){ +// console.log(response.data); +// if(response.data.status != 0){ +// $location.path("/login_tip"); +// } +// }); if(!data.comment || data.comment.length<=0){ return; @@ -266,13 +266,13 @@ app.factory('common', ['$http', 'auth', '$routeParams','rms','config','wx','$loc if(args.urlName == "discussion"){ var urlName = response.data.type_name + "_discussion"; - link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09454f171153c2d" + - "&redirect_uri=https://test.forge.trustie.net/wechat/user_activities?id="+response.data.data.act_id+ + link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+window.g_appid + + "&redirect_uri="+window.g_localhost+"/wechat/user_activities?id="+response.data.data.act_id+ "&response_type=code&scope=snsapi_base&state="+urlName+"&connect_redirect=1#wechat_redirect"; } else{ - link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09454f171153c2d" + - "&redirect_uri=https://test.forge.trustie.net/wechat/user_activities?id="+response.data.data.act_id+ + link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+window.g_appid + + "&redirect_uri="+window.g_localhost+"/wechat/user_activities?id="+response.data.data.act_id+ "&response_type=code&scope=snsapi_base&state="+args.urlName+"&connect_redirect=1#wechat_redirect"; }