diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index 65189daa1..ed890d1e1 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -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