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

This commit is contained in:
yuanke 2016-08-13 18:35:21 +08:00
commit 61680c6343
5 changed files with 56 additions and 26 deletions

View File

@ -19,9 +19,29 @@ module Mobile
raise "无法获取到openid,请在微信中打开本页面" unless openid raise "无法获取到openid,请在微信中打开本页面" unless openid
us = UsersService.new 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", :password=>"12345678", :password_confirmation=>"12345678",
:should_confirmation_password => true}) :should_confirmation_password => true})
user.update_attributes(:lastname=>name)
raise user.errors.full_messages.first if user.new_record? raise user.errors.full_messages.first if user.new_record?
UserWechat.create!( UserWechat.create!(

View File

@ -122,33 +122,22 @@ module Wechat
get 'access_token', params: params, base: OAUTH2_BASE get 'access_token', params: params, base: OAUTH2_BASE
end 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 = { params = {
appid: access_token.appid, appid: access_token.appid,
grant_type: 'refresh_token', grant_type: 'refresh_token',
refresh_token: refresh_token refresh_token: user_refresh_token
} }
get 'refresh_token', params: params, base: OAUTH2_BASE get 'refresh_token', params: params, base: OAUTH2_BASE
end end
def web_userinfo(access_token,openid) def web_userinfo(web_access_token, openid, lang = 'zh_CN')
# params = { get 'userinfo', params: { access_token: web_access_token, openid: openid, lang: lang }, base: OAUTH2_USERINFO
# 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
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 end
end end

View File

@ -1,6 +1,6 @@
module Wechat module Wechat
class ApiBase 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/' MP_BASE = 'https://mp.weixin.qq.com/cgi-bin/'

View File

@ -0,0 +1,21 @@
require 'wechat/token/access_token_base'
module Wechat
module Token
class AuthAccessToken < AccessTokenBase
# def refresh
# params = {
# appid: access_token.appid,
# grant_type: 'refresh_token',
# refresh_token: user_refresh_token
# }
# data = client.get 'oauth2/refresh_token', params: params, base: OAUTH2_BASE
# write_token_to_file(data)
# read_token_from_file
# end
end
end
end

View File

@ -268,12 +268,12 @@ app.factory('common', ['$http', 'auth', '$routeParams','rms','config','wx','$loc
var urlName = response.data.type_name + "_discussion"; var urlName = response.data.type_name + "_discussion";
link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+window.g_appid + 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+ "&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{ else{
link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+window.g_appid + 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+ "&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"){ // if(args.urlName == "discussion"){