From 5c42cdb39c2fe67d36130d3b2f8f9dfc2ae1dc7f Mon Sep 17 00:00:00 2001 From: yuanke <249218296@qq.com> Date: Fri, 12 Aug 2016 10:40:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E4=BA=AB=E5=86=85=E5=AE=B9=E6=97=B6?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/new_comment.rb | 9 ++++++++- app/api/mobile/apis/users.rb | 1 - app/controllers/wechats_controller.rb | 4 ++++ lib/wechat/lib/wechat/api.rb | 9 +++++++++ public/javascripts/wechat/others/factory.js | 4 ++-- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/api/mobile/apis/new_comment.rb b/app/api/mobile/apis/new_comment.rb index 2c5ab7754..63d476601 100644 --- a/app/api/mobile/apis/new_comment.rb +++ b/app/api/mobile/apis/new_comment.rb @@ -26,7 +26,14 @@ module Mobile login = openid[0..10]+openid[openid_length-3..openid_length-1] - name = openid[0..3]+"***"+openid.last + access_token = session[:access_token] + + if access_token + userinfo = wechat.web_userinfo(access_token,openid) + 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", diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index 19cce07e7..30e4cc1f9 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -67,7 +67,6 @@ module Mobile user[:password] = params[:password] user[:password_confirmation] = params[:password] user[:should_confirmation_password] = true - user[:lastname]="" user.save! else us = UsersService.new diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index 75ac7aba5..e1fa7dcc2 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -445,6 +445,10 @@ class WechatsController < ActionController::Base unless openid if code openid = wechat.web_access_token(code)["openid"] + access_token = wechat.web_access_token(code)["access_token"] + if access_token + session[:access_token] = access_token + end end end diff --git a/lib/wechat/lib/wechat/api.rb b/lib/wechat/lib/wechat/api.rb index 5cb457b9f..f1255939a 100644 --- a/lib/wechat/lib/wechat/api.rb +++ b/lib/wechat/lib/wechat/api.rb @@ -120,5 +120,14 @@ module Wechat } get 'access_token', params: params, base: OAUTH2_BASE end + + def web_userinfo(access_token,openid) + params = { + access_token: access_token, + openid: openid, + lang: "zh_CN" + } + get 'user_info', params: params, base: OAUTH2_BASE + end end end diff --git a/public/javascripts/wechat/others/factory.js b/public/javascripts/wechat/others/factory.js index f0d48dda9..19acf39c9 100644 --- a/public/javascripts/wechat/others/factory.js +++ b/public/javascripts/wechat/others/factory.js @@ -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"; } console.log("link= "+link);