Advertisement
widana

MogawayProviders

Aug 31st, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by sts on 29/03/16.
  3.  * @author Kelvindo Sutan
  4.  */
  5. (function(){
  6.   'use strict';
  7.   angular.module('mogaway',['util'])
  8.   .provider('Mogaway',MogawayProvider);
  9.  
  10.   function MogawayProvider(){
  11.     var mogawayUrl;
  12.     this.setUrl = function(url){
  13.       mogawayUrl = url;
  14.     }
  15.     this.$get = MogawayService;
  16.  
  17.     MogawayService.$inject = ['$http','LocalStorageService','$ionicLoading','$q'];
  18.  
  19.     function MogawayService($http,LocalStorageService,$ionicLoading,$q){
  20.       var service = {
  21.         login : login,
  22.         execute : execute
  23.       };
  24.       return service;
  25.       function login(username,password){
  26.         showSpinner();
  27.         var request = {
  28.           name: 'login',
  29.           proc: 'doLogin',
  30.           params: [username,password]
  31.         }
  32.         return $http.post(mogawayUrl,request)
  33.         .then(function(response){
  34.           //console.log(angular.toJson(response,true));
  35.           var data = response.data;
  36.           var status = data.status;
  37.           if(status == 'FAIL'){
  38.             var errorKey = data.errorKey;
  39.             return $q.reject(data);
  40.           }else{
  41.             $ionicLoading.hide();
  42.             //save userId, roleId, tenantId, partnerId
  43.             LocalStorageService.set('sessionId',data.result.sessionId);
  44.             LocalStorageService.set('tenantId',data.result.tenantId);
  45.             LocalStorageService.set('userId',data.result.userId);
  46.             LocalStorageService.set('partnerId',data.result.partnerId);
  47.             LocalStorageService.set('isLogged',true);
  48.             //LocalStorageService.setObject('availableRoles',data.roles);
  49.             return data;
  50.           }
  51.         }).catch(function(response){
  52.           console.log(angular.toJson(response,true));
  53.           if(response.status == 'FAIL'){
  54.             var errorKey = response.errorKey;
  55.             return $q.reject(errorKey);
  56.           }else{
  57.             return $q.reject('connection.error');
  58.           }
  59.          
  60.         });
  61.         ;
  62.       }
  63.  
  64.       function execute(name,proc,params,loadingAnimation){
  65.         if(loadingAnimation == true){
  66.           console.log('SHOW SPINNER');
  67.           showSpinner();
  68.         }
  69.  
  70.         var request = {
  71.           name: name,
  72.           proc: proc,
  73.           params: params
  74.         }
  75.  
  76.         return $http.post(mogawayUrl,request,{
  77.           timeout : 30000
  78.         })
  79.         .then(function(response){
  80.           if(loadingAnimation == true){
  81.             $ionicLoading.hide();
  82.           }
  83.           //console.log(angular.toJson(response,true));
  84.           var data = response.data;
  85.           var result = data.result;
  86.           var status = result.status;
  87.           if(status == 'FAIL'){
  88.             return $q.reject(result);
  89.           }
  90.           return response;
  91.         })
  92.         .catch(function(response){
  93.           console.log(angular.toJson(response,true));
  94.           if(loadingAnimation == true){
  95.             $ionicLoading.hide();
  96.           }
  97.           if(response.status == 'FAIL'){
  98.             var errorKey = response.errorKey;
  99.             return $q.reject(response);
  100.           }else{
  101.             var customResponse = {
  102.               errorKey : 'connection.error',
  103.               status : 'FAIL'
  104.             }
  105.             console.log(angular.toJson(customResponse,true));
  106.             return $q.reject(customResponse);
  107.           }
  108.         });
  109.       }
  110.  
  111.       function showSpinner(){
  112.         $ionicLoading.show({
  113.           template : '<ion-spinner></ion-spinner>'
  114.         });
  115.       }
  116.     }
  117.  
  118.   }
  119. })();
  120.  
  121. MogawayProvider.setUrl("http://localhost:50000/api/service");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement