| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- define(['angular', 'toaster', 'big'], function(angular, big) {
- 'use strict';
- angular.module('common.services', ['toaster']).factory('SessionService', function() {
- return {
- get: function(key) {
- var storage = window.sessionStorage;
- if (storage) return sessionStorage.getItem(key);
- return null;
- },
- set: function(key, val) {
- var storage = window.sessionStorage;
- if (storage) return sessionStorage.setItem(key, val);
- return null;
- },
- unset: function(key) {
- var storage = window.sessionStorage;
- if (storage) return sessionStorage.removeItem(key);
- return null;
- },
- getCookie: function(key) {
- var storage = window.localStorage;
- if (storage) return storage.getItem(key);
- else {
- var val = document.cookie.match(new RegExp("(^| )" + key + "=([^;]*)(;|$)"));
- if (val != null) {
- return unescape(val[2]);
- }
- return null
- }
- },
- setCookie: function(key, val) {
- var storage = window.localStorage;
- if (storage) storage.setItem(key, val);
- else {
- var date = new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000);
- document.cookie = key + "=" + escape(val) + ";expires=" + date.toGMTString();
- }
- },
- removeCookie: function(key) {
- var storage = window.localStorage;
- if (storage) storage.removeItem(key);
- else {
- var val = this.getCookie(key);
- if (val != null) {
- var date = new Date(new Date().getTime() - 1);
- document.cookie = key + "=" + val + ";expires=" + date.toGMTString()
- }
- }
- }
- };
- }).factory('BaseService', ['$rootScope', function($rootScope) {
- return {
- getRootPath: function() {
- var pathName = window.location.pathname.substring(1);
- var webName = pathName == '' ? '': pathName.substring(0, pathName.indexOf('/'));
- if (webName == "") {
- return window.location.protocol + '//' + window.location.host;
- } else {
- return window.location.protocol + '//' + window.location.host + '/' + webName;
- }
- },
- isNumber: function(n) {
- return ! isNaN(parseFloat(n)) && isFinite(n);
- },
- /**
- * parse url params
- */
- parseParams: function(requestParams) {
- var me = this;
- for (var key in requestParams) {
- if (key.indexOf('[') >= 0) {
- var params = key.split(/\[(.*)\]/),
- value = requestParams[key],
- lastKey = '';
- angular.forEach(params.reverse(),
- function(name) {
- if (name != '') {
- var v = value;
- value = {};
- value[lastKey = name] = me.isNumber(v) ? parseFloat(v) : v;
- }
- });
- requestParams[lastKey] = angular.extend(requestParams[lastKey] || {},
- value[lastKey]);
- delete requestParams[key];
- } else {
- requestParams[key] = me.isNumber(requestParams[key]) ? parseFloat(requestParams[key]) : requestParams[key];
- }
- }
- return requestParams;
- },
- // 滚动到顶部
- scrollBackToTop: function() {
- var scrollToTop = function() {
- if(angular.element(document).scrollTop() > 0) {
- window.scrollBy(0, -50);
- setTimeout(function() {
- $rootScope.$apply(function() {
- scrollToTop();
- });
- }, 10);
- }
- }
- scrollToTop();
- },
- };
- }]).factory('AuthenticationService', ['$http', 'SessionService', 'BaseService', 'SerializerUtil', function($http, SessionService, BaseService, SerializerUtil) {
- var cacheSession = function() {
- SessionService.set('authenticated', true);
- };
- var uncacheSession = function() {
- SessionService.unset('authenticated');
- };
- var rootPath = BaseService.getRootPath();
- return {
- root: function() {
- return rootPath;
- },
- logout: function() {
- var config = {
- cache: false,
- headers: {
- 'Cache-Control': 'no-cache',
- 'Pragma': 'no-cache'
- }
- };
- var logout = $http.get(rootPath + "/logout", config);
- logout.success(uncacheSession);
- logout.success(function(data){
- if (data.content) {
- window.location.href = data.content;
- }
- });
- return logout;
- },
- redirectSignin: function() {
- $http.get(rootPath + '/login/page').success(function(data) {
- if (data.content) {
- window.location.href = data.content;
- } else {
- toaster.pop('error', '系统错误');
- }
- }).error(function() {
- toaster.pop('error', '系统错误');
- });
- },
- isAuthed: function() {
- return SessionService.get('authenticated');
- },
- getAuthentication: function() {
- var request = $http.get(rootPath + '/authentication', {cache: true});
- request.success(function(data) {
- if (data) cacheSession();
- else uncacheSession();
- });
- request.error(uncacheSession);
- return request;
- },
- reSignin: function(enUU) {
- return $http.get(rootPath + '/authentication/' + enUU);
- }
- };
- }]).factory('SnapshotService', ['$http', 'BaseService', function($http, BaseService) {
- var rootPath = BaseService.getRootPath();
- return {
- getTodo: function(success) {
- var request = $http.get(rootPath + '/snapshot/todo', {cache: true});
- request.success(function(data) {
- success.call(null, data);
- });
- },
- getUnread: function(type, success) {
- var request = $http.get(rootPath + '/snapshot/unread/'+(type==null?'all':type), {cache: false});
- request.success(function(data) {
- success.call(null, data);
- });
- },
- getNotice: function(count, success) {
- var request = $http.get(rootPath + '/public/notice?page=1&count=' + count); // NoticeController.java
- request.success(function(data) {
- success.call(null, data.content);
- });
- },
- getOpenTender: function(count, success) {
- var request = $http.get(rootPath + '/tender/latestOpenTender?page=1&count=' + count);
- request.success(function(data) {
- success.call(null, data.content);
- });
- },
- getMessage: function(success) {
- var request = $http.get(rootPath + '/snapshot/message');
- request.success(function(data) {
- success.call(null, data);
- });
- }
- };
- }]).factory('SerializerUtil', function() {
- return {
- /**
- * @description 将元素值转换为序列化的字符串表示
- */
- param: function(obj) {
- var query = '',
- name, value, fullSubName, subName, subValue, innerObj, i, me = this;
- for (name in obj) {
- value = obj[name];
- if (value instanceof Array) {
- for (i = 0; i < value.length; ++i) {
- subValue = value[i];
- fullSubName = name + '[' + i + ']';
- innerObj = {};
- innerObj[fullSubName] = subValue;
- query += me.param(innerObj) + '&';
- }
- } else if (value instanceof Object) {
- for (subName in value) {
- subValue = value[subName];
- fullSubName = name + '[' + subName + ']';
- innerObj = {};
- innerObj[fullSubName] = subValue;
- query += me.param(innerObj) + '&';
- }
- } else if (value !== undefined && value !== null) query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
- }
- return query.length ? query.substr(0, query.length - 1) : query;
- }
- };
- }).factory('ReportService', ['$http', 'BaseService', 'toaster', function($http, BaseService, toaster) {
- var rootPath = BaseService.getRootPath();
- return {
- /**
- * 打印方法 enuu 企业的UU号 reportName 对应报表的名称 whereCondition
- * 单据的条件(格式:where tableName.propertyName = value)
- *
- */
- print: function(enuu, reportName, whereCondition) {
- window.open(rootPath + "/report/print?enuu=" + enuu + "&reportName=" + reportName + "&whereCondition=" + whereCondition);
- /*$http.get(rootPath + '/report/print', {
- params: {
- enuu: enuu,
- reportName: reportName,
- whereCondition: whereCondition
- }
- }).success(function(data) {
- if (data) {
- data = eval(data);
- var url = 'http://report.ubtob.com/report/?reportfile=' + data + '&rcondition=' + condition + '&enuu=' + enuu;
- 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');
- } else {
- toaster.pop('error', '系统错误', '没有设置相应的报表');
- }
- }).error(function() {
- console.log("out");
- toaster.pop('error', '系统错误', '获取报表名称失败');
- });*/
- }
- }
- }]).factory('VendorService', ['$http', '$resource', 'BaseService', function($http, $resource, BaseService) {
- var rootPath = BaseService.getRootPath();
- return {
- getCount: function(success) {
- var request = $http.get(rootPath + '/vendor/count');
- request.success(function(data) {
- success.call(null, data);
- });
- },
- customer: $resource('vendor/customer/info/search', {}),
- vendor: $resource('vendor/info/search', {}),
- userInfo: $resource('vendor/userInfo/:uu', {}),
- user: $resource('vendor/user', {}),
- exportXls: $resource('vendor/customer/xls/permission', {})
- };
- }]).factory('VendorPerformanceAssessService', ['$resource', function($resource) {
- return $resource('vendorPerformanceAssess/info/search', {}, {
- getAll: {
- url: 'vendorPerformanceAssess/info/search',
- method: 'GET'
- },
- setRead: {
- url: 'vendorPerformanceAssess/setRead',
- method: 'POST'
- },getDetail: {
- url: 'vendorPerformanceAssess/:id/info',
- method: 'GET'
- }
- });
- }]).factory('AccountUser', ['$resource', function($resource) {
- return $resource('account/user/:uu', {}, {
- findUsers: {
- url: 'account/user/customer/:uu',
- method: 'POST',
- isArray: true
- },
- addUserToVendor: {
- url: 'account/user/bindUserToVendor/:uu',
- method: 'POST'
- },
- transferUserToVendor: {
- url: 'account/user/transferUserToVendor/:uu',
- method: 'POST'
- },
- transferMyDistribute: {
- url: 'account/user/transferMyDistribute',
- method: 'POST'
- },
- findDistribute: {
- url: 'account/user/findDistribute',
- method: 'GET',
- isArray: true
- },
- getDistribute: {
- url: 'account/user/getDistribute/:custUU',
- method: 'GET'
- },
- getEnTransfer: {
- url: 'account/user/getEnTransfer/:custUU',
- method: 'GET'
- },
- findVendor: {
- url: 'account/user/findVendor/:uu',
- method: 'GET',
- isArray: true
- },
- findChooseVendor: {
- url: 'account/user/removeVendor/:uu',
- method: 'GET',
- isArray: true
- },
- removeChooseVendor: {
- url: 'account/user/removeVendor/:uu',
- method: 'POST',
- isArray: true
- },
- addVendor: {
- url: 'account/user/addVendor/:uu',
- method: 'POST'
- },
- update: {
- method: 'PUT'
- },
- updateRole: {
- url: 'account/user/update/role',
- method: 'POST'
- },
- checkPassword: {
- url: 'account/user/checkPassword',
- method: 'GET'
- },
- updatePassword: {
- url: 'account/user/updatePassword',
- method: 'POST'
- },
- telEnable: {
- url: 'account/user/telEnable',
- method: 'GET'
- },
- emailEnable: {
- url: 'account/user/emailEnable',
- method: 'GET'
- },
- findUsersByKeyword: {
- url: 'account/user/info',
- method: 'GET',
- isArray: true
- }
- });
- }]).factory('FeedBackService', ['$resource', 'BaseService', function($resource, BaseService) {
- var rootPath = BaseService.getRootPath();
- return $resource('serve/question', {}, {
- // 反馈问题
- feedBackQuestion: {
- url: rootPath + '/serve/question/feedback',
- method: 'POST'
- }
- });
- }]).filter('currency', function() { // 币别符号表示
- return function(cur) {
- if (cur == 'RMB') return '¥';
- else if (cur == 'USD') return '$';
- else if (cur == 'EUR') return '€';
- else if (cur == null || cur == '') return '¥';
- else return cur;
- };
- }).factory('SmoothScroll', function(){// 跳转到顶部
- var currentYPosition = function (pID) {
- var parent = pID ? document.getElementById(pID) :
- (self || document.documentElement || document.body);
- return parent.pageYOffset || parent.scrollTop || 0;
- }
- var elmYPosition = function(pID, eID) {
- var elm = document.getElementById(eID), parent = pID ? document.getElementById(pID) : document.body;
- var y = elm.offsetTop;
- var node = elm;
- while (node.offsetParent && node.offsetParent != parent) {
- node = node.offsetParent;
- y += node.offsetTop;
- }
- return y;
- }
- return {
- scrollTo: function(pID, eID, offset) {
- var startY = currentYPosition(pID);
- var stopY = elmYPosition(pID, eID) - (offset || 0);
- var distance = stopY > startY ? stopY - startY : startY - stopY;
- if (distance < 100) {
- scrollTo(0, stopY);
- return;
- }
- var speed = Math.round(distance / 100);
- if (speed >= 20)
- speed = 20;
- var step = Math.round(distance / 25);
- var leapY = stopY > startY ? startY + step : startY - step;
- var timer = 0;
- var parent = pID ? "#" + pID + "" : "document";
- if (stopY > startY) {
- for (var i = startY; i < stopY; i += step) {
- setTimeout("$(" + parent + ").scrollTop(" + leapY + ")", timer * speed);
- leapY += step;
- if (leapY > stopY)
- leapY = stopY;
- timer++;
- }
- return;
- }
- for (var i = startY; i > stopY; i -= step) {
- setTimeout("$(" + parent + ").scrollTop(" + leapY + ")", timer * speed);
- leapY -= step;
- if (leapY < stopY)
- leapY = stopY;
- timer++;
- }
- }
- };
- }).factory('Search', ['$resource', function($resource) {
- return $resource('search/', {}, {// 搜索接口
- getSimilarBrands: {
- // 根据品牌名获取品牌联想词
- url: 'search/similarBrands',
- method: 'GET',
- isArray: true
- },
- getSimilarKinds: {
- // 根据类目名获取类目联想词
- url: 'search/similarKinds',
- method: 'GET',
- isArray: true
- },
- getSimilarLeafKinds: {
- // 根据类目名获取 末级类目 联想词
- url: 'search/similarLeafKinds',
- method: 'GET',
- isArray: true
- },
- getSimilarComponents: {
- // 根据器件名获取器件联想词
- url: 'search/similarComponents',
- method: 'GET',
- isArray: true
- }
- });
- }]).factory('DecimalNumber', [function() {
- return {
- // accDiv: function (arg1, arg2) {
- // var t1 = 0, t2 = 0, r1, r2;
- // try {
- // t1 = arg1.toString().split(".")[1].length
- // } catch (e) {
- // }
- // try {
- // t2 = arg2.toString().split(".")[1].length
- // } catch (e) {
- // }
- // with (Math) {
- // r1 = Number(arg1.toString().replace(".", ""))
- // r2 = Number(arg2.toString().replace(".", ""))
- // return accMul((r1 / r2), pow(10, t2 - t1));
- // }
- // },
- // 乘法
- accMul: function (arg1, arg2) {
- var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
- try {
- m += s1.split(".")[1].length
- } catch (e) {
- }
- try {
- m += s2.split(".")[1].length
- } catch (e) {
- }
- return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
- },
- // 加法
- accAdd: function (arg1, arg2) {
- var r1, r2, m;
- try {
- r1 = arg1.toString().split(".")[1].length
- } catch (e) {
- r1 = 0
- }
- try {
- r2 = arg2.toString().split(".")[1].length
- } catch (e) {
- r2 = 0
- }
- m = Math.pow(10, Math.max(r1, r2));
- return (arg1 * m + arg2 * m) / m;
- },
- //减法
- Subtr: function(arg1, arg2) {
- var r1, r2, m, n;
- try {
- r1 = arg1.toString().split(".")[1].length
- } catch (e) {
- r1 = 0
- }
- try {
- r2 = arg2.toString().split(".")[1].length
- } catch (e) {
- r2 = 0
- }
- m = Math.pow(10, Math.max(r1, r2));
- n = (r1 >= r2) ? r1 : r2;
- return ((arg1 * m - arg2 * m) / m).toFixed(n);
- }
- };
- }]);
- });
|