config.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. var starter = angular.module('starter', ['ionic', 'ngResource', 'underscore', 'ngCordova', 'pascalprecht.translate']);
  2. starter.run(function ($ionicPlatform, $rootScope, $http, formatFilter, $q, $cordovaPreferences, $ionicLoading, $timeout, $ionicHistory, $cordovaKeyboard, $translate, global, cfg, showPopup) {
  3. // $ionicLoading.show({
  4. // template: '数据加载中...'
  5. // }).then(function () {
  6. // console.log("loading starter.run");
  7. // });
  8. $rootScope.commons = {
  9. refresh: false,
  10. upload_maxcount: 10,
  11. bigImage: undefined, //显示大图
  12. modal: undefined, //模态窗口
  13. goBackIsDisable: undefined, //禁用返回键
  14. isShowPopup: undefined, //加载动画
  15. fun: null
  16. };
  17. $ionicPlatform.ready(function () {
  18. if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
  19. cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  20. cordova.plugins.Keyboard.disableScroll(true);
  21. }
  22. if (window.StatusBar) {
  23. StatusBar.styleDefault();
  24. }
  25. // $ionicLoading.hide();
  26. if (window.cordovaLinker)
  27. window.cordovaLinker.loadFinished("", function () {
  28. }, function () {
  29. });
  30. if (typeof navigator.globalization !== "undefined") {
  31. navigator.globalization.getPreferredLanguage(function (language) {
  32. console.log("globalization -> " + JSON.stringify(language));
  33. // alert(JSON.stringify(language))
  34. var value = (language.value).split('-')[0];
  35. if ((language.value).split('-')[1] == 'TW') {
  36. value = language.value
  37. }
  38. switch (value) {
  39. case 'zh-TW':
  40. value = 'locate-zh_tw';
  41. global.language = 'zh-TW';
  42. break;
  43. case 'en':
  44. value = 'locate-en_us';
  45. global.language = 'en';
  46. break;
  47. default:
  48. value = 'locate-zh_cn';
  49. global.language = 'zh';
  50. break;
  51. }
  52. $translate.use(value).then(function (data) {
  53. console.log("SUCCESS -> " + data);
  54. }, function (error) {
  55. console.log("ERROR -> " + error);
  56. });
  57. });
  58. }
  59. });
  60. global.fetch_user = function () {
  61. var deferred = $q.defer();
  62. if (global.user && global.user.token) {
  63. usergot(global.user, deferred)
  64. } else if (ionic.Platform.platform() == 'win32' || ionic.Platform.platform() == 'linux') {
  65. $http.post(formatFilter('{0}authcheck/', "http://srv.linkerplus.com/api/"), "imid=66J2Z5sMT&password=abc.123", {
  66. headers: {
  67. 'Content-Type': 'application/x-www-form-urlencoded'
  68. }
  69. }).then(function (response) {
  70. usergot(response.data, deferred);
  71. }, function (error) {
  72. console.log('通过post方式获取用户信息authcheck失败' + error);
  73. deferred.reject(error);
  74. })
  75. } else {
  76. $ionicPlatform.ready(function () {
  77. $cordovaPreferences.fetch('user')
  78. .success(function (value) {
  79. usergot(ionic.Platform.isAndroid() ? JSON.parse(value) : value, deferred);
  80. })
  81. .error(function (error) {
  82. console.error("通过$cordovaPreferences方式获取用户信息失败: " + error);
  83. deferred.reject(error);
  84. });
  85. });
  86. }
  87. return deferred.promise;
  88. };
  89. usergot = function (data, deferred) {
  90. global.user = data;
  91. if (data.cfg && (ionic.Platform.platform() == "ios" || ionic.Platform.platform() == 'android')) {
  92. global.api = data.cfg.api;
  93. global.api = "http://srv.linkerplus.com";//todo:测试
  94. } else {
  95. global.api = "http://srv.linkerplus.com";
  96. }
  97. deferred.resolve(data);
  98. console.warn(JSON.stringify(global.user));
  99. };
  100. global.fetch_user();
  101. global.goBack = function () {
  102. if (window.cordovaLinker != undefined) {
  103. if ($ionicHistory.backView()) {
  104. $ionicHistory.goBack();
  105. } else {
  106. window.cordovaLinker.goBack(1, function (s) {
  107. console.log(s);
  108. }, function (err) {
  109. console.error(err);
  110. });
  111. }
  112. }
  113. }
  114. function keyboardHideHandler(e) {
  115. cordova.plugins.Keyboard.isVisible = true;
  116. $timeout(function () {
  117. cordova.plugins.Keyboard.isVisible = false;
  118. }, 100);
  119. }
  120. window.addEventListener('native.keyboardhide', keyboardHideHandler);
  121. $ionicPlatform.registerBackButtonAction(function (e) {
  122. $timeout(function () {
  123. if ($rootScope.commons.isShowPopup) {
  124. showPopup.hideLoading();
  125. } else if ($rootScope.commons.goBackIsDisable) {
  126. return false;
  127. } else if ($cordovaKeyboard.isVisible()) {
  128. $cordovaKeyboard.close();
  129. } else if ($rootScope.commons.bigImage) {
  130. if ($rootScope.commons.fun == null) $rootScope.commons.bigImage = false;
  131. else {
  132. $rootScope.commons.fun();
  133. $rootScope.commons.fun = null;
  134. }
  135. } else if ($rootScope.commons.modal != null) {
  136. if ($rootScope.commons.fun == null) {
  137. $rootScope.commons.modal.hide();
  138. $rootScope.commons.modal = null;
  139. } else {
  140. $rootScope.commons.fun();
  141. $rootScope.commons.fun = null;
  142. }
  143. } else {
  144. if ($ionicHistory.backView()) {
  145. if ($rootScope.commons.fun == null) {
  146. $ionicHistory.goBack();
  147. } else {
  148. $rootScope.commons.fun();
  149. $rootScope.commons.fun = null;
  150. }
  151. } else {
  152. global.goBack();
  153. }
  154. }
  155. return false;
  156. })
  157. }, 201);
  158. var _user;
  159. _user = window.localStorage.getItem("global.user");
  160. if (_user != undefined) {
  161. console.log("localStorage get: " + _user);
  162. global.user = JSON.parse(_user);
  163. }
  164. })
  165. .config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider, $httpProvider, $provide, $translateProvider, $resourceProvider, cfg) {
  166. $ionicConfigProvider.platform.ios.tabs.style('standard');
  167. $ionicConfigProvider.platform.ios.tabs.position('bottom');
  168. $ionicConfigProvider.platform.android.tabs.style('standard');
  169. $ionicConfigProvider.platform.android.tabs.position('bottom');
  170. $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
  171. $ionicConfigProvider.platform.android.navBar.alignTitle('center');
  172. $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
  173. $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');
  174. $ionicConfigProvider.platform.ios.views.transition('ios');
  175. $ionicConfigProvider.platform.android.views.transition('android');
  176. var regex = new RegExp('\.(html|js|css|png|jpg)$', 'i');
  177. var isAsset = function (url) {
  178. return regex.test(url);
  179. };
  180. $stateProvider.state('selectmember', {
  181. url: '/selectmember',
  182. templateUrl: '/templates/common/selectmember.html',
  183. controller: 'SelectMemberCtrl'
  184. })
  185. .state('selectsinglemember', {
  186. url: '/selectsinglemember',
  187. templateUrl: '/templates/common/selectsinglemember.html',
  188. controller: 'SelectSingleMemberCtrl'
  189. })
  190. .state('selectdept', {
  191. url: '/selectdept/:id',
  192. templateUrl: '/templates/common/selectdept.html',
  193. controller: 'SelectDeptCtrl'
  194. })
  195. .state('fieldtype', {
  196. url: '/fieldtype/:id',
  197. templateUrl: '/templates/common/fieldtype.html',
  198. controller: 'FormFieldTypeCtrl'
  199. })
  200. .state('transfer', {
  201. url: '/transfer/:module/:id',
  202. templateUrl: '/templates/common/transfer.html',
  203. controller: 'TransferCtrl'
  204. })
  205. .state('selectsingledept', {
  206. url: '/selectsingledept',
  207. templateUrl: '/templates/common/selectsingledept.html',
  208. controller: 'SelectSingleDeptCtrl'
  209. })
  210. $urlRouterProvider.otherwise('/index');
  211. $translateProvider.useStaticFilesLoader({
  212. prefix: '/i18n/',
  213. suffix: '.json'
  214. });
  215. $httpProvider.interceptors.push(function ($q, cfg, global) {
  216. return {
  217. 'request': function (config) {
  218. if (!isAsset(config.url) && (global.refresh || global.debug)) { //if the call is not for an asset file
  219. config.url += (config.url.indexOf("?") === -1 ? "?" : "&") + "v=" + Date.now();
  220. if (global.refresh)
  221. global.refresh = false;
  222. }
  223. if (config.url.startsWith(cfg.api) || config.url.indexOf(global.api) == 0) {//分页时自带了请求路径
  224. if (config.url.startsWith(cfg.api)) config.url = global.api + config.url;
  225. console.log(angular.lowercase(config.method) + " url: " + config.url);
  226. if (global.user.token) {
  227. config.headers['authorization'] = 'Token ' + global.user.token;
  228. console.log(' authorization:' + config.headers['authorization']);
  229. }
  230. }
  231. config.timeout = 60000;
  232. return config;
  233. },
  234. 'responseError': function (rejection) {
  235. console.error(JSON.stringify(rejection));
  236. return $q.reject(rejection);
  237. }
  238. };
  239. });
  240. $resourceProvider.defaults.stripTrailingSlashes = false;
  241. $resourceProvider.defaults.actions.update = {
  242. method: 'PUT',
  243. params: {
  244. id: "@id"
  245. }
  246. };
  247. $resourceProvider.defaults.actions.patch = {
  248. method: 'PATCH',
  249. };
  250. })
  251. .constant('cfg', {
  252. // api: 'http://192.168.1.5:8000/api/'
  253. api: '/api/'
  254. })
  255. .value('global', {
  256. user: {
  257. usrid: "",
  258. token: "",
  259. usrname: "",
  260. compno: "",
  261. compname: "",
  262. deptno: "",
  263. deptname: "",
  264. roleid: "",
  265. im_usrid: "",
  266. cellphone: "",
  267. password: ""
  268. },
  269. api: "",
  270. debug: true,
  271. refresh: false,
  272. language: null
  273. })
  274. _.mixin({
  275. diff: function (_new, _old) {
  276. var r = {C: [], D: [], U: []};
  277. list_new = _.pluck(_new, 'id');
  278. list_old = _.pluck(_old, 'id');
  279. r.D = _.difference(list_old, list_new).join(',');
  280. r.C = _.filter(_new, function (item) {
  281. return item.id == undefined;
  282. });
  283. r.U = _.map(_.filter(_new, function (item) {
  284. return item.isModified && item.id != undefined;
  285. }), _.clone);
  286. _.each(r.U, function (item) {
  287. delete item.isModified;
  288. });
  289. _.each(r.C, function (item) {
  290. delete item.isModified;
  291. });
  292. return r;
  293. }
  294. });
  295. //http://stackoverflow.com/questions/33106114/cordova-javascript-error-has-no-method-startswith-android
  296. if (typeof String.prototype.startsWith != 'function') {
  297. String.prototype.startsWith = function (str) {
  298. return this.indexOf(str) === 0;
  299. };
  300. }