|
|
|
@ -3,10 +3,23 @@ class WechatsController < ActionController::Base
|
|
|
|
|
wechat_responder
|
|
|
|
|
|
|
|
|
|
include ApplicationHelper
|
|
|
|
|
|
|
|
|
|
# ROOT_URL = "#{Setting.protocol}://#{Setting.host_name}/"
|
|
|
|
|
ROOT_URL = "http://wechat.trustie.net"
|
|
|
|
|
# default text responder when no other match
|
|
|
|
|
on :text do |request, content|
|
|
|
|
|
request.reply.text "您的意见已收到,感谢您的反馈!" # Just echo
|
|
|
|
|
#邀请码
|
|
|
|
|
begin
|
|
|
|
|
uw = user_binded?(request[:FromUserName])
|
|
|
|
|
if !uw
|
|
|
|
|
return sendBind()
|
|
|
|
|
else
|
|
|
|
|
return join_class({invite_code: content}, uw.user)
|
|
|
|
|
end
|
|
|
|
|
rescue => e
|
|
|
|
|
logger.error e.inspect
|
|
|
|
|
logger.error e.backtrace.join("\n")
|
|
|
|
|
return request.reply.text e
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# When receive 'help', will trigger this responder
|
|
|
|
@ -35,7 +48,18 @@ class WechatsController < ActionController::Base
|
|
|
|
|
|
|
|
|
|
# When subscribe user scan scene_id in public account
|
|
|
|
|
on :scan, with: 'scene_id' do |request, ticket|
|
|
|
|
|
request.reply.text "Subscribe user #{request[:FromUserName]} Ticket #{ticket}"
|
|
|
|
|
begin
|
|
|
|
|
uw = user_binded?(request[:FromUserName])
|
|
|
|
|
if !uw
|
|
|
|
|
return sendBind()
|
|
|
|
|
else
|
|
|
|
|
return join_class({ticket: ticket}, uw.user)
|
|
|
|
|
end
|
|
|
|
|
rescue => e
|
|
|
|
|
logger.error e.inspect
|
|
|
|
|
logger.error e.backtrace.join("\n")
|
|
|
|
|
return request.reply.text e
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# When no any on :scan responder can match subscribe user scaned scene_id
|
|
|
|
@ -128,6 +152,15 @@ class WechatsController < ActionController::Base
|
|
|
|
|
default_msg(request)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
on :click, with: 'JOIN_CLASS' do |request, key|
|
|
|
|
|
uw = user_binded?(request[:FromUserName])
|
|
|
|
|
unless uw
|
|
|
|
|
sendBind(request)
|
|
|
|
|
else
|
|
|
|
|
request.reply.text "请直接回复6位班级邀请码\n(不区分大小写):"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def default_msg(request)
|
|
|
|
|
uw = user_binded?(request[:FromUserName])
|
|
|
|
|
if uw && uw.user
|
|
|
|
@ -145,27 +178,62 @@ class WechatsController < ActionController::Base
|
|
|
|
|
|
|
|
|
|
您还未绑定确实的用户,请先绑定,谢谢!" } }
|
|
|
|
|
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"
|
|
|
|
|
pic_url = "#{Setting.protocol}://#{Setting.host_name}/images/weixin_pic.jpg"
|
|
|
|
|
url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{Wechat.config.appid}&redirect_uri=#{ROOT_URL+'/wechat/user_activities'}&response_type=code&scope=snsapi_base&state=login#wechat_redirect"
|
|
|
|
|
pic_url = "#{ROOT_URL}/images/weixin_pic.jpg"
|
|
|
|
|
article.item title: "#{n[:title]}",
|
|
|
|
|
description: n[:content],
|
|
|
|
|
pic_url: pic_url,
|
|
|
|
|
url: url
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def join_class(params, user)
|
|
|
|
|
course = nil
|
|
|
|
|
course = Course.where(qrcode: params[:ticket]) if params[:ticket]
|
|
|
|
|
course = Course.where(invite_code: params[:invite_code]) if params[:invite_code]
|
|
|
|
|
raise "课程不存在" if course.blank?
|
|
|
|
|
|
|
|
|
|
cs = CoursesService.new
|
|
|
|
|
status = cs.join_course(course.invite_code, user)
|
|
|
|
|
logger.info status
|
|
|
|
|
if status[:state] != 0
|
|
|
|
|
raise CoursesService::JoinCourseError.message(status[:state])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
course = status[:course]
|
|
|
|
|
news = (1..1).each_with_object([]) { |n, memo| memo << { title: '恭喜您成功加入班级,开始学习吧!',
|
|
|
|
|
content: "课程名称: #{course.name}\n班级名称: #{course.name}\n任课老师: #{course.teacher.show_name}\n进入班级,和小伙伴愉快的学习吧!"} }
|
|
|
|
|
return 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=#{ROOT_URL+'/wechat/user_activities?id='+course.id.to_s}&response_type=code&scope=snsapi_base&state=myclass#wechat_redirect"
|
|
|
|
|
pic_url = "#{ROOT_URL}/images/wechat/class.jpg"
|
|
|
|
|
article.item title: "#{n[:title]}",
|
|
|
|
|
description: n[:content],
|
|
|
|
|
pic_url: pic_url,
|
|
|
|
|
url: url
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
### controller method
|
|
|
|
|
module Controllers
|
|
|
|
|
def get_open_id
|
|
|
|
|
def get_bind
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
code = params[:code] || session[:wechat_code]
|
|
|
|
|
openid = get_openid_from_code(code)
|
|
|
|
|
|
|
|
|
|
raise "无法获取到微信openid" unless openid
|
|
|
|
|
render :json => {status:0, openid: openid}
|
|
|
|
|
|
|
|
|
|
uw = UserWechat.where(openid: openid).first
|
|
|
|
|
raise "还未绑定trustie帐户" unless uw
|
|
|
|
|
logger.debug "get_bind ============= #{uw}"
|
|
|
|
|
|
|
|
|
|
user = uw.user
|
|
|
|
|
::ApiKey.delete_all(user_id: user.id)
|
|
|
|
|
key = ::ApiKey.create!(user_id: user.id)
|
|
|
|
|
|
|
|
|
|
render :json =>{status: 0, token: key.access_token}
|
|
|
|
|
rescue Exception=>e
|
|
|
|
|
render :json => {status: -1, msg: e.message}
|
|
|
|
|
render :json => {status: -1, message: e.message}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -175,7 +243,7 @@ class WechatsController < ActionController::Base
|
|
|
|
|
code = params[:code] || session[:wechat_code]
|
|
|
|
|
openid = get_openid_from_code(code)
|
|
|
|
|
|
|
|
|
|
raise "无法获取到openid" unless openid
|
|
|
|
|
raise "无法获取到openid,请在微信中打开本页面" unless openid
|
|
|
|
|
raise "此微信号已绑定用户, 不能重复绑定" if user_binded?(openid)
|
|
|
|
|
|
|
|
|
|
user, last_login_on = User.try_to_login(params[:username], params[:password])
|
|
|
|
@ -206,19 +274,26 @@ class WechatsController < ActionController::Base
|
|
|
|
|
|
|
|
|
|
def user_activities
|
|
|
|
|
session[:wechat_code] = params[:code] if params[:code]
|
|
|
|
|
code = params[:code] || session[:wechat_code]
|
|
|
|
|
openid = get_openid_from_code(code)
|
|
|
|
|
@wechat_user = user_binded?(openid)
|
|
|
|
|
unless @wechat_user
|
|
|
|
|
redirect_to login_wechat_path
|
|
|
|
|
return
|
|
|
|
|
@path = '/'+(params[:state] || '')
|
|
|
|
|
open_id = get_openid_from_code(params[:code]) rescue
|
|
|
|
|
unless open_id
|
|
|
|
|
render 'wechats/open_wechat', layout: nil and return
|
|
|
|
|
end
|
|
|
|
|
if params[:state] == 'myclass'
|
|
|
|
|
@course_id = params[:id];
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
session[:wechat_openid] = open_id
|
|
|
|
|
if params[:code]
|
|
|
|
|
redirect_to "/wechat/user_activities##{@path}?id=#{params[:id]}" and return
|
|
|
|
|
end
|
|
|
|
|
render 'wechats/user_activities', layout: nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
def get_openid_from_code(code)
|
|
|
|
|
return 'oCnvgvz8R7QheXE-R9Kkr39j8Ndg' if code =='only-for-test'
|
|
|
|
|
openid = session[:wechat_openid]
|
|
|
|
|
|
|
|
|
|
unless openid
|
|
|
|
|