diff --git a/app/api/mobile/apis/cnmooc.rb b/app/api/mobile/apis/cnmooc.rb index f49b4f66..a45989c3 100644 --- a/app/api/mobile/apis/cnmooc.rb +++ b/app/api/mobile/apis/cnmooc.rb @@ -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 diff --git a/app/models/cnmooc_user.rb b/app/models/cnmooc_user.rb new file mode 100644 index 00000000..e834c768 --- /dev/null +++ b/app/models/cnmooc_user.rb @@ -0,0 +1,8 @@ +class CnmoocUser < UserSource + + private + + def email_prefix + 'cnmooc_' + end +end diff --git a/app/models/user_source.rb b/app/models/user_source.rb new file mode 100644 index 00000000..d8fb350d --- /dev/null +++ b/app/models/user_source.rb @@ -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 diff --git a/app/services/cnmoocs_service.rb b/app/services/cnmoocs_service.rb index 33f166da..5057fabe 100644 --- a/app/services/cnmoocs_service.rb +++ b/app/services/cnmoocs_service.rb @@ -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 \ No newline at end of file diff --git a/db/migrate/20190528075558_create_user_sources.rb b/db/migrate/20190528075558_create_user_sources.rb new file mode 100644 index 00000000..03f6c12a --- /dev/null +++ b/db/migrate/20190528075558_create_user_sources.rb @@ -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