Merge branch 'weixin_guange' of https://git.trustie.net/jacknudt/trustieforge into weixin_guange
commit
85cfd542e6
@ -0,0 +1,32 @@
|
|||||||
|
#coding=utf-8
|
||||||
|
|
||||||
|
module Mobile
|
||||||
|
module Apis
|
||||||
|
class Activities< Grape::API
|
||||||
|
resources :activities do
|
||||||
|
|
||||||
|
desc "get user activities"
|
||||||
|
get ':id' do
|
||||||
|
#uw = UserWechat.find params[:openid]
|
||||||
|
user = User.find params[:id]
|
||||||
|
shield_project_ids = ShieldActivity.where("container_type='User' and container_id=#{user.id} and shield_type='Project'").map(&:shield_id)
|
||||||
|
shield_course_ids = ShieldActivity.where("container_type='User' and container_id=#{user.id} and shield_type='Course'").map(&:shield_id)
|
||||||
|
page = params[:page] ? params[:page].to_i + 1 : 0
|
||||||
|
user_project_ids = (user.projects.visible.map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (user.projects.visible.map{|project| project.id}-shield_project_ids).join(",") + ")"
|
||||||
|
user_course_ids = (user.courses.visible.map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (user.courses.visible.map{|course| course.id}-shield_course_ids).join(",") + ")"
|
||||||
|
course_types = "('Message','News','HomeworkCommon','Poll','Course')"
|
||||||
|
project_types = "('Message','Issue','ProjectCreateInfo')"
|
||||||
|
principal_types = "JournalsForMessage"
|
||||||
|
|
||||||
|
blog_ids = "("+user.blog.id.to_s+","+((User.watched_by(user.id).count == 0 )? '0' :User.watched_by(user.id).map{|u| u.blog.id}.join(','))+")"
|
||||||
|
activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" +
|
||||||
|
"or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+
|
||||||
|
"or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{user.id}) " +
|
||||||
|
"or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc').limit(10).offset(page * 10)
|
||||||
|
present :data, activities, with: Mobile::Entities::Activity
|
||||||
|
present :status, 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,17 @@
|
|||||||
|
#coding=utf-8
|
||||||
|
|
||||||
|
module Mobile
|
||||||
|
module Apis
|
||||||
|
class Whomeworks< Grape::API
|
||||||
|
resources :whomeworks do
|
||||||
|
|
||||||
|
desc "get one homework"
|
||||||
|
get ':id' do
|
||||||
|
homework = HomeworkCommon.find params[:id]
|
||||||
|
present :data, homework, with: Mobile::Entities::Whomework
|
||||||
|
present :status, 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,102 @@
|
|||||||
|
# encoding: utf-8
|
||||||
|
module Mobile
|
||||||
|
module Entities
|
||||||
|
class Activity <Grape::Entity
|
||||||
|
include ApplicationHelper
|
||||||
|
include ApiHelper
|
||||||
|
def self.act_expose(f)
|
||||||
|
expose f do |ac,opt|
|
||||||
|
if ac.is_a?(Hash) && ac.key?(f)
|
||||||
|
ac[f]
|
||||||
|
elsif ac.is_a?(::UserActivity)
|
||||||
|
if ac.respond_to?(f)
|
||||||
|
ac.send(f)
|
||||||
|
else
|
||||||
|
case f
|
||||||
|
when :user_act
|
||||||
|
if ac.act_type == "ProjectCreateInfo"
|
||||||
|
ac unless ac.nil?
|
||||||
|
else
|
||||||
|
ac.act unless ac.nil? || ac.act.nil?
|
||||||
|
end
|
||||||
|
when :reply_num
|
||||||
|
if ac.act_type == "HomeworkCommon"
|
||||||
|
ac.nil? || ac.act.nil? ? 0 : ac.act.journals_for_messages.count
|
||||||
|
elsif ac.act_type == "News"
|
||||||
|
ac.nil? || ac.act.nil? ? 0 : ac.act.comments.count
|
||||||
|
elsif ac.act_type == "Message" || ac.act_type == "BlogComment" || ac.act_type == "JournalsForMessage"
|
||||||
|
ac.nil? || ac.act.nil? ? 0 : ac.act.children.count
|
||||||
|
elsif ac.act_type == "Issue"
|
||||||
|
ac.nil? || ac.act.nil? ? 0 : ac.act.journals.count
|
||||||
|
end
|
||||||
|
when :activity_praise_num
|
||||||
|
if ac.act_type == "HomeworkCommon" || ac.act_type == "News" || ac.act_type == "Message" || ac.act_type == "BlogComment" || ac.act_type == "JournalsForMessage" || ac.act_type == "Issue"
|
||||||
|
ac.nil? || ac.act.nil? ? 0 : get_activity_praise_num(ac.act)
|
||||||
|
end
|
||||||
|
when :homework_common_detail_manual
|
||||||
|
if ac.act_type == "HomeworkCommon"
|
||||||
|
ac.act.homework_detail_manual unless ac.nil? || ac.act.nil? || ac.act.homework_detail_manual.nil?
|
||||||
|
end
|
||||||
|
when :course_project_name
|
||||||
|
if ac.container_type == "Course"
|
||||||
|
name = (get_course(ac.container_id)).name
|
||||||
|
name
|
||||||
|
elsif ac.container_type == "Project"
|
||||||
|
name = (get_project(ac.container_id)).name
|
||||||
|
name
|
||||||
|
elsif ac.container_type == "Blog"
|
||||||
|
"发表博客"
|
||||||
|
end
|
||||||
|
when :activity_type_name
|
||||||
|
if ac.container_type == "Course"
|
||||||
|
case ac.act_type
|
||||||
|
when "HomeworkCommon"
|
||||||
|
"课程作业"
|
||||||
|
when "News"
|
||||||
|
"课程通知"
|
||||||
|
when "Message"
|
||||||
|
"课程问答区"
|
||||||
|
when "Poll"
|
||||||
|
"课程问卷"
|
||||||
|
when "Course"
|
||||||
|
"课程"
|
||||||
|
end
|
||||||
|
elsif ac.container_type == "Project"
|
||||||
|
case ac.act_type
|
||||||
|
when "Issue"
|
||||||
|
"项目缺陷"
|
||||||
|
when "Message"
|
||||||
|
"项目问答区"
|
||||||
|
when "ProjectCreateInfo"
|
||||||
|
"项目"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
expose :act_type
|
||||||
|
expose :container_type
|
||||||
|
expose :author, using: Mobile::Entities::User do |a, opt|
|
||||||
|
if a.is_a? ::UserActivity
|
||||||
|
if a.act_type == "ProjectCreateInfo"
|
||||||
|
get_user(get_project(a.act_id).user_id)
|
||||||
|
elsif a.act_type == 'Issue' || a.act_type == 'News' || a.act_type == 'Message' || a.act_type == 'BlogComment'
|
||||||
|
a.act.author
|
||||||
|
elsif a.act_type == 'HomeworkCommon' || a.act_type == 'Poll' || a.act_type == 'JournalsForMessage'
|
||||||
|
a.act.user
|
||||||
|
elsif a.act_type == 'Course'
|
||||||
|
a.act.teacher
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
act_expose :homework_common_detail_manual
|
||||||
|
act_expose :reply_num
|
||||||
|
act_expose :activity_praise_num
|
||||||
|
act_expose :user_act
|
||||||
|
act_expose :course_project_name
|
||||||
|
act_expose :activity_type_name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,53 @@
|
|||||||
|
# encoding: utf-8
|
||||||
|
module Mobile
|
||||||
|
module Entities
|
||||||
|
class Whomework <Grape::Entity
|
||||||
|
include ApiHelper
|
||||||
|
include ApplicationHelper
|
||||||
|
include Redmine::I18n
|
||||||
|
def self.whomework_expose(f)
|
||||||
|
expose f do |wh, opt|
|
||||||
|
if wh.is_a?(Hash) && wh.key?(f)
|
||||||
|
if f == :created_at
|
||||||
|
format_time(wh[f])
|
||||||
|
else
|
||||||
|
wh[f]
|
||||||
|
end
|
||||||
|
elsif wh.is_a?(::HomeworkCommon)
|
||||||
|
if wh.respond_to?(f)
|
||||||
|
wh.send(f)
|
||||||
|
else
|
||||||
|
case f
|
||||||
|
when :absence_penalty
|
||||||
|
wh.nil? || wh.homework_detail_manual.nil? ? 0 : wh.homework_detail_manual.absence_penalty
|
||||||
|
when :evaluation_start
|
||||||
|
wh.nil? || wh.homework_detail_manual.nil? ? nil : convert_to_time(wh.homework_detail_manual.evaluation_start, 0)
|
||||||
|
when :evaluation_end
|
||||||
|
wh.nil? || wh.homework_detail_manual.nil? ? nil : convert_to_time(wh.homework_detail_manual.evaluation_end, 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
expose :name
|
||||||
|
expose :description
|
||||||
|
expose :publish_time
|
||||||
|
expose :end_time
|
||||||
|
expose :homework_type
|
||||||
|
expose :late_penalty
|
||||||
|
expose :course_id
|
||||||
|
expose :anonymous_comment
|
||||||
|
expose :quotes
|
||||||
|
expose :is_open
|
||||||
|
whomework_expose :created_at
|
||||||
|
whomework_expose :absence_penalty
|
||||||
|
whomework_expose :evaluation_start
|
||||||
|
whomework_expose :evaluation_end
|
||||||
|
expose :author, using: Mobile::Entities::User do |w, opt|
|
||||||
|
if w.is_a?(::HomeworkCommon)
|
||||||
|
w.user
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,256 +1,256 @@
|
|||||||
#coding=utf-8
|
#coding=utf-8
|
||||||
class WechatsController < ActionController::Base
|
class WechatsController < ActionController::Base
|
||||||
wechat_responder
|
wechat_responder
|
||||||
|
|
||||||
include ApplicationHelper
|
include ApplicationHelper
|
||||||
|
|
||||||
# default text responder when no other match
|
# default text responder when no other match
|
||||||
on :text do |request, content|
|
on :text do |request, content|
|
||||||
request.reply.text "您的意见已收到" # Just echo
|
request.reply.text "您的意见已收到" # Just echo
|
||||||
end
|
end
|
||||||
|
|
||||||
# When receive 'help', will trigger this responder
|
# When receive 'help', will trigger this responder
|
||||||
on :text, with: 'help' do |request|
|
on :text, with: 'help' do |request|
|
||||||
request.reply.text 'help content'
|
request.reply.text 'help content'
|
||||||
end
|
end
|
||||||
|
|
||||||
# When receive '<n>news', will match and will got count as <n> as parameter
|
# When receive '<n>news', will match and will got count as <n> as parameter
|
||||||
on :text, with: /^(\d+) news$/ do |request, count|
|
on :text, with: /^(\d+) news$/ do |request, count|
|
||||||
# Wechat article can only contain max 10 items, large than 10 will dropped.
|
# Wechat article can only contain max 10 items, large than 10 will dropped.
|
||||||
news = (1..count.to_i).each_with_object([]) { |n, memo| memo << { title: 'News title', content: "No. #{n} news content" } }
|
news = (1..count.to_i).each_with_object([]) { |n, memo| memo << { title: 'News title', content: "No. #{n} news content" } }
|
||||||
request.reply.news(news) do |article, n, index| # article is return object
|
request.reply.news(news) do |article, n, index| # article is return object
|
||||||
article.item title: "#{index} #{n[:title]}", description: n[:content], pic_url: 'http://www.baidu.com/img/bdlogo.gif', url: 'http://www.baidu.com/'
|
article.item title: "#{index} #{n[:title]}", description: n[:content], pic_url: 'http://www.baidu.com/img/bdlogo.gif', url: 'http://www.baidu.com/'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
on :event, with: 'subscribe' do |request|
|
on :event, with: 'subscribe' do |request|
|
||||||
default_msg(request)
|
default_msg(request)
|
||||||
end
|
end
|
||||||
|
|
||||||
# When unsubscribe user scan qrcode qrscene_xxxxxx to subscribe in public account
|
# When unsubscribe user scan qrcode qrscene_xxxxxx to subscribe in public account
|
||||||
# notice user will subscribe public account at same time, so wechat won't trigger subscribe event any more
|
# notice user will subscribe public account at same time, so wechat won't trigger subscribe event any more
|
||||||
on :scan, with: 'qrscene_xxxxxx' do |request, ticket|
|
on :scan, with: 'qrscene_xxxxxx' do |request, ticket|
|
||||||
request.reply.text "Unsubscribe user #{request[:FromUserName]} Ticket #{ticket}"
|
request.reply.text "Unsubscribe user #{request[:FromUserName]} Ticket #{ticket}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# When subscribe user scan scene_id in public account
|
# When subscribe user scan scene_id in public account
|
||||||
on :scan, with: 'scene_id' do |request, ticket|
|
on :scan, with: 'scene_id' do |request, ticket|
|
||||||
request.reply.text "Subscribe user #{request[:FromUserName]} Ticket #{ticket}"
|
request.reply.text "Subscribe user #{request[:FromUserName]} Ticket #{ticket}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# When no any on :scan responder can match subscribe user scaned scene_id
|
# When no any on :scan responder can match subscribe user scaned scene_id
|
||||||
on :event, with: 'scan' do |request|
|
on :event, with: 'scan' do |request|
|
||||||
if request[:EventKey].present?
|
if request[:EventKey].present?
|
||||||
request.reply.text "event scan got EventKey #{request[:EventKey]} Ticket #{request[:Ticket]}"
|
request.reply.text "event scan got EventKey #{request[:EventKey]} Ticket #{request[:Ticket]}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# When enterprise user press menu BINDING_QR_CODE and success to scan bar code
|
# When enterprise user press menu BINDING_QR_CODE and success to scan bar code
|
||||||
on :scan, with: 'BINDING_QR_CODE' do |request, scan_result, scan_type|
|
on :scan, with: 'BINDING_QR_CODE' do |request, scan_result, scan_type|
|
||||||
request.reply.text "User #{request[:FromUserName]} ScanResult #{scan_result} ScanType #{scan_type}"
|
request.reply.text "User #{request[:FromUserName]} ScanResult #{scan_result} ScanType #{scan_type}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Except QR code, wechat can also scan CODE_39 bar code in enterprise account
|
# Except QR code, wechat can also scan CODE_39 bar code in enterprise account
|
||||||
on :scan, with: 'BINDING_BARCODE' do |message, scan_result|
|
on :scan, with: 'BINDING_BARCODE' do |message, scan_result|
|
||||||
if scan_result.start_with? 'CODE_39,'
|
if scan_result.start_with? 'CODE_39,'
|
||||||
message.reply.text "User: #{message[:FromUserName]} scan barcode, result is #{scan_result.split(',')[1]}"
|
message.reply.text "User: #{message[:FromUserName]} scan barcode, result is #{scan_result.split(',')[1]}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# When user click the menu button
|
# When user click the menu button
|
||||||
on :click, with: 'BOOK_LUNCH' do |request, key|
|
on :click, with: 'BOOK_LUNCH' do |request, key|
|
||||||
request.reply.text "User: #{request[:FromUserName]} click #{key}"
|
request.reply.text "User: #{request[:FromUserName]} click #{key}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# When user view URL in the menu button
|
# When user view URL in the menu button
|
||||||
on :view, with: 'http://wechat.somewhere.com/view_url' do |request, view|
|
on :view, with: 'http://wechat.somewhere.com/view_url' do |request, view|
|
||||||
request.reply.text "#{request[:FromUserName]} view #{view}"
|
request.reply.text "#{request[:FromUserName]} view #{view}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# When user sent the imsage
|
# When user sent the imsage
|
||||||
on :image do |request|
|
on :image do |request|
|
||||||
request.reply.image(request[:MediaId]) # Echo the sent image to user
|
request.reply.image(request[:MediaId]) # Echo the sent image to user
|
||||||
end
|
end
|
||||||
|
|
||||||
# When user sent the voice
|
# When user sent the voice
|
||||||
on :voice do |request|
|
on :voice do |request|
|
||||||
request.reply.voice(request[:MediaId]) # Echo the sent voice to user
|
request.reply.voice(request[:MediaId]) # Echo the sent voice to user
|
||||||
end
|
end
|
||||||
|
|
||||||
# When user sent the video
|
# When user sent the video
|
||||||
on :video do |request|
|
on :video do |request|
|
||||||
nickname = wechat.user(request[:FromUserName])['nickname'] # Call wechat api to get sender nickname
|
nickname = wechat.user(request[:FromUserName])['nickname'] # Call wechat api to get sender nickname
|
||||||
request.reply.video(request[:MediaId], title: 'Echo', description: "Got #{nickname} sent video") # Echo the sent video to user
|
request.reply.video(request[:MediaId], title: 'Echo', description: "Got #{nickname} sent video") # Echo the sent video to user
|
||||||
end
|
end
|
||||||
|
|
||||||
# When user sent location
|
# When user sent location
|
||||||
on :location do |request|
|
on :location do |request|
|
||||||
request.reply.text("Latitude: #{message[:Latitude]} Longitude: #{message[:Longitude]} Precision: #{message[:Precision]}")
|
request.reply.text("Latitude: #{message[:Latitude]} Longitude: #{message[:Longitude]} Precision: #{message[:Precision]}")
|
||||||
end
|
end
|
||||||
|
|
||||||
on :event, with: 'unsubscribe' do |request|
|
on :event, with: 'unsubscribe' do |request|
|
||||||
request.reply.success # user can not receive this message
|
request.reply.success # user can not receive this message
|
||||||
end
|
end
|
||||||
|
|
||||||
# When user enter the app / agent app
|
# When user enter the app / agent app
|
||||||
on :event, with: 'enter_agent' do |request|
|
on :event, with: 'enter_agent' do |request|
|
||||||
request.reply.text "#{request[:FromUserName]} enter agent app now"
|
request.reply.text "#{request[:FromUserName]} enter agent app now"
|
||||||
end
|
end
|
||||||
|
|
||||||
# When batch job create/update user (incremental) finished.
|
# When batch job create/update user (incremental) finished.
|
||||||
on :batch_job, with: 'sync_user' do |request, batch_job|
|
on :batch_job, with: 'sync_user' do |request, batch_job|
|
||||||
request.reply.text "sync_user job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
request.reply.text "sync_user job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# When batch job replace user (full sync) finished.
|
# When batch job replace user (full sync) finished.
|
||||||
on :batch_job, with: 'replace_user' do |request, batch_job|
|
on :batch_job, with: 'replace_user' do |request, batch_job|
|
||||||
request.reply.text "replace_user job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
request.reply.text "replace_user job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# When batch job invent user finished.
|
# When batch job invent user finished.
|
||||||
on :batch_job, with: 'invite_user' do |request, batch_job|
|
on :batch_job, with: 'invite_user' do |request, batch_job|
|
||||||
request.reply.text "invite_user job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
request.reply.text "invite_user job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# When batch job replace department (full sync) finished.
|
# When batch job replace department (full sync) finished.
|
||||||
on :batch_job, with: 'replace_party' do |request, batch_job|
|
on :batch_job, with: 'replace_party' do |request, batch_job|
|
||||||
request.reply.text "replace_party job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
request.reply.text "replace_party job #{batch_job[:JobId]} finished, return code #{batch_job[:ErrCode]}, return message #{batch_job[:ErrMsg]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Any not match above will fail to below
|
# Any not match above will fail to below
|
||||||
on :fallback, respond: 'fallback message'
|
on :fallback, respond: 'fallback message'
|
||||||
|
|
||||||
on :click, with: 'FEEDBACK' do |request, key|
|
on :click, with: 'FEEDBACK' do |request, key|
|
||||||
request.reply.text "如有反馈问题,请直接切入至输入框,发微信给我们即可"
|
request.reply.text "如有反馈问题,请直接切入至输入框,发微信给我们即可"
|
||||||
end
|
end
|
||||||
|
|
||||||
on :click, with: 'MY_NEWS' do |request, key|
|
on :click, with: 'MY_NEWS' do |request, key|
|
||||||
default_msg(request)
|
default_msg(request)
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_msg(request)
|
def default_msg(request)
|
||||||
uw = user_binded?(request[:FromUserName])
|
uw = user_binded?(request[:FromUserName])
|
||||||
if uw && uw.user
|
if uw && uw.user
|
||||||
|
|
||||||
ua = user_activity(uw.user)
|
ua = user_activity(uw.user)
|
||||||
logo = "http://wechat.trustie.net/images/trustie_logo2.png"
|
logo = "http://wechat.trustie.net/images/trustie_logo2.png"
|
||||||
i = 0
|
i = 0
|
||||||
news =[]
|
news =[]
|
||||||
ua.each do |a|
|
ua.each do |a|
|
||||||
i += 1
|
i += 1
|
||||||
activity = process_activity(a)
|
activity = process_activity(a)
|
||||||
if activity
|
if activity
|
||||||
news << {title: activity[0],
|
news << {title: activity[0],
|
||||||
content: activity[1],
|
content: activity[1],
|
||||||
picurl: "#{i == 1 ? logo : 'https://www.trustie.net/'+activity[2]}",
|
picurl: "#{i == 1 ? logo : 'https://www.trustie.net/'+activity[2]}",
|
||||||
url: activity[3]
|
url: activity[3]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
request.reply.news(news) do |article, n, index| # article is return object
|
request.reply.news(news) do |article, n, index| # article is return object
|
||||||
article.item title: "#{n[:content]}",
|
article.item title: "#{n[:content]}",
|
||||||
description: n[:title],
|
description: n[:title],
|
||||||
pic_url: "#{n[:picurl]}",
|
pic_url: "#{n[:picurl]}",
|
||||||
url: n[:url]
|
url: n[:url]
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
sendBind(request)
|
sendBind(request)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def sendBind(request)
|
def sendBind(request)
|
||||||
news = (1..1).each_with_object([]) { |n, memo| memo << { title: '绑定登录', content: "您还未绑定确实的用户,请先绑定." } }
|
news = (1..1).each_with_object([]) { |n, memo| memo << { title: '绑定登录', content: "您还未绑定确实的用户,请先绑定." } }
|
||||||
request.reply.news(news) do |article, n, index| # article is return object
|
request.reply.news(news) do |article, n, index| # article is return object
|
||||||
url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{Wechat.config.appid}&redirect_uri=#{login_wechat_url}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"
|
url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{Wechat.config.appid}&redirect_uri=#{login_wechat_url}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"
|
||||||
article.item title: "#{n[:title]}",
|
article.item title: "#{n[:title]}",
|
||||||
description: n[:content],
|
description: n[:content],
|
||||||
pic_url: 'http://wechat.trustie.net/images/trustie_logo2.png',
|
pic_url: 'http://wechat.trustie.net/images/trustie_logo2.png',
|
||||||
url: url
|
url: url
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def get_open_id
|
def get_open_id
|
||||||
begin
|
begin
|
||||||
raise "非法操作, code不存在" unless params[:code]
|
raise "非法操作, code不存在" unless params[:code]
|
||||||
openid = get_openid(params[:code])
|
openid = get_openid(params[:code])
|
||||||
raise "无法获取到openid" unless openid
|
raise "无法获取到openid" unless openid
|
||||||
render :text => {status:0, openid: openid}.to_json
|
render :text => {status:0, openid: openid}.to_json
|
||||||
rescue Exception=>e
|
rescue Exception=>e
|
||||||
render :text => {status: -1, msg: e.message}.to_json
|
render :text => {status: -1, msg: e.message}.to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def bind
|
def bind
|
||||||
begin
|
begin
|
||||||
raise "非法操作, code不存在" unless params[:code]
|
raise "非法操作, code不存在" unless params[:code]
|
||||||
openid = get_openid(params[:code])
|
openid = get_openid(params[:code])
|
||||||
raise "无法获取到openid" unless openid
|
raise "无法获取到openid" unless openid
|
||||||
raise "此微信号已绑定用户, 不能得复绑定" if user_binded?(openid)
|
raise "此微信号已绑定用户, 不能得复绑定" if user_binded?(openid)
|
||||||
|
|
||||||
user, last_login_on = User.try_to_login(params[:username], params[:password])
|
user, last_login_on = User.try_to_login(params[:username], params[:password])
|
||||||
raise "用户名或密码错误,请重新登录" unless user
|
raise "用户名或密码错误,请重新登录" unless user
|
||||||
#补全用户信息
|
#补全用户信息
|
||||||
|
|
||||||
raise "此用户已经绑定了公众号" if user.user_wechat
|
raise "此用户已经绑定了公众号" if user.user_wechat
|
||||||
|
|
||||||
UserWechat.create!(
|
UserWechat.create!(
|
||||||
openid: openid,
|
openid: openid,
|
||||||
user: user
|
user: user
|
||||||
)
|
)
|
||||||
render :text => {status:0, msg: "绑定成功"}.to_json
|
render :text => {status:0, msg: "绑定成功"}.to_json
|
||||||
rescue Exception=>e
|
rescue Exception=>e
|
||||||
render :text => {status: -1, msg: e.message}.to_json
|
render :text => {status: -1, msg: e.message}.to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def login
|
def login
|
||||||
@code = params[:code] #TODO 安全性
|
@code = params[:code] #TODO 安全性
|
||||||
render 'wechats/login', layout: 'base_wechat'
|
render 'wechats/login', layout: 'base_wechat'
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def get_openid(code)
|
def get_openid(code)
|
||||||
url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{Wechat.config.appid}&secret=#{Wechat.config.secret}&code=#{code}&grant_type=authorization_code"
|
url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{Wechat.config.appid}&secret=#{Wechat.config.secret}&code=#{code}&grant_type=authorization_code"
|
||||||
JSON.parse(URI.parse(url).read)["openid"]
|
JSON.parse(URI.parse(url).read)["openid"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def user_binded?(openid)
|
def user_binded?(openid)
|
||||||
uw = UserWechat.where(openid: openid).first
|
uw = UserWechat.where(openid: openid).first
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_activity(user)
|
def user_activity(user)
|
||||||
@user = user
|
@user = user
|
||||||
shield_project_ids = ShieldActivity.where("container_type='User' and container_id=#{@user.id} and shield_type='Project'").map(&:shield_id)
|
shield_project_ids = ShieldActivity.where("container_type='User' and container_id=#{@user.id} and shield_type='Project'").map(&:shield_id)
|
||||||
shield_course_ids = ShieldActivity.where("container_type='User' and container_id=#{@user.id} and shield_type='Course'").map(&:shield_id)
|
shield_course_ids = ShieldActivity.where("container_type='User' and container_id=#{@user.id} and shield_type='Course'").map(&:shield_id)
|
||||||
@page = params[:page] ? params[:page].to_i + 1 : 0
|
@page = params[:page] ? params[:page].to_i + 1 : 0
|
||||||
user_project_ids = (@user.projects.visible.map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (@user.projects.visible.map{|project| project.id}-shield_project_ids).join(",") + ")"
|
user_project_ids = (@user.projects.visible.map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (@user.projects.visible.map{|project| project.id}-shield_project_ids).join(",") + ")"
|
||||||
user_course_ids = (@user.courses.visible.map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (@user.courses.visible.map{|course| course.id}-shield_course_ids).join(",") + ")"
|
user_course_ids = (@user.courses.visible.map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (@user.courses.visible.map{|course| course.id}-shield_course_ids).join(",") + ")"
|
||||||
course_types = "('Message','News','HomeworkCommon','Poll','Course')"
|
course_types = "('Message','News','HomeworkCommon','Poll','Course')"
|
||||||
project_types = "('Message','Issue','ProjectCreateInfo')"
|
project_types = "('Message','Issue','ProjectCreateInfo')"
|
||||||
principal_types = "JournalsForMessage"
|
principal_types = "JournalsForMessage"
|
||||||
|
|
||||||
blog_ids = "("+@user.blog.id.to_s+","+((User.watched_by(@user.id).count == 0 )? '0' :User.watched_by(@user.id).map{|u| u.blog.id}.join(','))+")"
|
blog_ids = "("+@user.blog.id.to_s+","+((User.watched_by(@user.id).count == 0 )? '0' :User.watched_by(@user.id).map{|u| u.blog.id}.join(','))+")"
|
||||||
@user_activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" +
|
@user_activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" +
|
||||||
"or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+
|
"or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+
|
||||||
"or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{@user.id}) " +
|
"or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{@user.id}) " +
|
||||||
"or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc').limit(10).offset(@page * 10)
|
"or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc').limit(10).offset(@page * 10)
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_activity(user_activity)
|
def process_activity(user_activity)
|
||||||
act= user_activity.act
|
act= user_activity.act
|
||||||
case user_activity.container_type.to_s
|
case user_activity.container_type.to_s
|
||||||
when 'Course'
|
when 'Course'
|
||||||
when 'Project'
|
when 'Project'
|
||||||
case user_activity.act_type.to_s
|
case user_activity.act_type.to_s
|
||||||
when 'Issue'
|
when 'Issue'
|
||||||
[act.project.name.to_s+" | 项目问题", act.subject.to_s, url_to_avatar(act.author),"http://wechat.trustie.net/app.html#/issue/#{act.id}"]
|
[act.project.name.to_s+" | 项目问题", act.subject.to_s, url_to_avatar(act.author),"http://wechat.trustie.net/app.html#/issue/#{act.id}"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
button:
|
button:
|
||||||
-
|
-
|
||||||
type: "view"
|
type: "view"
|
||||||
name: "最新动态"
|
name: "最新动态"
|
||||||
url: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09454f171153c2d&redirect_uri=http://wechat.trustie.net/assets/wechat/issue.html&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
|
url: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09454f171153c2d&redirect_uri=http://wechat.trustie.net/assets/wechat/issue.html&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
|
||||||
-
|
-
|
||||||
type: "click"
|
type: "click"
|
||||||
name: "意见返馈"
|
name: "意见返馈"
|
||||||
key: "FEEDBACK"
|
key: "FEEDBACK"
|
||||||
-
|
-
|
||||||
name: "更多"
|
name: "更多"
|
||||||
sub_button:
|
sub_button:
|
||||||
-
|
-
|
||||||
type: "view"
|
type: "view"
|
||||||
name: "进入网站"
|
name: "进入网站"
|
||||||
url: "http://www.trustie.net/"
|
url: "http://www.trustie.net/"
|
||||||
-
|
-
|
||||||
type: "view"
|
type: "view"
|
||||||
name: "使用手册"
|
name: "使用手册"
|
||||||
url: "https://www.trustie.net/organizations/1/downloads"
|
url: "https://www.trustie.net/organizations/1/downloads"
|
@ -1,35 +1,35 @@
|
|||||||
$(function(){
|
$(function(){
|
||||||
//获取url中的参数
|
//获取url中的参数
|
||||||
function getUrlParam(name) {
|
function getUrlParam(name) {
|
||||||
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
|
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
|
||||||
var r = window.location.search.substr(1).match(reg); //匹配目标参数
|
var r = window.location.search.substr(1).match(reg); //匹配目标参数
|
||||||
if (r != null) return unescape(r[2]); return null; //返回参数值
|
if (r != null) return unescape(r[2]); return null; //返回参数值
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var debug = false;
|
var debug = false;
|
||||||
|
|
||||||
var g_openid = "";
|
var g_openid = "";
|
||||||
if(debug){
|
if(debug){
|
||||||
g_openid = "填写要调试的openid即可";
|
g_openid = "填写要调试的openid即可";
|
||||||
}
|
}
|
||||||
|
|
||||||
window.getOpenId = function(cb){
|
window.getOpenId = function(cb){
|
||||||
if (g_openid.length>0){
|
if (g_openid.length>0){
|
||||||
cb(g_openid);
|
cb(g_openid);
|
||||||
}
|
}
|
||||||
var code = getUrlParam("code");
|
var code = getUrlParam("code");
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '/wechat/get_open_id?code='+code,
|
url: '/wechat/get_open_id?code='+code,
|
||||||
type: 'get',
|
type: 'get',
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function(data){
|
success: function(data){
|
||||||
g_openid = data.openid;
|
g_openid = data.openid;
|
||||||
cb(g_openid);
|
cb(g_openid);
|
||||||
},
|
},
|
||||||
error: function(xhr,err){
|
error: function(xhr,err){
|
||||||
alert("认证失败: "+err);
|
alert("认证失败: "+err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
Loading…
Reference in new issue