config.js 11 KB

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