diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 058687776..9ffac4e10 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -23,6 +23,7 @@ module Mobile version 'v1', using: :path format :json content_type :json, "application/json;charset=UTF-8" + use ActionDispatch::Session::CookieStore use Mobile::Middleware::ErrorHandler helpers do @@ -34,6 +35,10 @@ module Mobile raise('Unauthorized. 用户认证失败.') unless current_user end + def session + env['rack.session'] + end + def current_user openid = params[:openid] if openid diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index e3041abcd..b5ee14d19 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -23,10 +23,10 @@ module Mobile params do requires :login, type: String, desc: 'username' requires :password, type: String, desc: 'password' - requires :openid, type: String, desc: 'wechat openid' end post 'wxbind' do - openid = params[:openid] + openid = session[:wechat_openid] + logger.debug "openid ============== #{openid}" raise "无法获取到openid,请在微信中打开本页面" unless openid uw = UserWechat.where(openid: openid).first raise "此微信号已绑定用户(#{uw.user.login}), 不能重复绑定" if uw diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index 6517c756e..dc1e26508 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -191,14 +191,22 @@ class WechatsController < ActionController::Base ### controller method module Controllers - def get_open_id + def get_bind begin - code = params[:code] || session[:wechat_code] openid = get_openid_from_code(code) raise "无法获取到微信openid" unless openid - render :json => {status:0, openid: openid} + + uw = UserWechat.where(openid: openid).first + raise "还未绑定trustie帐户" unless uw + logger.debug "get_bind ============= #{uw}" + + user = uw.user + ::ApiKey.delete_all(user_id: user.id) + key = ::ApiKey.create!(user_id: user.id) + + render :json =>{status: 0, token: key.access_token} rescue Exception=>e render :json => {status: -1, message: e.message} end @@ -240,21 +248,20 @@ class WechatsController < ActionController::Base end def user_activities - # session[:wechat_code] = params[:code] if params[:code] - # code = params[:code] || session[:wechat_code] - # openid = get_openid_from_code(code) - # @wechat_user = user_binded?(openid) - # unless @wechat_user - # redirect_to login_wechat_path - # return - # end session[:wechat_code] = params[:code] if params[:code] - @code = params[:code] || session[:wechat_code] + @path = '/'+(params[:state] || '') + open_id = get_openid_from_code(params[:code]) rescue + unless open_id + render 'wechats/open_wechat', layout: nil and return + end + session[:wechat_openid] = open_id render 'wechats/user_activities', layout: nil end + private def get_openid_from_code(code) + return 'oCnvgvz8R7QheXE-R9Kkr39j8Ndg' if code =='only-for-test' openid = session[:wechat_openid] unless openid diff --git a/app/views/wechats/open_wechat.html.erb b/app/views/wechats/open_wechat.html.erb new file mode 100644 index 000000000..4584e81b5 --- /dev/null +++ b/app/views/wechats/open_wechat.html.erb @@ -0,0 +1,18 @@ + + +
+ + + + + + diff --git a/app/views/wechats/user_activities.html.erb b/app/views/wechats/user_activities.html.erb index 6cb32850a..f920aed4b 100644 --- a/app/views/wechats/user_activities.html.erb +++ b/app/views/wechats/user_activities.html.erb @@ -13,9 +13,9 @@ @@ -26,6 +26,24 @@ + + + + + + + + + + + + + + + + + +