添加邀请码

This commit is contained in:
guange 2016-06-15 13:44:28 +08:00
parent ada15b4212
commit 8fd4d518b2
7 changed files with 81 additions and 30 deletions

View File

@ -67,8 +67,7 @@ module Mobile
mount Apis::NewComment mount Apis::NewComment
mount Apis::Praise 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
end end

View File

@ -97,25 +97,22 @@ module Mobile
desc "加入课程" desc "加入课程"
params do params do
requires :course_password, type: String optional :openid, type: String, desc: '微信ID'
requires :invite_code, type: String, desc: '邀请码'
end end
post ":id" do post "join" do
authenticate! authenticate!
cs = CoursesService.new cs = CoursesService.new
status = cs.join_course({:object_id => params[:id],:course_password => params[:course_password]},current_user) status = cs.join_course({openid: params[:openid], invite_code: params[:invite_code]}, current_user)
out = {status: status[:state]}
message = case status[:state] {
when 0; "加入成功" status: status[:state],
when 1; "密码错误" messsge:CoursesService::JoinCourseError.message(status[:state])
when 2; "课程已过期 请联系课程管理员重启课程。(在配置课程处)" }
when 3; "您已经加入了课程"
when 4; "您加入的课程不存在"
when 5; "您还未登录"
else; "未知错误,请稍后再试"
end
out.merge(message: message)
end end
desc "退出课程" desc "退出课程"
params do params do
requires :token, type: String requires :token, type: String

View File

@ -489,4 +489,26 @@ module ApiHelper
self.update_attribute(:praise_num, self.praise_num.to_i - num) self.update_attribute(:praise_num, self.praise_num.to_i - num)
end 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 end

View File

@ -1,3 +1,5 @@
#coding=utf-8
require 'elasticsearch/model' require 'elasticsearch/model'
class Course < ActiveRecord::Base class Course < ActiveRecord::Base
include Redmine::SafeAttributes 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) self.course_messages << CourseMessage.new(:user_id => self.tea_id, :course_id => self.id, :viewed => false)
end end
#项目与课程分离后,很多课程的名称等信息为空,这些数据信息存储在项目表中!!就是数据兼容的问题 #项目与课程分离后,很多课程的名称等信息为空,这些数据信息存储在项目表中!!就是数据兼容的问题
#def name #def name
# read_attribute('name') || Project.find_by_identifier(self.extra).try(:name) # read_attribute('name') || Project.find_by_identifier(self.extra).try(:name)
@ -450,6 +453,22 @@ class Course < ActiveRecord::Base
end end
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 end

View File

@ -300,23 +300,31 @@ class CoursesService
@state @state
end end
class JoinCourseError < Errors
define_error [
0, '加入成功',
1, '密码错误',
2, '课程已过期 请联系课程管理员重启课程。',
3, '您已经加入了课程',
4, '您加入的课程不存在',
5, '您还未登录',
6, '申请成功,请等待审核完毕',
7, '您已经发送过申请了,请耐心等待',
8, '您已经是该课程的教师了',
9, '您已经是该课程的教辅了',
10, '您已经是该课程的管理员了',
'未知错误,请稍后再试'
]
end
#加入课程 #加入课程
#object_id课程id #object_id课程id
#course_password :加入课程的密码 #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 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 @state = 10
if course if course

View File

@ -0,0 +1,5 @@
class AddInviteCodeToCourse < ActiveRecord::Migration
def change
add_column :courses, :invite_code, :string
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # 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| create_table "activities", :force => true do |t|
t.integer "act_id", :null => false t.integer "act_id", :null => false
@ -550,6 +550,7 @@ ActiveRecord::Schema.define(:version => 20160612043259) do
t.integer "excellent_option", :default => 0 t.integer "excellent_option", :default => 0
t.integer "is_copy", :default => 0 t.integer "is_copy", :default => 0
t.integer "visits", :default => 0 t.integer "visits", :default => 0
t.string "invite_code"
end end
create_table "custom_fields", :force => true do |t| create_table "custom_fields", :force => true do |t|