parent
11b107987e
commit
6f05ea63cd
@ -0,0 +1,22 @@
|
|||||||
|
class BindUsersController < ApplicationController
|
||||||
|
before_action :require_login
|
||||||
|
|
||||||
|
def create
|
||||||
|
user = CreateBindUserService.call(current_user, create_params)
|
||||||
|
successful_authentication(user) if user.id != current_user.id
|
||||||
|
|
||||||
|
render_ok
|
||||||
|
rescue ApplicationService::Error => ex
|
||||||
|
render_error(ex.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_user
|
||||||
|
current_user
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def create_params
|
||||||
|
params.permit(:login, :password, :type, :not_bind)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,51 @@
|
|||||||
|
class CreateBindUserService < ApplicationService
|
||||||
|
attr_reader :user, :params
|
||||||
|
|
||||||
|
def initialize(user, params)
|
||||||
|
@user = user
|
||||||
|
@params = params
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
raise Error, '系统错误' if open_user.blank?
|
||||||
|
raise Error, '系统错误' unless can_bind_user?
|
||||||
|
|
||||||
|
if params[:not_bind].to_s == 'true'
|
||||||
|
clear_can_bind_user_flag
|
||||||
|
return user
|
||||||
|
end
|
||||||
|
|
||||||
|
bind_user = User.try_to_login(params[:login], params[:password])
|
||||||
|
raise Error, '用户名或者密码错误' if bind_user.blank?
|
||||||
|
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
open_user.user_id = bind_user.id
|
||||||
|
open_user.save!
|
||||||
|
|
||||||
|
user.destroy!
|
||||||
|
end
|
||||||
|
|
||||||
|
clear_can_bind_user_flag
|
||||||
|
|
||||||
|
bind_user
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def open_user
|
||||||
|
@_open_user ||= begin
|
||||||
|
case params[:type].to_s
|
||||||
|
when 'wechat' then user.wechat_open_user
|
||||||
|
when 'qq' then user.qq_open_user
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def can_bind_user?
|
||||||
|
Rails.cache.read(open_user.can_bind_cache_key).present?
|
||||||
|
end
|
||||||
|
|
||||||
|
def clear_can_bind_user_flag
|
||||||
|
Rails.cache.delete(open_user.can_bind_cache_key)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue