Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by sts on 29/03/16.
- * @author Kelvindo Sutan
- */
- (function(){
- 'use strict';
- angular.module('mogaway',['util'])
- .provider('Mogaway',MogawayProvider);
- function MogawayProvider(){
- var mogawayUrl;
- this.setUrl = function(url){
- mogawayUrl = url;
- }
- this.$get = MogawayService;
- MogawayService.$inject = ['$http','LocalStorageService','$ionicLoading','$q'];
- function MogawayService($http,LocalStorageService,$ionicLoading,$q){
- var service = {
- login : login,
- execute : execute
- };
- return service;
- function login(username,password){
- showSpinner();
- var request = {
- name: 'login',
- proc: 'doLogin',
- params: [username,password]
- }
- return $http.post(mogawayUrl,request)
- .then(function(response){
- //console.log(angular.toJson(response,true));
- var data = response.data;
- var status = data.status;
- if(status == 'FAIL'){
- var errorKey = data.errorKey;
- return $q.reject(data);
- }else{
- $ionicLoading.hide();
- //save userId, roleId, tenantId, partnerId
- LocalStorageService.set('sessionId',data.result.sessionId);
- LocalStorageService.set('tenantId',data.result.tenantId);
- LocalStorageService.set('userId',data.result.userId);
- LocalStorageService.set('partnerId',data.result.partnerId);
- LocalStorageService.set('isLogged',true);
- //LocalStorageService.setObject('availableRoles',data.roles);
- return data;
- }
- }).catch(function(response){
- console.log(angular.toJson(response,true));
- if(response.status == 'FAIL'){
- var errorKey = response.errorKey;
- return $q.reject(errorKey);
- }else{
- return $q.reject('connection.error');
- }
- });
- ;
- }
- function execute(name,proc,params,loadingAnimation){
- if(loadingAnimation == true){
- console.log('SHOW SPINNER');
- showSpinner();
- }
- var request = {
- name: name,
- proc: proc,
- params: params
- }
- return $http.post(mogawayUrl,request,{
- timeout : 30000
- })
- .then(function(response){
- if(loadingAnimation == true){
- $ionicLoading.hide();
- }
- //console.log(angular.toJson(response,true));
- var data = response.data;
- var result = data.result;
- var status = result.status;
- if(status == 'FAIL'){
- return $q.reject(result);
- }
- return response;
- })
- .catch(function(response){
- console.log(angular.toJson(response,true));
- if(loadingAnimation == true){
- $ionicLoading.hide();
- }
- if(response.status == 'FAIL'){
- var errorKey = response.errorKey;
- return $q.reject(response);
- }else{
- var customResponse = {
- errorKey : 'connection.error',
- status : 'FAIL'
- }
- console.log(angular.toJson(customResponse,true));
- return $q.reject(customResponse);
- }
- });
- }
- function showSpinner(){
- $ionicLoading.show({
- template : '<ion-spinner></ion-spinner>'
- });
- }
- }
- }
- })();
- MogawayProvider.setUrl("http://localhost:50000/api/service");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement