config.js 11 KB

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