services.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 keys = document.cookie.match(/[^ =;]+(?=\=)/g);
  121. if(keys) {
  122. for(var i = keys.length; i--;)
  123. document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
  124. }
  125. var config = {
  126. cache: false,
  127. headers: {
  128. 'Cache-Control': 'no-cache',
  129. 'Pragma': 'no-cache',
  130. },
  131. ifModified :true ,
  132. };
  133. var logout = $http.get(rootPath + "/logout", config);
  134. logout.success(uncacheSession);
  135. logout.success(function(data){
  136. if (data.url) $http.get(data.url, config);
  137. var pathName = document.location.pathname;
  138. var index = pathName.substr(1).indexOf("/");// platform-b2c/logout/proxy
  139. var result = pathName.substr(0,index);
  140. var uri = pathName.substr(13,index+1000) + document.location.hash;
  141. if (result == '/platform-b2b') {
  142. var contextPath = ''
  143. data.content = contextPath + uri;
  144. }
  145. if (data.content) {
  146. if (data.content == window.location.href) {
  147. // window.location.reload();
  148. } else {
  149. window.location.href = data.content;
  150. }
  151. }
  152. });
  153. return logout;
  154. },
  155. redirectSignin: function() {
  156. $http.get(rootPath + '/login/page').success(function(data) {
  157. if (data.content) {
  158. window.location.href = data.content;
  159. } else {
  160. toaster.pop('error', '系统错误');
  161. }
  162. }).error(function() {
  163. toaster.pop('error', '系统错误');
  164. });
  165. },
  166. redirectRegister: function() {
  167. // 获取跳转注册的url
  168. $http.get(rootPath + '/register/page', {
  169. params: {
  170. returnUrl: window.location.href
  171. }
  172. }).success(function(data) {
  173. if (data.content) {
  174. window.location.href = data.content;
  175. } else {
  176. alert('系统错误');
  177. }
  178. }).error(function() {
  179. alert('系统错误');
  180. });
  181. },
  182. isAuthed: function() {
  183. return SessionService.get('authenticated');
  184. },
  185. getAuthentication: function() {
  186. var request = $http.get(rootPath + '/authentication', {cache: true});
  187. request.success(function(data) {
  188. if (data) cacheSession();
  189. else uncacheSession();
  190. });
  191. request.error(uncacheSession);
  192. return request;
  193. },
  194. reSignin: function(enUU) {
  195. return $http.get(rootPath + '/authentication/' + enUU);
  196. }
  197. };
  198. }]).factory('SnapshotService', ['$http', 'BaseService', function($http, BaseService) {
  199. var rootPath = BaseService.getRootPath();
  200. return {
  201. getTodo: function(success) {
  202. var request = $http.get(rootPath + '/snapshot/todo', {cache: true});
  203. request.success(function(data) {
  204. success.call(null, data);
  205. });
  206. },
  207. getUnread: function(type, success) {
  208. var request = $http.get(rootPath + '/snapshot/unread/'+(type==null?'all':type), {cache: false});
  209. request.success(function(data) {
  210. success.call(null, data);
  211. });
  212. },
  213. getNotice: function(count, useruu, success) {
  214. var request = $http.get(rootPath + '/public/notice?page=1&count=' + count + '&useruu=' + useruu); // NoticeController.java
  215. request.success(function(data) {
  216. success.call(null, data.content);
  217. });
  218. },
  219. getOpenTender: function(count, success) {
  220. var request = $http.get(rootPath + '/tender/latestOpenTender?page=1&count=' + count);
  221. request.success(function(data) {
  222. success.call(null, data.content);
  223. });
  224. },
  225. getMessage: function(success) {
  226. var request = $http.get(rootPath + '/snapshot/message');
  227. request.success(function(data) {
  228. success.call(null, data);
  229. });
  230. },
  231. setNoticeStatusAfterRead: function (id, useruu, success) {
  232. var request = $http.post(rootPath + '/public/notice/setReadStatus?id=' + id + '&useruu=' + useruu);
  233. request.success(function() {
  234. success.call(null);
  235. });
  236. }
  237. };
  238. }]).factory('SerializerUtil', function() {
  239. return {
  240. /**
  241. * @description 将元素值转换为序列化的字符串表示
  242. */
  243. param: function(obj) {
  244. var query = '',
  245. name, value, fullSubName, subName, subValue, innerObj, i, me = this;
  246. for (name in obj) {
  247. value = obj[name];
  248. if (value instanceof Array) {
  249. for (i = 0; i < value.length; ++i) {
  250. subValue = value[i];
  251. fullSubName = name + '[' + i + ']';
  252. innerObj = {};
  253. innerObj[fullSubName] = subValue;
  254. query += me.param(innerObj) + '&';
  255. }
  256. } else if (value instanceof Object) {
  257. for (subName in value) {
  258. subValue = value[subName];
  259. fullSubName = name + '[' + subName + ']';
  260. innerObj = {};
  261. innerObj[fullSubName] = subValue;
  262. query += me.param(innerObj) + '&';
  263. }
  264. } else if (value !== undefined && value !== null) query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
  265. }
  266. return query.length ? query.substr(0, query.length - 1) : query;
  267. }
  268. };
  269. }).factory('ReportService', ['$http', 'BaseService', 'toaster', '$rootScope', function($http, BaseService, toaster, $rootScope) {
  270. var rootPath = BaseService.getRootPath();
  271. return {
  272. /**
  273. * 打印方法 enuu 企业的UU号 reportName 对应报表的名称 whereCondition
  274. * 单据的条件(格式:where tableName.propertyName = value)
  275. *
  276. */
  277. print: function(enuu, reportName, whereCondition, newPage) {
  278. var printUrl = rootPath + "/report/print?enuu=" + enuu + "&reportName=" + reportName + "&whereCondition=" + whereCondition;
  279. // window.open(rootPath + "/report/print?enuu=" + enuu + "&reportName=" + reportName + "&whereCondition=" + whereCondition);
  280. // 不为空的为需要验证权限的,否则是不用验证权限的
  281. if (null != newPage) {
  282. newPage.location.href=printUrl;
  283. } else {
  284. window.open(rootPath + "/report/print?enuu=" + enuu + "&reportName=" + reportName + "&whereCondition=" + whereCondition);
  285. }
  286. /*$http.get(rootPath + '/report/print', {
  287. params: {
  288. enuu: enuu,
  289. reportName: reportName,
  290. whereCondition: whereCondition
  291. }
  292. }).success(function(data) {
  293. if (data) {
  294. data = eval(data);
  295. var url = 'http://report.ubtob.com/report/?reportfile=' + data + '&rcondition=' + condition + '&enuu=' + enuu;
  296. 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');
  297. } else {
  298. toaster.pop('error', '系统错误', '没有设置相应的报表');
  299. }
  300. }).error(function() {
  301. console.log("out");
  302. toaster.pop('error', '系统错误', '获取报表名称失败');
  303. });*/
  304. }
  305. }
  306. }]).factory('VendorService', ['$http', '$resource', 'BaseService', function($http, $resource, BaseService) {
  307. var rootPath = BaseService.getRootPath();
  308. return {
  309. getCount: function(success) {
  310. var request = $http.get(rootPath + '/vendor/count');
  311. request.success(function(data) {
  312. success.call(null, data);
  313. });
  314. },
  315. customer: $resource('vendor/customer/info/search', {}),
  316. vendor: $resource('vendor/info/search', {}),
  317. userInfo: $resource('vendor/userInfo/:uu', {}),
  318. user: $resource('vendor/user', {}),
  319. exportXls: $resource('vendor/customer/xls/permission', {})
  320. };
  321. }]).factory('VendorPerformanceAssessService', ['$resource', function($resource) {
  322. return $resource('vendorPerformanceAssess/info/search', {}, {
  323. getAll: {
  324. url: 'vendorPerformanceAssess/info/search',
  325. method: 'GET'
  326. },
  327. setRead: {
  328. url: 'vendorPerformanceAssess/setRead',
  329. method: 'POST'
  330. },getDetail: {
  331. url: 'vendorPerformanceAssess/:id/info',
  332. method: 'GET'
  333. }
  334. });
  335. }]).factory('AccountUser', ['$resource', function($resource) {
  336. return $resource('account/user/:uu', {}, {
  337. getAll: {
  338. url: 'account/user',
  339. method: 'GET'
  340. },
  341. findUsers: {
  342. url: 'account/user/customer/:uu',
  343. method: 'POST',
  344. isArray: true
  345. },
  346. addUserToVendor: {
  347. url: 'account/user/bindUserToVendor/:uu',
  348. method: 'POST'
  349. },
  350. transferUserToVendor: {
  351. url: 'account/user/transferUserToVendor/:uu',
  352. method: 'POST'
  353. },
  354. transferMyDistribute: {
  355. url: 'account/user/transferMyDistribute',
  356. method: 'POST'
  357. },
  358. findDistribute: {
  359. url: 'account/user/findDistribute',
  360. method: 'GET',
  361. isArray: true
  362. },
  363. getDistribute: {
  364. url: 'account/user/getDistribute/:custUU',
  365. method: 'GET'
  366. },
  367. getEnTransfer: {
  368. url: 'account/user/getEnTransfer/:custUU',
  369. method: 'GET'
  370. },
  371. findVendor: {
  372. url: 'account/user/findVendor/:uu',
  373. method: 'GET',
  374. isArray: true
  375. },
  376. findChooseVendor: {
  377. url: 'account/user/removeVendor/:uu',
  378. method: 'GET',
  379. isArray: true
  380. },
  381. removeChooseVendor: {
  382. url: 'account/user/removeVendor/:uu',
  383. method: 'POST',
  384. isArray: true
  385. },
  386. addVendor: {
  387. url: 'account/user/addVendor/:uu',
  388. method: 'POST'
  389. },
  390. update: {
  391. method: 'PUT'
  392. },
  393. updateRole: {
  394. url: 'account/user/update/role',
  395. method: 'POST'
  396. },
  397. checkPassword: {
  398. url: 'account/user/checkPassword',
  399. method: 'GET'
  400. },
  401. updatePassword: {
  402. url: 'account/user/updatePassword',
  403. method: 'POST'
  404. },
  405. telEnable: {
  406. url: 'account/user/telEnable',
  407. method: 'GET'
  408. },
  409. emailEnable: {
  410. url: 'account/user/emailEnable',
  411. method: 'GET'
  412. },
  413. findUsersByKeyword: {
  414. url: 'account/user/info',
  415. method: 'GET',
  416. isArray: true
  417. },
  418. getUpdatePasswordUrl: {
  419. url: 'account/user/getUrl',
  420. params: {
  421. "_operate": "updatePassword"
  422. },
  423. method: 'GET'
  424. },
  425. getCloudCenterUrl: {
  426. url: 'account/user/getUrl',
  427. params: {
  428. "_operate": "cloudcenter"
  429. },
  430. method: 'GET'
  431. }
  432. });
  433. }]).factory('FeedBackService', ['$resource', 'BaseService', function($resource, BaseService) {
  434. var rootPath = BaseService.getRootPath();
  435. return $resource('serve/question', {}, {
  436. // 反馈问题
  437. feedBackQuestion: {
  438. url: rootPath + '/serve/question/feedback',
  439. method: 'POST'
  440. }
  441. });
  442. }]).filter('currency', function() { // 币别符号表示
  443. return function(cur) {
  444. if (cur == 'RMB') return '¥';
  445. else if (cur == 'USD') return '$';
  446. else if (cur == 'EUR') return '€';
  447. else if (cur == null || cur == '') return '¥';
  448. else return cur;
  449. };
  450. }).factory('SmoothScroll', function(){// 跳转到顶部
  451. var currentYPosition = function (pID) {
  452. var parent = pID ? document.getElementById(pID) :
  453. (self || document.documentElement || document.body);
  454. return parent.pageYOffset || parent.scrollTop || 0;
  455. }
  456. var elmYPosition = function(pID, eID) {
  457. var elm = document.getElementById(eID), parent = pID ? document.getElementById(pID) : document.body;
  458. var y = elm.offsetTop;
  459. var node = elm;
  460. while (node.offsetParent && node.offsetParent != parent) {
  461. node = node.offsetParent;
  462. y += node.offsetTop;
  463. }
  464. return y;
  465. }
  466. return {
  467. scrollTo: function(pID, eID, offset) {
  468. var startY = currentYPosition(pID);
  469. var stopY = elmYPosition(pID, eID) - (offset || 0);
  470. var distance = stopY > startY ? stopY - startY : startY - stopY;
  471. if (distance < 100) {
  472. scrollTo(0, stopY);
  473. return;
  474. }
  475. var speed = Math.round(distance / 100);
  476. if (speed >= 20)
  477. speed = 20;
  478. var step = Math.round(distance / 25);
  479. var leapY = stopY > startY ? startY + step : startY - step;
  480. var timer = 0;
  481. var parent = pID ? "#" + pID + "" : "document";
  482. if (stopY > startY) {
  483. for (var i = startY; i < stopY; i += step) {
  484. setTimeout("$(" + parent + ").scrollTop(" + leapY + ")", timer * speed);
  485. leapY += step;
  486. if (leapY > stopY)
  487. leapY = stopY;
  488. timer++;
  489. }
  490. return;
  491. }
  492. for (var i = startY; i > stopY; i -= step) {
  493. setTimeout("$(" + parent + ").scrollTop(" + leapY + ")", timer * speed);
  494. leapY -= step;
  495. if (leapY < stopY)
  496. leapY = stopY;
  497. timer++;
  498. }
  499. }
  500. };
  501. }).factory('Search', ['$resource', function($resource) {
  502. return $resource('search/', {}, {// 搜索接口
  503. getSimilarBrands: {
  504. // 根据品牌名获取品牌联想词
  505. url: 'search/similarBrands',
  506. method: 'GET',
  507. isArray: true
  508. },
  509. getSimilarKinds: {
  510. // 根据类目名获取类目联想词
  511. url: 'search/similarKinds',
  512. method: 'GET',
  513. isArray: true
  514. },
  515. getSimilarLeafKinds: {
  516. // 根据类目名获取 末级类目 联想词
  517. url: 'search/similarLeafKinds',
  518. method: 'GET',
  519. isArray: true
  520. },
  521. getSimilarComponents: {
  522. // 根据器件名获取器件联想词
  523. url: 'search/similarComponents',
  524. method: 'GET',
  525. isArray: true
  526. }
  527. });
  528. }]).factory('DecimalNumber', [function() {
  529. return {
  530. // accDiv: function (arg1, arg2) {
  531. // var t1 = 0, t2 = 0, r1, r2;
  532. // try {
  533. // t1 = arg1.toString().split(".")[1].length
  534. // } catch (e) {
  535. // }
  536. // try {
  537. // t2 = arg2.toString().split(".")[1].length
  538. // } catch (e) {
  539. // }
  540. // with (Math) {
  541. // r1 = Number(arg1.toString().replace(".", ""))
  542. // r2 = Number(arg2.toString().replace(".", ""))
  543. // return accMul((r1 / r2), pow(10, t2 - t1));
  544. // }
  545. // },
  546. // 乘法
  547. accMul: function (arg1, arg2) {
  548. var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
  549. try {
  550. m += s1.split(".")[1].length
  551. } catch (e) {
  552. }
  553. try {
  554. m += s2.split(".")[1].length
  555. } catch (e) {
  556. }
  557. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
  558. },
  559. // 加法
  560. accAdd: function (arg1, arg2) {
  561. var r1, r2, m;
  562. try {
  563. r1 = arg1.toString().split(".")[1].length
  564. } catch (e) {
  565. r1 = 0
  566. }
  567. try {
  568. r2 = arg2.toString().split(".")[1].length
  569. } catch (e) {
  570. r2 = 0
  571. }
  572. m = Math.pow(10, Math.max(r1, r2));
  573. return (arg1 * m + arg2 * m) / m;
  574. },
  575. //减法
  576. Subtr: function(arg1, arg2) {
  577. var r1, r2, m, n;
  578. try {
  579. r1 = arg1.toString().split(".")[1].length
  580. } catch (e) {
  581. r1 = 0
  582. }
  583. try {
  584. r2 = arg2.toString().split(".")[1].length
  585. } catch (e) {
  586. r2 = 0
  587. }
  588. m = Math.pow(10, Math.max(r1, r2));
  589. n = (r1 >= r2) ? r1 : r2;
  590. return ((arg1 * m - arg2 * m) / m).toFixed(n);
  591. }
  592. };
  593. }]);
  594. });