将openid存到session
This commit is contained in:
parent
a3d90df5ce
commit
e4653426d2
|
@ -150,11 +150,20 @@ class WechatsController < ActionController::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### controller method
|
||||||
|
include Controllers
|
||||||
|
|
||||||
|
module Controllers
|
||||||
def get_open_id
|
def get_open_id
|
||||||
begin
|
begin
|
||||||
raise "非法操作, code不存在" unless params[:code]
|
|
||||||
openid = get_openid_from_code(params[:code])
|
code = params[:code] || session[:wechat_code]
|
||||||
raise "无法获取到openid" unless openid
|
openid = get_openid_from_code(code)
|
||||||
|
|
||||||
|
|
||||||
|
raise "无法获取到微信openid" unless openid
|
||||||
render :json => {status:0, openid: openid}
|
render :json => {status:0, openid: openid}
|
||||||
rescue Exception=>e
|
rescue Exception=>e
|
||||||
render :json => {status: -1, msg: e.message}
|
render :json => {status: -1, msg: e.message}
|
||||||
|
@ -163,8 +172,10 @@ class WechatsController < ActionController::Base
|
||||||
|
|
||||||
def bind
|
def bind
|
||||||
begin
|
begin
|
||||||
raise "非法操作, code不存在" unless params[:code]
|
|
||||||
openid = get_openid_from_code(params[:code])
|
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)
|
raise "此微信号已绑定用户, 不能重复绑定" if user_binded?(openid)
|
||||||
|
|
||||||
|
@ -172,7 +183,7 @@ class WechatsController < ActionController::Base
|
||||||
raise "用户名或密码错误,请重新登录" unless user
|
raise "用户名或密码错误,请重新登录" unless user
|
||||||
#补全用户信息
|
#补全用户信息
|
||||||
|
|
||||||
raise "此用户已经绑定了公众号" if user.user_wechat
|
raise "此用户已经绑定过公众号, 请换一个帐户试试" if user.user_wechat
|
||||||
|
|
||||||
UserWechat.create!(
|
UserWechat.create!(
|
||||||
openid: openid,
|
openid: openid,
|
||||||
|
@ -185,17 +196,25 @@ class WechatsController < ActionController::Base
|
||||||
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
|
unless openid
|
||||||
logger.debug body
|
if code
|
||||||
JSON.parse(body)["openid"]
|
openid = wechat.web_access_token(code)["openid"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if openid
|
||||||
|
session[:wechat_openid] = openid
|
||||||
|
end
|
||||||
|
|
||||||
|
return openid
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_binded?(openid)
|
def user_binded?(openid)
|
||||||
|
@ -233,4 +252,6 @@ class WechatsController < ActionController::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue