app.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. define([ 'toaster', 'services' ], function() {
  2. 'use strict';
  3. var app = angular.module('myApp', [ 'toaster', 'common.services' ]);
  4. app.init = function() {
  5. angular.bootstrap(document, [ 'myApp' ]);
  6. };
  7. app.controller('AuthCtrl', ['$scope', '$window', '$location', 'toaster', 'AuthenticationService','BaseService','SessionService',function($scope, $window, $location, toaster, AuthenticationService,BaseService,SessionService) {
  8. $scope.loading = false;
  9. $scope.user = {
  10. j_username : SessionService.getCookie('PDA_USERNAME'),
  11. j_password : "",
  12. remember_me : true,
  13. master:""
  14. };
  15. AuthenticationService.getMasters().success(function(responseText,status){//获取选择帐套
  16. if(responseText.masters){
  17. $scope.masters = responseText.masters;
  18. $scope.user.master = $scope.masters[0].ma_name;
  19. }
  20. });
  21. $scope.login = function(user, _url) {
  22. if($scope.user.j_username == '' ||$scope.user.j_password == ''){
  23. alert("还有必填项没有填写!");
  24. return ;
  25. }else{
  26. $scope.loading = true;
  27. AuthenticationService.login(user).success(function(responseText, status) {
  28. if(responseText) {
  29. $scope.loading = false;
  30. toaster.pop('error', '登录失败', responseText);
  31. }else if(status == 200){
  32. if(user.remember_me)
  33. SessionService.setCookie('PDA_USERNAME', user.j_username);
  34. else
  35. SessionService.removeCookie('PDA_USERNAME');
  36. var rootPath= BaseService.getRootPath();
  37. $window.location.href = rootPath+'/jsps/PDAmobile/index.html'
  38. }
  39. }).error(function(responseText) {
  40. $scope.loading = false;
  41. toaster.pop('error', '登录失败', responseText || '用户名或密码错误');
  42. });
  43. }
  44. };
  45. $scope.enter = function(event){
  46. if(event.keyCode == 13)
  47. document.getElementById("j_password").focus();
  48. }
  49. var decodeUrl = function(url) {
  50. url = unescape(url.replace(/\$2F/g, '%2F').replace(/@/g, '#'));
  51. if(url.indexOf('http:') == -1 && url.indexOf('https:') == -1) {
  52. url = AuthenticationService.root() + '/' + url;
  53. }
  54. return url;
  55. };
  56. var loginAndRedirect = function() {
  57. var path = $location.path();
  58. if(path) {
  59. var params = path.split('/'), _params = [];
  60. angular.forEach(params, function(param){
  61. param && _params.push(param);
  62. });
  63. if(_params.length == 4 && _params[0] == 'redirect') {
  64. $scope.login({
  65. j_username: _params[1],
  66. j_password: _params[2]
  67. }, decodeUrl(_params[3]));
  68. }
  69. }
  70. };
  71. loginAndRedirect();
  72. }]);
  73. return app;
  74. });