config.js 11 KB

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