添加获取openid的功能

This commit is contained in:
guange 2016-03-26 15:52:14 +08:00
parent 4f7d698076
commit 60931dde6a
6 changed files with 58 additions and 12 deletions

View File

@ -171,6 +171,18 @@ class WechatsController < ActionController::Base
end
end
def get_open_id
begin
raise "非法操作, code不存在" unless params[:code]
openid = get_openid(params[:code])
raise "无法获取到openid" unless openid
render :text => {status:0, openid: openid}.to_json
rescue Exception=>e
render :text => {status: -1, msg: e.message}.to_json
end
end
def bind
begin
raise "非法操作, code不存在" unless params[:code]

View File

@ -2,7 +2,7 @@ button:
-
type: "view"
name: "最新动态"
url: "http://wechat.trustie.net/assets/wechat/issue.html"
url: "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09454f171153c2d&redirect_uri=http://wechat.trustie.net/assets/wechat/issue.html&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
-
type: "click"
name: "意见返馈"

View File

@ -1165,6 +1165,7 @@ RedmineApp::Application.routes.draw do
collection do
get :login
post :bind
get :get_open_id
end
end

View File

@ -47,6 +47,7 @@
<script src="/javascripts/jquery-1.3.2.js"></script>
<script src="/javascripts/baiduTemplate.js"></script>
<script src="/javascripts/wechat/auth.js"></script>
<script src="/javascripts/wechat/wechat-dev.js"></script>
</body>
</html>

View File

@ -0,0 +1,29 @@
$(function(){
//获取url中的参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]); return null; //返回参数值
}
var g_openid = "";
window.getOpenId = function(cb){
if (g_openid.length>0){
cb(g_openid);
}
var code = getUrlParam("code");
$.ajax({
url: '/wechat/get_open_id?code='+code,
type: 'get',
dataType: 'json',
success: function(data){
g_openid = data.openid;
cb(g_openid);
},
error: function(xhr,err){
alert("认证失败: "+err);
}
});
}
});

View File

@ -11,15 +11,15 @@ $(document).ready(function(){
var apiUrl = '/api/v1/'
var setTemplate = function(data){
console.log(data);
var html=bt('t:result-list',{issues: data});
$('#container').prepend(html);
}
var loadDataFromServer = function(id){
getOpenId(function(openid){
alert(openid);
$.ajax({
url: apiUrl + 'issues/' + id,
url: apiUrl + 'issues/' + id + "?openid="+openid,
dataType: 'json',
success: function(data){
setTemplate(data.data);
@ -27,7 +27,10 @@ $(document).ready(function(){
error: function(xhr,status,err){
console.log(err);
}
});
})
};