Merge branch 'weixin_guange' of https://git.trustie.net/jacknudt/trustieforge into weixin_guange

This commit is contained in:
txz 2016-05-26 13:48:13 +08:00
commit 8fbe4d636c
3 changed files with 97 additions and 75 deletions

View File

@ -150,87 +150,111 @@ class WechatsController < ActionController::Base
end end
def get_open_id
begin
raise "非法操作, code不存在" unless params[:code] ### controller method
openid = get_openid_from_code(params[:code])
raise "无法获取到openid" unless openid
render :json => {status:0, openid: openid} module Controllers
rescue Exception=>e def get_open_id
render :json => {status: -1, msg: e.message} begin
code = params[:code] || session[:wechat_code]
openid = get_openid_from_code(code)
raise "无法获取到微信openid" unless openid
render :json => {status:0, openid: openid}
rescue Exception=>e
render :json => {status: -1, msg: e.message}
end
end end
end
def bind def bind
begin begin
raise "非法操作, code不存在" unless params[:code]
openid = get_openid_from_code(params[:code])
raise "无法获取到openid" unless openid
raise "此微信号已绑定用户, 不能重复绑定" if user_binded?(openid)
user, last_login_on = User.try_to_login(params[:username], params[:password]) code = params[:code] || session[:wechat_code]
raise "用户名或密码错误,请重新登录" unless user openid = get_openid_from_code(code)
#补全用户信息
raise "此用户已经绑定了公众号" if user.user_wechat raise "无法获取到openid" unless openid
raise "此微信号已绑定用户, 不能重复绑定" if user_binded?(openid)
UserWechat.create!( user, last_login_on = User.try_to_login(params[:username], params[:password])
openid: openid, raise "用户名或密码错误,请重新登录" unless user
user: user #补全用户信息
)
render :json => {status:0, msg: "绑定成功"} raise "此用户已经绑定过公众号, 请换一个帐户试试" if user.user_wechat
rescue Exception=>e
render :json => {status: -1, msg: e.message} UserWechat.create!(
openid: openid,
user: user
)
render :json => {status:0, msg: "绑定成功"}
rescue Exception=>e
render :json => {status: -1, msg: e.message}
end
end end
end
def login def login
@code = params[:code] #TODO 安全性 session[:wechat_code] = params[:code] if params[:code]
render 'wechats/login', layout: 'base_wechat' render 'wechats/login', layout: 'base_wechat'
end end
private private
def get_openid_from_code(code) def get_openid_from_code(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" openid = session[:wechat_openid]
logger.debug url
body = URI.parse(url).read
logger.debug body
JSON.parse(body)["openid"]
end
def user_binded?(openid) unless openid
uw = UserWechat.where(openid: openid).first if code
end openid = wechat.web_access_token(code)["openid"]
def user_activity(user)
@user = user
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','Project')"
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(','))+")"
@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 = '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)
end
def process_activity(user_activity)
act= user_activity.act
case user_activity.container_type.to_s
when 'Course'
when 'Project'
case user_activity.act_type.to_s
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}"]
end end
end
if openid
session[:wechat_openid] = openid
end
return openid
end
def user_binded?(openid)
uw = UserWechat.where(openid: openid).first
end
def user_activity(user)
@user = user
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','Project')"
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(','))+")"
@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 = '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)
end
def process_activity(user_activity)
act= user_activity.act
case user_activity.container_type.to_s
when 'Course'
when 'Project'
case user_activity.act_type.to_s
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}"]
end
end
end end
end end
include Controllers
end end

View File

@ -12,19 +12,17 @@
<div class="weui_cell"> <div class="weui_cell">
<div class="weui_cell_hd"><label class="weui_label">用户名</label></div> <div class="weui_cell_hd"><label class="weui_label">用户名</label></div>
<div class="weui_cell_bd weui_cell_primary"> <div class="weui_cell_bd weui_cell_primary">
<input class="weui_input" autocapitalize="off" type="text" name="username" placeholder="请输入邮箱地址或昵称"/> <input class="weui_input" autocapitalize="off" type="text" name="username" placeholder="请输入邮箱地址/登录名"/>
</div> </div>
</div> </div>
<div class="weui_cell"> <div class="weui_cell">
<div class="weui_cell_hd"><label class="weui_label">密码</label></div> <div class="weui_cell_hd"><label class="weui_label">密码</label></div>
<div class="weui_cell_bd weui_cell_primary"> <div class="weui_cell_bd weui_cell_primary">
<input class="weui_input" type="password" name="password" placeholder="请输密码"/> <input class="weui_input" type="password" name="password" placeholder="请输密码"/>
</div> </div>
</div> </div>
<input type="hidden" value="<%=@code%>" name="code">
<div class="weui_btn_area"> <div class="weui_btn_area">
<a class="weui_btn weui_btn_primary" id="submitForm">确定</a> <a class="weui_btn weui_btn_primary" id="submitForm">确定</a>
</div> </div>