services.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. define(['angular', 'toaster', 'big'], function(angular, big) {
  2. 'use strict';
  3. angular.module('common.services', ['toaster']).factory('SessionService', function() {
  4. return {
  5. get: function(key) {
  6. var storage = window.sessionStorage;
  7. if (storage) return sessionStorage.getItem(key);
  8. return null;
  9. },
  10. set: function(key, val) {
  11. var storage = window.sessionStorage;
  12. if (storage) return sessionStorage.setItem(key, val);
  13. return null;
  14. },
  15. unset: function(key) {
  16. var storage = window.sessionStorage;
  17. if (storage) return sessionStorage.removeItem(key);
  18. return null;
  19. },
  20. getCookie: function(key) {
  21. var storage = window.localStorage;
  22. if (storage) return storage.getItem(key);
  23. else {
  24. var val = document.cookie.match(new RegExp("(^| )" + key + "=([^;]*)(;|$)"));
  25. if (val != null) {
  26. return unescape(val[2]);
  27. }
  28. return null
  29. }
  30. },
  31. setCookie: function(key, val) {
  32. var storage = window.localStorage;
  33. if (storage) storage.setItem(key, val);
  34. else {
  35. var date = new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000);
  36. document.cookie = key + "=" + escape(val) + ";expires=" + date.toGMTString();
  37. }
  38. },
  39. removeCookie: function(key) {
  40. var storage = window.localStorage;
  41. if (storage) storage.removeItem(key);
  42. else {
  43. var val = this.getCookie(key);
  44. if (val != null) {
  45. var date = new Date(new Date().getTime() - 1);
  46. document.cookie = key + "=" + val + ";expires=" + date.toGMTString()
  47. }
  48. }
  49. }
  50. };
  51. }).factory('BaseService', ['$rootScope', function($rootScope) {
  52. return {
  53. getRootPath: function() {
  54. var pathName = window.location.pathname.substring(1);
  55. var webName = pathName == '' ? '': pathName.substring(0, pathName.indexOf('/'));
  56. if (webName == "") {
  57. return window.location.protocol + '//' + window.location.host;
  58. } else {
  59. return window.location.protocol + '//' + window.location.host + '/' + webName;
  60. }
  61. },
  62. isNumber: function(n) {
  63. return ! isNaN(parseFloat(n)) && isFinite(n);
  64. },
  65. /**
  66. * parse url params
  67. */
  68. parseParams: function(requestParams) {
  69. var me = this;
  70. for (var key in requestParams) {
  71. if (key.indexOf('[') >= 0) {
  72. var params = key.split(/\[(.*)\]/),
  73. value = requestParams[key],
  74. lastKey = '';
  75. angular.forEach(params.reverse(),
  76. function(name) {
  77. if (name != '') {
  78. var v = value;
  79. value = {};
  80. value[lastKey = name] = me.isNumber(v) ? parseFloat(v) : v;
  81. }
  82. });
  83. requestParams[lastKey] = angular.extend(requestParams[lastKey] || {},
  84. value[lastKey]);
  85. delete requestParams[key];
  86. } else {
  87. requestParams[key] = me.isNumber(requestParams[key]) ? parseFloat(requestParams[key]) : requestParams[key];
  88. }
  89. }
  90. return requestParams;
  91. },
  92. // 滚动到顶部
  93. scrollBackToTop: function() {
  94. var scrollToTop = function() {
  95. if(angular.element(document).scrollTop() > 0) {
  96. window.scrollBy(0, -50);
  97. setTimeout(function() {
  98. $rootScope.$apply(function() {
  99. scrollToTop();
  100. });
  101. }, 10);
  102. }
  103. }
  104. scrollToTop();
  105. },
  106. };
  107. }]).factory('AuthenticationService', ['$http', 'SessionService', 'BaseService', 'SerializerUtil', function($http, SessionService, BaseService, SerializerUtil) {
  108. var cacheSession = function() {
  109. SessionService.set('authenticated', true);
  110. };
  111. var uncacheSession = function() {
  112. SessionService.unset('authenticated');
  113. };
  114. var rootPath = BaseService.getRootPath();
  115. return {
  116. root: function() {
  117. return rootPath;
  118. },
  119. logout: function() {
  120. var config = {
  121. cache: false,
  122. headers: {
  123. 'Cache-Control': 'no-cache',
  124. 'Pragma': 'no-cache'
  125. }
  126. };
  127. var logout = $http.get(rootPath + "/logout", config);
  128. logout.success(uncacheSession);
  129. logout.success(function(data){
  130. if (data.content) {
  131. window.location.href = data.content;
  132. }
  133. });
  134. return logout;
  135. },
  136. redirectSignin: function() {
  137. $http.get(rootPath + '/login/page').success(function(data) {
  138. if (data.content) {
  139. window.location.href = data.content;
  140. } else {
  141. toaster.pop('error', '系统错误');
  142. }
  143. }).error(function() {
  144. toaster.pop('error', '系统错误');
  145. });
  146. },
  147. isAuthed: function() {
  148. return SessionService.get('authenticated');
  149. },
  150. getAuthentication: function() {
  151. var request = $http.get(rootPath + '/authentication', {cache: true});
  152. request.success(function(data) {
  153. if (data) cacheSession();
  154. else uncacheSession();
  155. });
  156. request.error(uncacheSession);
  157. return request;
  158. },
  159. reSignin: function(enUU) {
  160. return $http.get(rootPath + '/authentication/' + enUU);
  161. }
  162. };
  163. }]).factory('SnapshotService', ['$http', 'BaseService', function($http, BaseService) {
  164. var rootPath = BaseService.getRootPath();
  165. return {
  166. getTodo: function(success) {
  167. var request = $http.get(rootPath + '/snapshot/todo', {cache: true});
  168. request.success(function(data) {
  169. success.call(null, data);
  170. });
  171. },
  172. getUnread: function(type, success) {
  173. var request = $http.get(rootPath + '/snapshot/unread/'+(type==null?'all':type), {cache: false});
  174. request.success(function(data) {
  175. success.call(null, data);
  176. });
  177. },
  178. getNotice: function(count, success) {
  179. var request = $http.get(rootPath + '/public/notice?page=1&count=' + count); // NoticeController.java
  180. request.success(function(data) {
  181. success.call(null, data.content);
  182. });
  183. },
  184. getOpenTender: function(count, success) {
  185. var request = $http.get(rootPath + '/tender/latestOpenTender?page=1&count=' + count);
  186. request.success(function(data) {
  187. success.call(null, data.content);
  188. });
  189. },
  190. getMessage: function(success) {
  191. var request = $http.get(rootPath + '/snapshot/message');
  192. request.success(function(data) {
  193. success.call(null, data);
  194. });
  195. }
  196. };
  197. }]).factory('SerializerUtil', function() {
  198. return {
  199. /**
  200. * @description 将元素值转换为序列化的字符串表示
  201. */
  202. param: function(obj) {
  203. var query = '',
  204. name, value, fullSubName, subName, subValue, innerObj, i, me = this;
  205. for (name in obj) {
  206. value = obj[name];
  207. if (value instanceof Array) {
  208. for (i = 0; i < value.length; ++i) {
  209. subValue = value[i];
  210. fullSubName = name + '[' + i + ']';
  211. innerObj = {};
  212. innerObj[fullSubName] = subValue;
  213. query += me.param(innerObj) + '&';
  214. }
  215. } else if (value instanceof Object) {
  216. for (subName in value) {
  217. subValue = value[subName];
  218. fullSubName = name + '[' + subName + ']';
  219. innerObj = {};
  220. innerObj[fullSubName] = subValue;
  221. query += me.param(innerObj) + '&';
  222. }
  223. } else if (value !== undefined && value !== null) query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
  224. }
  225. return query.length ? query.substr(0, query.length - 1) : query;
  226. }
  227. };
  228. }).factory('ReportService', ['$http', 'BaseService', 'toaster', function($http, BaseService, toaster) {
  229. var rootPath = BaseService.getRootPath();
  230. return {
  231. /**
  232. * 打印方法 enuu 企业的UU号 reportName 对应报表的名称 whereCondition
  233. * 单据的条件(格式:where tableName.propertyName = value)
  234. *
  235. */
  236. print: function(enuu, reportName, whereCondition) {
  237. window.open(rootPath + "/report/print?enuu=" + enuu + "&reportName=" + reportName + "&whereCondition=" + whereCondition);
  238. /*$http.get(rootPath + '/report/print', {
  239. params: {
  240. enuu: enuu,
  241. reportName: reportName,
  242. whereCondition: whereCondition
  243. }
  244. }).success(function(data) {
  245. if (data) {
  246. data = eval(data);
  247. var url = 'http://report.ubtob.com/report/?reportfile=' + data + '&rcondition=' + condition + '&enuu=' + enuu;
  248. window.open(url, title + '-打印', 'width=' + (window.screen.width - 10) + ',height=' + (window.screen.height * 0.87) + ',top=0,left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
  249. } else {
  250. toaster.pop('error', '系统错误', '没有设置相应的报表');
  251. }
  252. }).error(function() {
  253. console.log("out");
  254. toaster.pop('error', '系统错误', '获取报表名称失败');
  255. });*/
  256. }
  257. }
  258. }]).factory('VendorService', ['$http', '$resource', 'BaseService', function($http, $resource, BaseService) {
  259. var rootPath = BaseService.getRootPath();
  260. return {
  261. getCount: function(success) {
  262. var request = $http.get(rootPath + '/vendor/count');
  263. request.success(function(data) {
  264. success.call(null, data);
  265. });
  266. },
  267. customer: $resource('vendor/customer/info/search', {}),
  268. vendor: $resource('vendor/info/search', {}),
  269. userInfo: $resource('vendor/userInfo/:uu', {}),
  270. user: $resource('vendor/user', {}),
  271. exportXls: $resource('vendor/customer/xls/permission', {})
  272. };
  273. }]).factory('VendorPerformanceAssessService', ['$resource', function($resource) {
  274. return $resource('vendorPerformanceAssess/info/search', {}, {
  275. getAll: {
  276. url: 'vendorPerformanceAssess/info/search',
  277. method: 'GET'
  278. },
  279. setRead: {
  280. url: 'vendorPerformanceAssess/setRead',
  281. method: 'POST'
  282. },getDetail: {
  283. url: 'vendorPerformanceAssess/:id/info',
  284. method: 'GET'
  285. }
  286. });
  287. }]).factory('AccountUser', ['$resource', function($resource) {
  288. return $resource('account/user/:uu', {}, {
  289. findUsers: {
  290. url: 'account/user/customer/:uu',
  291. method: 'POST',
  292. isArray: true
  293. },
  294. addUserToVendor: {
  295. url: 'account/user/bindUserToVendor/:uu',
  296. method: 'POST'
  297. },
  298. transferUserToVendor: {
  299. url: 'account/user/transferUserToVendor/:uu',
  300. method: 'POST'
  301. },
  302. transferMyDistribute: {
  303. url: 'account/user/transferMyDistribute',
  304. method: 'POST'
  305. },
  306. findDistribute: {
  307. url: 'account/user/findDistribute',
  308. method: 'GET',
  309. isArray: true
  310. },
  311. getDistribute: {
  312. url: 'account/user/getDistribute/:custUU',
  313. method: 'GET'
  314. },
  315. getEnTransfer: {
  316. url: 'account/user/getEnTransfer/:custUU',
  317. method: 'GET'
  318. },
  319. findVendor: {
  320. url: 'account/user/findVendor/:uu',
  321. method: 'GET',
  322. isArray: true
  323. },
  324. findChooseVendor: {
  325. url: 'account/user/removeVendor/:uu',
  326. method: 'GET',
  327. isArray: true
  328. },
  329. removeChooseVendor: {
  330. url: 'account/user/removeVendor/:uu',
  331. method: 'POST',
  332. isArray: true
  333. },
  334. addVendor: {
  335. url: 'account/user/addVendor/:uu',
  336. method: 'POST'
  337. },
  338. update: {
  339. method: 'PUT'
  340. },
  341. updateRole: {
  342. url: 'account/user/update/role',
  343. method: 'POST'
  344. },
  345. checkPassword: {
  346. url: 'account/user/checkPassword',
  347. method: 'GET'
  348. },
  349. updatePassword: {
  350. url: 'account/user/updatePassword',
  351. method: 'POST'
  352. },
  353. telEnable: {
  354. url: 'account/user/telEnable',
  355. method: 'GET'
  356. },
  357. emailEnable: {
  358. url: 'account/user/emailEnable',
  359. method: 'GET'
  360. },
  361. findUsersByKeyword: {
  362. url: 'account/user/info',
  363. method: 'GET',
  364. isArray: true
  365. }
  366. });
  367. }]).factory('FeedBackService', ['$resource', 'BaseService', function($resource, BaseService) {
  368. var rootPath = BaseService.getRootPath();
  369. return $resource('serve/question', {}, {
  370. // 反馈问题
  371. feedBackQuestion: {
  372. url: rootPath + '/serve/question/feedback',
  373. method: 'POST'
  374. }
  375. });
  376. }]).filter('currency', function() { // 币别符号表示
  377. return function(cur) {
  378. if (cur == 'RMB') return '¥';
  379. else if (cur == 'USD') return '$';
  380. else if (cur == 'EUR') return '€';
  381. else if (cur == null || cur == '') return '¥';
  382. else return cur;
  383. };
  384. }).factory('SmoothScroll', function(){// 跳转到顶部
  385. var currentYPosition = function (pID) {
  386. var parent = pID ? document.getElementById(pID) :
  387. (self || document.documentElement || document.body);
  388. return parent.pageYOffset || parent.scrollTop || 0;
  389. }
  390. var elmYPosition = function(pID, eID) {
  391. var elm = document.getElementById(eID), parent = pID ? document.getElementById(pID) : document.body;
  392. var y = elm.offsetTop;
  393. var node = elm;
  394. while (node.offsetParent && node.offsetParent != parent) {
  395. node = node.offsetParent;
  396. y += node.offsetTop;
  397. }
  398. return y;
  399. }
  400. return {
  401. scrollTo: function(pID, eID, offset) {
  402. var startY = currentYPosition(pID);
  403. var stopY = elmYPosition(pID, eID) - (offset || 0);
  404. var distance = stopY > startY ? stopY - startY : startY - stopY;
  405. if (distance < 100) {
  406. scrollTo(0, stopY);
  407. return;
  408. }
  409. var speed = Math.round(distance / 100);
  410. if (speed >= 20)
  411. speed = 20;
  412. var step = Math.round(distance / 25);
  413. var leapY = stopY > startY ? startY + step : startY - step;
  414. var timer = 0;
  415. var parent = pID ? "#" + pID + "" : "document";
  416. if (stopY > startY) {
  417. for (var i = startY; i < stopY; i += step) {
  418. setTimeout("$(" + parent + ").scrollTop(" + leapY + ")", timer * speed);
  419. leapY += step;
  420. if (leapY > stopY)
  421. leapY = stopY;
  422. timer++;
  423. }
  424. return;
  425. }
  426. for (var i = startY; i > stopY; i -= step) {
  427. setTimeout("$(" + parent + ").scrollTop(" + leapY + ")", timer * speed);
  428. leapY -= step;
  429. if (leapY < stopY)
  430. leapY = stopY;
  431. timer++;
  432. }
  433. }
  434. };
  435. }).factory('Search', ['$resource', function($resource) {
  436. return $resource('search/', {}, {// 搜索接口
  437. getSimilarBrands: {
  438. // 根据品牌名获取品牌联想词
  439. url: 'search/similarBrands',
  440. method: 'GET',
  441. isArray: true
  442. },
  443. getSimilarKinds: {
  444. // 根据类目名获取类目联想词
  445. url: 'search/similarKinds',
  446. method: 'GET',
  447. isArray: true
  448. },
  449. getSimilarLeafKinds: {
  450. // 根据类目名获取 末级类目 联想词
  451. url: 'search/similarLeafKinds',
  452. method: 'GET',
  453. isArray: true
  454. },
  455. getSimilarComponents: {
  456. // 根据器件名获取器件联想词
  457. url: 'search/similarComponents',
  458. method: 'GET',
  459. isArray: true
  460. }
  461. });
  462. }]).factory('DecimalNumber', [function() {
  463. return {
  464. // accDiv: function (arg1, arg2) {
  465. // var t1 = 0, t2 = 0, r1, r2;
  466. // try {
  467. // t1 = arg1.toString().split(".")[1].length
  468. // } catch (e) {
  469. // }
  470. // try {
  471. // t2 = arg2.toString().split(".")[1].length
  472. // } catch (e) {
  473. // }
  474. // with (Math) {
  475. // r1 = Number(arg1.toString().replace(".", ""))
  476. // r2 = Number(arg2.toString().replace(".", ""))
  477. // return accMul((r1 / r2), pow(10, t2 - t1));
  478. // }
  479. // },
  480. // 乘法
  481. accMul: function (arg1, arg2) {
  482. var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
  483. try {
  484. m += s1.split(".")[1].length
  485. } catch (e) {
  486. }
  487. try {
  488. m += s2.split(".")[1].length
  489. } catch (e) {
  490. }
  491. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  492. },
  493. // 加法
  494. accAdd: function (arg1, arg2) {
  495. var r1, r2, m;
  496. try {
  497. r1 = arg1.toString().split(".")[1].length
  498. } catch (e) {
  499. r1 = 0
  500. }
  501. try {
  502. r2 = arg2.toString().split(".")[1].length
  503. } catch (e) {
  504. r2 = 0
  505. }
  506. m = Math.pow(10, Math.max(r1, r2));
  507. return (arg1 * m + arg2 * m) / m;
  508. },
  509. //减法
  510. Subtr: function(arg1, arg2) {
  511. var r1, r2, m, n;
  512. try {
  513. r1 = arg1.toString().split(".")[1].length
  514. } catch (e) {
  515. r1 = 0
  516. }
  517. try {
  518. r2 = arg2.toString().split(".")[1].length
  519. } catch (e) {
  520. r2 = 0
  521. }
  522. m = Math.pow(10, Math.max(r1, r2));
  523. n = (r1 >= r2) ? r1 : r2;
  524. return ((arg1 * m - arg2 * m) / m).toFixed(n);
  525. }
  526. };
  527. }]);
  528. });