Home  >  Article  >  Web Front-end  >  AngularJs user login interaction and verification, blocking FQ processing detailed explanation

AngularJs user login interaction and verification, blocking FQ processing detailed explanation

小云云
小云云Original
2018-01-20 14:09:571405browse

This article mainly introduces the handling of AngularJs user login issues in detail, including interaction and verification, and blocking FQ processing. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

1. Static page construction and ng form verification implementation:


2. Define the controller for user login and use http in the controller Service processing login interface:


$http({
    url:G.apiUrl_dl+'loginByPhone',
    method:'post',
    data:{
     'phone':loginName,
     'pwd':pwd
    },
    headers:{'Content-Type':'application/x-www-form-urlencoded'},
    transformRequest: function(obj) {
     var str = [];
     for(var p in obj){
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
     }
     return str.join("&");
    }
   }).success(function(data){
    // 登录成功后的操作...18     19    });

3. If the login is successful, the user's data will be saved in cookie or session and the $state service will be used to jump to the specified page:


// 登录成功
    if($scope.loginActionData.token){
     sessionStorage.setItem("token", $scope.loginActionData.token);
     sessionStorage.setItem("tsname", $scope.loginActionData.name);
     sessionStorage.setItem("rights", $scope.loginActionData.rights);
     sessionStorage.setItem("userId", $scope.loginActionData.userId);
     sessionStorage.setItem("departmentsId", $scope.loginActionData.departmentsId);
     sessionStorage.setItem("departmentsName", $scope.loginActionData.departmentsName);
     $state.go('index');
    }else{
     // 登录失败的弹框提示
     $('#loginAction').modal('show');
    }

4. The next step is to prevent users from skipping the login page through other methods (such as directly entering the address in the address bar to enter the page):

The operation of this method is executed in the run method that is executed first by the controller as mentioned before. Every time before entering a page, it will be checked whether the user is logged in legally. If it is not logged in legally, we will let him Jump to the login page


angular.module.run(['$rootScope','$state',function($rootScope,$state){
  $rootScope.$on('$stateChangeStart',function(event,toState){
   // 防止FQ
   if(!(sessionStorage.getItem("token")))$state.go('register');
  });
 }]);

Related recommendations:

AngularJS environment setting tutorial for beginners

Detailed explanation of the method of implementing custom instructions in AngularJS

Detailed explanation of the method of implementing custom instructions and instruction configuration items in AngularJS

The above is the detailed content of AngularJs user login interaction and verification, blocking FQ processing detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn