Created
          May 10, 2015 05:31 
        
      - 
      
- 
        Save CocaColaCat/144ad175c3ce45d40b4f to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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