pre_develop
daiao 6 years ago
commit c98a07c7df

@ -30,14 +30,23 @@ module Mobile
desc "创建用户" desc "创建用户"
params do params do
requires :mail, type: String, desc: "邮箱地址" requires :userName, type: String, desc: "好大学用户名"
requires :name, type: String, desc: "昵称" optional :name, type: String, desc: "用户姓名"
requires :password, type: String, desc: "密码"
end end
post "create_user" do post "create_user" do
CnmoocsService.new.create_user params CnmoocsService.new.create_user params
end end
desc "获取资源访问地址"
params do
requires :userId, type: Integer, desc: "用户ID"
requires :resouceId, type: String, desc: "资源唯一标示"
requires :accessType, type: Integer, desc: "资源类型"
end
get "source_url" do
CnmoocsService.new.source_url(params)
end
desc "远程登录" desc "远程登录"
params do params do
requires :mail, type: String, desc: "邮箱地址" requires :mail, type: String, desc: "邮箱地址"

@ -207,6 +207,9 @@ class ApplicationController < ActionController::Base
elsif session[:wechat_openid] elsif session[:wechat_openid]
uw = UserWechat.find_by_openid(session[:wechat_openid]) uw = UserWechat.find_by_openid(session[:wechat_openid])
user = uw.user if uw user = uw.user if uw
elsif session[:third_party_user_id]
c_user = UserSource.find_by_id(session[:third_party_user_id])
user = c_user.user if c_user
end end
end end
if user.nil? && Setting.rest_api_enabled? && accept_api_auth? if user.nil? && Setting.rest_api_enabled? && accept_api_auth?

@ -0,0 +1,8 @@
class CnmoocUser < UserSource
private
def email_prefix
'cnmooc_'
end
end

@ -269,6 +269,8 @@ class User < Principal
has_many :article_homepages, :dependent => :destroy has_many :article_homepages, :dependent => :destroy
has_many :competition_lists, :dependent => :destroy has_many :competition_lists, :dependent => :destroy
has_one :user_source
## end ## end
# default_scope -> { includes(:user_extensions, :user_score) } # default_scope -> { includes(:user_extensions, :user_score) }

@ -0,0 +1,21 @@
class UserSource < ActiveRecord::Base
belongs_to :user
def generate_email
email = rand_email
while User.exists?(mail: email) do
email = rand_email
end
email
end
private
def rand_email
email_prefix + Random.rand.to_s[2..8] + '@educoder.com'
end
def email_prefix
''
end
end

@ -41,22 +41,32 @@ class CnmoocsService
end end
end end
def create_user params def create_user(params)
user = User.find_by_mail params[:mail] c_user = CnmoocUser.find_by_uuid(params[:userName])
if user.blank?
ActiveRecord::Base.transaction do if c_user.present?
# 如果Educoder中已存在与该OpenI用户的邮箱相同的用户则会直接跳转到登录educoder的登录页面 return { error: -1, messages: '用户已存在' }
user = User.new(lastname: params[:name], mail: params[:mail], mail_notification: email) end
user.login = generate_login('m')
user.password = params[:password] mail = c_user.generate_email
user.certification = 1 create_params = {
user.save! lastname: params[:name],
UserExtensions.create!(user_id: user.id, school_id: School.first.id, identity: 4, gender: 0) mail: mail,
end mail_notification: mail,
{error: 0, messages: "创建成功"} login: generate_login('m'),
else password: OauthController::DEFAULT_PASSWORD,
{error: -1, messages: "邮箱已经存在,请直接使用邮箱登录"} certification: 1
}
ActiveRecord::Base.transaction do
user = User.create!(create_params)
UserExtensions.create!(user_id: user.id, school_id: School.first.id, identity: 4, gender: 0)
c_user.user_id = user.id
c_user.save!
end end
{ error: 0, messages: "创建成功", data: { userId: user.id } }
end end
def login_educoder params def login_educoder params
@ -70,6 +80,20 @@ class CnmoocsService
end end
def source_url(params)
if session[:third_party_user_id].blank?
user = User.find(params[:userId])
session[:third_party_user_id] = user.user_source.id
end
shixun = Shixun.find_by_identifier(params[:resouceId])
if shixun.blank?
return { error: -1, messages: '资源不存在' }
end
{ error: 0, messages: '成功', accessUrl: "#{Redmine::Configuration['educoder_domain']}/shixuns/#{shixun.id}" }
end
private private
def shixun_data shixuns def shixun_data shixuns
@ -83,5 +107,4 @@ class CnmoocsService
end end
{resouces: shixun_list} {resouces: shixun_list}
end end
end end

@ -0,0 +1,15 @@
class CreateUserSources < ActiveRecord::Migration
def change
create_table :user_sources do |t|
t.string :type
t.integer :user_id
t.string :uuid
t.string :name
t.timestamps
end
add_index :user_sources, [:type, :uuid], unique: true
add_index :user_sources, :user_id
end
end
Loading…
Cancel
Save