diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index c86a36d05..058687776 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -67,8 +67,7 @@ module Mobile mount Apis::NewComment mount Apis::Praise - #add_swagger_documentation ({api_version: 'v1', base_path: 'http://u06.shellinfo.cn/trustie/api'}) - #add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development? + add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development? end end diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index 3a36a9e37..b585e7dae 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -97,25 +97,22 @@ module Mobile desc "加入课程" params do - requires :course_password, type: String + optional :openid, type: String, desc: '微信ID' + requires :invite_code, type: String, desc: '邀请码' end - post ":id" do + post "join" do authenticate! + cs = CoursesService.new - status = cs.join_course({:object_id => params[:id],:course_password => params[:course_password]},current_user) - out = {status: status[:state]} - message = case status[:state] - when 0; "加入成功" - when 1; "密码错误" - when 2; "课程已过期 请联系课程管理员重启课程。(在配置课程处)" - when 3; "您已经加入了课程" - when 4; "您加入的课程不存在" - when 5; "您还未登录" - else; "未知错误,请稍后再试" - end - out.merge(message: message) + status = cs.join_course({openid: params[:openid], invite_code: params[:invite_code]}, current_user) + + { + status: status[:state], + messsge:CoursesService::JoinCourseError.message(status[:state]) + } end + desc "退出课程" params do requires :token, type: String diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb index fb1231287..e993b120f 100644 --- a/app/helpers/api_helper.rb +++ b/app/helpers/api_helper.rb @@ -489,4 +489,26 @@ module ApiHelper self.update_attribute(:praise_num, self.praise_num.to_i - num) end + + class Errors + def self.define_error(arr) + @errors = {} + arr.each_with_index { |item, index| + if index %2 == 1 + @errors[arr[index-1]] = item + end + } + if arr.count % 2== 1 + @default_error = arr.last + else + @default_error = "未知错误" + end + + end + + def self.message(msg_id) + @errors[msg_id] || @default_error + end + end + end \ No newline at end of file diff --git a/app/models/course.rb b/app/models/course.rb index 69dbec5d0..d379b24d3 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,3 +1,5 @@ +#coding=utf-8 + require 'elasticsearch/model' class Course < ActiveRecord::Base include Redmine::SafeAttributes @@ -404,6 +406,7 @@ class Course < ActiveRecord::Base self.course_messages << CourseMessage.new(:user_id => self.tea_id, :course_id => self.id, :viewed => false) end + #项目与课程分离后,很多课程的名称等信息为空,这些数据信息存储在项目表中!!就是数据兼容的问题 #def name # read_attribute('name') || Project.find_by_identifier(self.extra).try(:name) @@ -450,6 +453,22 @@ class Course < ActiveRecord::Base end end + # 生成邀请码 + CODES = %W(2 3 4 5 6 7 8 9 A B C D E F G H J K L N M O P Q R S T U V W X Y Z) + def generate_invite_code + code = invite_code + if !invite_code || invite_code.size <6 + self.invite_code = CODES.sample(6).join + save! && reload + code = invite_code + end + code + end + + def + + end + end diff --git a/app/services/courses_service.rb b/app/services/courses_service.rb index 2c8387349..93529f57c 100644 --- a/app/services/courses_service.rb +++ b/app/services/courses_service.rb @@ -300,23 +300,31 @@ class CoursesService @state end + class JoinCourseError < Errors + define_error [ + 0, '加入成功', + 1, '密码错误', + 2, '课程已过期 请联系课程管理员重启课程。', + 3, '您已经加入了课程', + 4, '您加入的课程不存在', + 5, '您还未登录', + 6, '申请成功,请等待审核完毕', + 7, '您已经发送过申请了,请耐心等待', + 8, '您已经是该课程的教师了', + 9, '您已经是该课程的教辅了', + 10, '您已经是该课程的管理员了', + '未知错误,请稍后再试' + ] + end #加入课程 #object_id:课程id #course_password :加入课程的密码 - #@state == 0 加入成功 - #@state == 1 密码错误 - #@state == 2 课程已过期 请联系课程管理员重启课程。(在配置课程处) - #@state == 3 您已经加入了课程 - #@state == 4 您加入的课程不存在 - #@state == 5 您还未登录 - #@state == 6 申请成功,请等待审核完毕 - #@state == 7 您已经发送过申请了,请耐心等待 - #@state == 8 您已经是该课程的教师了 - #@state == 9 您已经是该课程的教辅了 - #@state == 10 您已经是该课程的管理员了 - #@state 其他 未知错误,请稍后再试 def join_course params,current_user - course = Course.find_by_id params[:object_id] + course = if params[:invite_code] + Course.find_by_invite_code(params[:invite_code]) + else + Course.find_by_id params[:object_id] + end @state = 10 if course diff --git a/db/migrate/20160614072229_add_invite_code_to_course.rb b/db/migrate/20160614072229_add_invite_code_to_course.rb new file mode 100644 index 000000000..8f7bb8da1 --- /dev/null +++ b/db/migrate/20160614072229_add_invite_code_to_course.rb @@ -0,0 +1,5 @@ +class AddInviteCodeToCourse < ActiveRecord::Migration + def change + add_column :courses, :invite_code, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 95ffceef1..820c6a035 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20160612043259) do +ActiveRecord::Schema.define(:version => 20160614072229) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -550,6 +550,7 @@ ActiveRecord::Schema.define(:version => 20160612043259) do t.integer "excellent_option", :default => 0 t.integer "is_copy", :default => 0 t.integer "visits", :default => 0 + t.string "invite_code" end create_table "custom_fields", :force => true do |t|