好大学接口数据

pre_develop
daiao 6 years ago
parent ecff82dc0c
commit 5afb8d00d6

@ -36,7 +36,7 @@ module Mobile
content_type :json, "application/json;charset=UTF-8" content_type :json, "application/json;charset=UTF-8"
# use ActionDispatch::Session::CookieStore # use ActionDispatch::Session::CookieStore
require 'digest'
use Mobile::Middleware::ErrorHandler use Mobile::Middleware::ErrorHandler
helpers do helpers do
@ -53,6 +53,16 @@ module Mobile
error!('401 Unauthorized', 401) if params[:private_token] != "hriEn3UwXfJs3PmyXnSG" error!('401 Unauthorized', 401) if params[:private_token] != "hriEn3UwXfJs3PmyXnSG"
end end
def cnmooc_access_key!
## 签名
accessKeyId = 'LTAISM4HFWpQHh3g'.freeze
accessKeySecret = '9NMU8ushmFu8SN1EKHOhvo9jmv1qp0'.freeze
sign = Digest::MD5.hexdigest("AccessKeyId=#{accessKeyId}AccessKeySecret=#{accessKeySecret}").upcase
if params[:sign] != sign
error!('401 Unauthorized', 401)
end
end
# 有一些接口没登录也能查看数据 # 有一些接口没登录也能查看数据
def career_authenticate! def career_authenticate!
pass = request.path.include?("introduction") || request.path.include?("get_published_careers")|| request.path.include?("get_current_user") pass = request.path.include?("introduction") || request.path.include?("get_published_careers")|| request.path.include?("get_current_user")
@ -160,6 +170,7 @@ module Mobile
mount Apis::Careers mount Apis::Careers
mount Apis::Assets mount Apis::Assets
mount Apis::Ecloud mount Apis::Ecloud
mount Apis::Cnmooc

@ -0,0 +1,54 @@
# encoding=utf-8
# 好大学接口数据
module Mobile
module Apis
class Cnmooc < Grape::API
before {cnmooc_access_key!}
content_type :json, 'application/json;charset=UTF-8'
resources :cnmoocs do
desc '获取实训数据'
get "get_resources_data" do
CnmoocsService.new.get_resources_data params
end
desc "实训搜索功能"
params do
requires :name, type: String, desc: "搜索名称"
end
get 'search_resources' do
CnmoocsService.new.search_resources params
end
desc " 查找用户"
params do
requires :mail, type: String, desc: "邮箱地址"
end
get 'find_user' do
CnmoocsService.new.find_user params
end
desc "创建用户"
params do
requires :mail, type: String, desc: "邮箱地址"
requires :name, type: String, desc: "昵称"
requires :password, type: String, desc: "密码"
end
post "create_user" do
CnmoocsService.new.create_user params
end
desc "远程登录"
params do
requires :mail, type: String, desc: "邮箱地址"
requires :password, type: String, desc: "密码"
end
get "login_educoder" do
CnmoocsService.new.login_educoder params
end
end
end
end
end

@ -0,0 +1,82 @@
class CnmoocsService
include ApplicationHelper
include GamesHelper
def get_resources_data params
page = params[:pageNo].to_i
limit = params[:pageSize] || 16
offset = page * limit.to_i
shixuns = Shixun.select([:id, :identifier, :name, :myshixuns_count, :averge_star, :challenges_count, :trainee]).
where(status: 0, hidden: 0).order("myshixun_count desc")
shixun_count = shixuns.count
pageCount = ((shixun_count / limit.to_f) == (shixun_count / limit)) ? (shixun_count / limit) : ((shixun_count / limit) + 1)
shixuns = shixuns.offset(offset).limit(limit)
shixun_list = shixun_data shixuns
{error: 0, messages: "请求成功",
page: {count: shixuns.count, totalCount: shixun_count, pageNo: page, pageSize: limit, pageCount: pageCount},
data: shixun_list }
end
def search_resources params
page = params[:pageNo].to_i
limit = params[:pageSize] || 16
offset = page * limit.to_i
shixuns = Shixun.select([:id, :identifier, :name, :myshixuns_count, :averge_star, :challenges_count, :trainee]).
where(status: 2, hidden: 0).where("name like ?", "%#{params[:name]}%").offset(offset).limit(limit)
shixun_count = shixuns.count
pageCount = ((shixun_count / limit.to_f) == (shixun_count / limit)) ? (shixun_count / limit) : ((shixun_count / limit) + 1)
shixuns = shixuns.offset(offset).limit(limit)
shixun_list = shixun_data shixuns
{error: 0, messages: "请求成功",
page: {count: shixuns.count, totalCount: shixun_count, pageNo: page, pageSize: limit, pageCount: pageCount},
data: shixun_list }
end
def find_user params
user = User.find_by_mail params[:mail]
if user
{error: 0, message: "找到用户"}
else
{error: -1, message: "找不到用户"}
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, message: "创建成功"}
else
{error: -1, message: "邮箱已经存在,请直接使用邮箱登录"}
end
end
def login_educoder params
user, last_login_on = User.try_to_login(params[:mail], params[:password])
self.logged_user = user
{error: 0, message: "登录成功"}
end
private
def shixun_data shixuns
shixun_list = []
shixuns.includes(:tag_repertoires).find_each do |shixun|
tag_name = shixun.tag_repertoires.first.try(:name)
level = %W(初级 中级 高级 顶级)[shixun.trainee - 1]
shixun_list << {identifier: shixun.identifier, name: shixun.name, students_count: shixun.myshixuns_count,
challenges_count: shixun.challenges_count, score_info: shixun.averge_star, level: level}
end
{resouces: shixun_list}
end
end
Loading…
Cancel
Save