Skip to content

Instantly share code, notes, and snippets.

@CocaColaCat
Created May 10, 2015 05:31
Show Gist options
  • Save CocaColaCat/144ad175c3ce45d40b4f to your computer and use it in GitHub Desktop.
Save CocaColaCat/144ad175c3ce45d40b4f to your computer and use it in GitHub Desktop.
angular.module('app.account')
.controller('WechatAuthCtrl',
['$scope', '$location','AuthService', 'Account', 'AuthToken', 'jwtHelper',
function($scope, $location, AuthService, Account, AuthToken, jwtHelper) {
// 获取返回参数 code 和 state
var code = $location.search().code;
var state = $location.search().state;
// 通过 code 来获得签名的 openid
var respond = AuthToken.get_wechat_token(code, state);
respond.then(function(data){
// 处理 2XX 回复,获取授权令牌
var auth_token = data.auth_token;
// 获取签名微信 openid
var wechat_token = data.wechat_token;
var account_id = undefined;
// 如果有授权令牌,存令牌,load 用户信息,转到 state 声明的路由
if (auth_token != undefined){
account_id = jwtHelper.decodeToken(auth_token).id;
Account.getById(account_id).then(function(account) {
AuthService.login(data, account);
if(state != ""){
$location.path(decodeURIComponent(state));
}else{
$location.path('/projects');
}
})
}
else{
// 没有授权令牌,说明没有绑定到微信 openid,存微信令牌用于后面的绑定
AuthService.storeWechatToken(wechat_token);
$location.path("/login");
}
},function(data){
$location.path("/login");
});
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment