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:
An example of the WeChat applet obtaining the mobile phone number authorized user login function
Detailed explanation of how javascript operates cookies to implement user login code examples
php object-oriented user login authentication
The above is the detailed content of AngularJs user login problem handling. For more information, please follow other related articles on the PHP Chinese website!