恢复授权页
This commit is contained in:
parent
dde0fbb3c1
commit
70d58ccf85
|
@ -19,9 +19,29 @@ module Mobile
|
|||
raise "无法获取到openid,请在微信中打开本页面" unless openid
|
||||
us = UsersService.new
|
||||
|
||||
user = us.register ({:login=>openid, :mail=>"#{openid}@163.com",
|
||||
access_token = session[:access_token]
|
||||
refresh_token = session[:refresh_token]
|
||||
|
||||
if access_token.present? && refresh_token.present?
|
||||
refreshinfo = Wechat.api.web_refresh_access_token(refresh_token)
|
||||
access_token = refreshinfo["access_token"]
|
||||
refresh_token = refreshinfo["refresh_token"]
|
||||
session[:access_token] = access_token
|
||||
session[:refresh_token] = refresh_token
|
||||
|
||||
#获取用户信息
|
||||
userinfo = Wechat.api.web_userinfo(access_token,openid)
|
||||
Rails.logger.info "userinfo!!!!!!!!!"
|
||||
Rails.logger.info userinfo
|
||||
name = userinfo["nickname"]
|
||||
else
|
||||
name = openid[0..3]+"***"+openid.last
|
||||
end
|
||||
|
||||
user = us.register ({:login=>login, :mail=>login+"@163.com",
|
||||
:password=>"12345678", :password_confirmation=>"12345678",
|
||||
:should_confirmation_password => true})
|
||||
user.update_attributes(:lastname=>name)
|
||||
|
||||
raise user.errors.full_messages.first if user.new_record?
|
||||
UserWechat.create!(
|
||||
|
|
|
@ -122,33 +122,22 @@ module Wechat
|
|||
get 'access_token', params: params, base: OAUTH2_BASE
|
||||
end
|
||||
|
||||
def web_refresh_access_token(refresh_token)
|
||||
def web_auth_access_token(web_access_token, openid)
|
||||
get 'auth', params: { access_token: web_access_token, openid: openid }, base: OAUTH2_BASE
|
||||
end
|
||||
|
||||
def web_refresh_access_token(user_refresh_token)
|
||||
params = {
|
||||
appid: access_token.appid,
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: refresh_token
|
||||
refresh_token: user_refresh_token
|
||||
}
|
||||
get 'refresh_token', params: params, base: OAUTH2_BASE
|
||||
get 'oauth2/refresh_token', params: params, base: OAUTH2_BASE
|
||||
end
|
||||
|
||||
def web_userinfo(access_token,openid)
|
||||
# params = {
|
||||
# access_token: access_token,
|
||||
# openid: openid,
|
||||
# lang: "zh_CN"
|
||||
# }
|
||||
#
|
||||
# # get 'https://api.weixin.qq.com/sns/userinfo',params: params
|
||||
# get 'userinfo', params: params, base: OAUTH2_USERINFO
|
||||
def web_userinfo(web_access_token, openid, lang = 'zh_CN')
|
||||
get 'userinfo', params: { access_token: web_access_token, openid: openid, lang: lang }, base: OAUTH2_BASE
|
||||
end
|
||||
|
||||
require "open-uri"
|
||||
#如果有GET请求参数直接写在URI地址中
|
||||
uri = 'https://api.weixin.qq.com/sns/userinfo?access_token='+access_token+'&openid='+openid+'&lang=zh_CN'
|
||||
userinfo = nil
|
||||
open(uri) do |http|
|
||||
userinfo = JSON.parse(http.read)
|
||||
end
|
||||
userinfo
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module Wechat
|
||||
class ApiBase
|
||||
attr_reader :access_token, :client, :jsapi_ticket
|
||||
attr_reader :access_token, :client, :jsapi_ticket, :auth_access_token
|
||||
|
||||
MP_BASE = 'https://mp.weixin.qq.com/cgi-bin/'
|
||||
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
require 'wechat/token/access_token_base'
|
||||
|
||||
module Wechat
|
||||
module Token
|
||||
class AuthAccessToken
|
||||
|
||||
def refresh
|
||||
data = client.get('token', params: { grant_type: 'client_credential', appid: appid, secret: secret })
|
||||
write_token_to_file(data)
|
||||
read_token_from_file
|
||||
end
|
||||
|
||||
def read_token_from_file
|
||||
td = JSON.parse(CacheFile.read(token_file))
|
||||
@got_token_at = td.fetch('got_token_at').to_i
|
||||
@token_life_in_seconds = td.fetch('expires_in').to_i
|
||||
@access_token = td.fetch('access_token')
|
||||
rescue JSON::ParserError, Errno::ENOENT, KeyError
|
||||
refresh
|
||||
end
|
||||
|
||||
def write_token_to_file(token_hash)
|
||||
token_hash.merge!('got_token_at'.freeze => Time.now.to_i)
|
||||
CacheFile.write(token_file, token_hash.to_json)
|
||||
end
|
||||
|
||||
def remain_life_seconds
|
||||
token_life_in_seconds - (Time.now.to_i - got_token_at)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -268,12 +268,12 @@ app.factory('common', ['$http', 'auth', '$routeParams','rms','config','wx','$loc
|
|||
var urlName = response.data.type_name + "_discussion";
|
||||
link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+window.g_appid +
|
||||
"&redirect_uri="+window.g_localhost+"/wechat/user_activities?id="+response.data.data.act_id+
|
||||
"&response_type=code&scope=snsapi_base&state="+urlName+"&connect_redirect=1#wechat_redirect";
|
||||
"&response_type=code&scope=snsapi_userinfo&state="+urlName+"&connect_redirect=1#wechat_redirect";
|
||||
}
|
||||
else{
|
||||
link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+window.g_appid +
|
||||
"&redirect_uri="+window.g_localhost+"/wechat/user_activities?id="+response.data.data.act_id+
|
||||
"&response_type=code&scope=snsapi_base&state="+args.urlName+"&connect_redirect=1#wechat_redirect";
|
||||
"&response_type=code&scope=snsapi_userinfo&state="+args.urlName+"&connect_redirect=1#wechat_redirect";
|
||||
}
|
||||
|
||||
// if(args.urlName == "discussion"){
|
||||
|
|
Loading…
Reference in New Issue