diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index 60a674446..c00a8a86e 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -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] diff --git a/config/menu.yml b/config/menu.yml index 711b087d1..106bbfd07 100644 --- a/config/menu.yml +++ b/config/menu.yml @@ -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: "意见返馈" diff --git a/config/routes.rb b/config/routes.rb index 396be9c66..2313088cc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1165,6 +1165,7 @@ RedmineApp::Application.routes.draw do collection do get :login post :bind + get :get_open_id end end diff --git a/public/assets/wechat/issue.html b/public/assets/wechat/issue.html index 7b6eedde2..86c4e494a 100644 --- a/public/assets/wechat/issue.html +++ b/public/assets/wechat/issue.html @@ -47,6 +47,7 @@ + \ No newline at end of file diff --git a/public/javascripts/wechat/auth.js b/public/javascripts/wechat/auth.js new file mode 100644 index 000000000..aeaa03db1 --- /dev/null +++ b/public/javascripts/wechat/auth.js @@ -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); + } + }); + } +}); \ No newline at end of file diff --git a/public/javascripts/wechat/wechat-dev.js b/public/javascripts/wechat/wechat-dev.js index 9c5704b8c..760140991 100644 --- a/public/javascripts/wechat/wechat-dev.js +++ b/public/javascripts/wechat/wechat-dev.js @@ -11,23 +11,26 @@ $(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){ - $.ajax({ - url: apiUrl + 'issues/' + id, - dataType: 'json', - success: function(data){ - setTemplate(data.data); - }, - error: function(xhr,status,err){ - console.log(err); - } + getOpenId(function(openid){ + alert(openid); + $.ajax({ + url: apiUrl + 'issues/' + id + "?openid="+openid, + dataType: 'json', + success: function(data){ + setTemplate(data.data); + }, + error: function(xhr,status,err){ + console.log(err); + } + }); }) + + };