From 9d2900d58f5390bf21b7e887ad1d4b49fd07cf68 Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Mon, 25 Jul 2016 16:59:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=B2=A1=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=B8=8D=E8=B7=B3=E8=BD=AC=E5=88=B0=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E9=A1=B5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 +- app/api/mobile/api.rb | 4 ++-- app/api/mobile/exceptions/auth_exception.rb | 13 ++++++++++++ app/api/mobile/middleware/error_handler.rb | 8 +++++++- app/controllers/wechats_controller.rb | 20 +++++++++++-------- .../lib/acts_as_watchable.rb | 2 +- public/javascripts/wechat/others/factory.js | 4 ++-- public/javascripts/wechat/others/routes.js | 12 +++++++++-- 8 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 app/api/mobile/exceptions/auth_exception.rb diff --git a/Gemfile b/Gemfile index 03dbb5785..a94f1b33b 100644 --- a/Gemfile +++ b/Gemfile @@ -51,7 +51,7 @@ gem 'elasticsearch-rails' ### profile -gem 'oneapm_rpm' +# gem 'oneapm_rpm' group :development do gem 'grape-swagger' diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index cf169cce7..3ff9895a6 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -43,9 +43,9 @@ module Mobile end def current_user - openid = params[:openid] + openid = session[:wechat_openid] if openid - uw = UserWechat.find_by_openid(params[:openid]) + uw = UserWechat.find_by_openid(openid) return uw.user if uw end diff --git a/app/api/mobile/exceptions/auth_exception.rb b/app/api/mobile/exceptions/auth_exception.rb new file mode 100644 index 000000000..5dceab572 --- /dev/null +++ b/app/api/mobile/exceptions/auth_exception.rb @@ -0,0 +1,13 @@ +#coding=utf-8 +# +module Mobile + module Exceptions + class AuthException < StandardError + attr_reader :err_code, :msg + def initialize(code, msg) + @err_code = code + @msg = msg + end + end + end +end diff --git a/app/api/mobile/middleware/error_handler.rb b/app/api/mobile/middleware/error_handler.rb index 699dd1537..94d74e568 100644 --- a/app/api/mobile/middleware/error_handler.rb +++ b/app/api/mobile/middleware/error_handler.rb @@ -1,3 +1,6 @@ +#coding=utf-8 + + module Mobile module Middleware class ErrorHandler < Grape::Middleware::Base @@ -6,7 +9,10 @@ module Mobile begin @app.call(@env) rescue =>e - message = {status: 1, message: e.message }.to_json + code = 1 + + message = {status: code, message: e.message }.to_json + Rails.logger.error e.inspect Rails.logger.error e.backtrace.join("\n") status = 200 diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index 6b1e3010d..c12590400 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -288,8 +288,7 @@ class WechatsController < ActionController::Base session[:wechat_code] = params[:code] if params[:code] openid = get_openid_from_code(params[:code]) @wechat_user = user_binded?(openid) - - render 'wechats/login', layout: 'base_wechat' + render 'wechats/user_activities', layout: nil end def user_activities @@ -299,13 +298,18 @@ class WechatsController < ActionController::Base unless open_id render 'wechats/open_wechat', layout: nil and return end - if params[:state] == 'myclass' - @course_id = params[:id]; - end + + unless user_binded?(open_id) + @path = '/login' + else + if params[:state] == 'myclass' + @course_id = params[:id]; + end - session[:wechat_openid] = open_id - if params[:code] - redirect_to "/wechat/user_activities##{@path}?id=#{params[:id]}" and return + session[:wechat_openid] = open_id + if params[:code] + redirect_to "/wechat/user_activities##{@path}?id=#{params[:id]}" and return + end end render 'wechats/user_activities', layout: nil end diff --git a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb index 06d661478..5fd428166 100644 --- a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb +++ b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb @@ -10,7 +10,7 @@ module Redmine def acts_as_watchable(options = {}) return if self.included_modules.include?(Redmine::Acts::Watchable::InstanceMethods) class_eval do - has_many :watchers, :as => :watchable, :dependent => :delete_all + has_many :watchers, :dependent => :delete_all has_many :watcher_users, :through => :watchers, :source => :user, :validate => false scope :watched_by, lambda { |user_id| diff --git a/public/javascripts/wechat/others/factory.js b/public/javascripts/wechat/others/factory.js index c9bdf4458..11281c8b8 100644 --- a/public/javascripts/wechat/others/factory.js +++ b/public/javascripts/wechat/others/factory.js @@ -60,7 +60,7 @@ app.factory('auth', ['$http','$routeParams', '$q', 'session', 'config',function( } var getToken = function(){ - return session.get("token"); + return 'token';//session.get("token"); } return {get_bind: isBind, token: getToken}; }]); @@ -300,4 +300,4 @@ app.factory('common', ['$http', 'auth', '$routeParams','rms','config', function( } return {init: init, addCommonReply: addCommonReply, loadCommonData: loadCommonData, addCommonPraise: addCommonPraise, decreaseCommonPraise: decreaseCommonPraise}; -}]); \ No newline at end of file +}]); diff --git a/public/javascripts/wechat/others/routes.js b/public/javascripts/wechat/others/routes.js index 5b131433e..458f45360 100644 --- a/public/javascripts/wechat/others/routes.js +++ b/public/javascripts/wechat/others/routes.js @@ -8,8 +8,8 @@ app.config(['$routeProvider',"$httpProvider", "$locationProvider",'config', func var makeRoute = function(path, ctrl){ return { templateUrl: rootPath + path, - controller: ctrl, - resolve: resolve + controller: ctrl + //resolve: resolve } } //$locationProvider.html5Mode(true); @@ -59,6 +59,14 @@ app.config(['$routeProvider',"$httpProvider", "$locationProvider",'config', func return rejection; }, response: function (response) { + // var data = response.data; + // if(data && data.status==405){ + // var rootScope = $injector.get('$rootScope'); + // var state = $injector.get('$rootScope').$state.current.name; + // rootScope.stateBeforLogin = state; + // rootScope.$state.go("login"); + // return $q.reject(response); + // } $rootScope.activeCalls -= 1; return response; },