api可以通过openid来认证

This commit is contained in:
guange 2016-06-12 17:18:05 +08:00
parent 15e074417c
commit 1cb63055cb
4 changed files with 17 additions and 10 deletions

View File

@ -29,16 +29,22 @@ module Mobile
end
def authenticate!
raise('Unauthorized. Invalid or expired token.') unless current_user
raise('Unauthorized. 用户认证失败.') unless current_user
end
def current_user
openid = params[:openid]
if openid
uw = UserWechat.find_by_openid(params[:openid])
return uw.user if uw
end
token = ApiKey.where(access_token: params[:token]).first
if token && !token.expired?
@current_user = User.find(token.user_id)
else
nil
return User.find(token.user_id)
end
nil
end
end

View File

@ -12,7 +12,9 @@ module Mobile
requires :openid, type: String
end
post do
user = UserWechat.find_by_openid(params[:openid]).user
authenticate!
user = current_user
shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Project'").map(&:shield_id)
shield_course_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Course'").map(&:shield_id)

View File

@ -97,7 +97,6 @@ module Mobile
desc "加入课程"
params do
requires :token, type: String
requires :course_password, type: String
end
post ":id" do

View File

@ -5,11 +5,11 @@ default: &default
# Or if using public account, only need above two line
# guange test
appid: "wxf694495398c7d470"
secret: "743e038392f1d89540e95f8f7645849a"
#appid: "wxf694495398c7d470"
#secret: "743e038392f1d89540e95f8f7645849a"
#appid: "wx8e1ab05163a28e37"
#secret: "beb4d3bc4b32b3557811680835357841"
appid: "wx8e1ab05163a28e37"
secret: "beb4d3bc4b32b3557811680835357841"
token: "123456"
access_token: "1234567"