修改openid路由

This commit is contained in:
guange 2016-04-02 07:47:34 +08:00
parent 9c93b3f9a6
commit a25fe6cef8
1 changed files with 10 additions and 4 deletions

View File

@ -175,7 +175,7 @@ class WechatsController < ActionController::Base
def get_open_id
begin
raise "非法操作, code不存在" unless params[:code]
openid = get_openid(params[:code])
openid = get_openid_from_code(params[:code])
raise "无法获取到openid" unless openid
render :text => {status:0, openid: openid}.to_json
rescue Exception=>e
@ -186,7 +186,7 @@ class WechatsController < ActionController::Base
def bind
begin
raise "非法操作, code不存在" unless params[:code]
openid = get_openid(params[:code])
openid = get_openid_from_code(params[:code])
raise "无法获取到openid" unless openid
raise "此微信号已绑定用户, 不能得复绑定" if user_binded?(openid)
@ -211,8 +211,15 @@ class WechatsController < ActionController::Base
render 'wechats/login', layout: 'base_wechat'
end
def get_openid
code = params[:code]
openid = get_openid_from_code(code)
render :text => {openid: openid}.to_json
end
private
def get_openid(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"
logger.debug url
body = URI.parse(url).read
@ -220,7 +227,6 @@ class WechatsController < ActionController::Base
JSON.parse(body)["openid"]
end
def user_binded?(openid)
uw = UserWechat.where(openid: openid).first
end