2016-04-01 18:23:12 +08:00
|
|
|
#coding=utf-8
|
|
|
|
|
|
|
|
module Mobile
|
|
|
|
module Apis
|
|
|
|
class NewComment< Grape::API
|
|
|
|
include ApplicationHelper
|
|
|
|
include ApiHelper
|
2016-08-14 12:32:47 +08:00
|
|
|
include AccountHelper
|
2016-04-01 18:23:12 +08:00
|
|
|
resources :new_comment do
|
|
|
|
|
|
|
|
desc "add a new comment"
|
|
|
|
params do
|
|
|
|
requires :type, type: String
|
|
|
|
requires :content, type: String
|
|
|
|
end
|
2016-04-01 22:38:45 +08:00
|
|
|
post ':id' do
|
2016-08-11 17:22:48 +08:00
|
|
|
unless current_user
|
2016-08-13 10:39:32 +08:00
|
|
|
#如果当前用户不存在 则需要根据微信昵称注册一个帐号
|
2016-08-11 17:22:48 +08:00
|
|
|
openid = session[:wechat_openid]
|
2016-08-12 09:23:09 +08:00
|
|
|
raise "无法获取到openid,请在微信中打开本页面" unless openid
|
2016-08-11 17:22:48 +08:00
|
|
|
us = UsersService.new
|
2016-08-12 10:10:10 +08:00
|
|
|
|
2016-08-13 15:24:52 +08:00
|
|
|
access_token = session[:access_token]
|
|
|
|
refresh_token = session[:refresh_token]
|
|
|
|
|
|
|
|
if access_token.present? && refresh_token.present?
|
2016-08-14 12:13:04 +08:00
|
|
|
begin
|
|
|
|
refreshinfo = Wechat.api.web_refresh_access_token(refresh_token)
|
|
|
|
rescue
|
2016-08-14 12:16:30 +08:00
|
|
|
info = Wechat.api.web_access_token(session[:wechat_code])
|
2016-08-14 12:13:04 +08:00
|
|
|
openid =info["openid"]
|
|
|
|
access_token =info["access_token"]
|
|
|
|
if access_token
|
|
|
|
session[:access_token] = access_token
|
|
|
|
end
|
|
|
|
refresh_token = info["refresh_token"]
|
|
|
|
if refresh_token
|
|
|
|
session[:refresh_token] = refresh_token
|
|
|
|
end
|
|
|
|
refreshinfo = Wechat.api.web_refresh_access_token(refresh_token)
|
|
|
|
end
|
2016-08-14 11:59:26 +08:00
|
|
|
|
2016-08-13 15:24:52 +08:00
|
|
|
access_token = refreshinfo["access_token"]
|
|
|
|
refresh_token = refreshinfo["refresh_token"]
|
|
|
|
session[:access_token] = access_token
|
|
|
|
session[:refresh_token] = refresh_token
|
|
|
|
|
|
|
|
#获取用户信息
|
|
|
|
userinfo = Wechat.api.web_userinfo(access_token,openid)
|
|
|
|
Rails.logger.info "userinfo!!!!!!!!!"
|
|
|
|
Rails.logger.info userinfo
|
|
|
|
name = userinfo["nickname"]
|
|
|
|
else
|
|
|
|
name = openid[0..3]+"***"+openid.last
|
|
|
|
end
|
|
|
|
|
2016-08-13 21:57:22 +08:00
|
|
|
user = us.register ({:login=>openid, :mail=>"#{openid}@163.com",
|
2016-08-12 10:10:10 +08:00
|
|
|
:password=>"12345678", :password_confirmation=>"12345678",
|
|
|
|
:should_confirmation_password => true})
|
2016-08-11 17:22:48 +08:00
|
|
|
raise user.errors.full_messages.first if user.new_record?
|
2016-08-14 12:29:10 +08:00
|
|
|
user.update_attributes(:lastname=>name)
|
|
|
|
#自动激活
|
2016-08-14 12:42:39 +08:00
|
|
|
user = AccountHelper.automatically_register(user)
|
2016-08-11 17:22:48 +08:00
|
|
|
UserWechat.create!(
|
|
|
|
openid: openid,
|
|
|
|
user: user,
|
|
|
|
bindtype: 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-08-13 11:37:37 +08:00
|
|
|
authenticate!
|
|
|
|
|
2016-08-14 09:38:47 +08:00
|
|
|
subscribe = 0 #默认未关注
|
|
|
|
|
|
|
|
#-------------------获取用户是否关注此公众号-----------------------------
|
2016-08-14 09:50:43 +08:00
|
|
|
openid = session[:wechat_openid]
|
|
|
|
raise "无法获取到openid,请在微信中打开本页面" unless openid
|
|
|
|
user_info = Wechat.api.user(openid)
|
|
|
|
Rails.logger.info "user_info!!!!!!!!!"
|
|
|
|
Rails.logger.info user_info
|
|
|
|
subscribe = user_info["subscribe"]
|
2016-08-14 09:38:47 +08:00
|
|
|
|
2016-08-11 09:58:45 +08:00
|
|
|
status = 0
|
|
|
|
tip = 0 #0班级1项目
|
2016-04-01 18:23:12 +08:00
|
|
|
type = params[:type]
|
|
|
|
result = 1
|
|
|
|
if params[:content]!="" && current_user
|
|
|
|
case type
|
|
|
|
when "HomeworkCommon"
|
|
|
|
homework_common = HomeworkCommon.find(params[:id])
|
2016-08-11 09:58:45 +08:00
|
|
|
|
|
|
|
#如果是私有的 并且不是成员则不能回复
|
2016-08-11 17:22:48 +08:00
|
|
|
# is_public = homework_common.course.is_public
|
|
|
|
# if is_public == 0 && !current_user.member_of_course?(homework_common.course)
|
|
|
|
# status = -1
|
|
|
|
# tip = 0
|
|
|
|
# else
|
|
|
|
# feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id])
|
|
|
|
# if (feedback.errors.empty?)
|
|
|
|
# homework_common.update_column(:updated_at, Time.now)
|
|
|
|
# result = 2
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id])
|
|
|
|
if (feedback.errors.empty?)
|
|
|
|
homework_common.update_column(:updated_at, Time.now)
|
|
|
|
result = 2
|
2016-04-01 18:23:12 +08:00
|
|
|
end
|
2016-08-11 09:58:45 +08:00
|
|
|
|
2016-04-01 18:23:12 +08:00
|
|
|
when "News"
|
|
|
|
news = News.find(params[:id])
|
2016-08-11 09:58:45 +08:00
|
|
|
|
2016-08-11 17:22:48 +08:00
|
|
|
# if news.project
|
|
|
|
# if news.project.is_public == false && !current_user.member_of?(news.project)
|
|
|
|
# status = -1
|
|
|
|
# tip = 1
|
|
|
|
# end
|
|
|
|
# elsif news.course
|
|
|
|
# if news.course.is_public == 0 && !current_user.member_of_course?(news.course)
|
|
|
|
# status = -1
|
|
|
|
# tip = 0
|
|
|
|
# end
|
|
|
|
# end
|
2016-08-11 09:58:45 +08:00
|
|
|
|
|
|
|
if status == 0
|
|
|
|
comment = Comment.new
|
|
|
|
comment.comments = params[:content]
|
|
|
|
comment.author = current_user
|
|
|
|
if news.comments << comment
|
|
|
|
result = 2
|
|
|
|
end
|
2016-04-01 18:23:12 +08:00
|
|
|
end
|
|
|
|
when "Message"
|
|
|
|
message = Message.find(params[:id])
|
|
|
|
board = Board.find(message.board_id)
|
2016-08-11 09:58:45 +08:00
|
|
|
|
2016-08-11 17:22:48 +08:00
|
|
|
# if message.project
|
|
|
|
# if message.project.is_public == false && !current_user.member_of?(message.project)
|
|
|
|
# status = -1
|
|
|
|
# tip = 1
|
|
|
|
# end
|
|
|
|
# elsif message.course
|
|
|
|
# if message.course.is_public == 0 && !current_user.member_of_course?(message.course)
|
|
|
|
# status = -1
|
|
|
|
# tip = 0
|
|
|
|
# end
|
|
|
|
# end
|
2016-08-11 09:58:45 +08:00
|
|
|
|
|
|
|
if status == 0
|
|
|
|
topic = message.root
|
|
|
|
reply = Message.new
|
|
|
|
reply.author = current_user
|
|
|
|
reply.board = board
|
|
|
|
reply.content = params[:content]
|
|
|
|
reply.parent_id = params[:id]
|
|
|
|
reply.subject = "RE: #{topic.subject}"
|
|
|
|
if topic.children << reply
|
|
|
|
result = 2
|
|
|
|
end
|
2016-04-01 18:23:12 +08:00
|
|
|
end
|
|
|
|
when "JournalsForMessage"
|
|
|
|
jour = JournalsForMessage.find params[:id]
|
2016-08-11 09:58:45 +08:00
|
|
|
|
2016-08-11 17:22:48 +08:00
|
|
|
# if jour.jour_type == "Project"
|
|
|
|
# if jour.project.is_public == false && !current_user.member_of?(jour.project)
|
|
|
|
# status = -1
|
|
|
|
# tip = 1
|
|
|
|
# end
|
|
|
|
# elsif jour.jour_type == "Course"
|
|
|
|
# if jour.course.is_public == 0 && !current_user.member_of_course?(jour.course)
|
|
|
|
# status = -1
|
|
|
|
# tip = 0
|
|
|
|
# end
|
|
|
|
# end
|
2016-08-11 09:58:45 +08:00
|
|
|
|
|
|
|
if status == 0
|
|
|
|
parent_id = params[:id]
|
|
|
|
author_id = current_user.id
|
|
|
|
reply_user_id = jour.user_id
|
|
|
|
reply_id = params[:id]
|
|
|
|
content = params[:content]
|
|
|
|
options = {:user_id => author_id,
|
|
|
|
:status => true,
|
|
|
|
:m_parent_id => parent_id,
|
|
|
|
:m_reply_id => reply_id,
|
|
|
|
:reply_id => reply_user_id,
|
|
|
|
:notes => content,
|
|
|
|
:is_readed => false}
|
|
|
|
jfm = jour.user.add_jour(nil, nil, nil, options)
|
|
|
|
if jfm.errors.empty?
|
|
|
|
(JournalsForMessage.find parent_id).update_attribute(:updated_on,Time.now)
|
|
|
|
result = 2
|
|
|
|
end
|
2016-04-01 18:23:12 +08:00
|
|
|
end
|
|
|
|
when 'Issue'
|
|
|
|
issue = Issue.find params[:id]
|
2016-08-11 09:58:45 +08:00
|
|
|
|
2016-08-11 17:22:48 +08:00
|
|
|
# if issue.project.is_public == false && !current_user.member_of?(issue.project)
|
|
|
|
# status = -1
|
|
|
|
# tip = 1
|
|
|
|
# end
|
2016-08-11 09:58:45 +08:00
|
|
|
|
|
|
|
if status == 0
|
|
|
|
is_jour = Journal.new
|
|
|
|
is_jour.user_id = current_user.id
|
|
|
|
is_jour.notes = params[:content]
|
|
|
|
is_jour.journalized = issue
|
|
|
|
if is_jour.save
|
|
|
|
result = 2
|
|
|
|
end
|
2016-04-01 18:23:12 +08:00
|
|
|
end
|
|
|
|
when 'BlogComment'
|
|
|
|
blog = BlogComment.find(params[:id]).root
|
|
|
|
blogComment = BlogComment.new
|
|
|
|
blogComment.author = current_user
|
|
|
|
blogComment.blog = blog.blog
|
|
|
|
blogComment.content = params[:content]
|
|
|
|
blogComment.title = "RE: #{blog.title}"
|
2016-04-06 11:40:05 +08:00
|
|
|
if blog.children << blogComment
|
2016-04-01 18:23:12 +08:00
|
|
|
result = 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if result == 2
|
|
|
|
update_course_activity_api(type,params[:id])
|
|
|
|
update_user_activity_api(type,params[:id])
|
|
|
|
update_org_activity_api(type,params[:id])
|
|
|
|
update_forge_activity_api(type,params[:id])
|
|
|
|
update_principal_activity_api(type,params[:id])
|
|
|
|
end
|
|
|
|
else
|
2016-04-06 11:40:05 +08:00
|
|
|
result = 3
|
2016-04-01 18:23:12 +08:00
|
|
|
end
|
2016-04-01 22:38:45 +08:00
|
|
|
present :result, result
|
2016-08-11 09:58:45 +08:00
|
|
|
present :status, status
|
|
|
|
present :tip, tip
|
2016-08-14 09:38:47 +08:00
|
|
|
present :subscribe,subscribe
|
2016-04-01 18:23:12 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|