123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- var starter = angular.module('starter', ['ionic', 'ngResource', 'underscore', 'ngCordova', 'pascalprecht.translate']);
- starter.run(function ($ionicPlatform, $rootScope, $http, formatFilter, $q, $cordovaPreferences, $ionicLoading, $timeout, $ionicHistory, $cordovaKeyboard, $translate, global, cfg, showPopup) {
- // $ionicLoading.show({
- // template: '数据加载中...'
- // }).then(function () {
- // console.log("loading starter.run");
- // });
- $rootScope.commons = {
- refresh: false,
- upload_maxcount: 10,
- bigImage: undefined, //显示大图
- modal: undefined, //模态窗口
- goBackIsDisable: undefined, //禁用返回键
- isShowPopup: undefined, //加载动画
- fun: null
- };
- $ionicPlatform.ready(function () {
- if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
- cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
- cordova.plugins.Keyboard.disableScroll(true);
- }
- if (window.StatusBar) {
- StatusBar.styleDefault();
- }
- // $ionicLoading.hide();
- if (window.cordovaLinker)
- window.cordovaLinker.loadFinished("", function () {
- }, function () {
- });
- if (typeof navigator.globalization !== "undefined") {
- navigator.globalization.getPreferredLanguage(function (language) {
- console.log("globalization -> " + JSON.stringify(language));
- // alert(JSON.stringify(language))
- var value = (language.value).split('-')[0];
- if ((language.value).split('-')[1] == 'TW') {
- value = language.value
- }
- switch (value) {
- case 'zh-TW':
- value = 'locate-zh_tw';
- global.language = 'zh-TW';
- break;
- case 'en':
- value = 'locate-en_us';
- global.language = 'en';
- break;
- default:
- value = 'locate-zh_cn';
- global.language = 'zh';
- break;
- }
- $translate.use(value).then(function (data) {
- console.log("SUCCESS -> " + data);
- }, function (error) {
- console.log("ERROR -> " + error);
- });
- });
- }
- });
- global.fetch_user = function () {
- var deferred = $q.defer();
- if (global.user && global.user.token) {
- usergot(global.user, deferred)
- } else if (ionic.Platform.platform() == 'win32' || ionic.Platform.platform() == 'linux') {
- $http.post(formatFilter('{0}authcheck/', "http://srv.linkerplus.com/api/"), "imid=66J2Z5sMT&password=abc.123", {
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded'
- }
- }).then(function (response) {
- usergot(response.data, deferred);
- }, function (error) {
- console.log('通过post方式获取用户信息authcheck失败' + error);
- deferred.reject(error);
- })
- } else {
- $ionicPlatform.ready(function () {
- $cordovaPreferences.fetch('user')
- .success(function (value) {
- usergot(ionic.Platform.isAndroid() ? JSON.parse(value) : value, deferred);
- })
- .error(function (error) {
- console.error("通过$cordovaPreferences方式获取用户信息失败: " + error);
- deferred.reject(error);
- });
- });
- }
- return deferred.promise;
- };
- usergot = function (data, deferred) {
- global.user = data;
- if (data.cfg && (ionic.Platform.platform() == "ios" || ionic.Platform.platform() == 'android')) {
- global.api = data.cfg.api;
- global.api = "http://srv.linkerplus.com";//todo:测试
- } else {
- global.api = "http://srv.linkerplus.com";
- }
- deferred.resolve(data);
- console.warn(JSON.stringify(global.user));
- };
- global.fetch_user();
- global.goBack = function () {
- if (window.cordovaLinker != undefined) {
- if ($ionicHistory.backView()) {
- $ionicHistory.goBack();
- } else {
- window.cordovaLinker.goBack(1, function (s) {
- console.log(s);
- }, function (err) {
- console.error(err);
- });
- }
- }
- }
- function keyboardHideHandler(e) {
- cordova.plugins.Keyboard.isVisible = true;
- $timeout(function () {
- cordova.plugins.Keyboard.isVisible = false;
- }, 100);
- }
- window.addEventListener('native.keyboardhide', keyboardHideHandler);
- $ionicPlatform.registerBackButtonAction(function (e) {
- $timeout(function () {
- if ($rootScope.commons.isShowPopup) {
- showPopup.hideLoading();
- } else if ($rootScope.commons.goBackIsDisable) {
- return false;
- } else if ($cordovaKeyboard.isVisible()) {
- $cordovaKeyboard.close();
- } else if ($rootScope.commons.bigImage) {
- if ($rootScope.commons.fun == null) $rootScope.commons.bigImage = false;
- else {
- $rootScope.commons.fun();
- $rootScope.commons.fun = null;
- }
- } else if ($rootScope.commons.modal != null) {
- if ($rootScope.commons.fun == null) {
- $rootScope.commons.modal.hide();
- $rootScope.commons.modal = null;
- } else {
- $rootScope.commons.fun();
- $rootScope.commons.fun = null;
- }
- } else {
- if ($ionicHistory.backView()) {
- if ($rootScope.commons.fun == null) {
- $ionicHistory.goBack();
- } else {
- $rootScope.commons.fun();
- $rootScope.commons.fun = null;
- }
- } else {
- global.goBack();
- }
- }
- return false;
- })
- }, 201);
- var _user;
- _user = window.localStorage.getItem("global.user");
- if (_user != undefined) {
- console.log("localStorage get: " + _user);
- global.user = JSON.parse(_user);
- }
- })
- .config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider, $httpProvider, $provide, $translateProvider, $resourceProvider, cfg) {
- $ionicConfigProvider.platform.ios.tabs.style('standard');
- $ionicConfigProvider.platform.ios.tabs.position('bottom');
- $ionicConfigProvider.platform.android.tabs.style('standard');
- $ionicConfigProvider.platform.android.tabs.position('bottom');
- $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
- $ionicConfigProvider.platform.android.navBar.alignTitle('center');
- $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
- $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');
- $ionicConfigProvider.platform.ios.views.transition('ios');
- $ionicConfigProvider.platform.android.views.transition('android');
- var regex = new RegExp('\.(html|js|css|png|jpg)$', 'i');
- var isAsset = function (url) {
- return regex.test(url);
- };
- $stateProvider.state('selectmember', {
- url: '/selectmember',
- templateUrl: '/templates/common/selectmember.html',
- controller: 'SelectMemberCtrl'
- })
- .state('selectsinglemember', {
- url: '/selectsinglemember',
- templateUrl: '/templates/common/selectsinglemember.html',
- controller: 'SelectSingleMemberCtrl'
- })
- .state('selectdept', {
- url: '/selectdept/:id',
- templateUrl: '/templates/common/selectdept.html',
- controller: 'SelectDeptCtrl'
- })
- .state('fieldtype', {
- url: '/fieldtype/:id',
- templateUrl: '/templates/common/fieldtype.html',
- controller: 'FormFieldTypeCtrl'
- })
- .state('transfer', {
- url: '/transfer/:module/:id',
- templateUrl: '/templates/common/transfer.html',
- controller: 'TransferCtrl'
- })
- .state('selectsingledept', {
- url: '/selectsingledept',
- templateUrl: '/templates/common/selectsingledept.html',
- controller: 'SelectSingleDeptCtrl'
- })
- $urlRouterProvider.otherwise('/index');
- $translateProvider.useStaticFilesLoader({
- prefix: '/i18n/',
- suffix: '.json'
- });
- $httpProvider.interceptors.push(function ($q, cfg, global) {
- return {
- 'request': function (config) {
- if (!isAsset(config.url) && (global.refresh || global.debug)) { //if the call is not for an asset file
- config.url += (config.url.indexOf("?") === -1 ? "?" : "&") + "v=" + Date.now();
- if (global.refresh)
- global.refresh = false;
- }
- if (config.url.startsWith(cfg.api) || config.url.indexOf(global.api) == 0) {//分页时自带了请求路径
- if (config.url.startsWith(cfg.api)) config.url = global.api + config.url;
- console.log(angular.lowercase(config.method) + " url: " + config.url);
- if (global.user.token) {
- config.headers['authorization'] = 'Token ' + global.user.token;
- console.log(' authorization:' + config.headers['authorization']);
- }
- }
- config.timeout = 60000;
- return config;
- },
- 'responseError': function (rejection) {
- console.error(JSON.stringify(rejection));
- return $q.reject(rejection);
- }
- };
- });
- $resourceProvider.defaults.stripTrailingSlashes = false;
- $resourceProvider.defaults.actions.update = {
- method: 'PUT',
- params: {
- id: "@id"
- }
- };
- $resourceProvider.defaults.actions.patch = {
- method: 'PATCH',
- };
- })
- .constant('cfg', {
- // api: 'http://192.168.1.5:8000/api/'
- api: '/api/'
- })
- .value('global', {
- user: {
- usrid: "",
- token: "",
- usrname: "",
- compno: "",
- compname: "",
- deptno: "",
- deptname: "",
- roleid: "",
- im_usrid: "",
- cellphone: "",
- password: ""
- },
- api: "",
- debug: true,
- refresh: false,
- language: null
- })
- _.mixin({
- diff: function (_new, _old) {
- var r = {C: [], D: [], U: []};
- list_new = _.pluck(_new, 'id');
- list_old = _.pluck(_old, 'id');
- r.D = _.difference(list_old, list_new).join(',');
- r.C = _.filter(_new, function (item) {
- return item.id == undefined;
- });
- r.U = _.map(_.filter(_new, function (item) {
- return item.isModified && item.id != undefined;
- }), _.clone);
- _.each(r.U, function (item) {
- delete item.isModified;
- });
- _.each(r.C, function (item) {
- delete item.isModified;
- });
- return r;
- }
- });
- //http://stackoverflow.com/questions/33106114/cordova-javascript-error-has-no-method-startswith-android
- if (typeof String.prototype.startsWith != 'function') {
- String.prototype.startsWith = function (str) {
- return this.indexOf(str) === 0;
- };
- }
|