add cnmooc use create api

pre_develop
p31729568 6 years ago
parent 71d2066131
commit 02aa938fe4

@ -30,9 +30,8 @@ module Mobile
desc "创建用户"
params do
requires :mail, type: String, desc: "邮箱地址"
requires :name, type: String, desc: "昵称"
requires :password, type: String, desc: "密码"
requires :userName, type: String, desc: "好大学用户名"
optional :name, type: String, desc: "用户姓名"
end
post "create_user" do
CnmoocsService.new.create_user params

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

@ -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

@ -43,22 +43,32 @@ class CnmoocsService
end
end
def create_user params
user = User.find_by_mail params[:mail]
if user.blank?
ActiveRecord::Base.transaction do
# 如果Educoder中已存在与该OpenI用户的邮箱相同的用户则会直接跳转到登录educoder的登录页面
user = User.new(lastname: params[:name], mail: params[:mail], mail_notification: email)
user.login = generate_login('m')
user.password = params[:password]
user.certification = 1
user.save!
UserExtensions.create!(user_id: user.id, school_id: School.first.id, identity: 4, gender: 0)
end
{error: 0, messages: "创建成功"}
else
{error: -1, messages: "邮箱已经存在,请直接使用邮箱登录"}
def create_user(params)
c_user = CnmoocUser.find_by_uuid(params[:userName])
if c_user.present?
return { error: -1, messages: '用户已存在' }
end
mail = c_user.generate_email
create_params = {
lastname: params[:name],
mail: mail,
mail_notification: mail,
login: generate_login('m'),
password: OauthController::DEFAULT_PASSWORD,
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
{ error: 0, messages: "创建成功", data: { userId: user.id } }
end
def login_educoder params
@ -85,5 +95,4 @@ class CnmoocsService
end
{resouces: shixun_list}
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