diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index a84ba559c..918c5d73c 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -9,6 +9,7 @@ module Mobile class API < Grape::API version 'v1', using: :path format :json + content_type :json, "application/json;charset=UTF-8" helpers do def logger diff --git a/app/api/mobile/apis/auth.rb b/app/api/mobile/apis/auth.rb index 41b948a13..acd5fe1e2 100644 --- a/app/api/mobile/apis/auth.rb +++ b/app/api/mobile/apis/auth.rb @@ -11,9 +11,10 @@ module Mobile if user ::ApiKey.delete_all(user_id: user.id) key = ::ApiKey.create!(user_id: user.id) - {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json) + data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json) + {status: 0, data: data} else - error!('Unauthorized.', 401) + {status: 1, message: 'Unauthorized.'} end end end diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index 7518e544b..bd146e235 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -20,6 +20,7 @@ module Mobile @courses = @courses_all.order("created_at desc") @s_type = 0 @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) + {status: 0, data: @courses} # @course_activity_count=get_course_activity @courses, @course_activity_count end @@ -29,7 +30,8 @@ module Mobile end route_param :id do get do - Course.find(params[:id]) + course = Course.find(params[:id]) + {status: 0, data: course} end end diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index 1eaf7c87b..8b7edd155 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -14,12 +14,12 @@ module Mobile post do user = User.new user.login = params[:login] - user.mail = params[:email] + user.mail = params[:mail] user.password = params[:password] user.password_confirmation = params[:password] user.activate if user.save! - UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0) + UserStatus.create(:user_id => user.id, :changsets_course => 0, :watchers_count => 0) end user end diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index 279e89f13..2ca202191 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -192,6 +192,11 @@ class AccountController < ApplicationController redirect_to signin_url end + def api_register login,password,email + users_service = UsersService.new + users_service.register({login: login, password: password, email: eamil}) + end + def valid_ajax req = Hash.new(false) req[:message] = '' diff --git a/app/services/users_service.rb b/app/services/users_service.rb new file mode 100644 index 000000000..aa2c6bf71 --- /dev/null +++ b/app/services/users_service.rb @@ -0,0 +1,31 @@ +class UsersService + + #将用户注册的功能函数写这里 + #参数约定 + #成功返回注册后的User实例,失败直接抛异常 + def register(params) + @user = User.new + @user.admin = false + @user.register + @user.login = params[:login] + @user.mail = params[:email] + unless password.blank? + @user.password = params[:password] + end + case Setting.self_registration + when '1' + register_by_email_activation(@user) + when '3' + register_automatically(@user) + else + register_manully_by_administrator(@user) + end + if @user.id != nil + ue = @user.user_extensions ||= UserExtensions.new + ue.user_id = @user.id + ue.save + end + @user + end + +end diff --git a/test/mobile/auth_test.rb b/test/mobile/auth_test.rb new file mode 100644 index 000000000..afc5719e8 --- /dev/null +++ b/test/mobile/auth_test.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../test_helper.rb',__FILE__) + +class AuthTest < ActionDispatch::IntegrationTest + def setup + ApiKey.delete_all + end + + test "login success when use correct password" do + post('/api/v1/auth/login.json', {login: 'guange', password: '123456'}) + o = ActiveSupport::JSON.decode response.body + puts o + puts o["token"] + assert_not_nil o["token"] + end + + test "login failure when incorrect password" do + post('/api/v1/auth/login.json', {login: 'guange', password: 'wrongpass'}) + o = ActiveSupport::JSON.decode response.body + assert_nil o["token"] + assert_equal o["error"], 'Unauthorized.' + end +end + + diff --git a/test/mobile/users_test.rb b/test/mobile/users_test.rb new file mode 100644 index 000000000..48b8010b4 --- /dev/null +++ b/test/mobile/users_test.rb @@ -0,0 +1,12 @@ +require File.expand_path('../../test_helper.rb',__FILE__) + +class UsersTest < ActionDispatch::IntegrationTest + test "register a user" do + end + test "get all users" do + get '/api/v1/users.json' + assert_response :success + end +end + +