|
|
@@ -0,0 +1,611 @@
|
|
|
+define([ 'angular', 'common/utils', 'ngResource'], function(angular, utils, Big) {
|
|
|
+ 'use strict';
|
|
|
+ angular.module('common.services', [ 'common.utils', 'ngResource' ]).factory('FlashService', ['$rootScope', function($rootScope) {
|
|
|
+ return {
|
|
|
+ show : function(message) {
|
|
|
+ $rootScope.flashMessage = message;
|
|
|
+ },
|
|
|
+ clear : function() {
|
|
|
+ $rootScope.flashMessage = "";
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }]).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 == "" || webName == "store") {
|
|
|
+ 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;
|
|
|
+ },
|
|
|
+ // 获取之后的路径
|
|
|
+ getCurrentUrl: function() {
|
|
|
+ var fullPath = window.document.location.href;
|
|
|
+ return fullPath.substring(this.getRootPath().length, fullPath.length);
|
|
|
+ },
|
|
|
+ // 返回路径中带的搜索关键字
|
|
|
+ getKeyWord: function() {
|
|
|
+ var urlParam = window.document.location.href;
|
|
|
+ var keyword = "";
|
|
|
+ if(urlParam) {
|
|
|
+ keyword = urlParam.substring(urlParam.indexOf("?wd=")+4, urlParam.length);
|
|
|
+ }
|
|
|
+ return keyword;
|
|
|
+ },
|
|
|
+ decodeSpecialCode: function(str) {
|
|
|
+ return str.replace('%2F', '/').replace('%3F', '?').replace('%25', '%')
|
|
|
+ .replace('%20', ' ').replace('%3D', '=').replace('%23', '#');
|
|
|
+ },
|
|
|
+ scrollBackToTop: function() {
|
|
|
+ var scrollToTop = function() {
|
|
|
+ if(angular.element(document).scrollTop() > 0) {
|
|
|
+ window.scrollBy(0, -50);
|
|
|
+ setTimeout(function() {
|
|
|
+ $rootScope.$apply(function() {
|
|
|
+ scrollToTop();
|
|
|
+ });
|
|
|
+ }, 10);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ scrollToTop();
|
|
|
+ },
|
|
|
+ // 获取指定模块pathname,主要用于兼容类似于产品模块同时使用两个product和products这类情况
|
|
|
+ getModulePathname: function (currentPathname, currentModule, nextModule) {
|
|
|
+ if(currentPathname.indexOf(currentModule+'s') !== -1) {
|
|
|
+ return currentPathname.replace(currentModule+'s', nextModule);
|
|
|
+ }else {
|
|
|
+ return currentPathname.replace(currentModule, nextModule);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }]).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;
|
|
|
+ },
|
|
|
+ /*login : function(user) {
|
|
|
+ var payload = SerializerUtil.param(user);
|
|
|
+ var config = {
|
|
|
+ headers : {
|
|
|
+ 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
|
|
|
+ }
|
|
|
+ };
|
|
|
+ var login = $http.post(rootPath + "/j_spring_security_check", payload, config);
|
|
|
+ login.success(cacheSession);
|
|
|
+ return login;
|
|
|
+ },*/
|
|
|
+ logout : function() {
|
|
|
+ var config = {
|
|
|
+ cache: false,
|
|
|
+ headers: {
|
|
|
+ 'Cache-Control': 'no-cache',
|
|
|
+ 'Pragma': 'no-cache'
|
|
|
+ },
|
|
|
+ params: {
|
|
|
+ returnUrl: window.location.href
|
|
|
+ }
|
|
|
+ };
|
|
|
+ var logout = $http.get(rootPath + "/logout", config);
|
|
|
+ logout.success(uncacheSession);
|
|
|
+ logout.success(function(data){
|
|
|
+ var pathName = document.location.pathname;
|
|
|
+ /*var str = pathName.replace('platform-b2c/','');
|
|
|
+ console.log(str);
|
|
|
+ console.log(document.location);*/
|
|
|
+ var index = pathName.substr(1).indexOf("/");// platform-b2c/logout/proxy
|
|
|
+ var result = pathName.substr(0,index);
|
|
|
+ var uri = pathName.substr(13,index+1000) + document.location.hash;
|
|
|
+ if (result == '/platform-b2c') {
|
|
|
+ var contextPath = ''
|
|
|
+ /*var contextPath = (function getContextPath() {
|
|
|
+ var pathName = document.location.pathname;
|
|
|
+ var index = pathName.substr(1).indexOf("/");
|
|
|
+ var result = pathName.substr(0,index+1);
|
|
|
+ return result;
|
|
|
+ })();*/
|
|
|
+ data.content = contextPath + uri;
|
|
|
+ }
|
|
|
+ if (data.content) {
|
|
|
+ if (data.content == window.location.href) {
|
|
|
+ window.location.reload();
|
|
|
+ } else {
|
|
|
+ window.location.href = data.content;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return logout;
|
|
|
+ },
|
|
|
+ redirectSignin: function() {
|
|
|
+ // 获取跳转登录的url
|
|
|
+ $http.get(rootPath + '/login/page', {
|
|
|
+ params: {
|
|
|
+ returnUrl: window.location.href
|
|
|
+ }
|
|
|
+ }).success(function(data) {
|
|
|
+ if (data.content) {
|
|
|
+ window.location.href = data.content;
|
|
|
+ } else {
|
|
|
+ alert('系统错误');
|
|
|
+ }
|
|
|
+ }).error(function() {
|
|
|
+ alert('系统错误');
|
|
|
+ });
|
|
|
+ },
|
|
|
+ redirectRegister: function() {
|
|
|
+ // 获取跳转注册的url
|
|
|
+ $http.get(rootPath + '/register/page', {
|
|
|
+ params: {
|
|
|
+ returnUrl: window.location.href
|
|
|
+ }
|
|
|
+ }).success(function(data) {
|
|
|
+ if (data.content) {
|
|
|
+ window.location.href = data.content;
|
|
|
+ } else {
|
|
|
+ alert('系统错误');
|
|
|
+ }
|
|
|
+ }).error(function() {
|
|
|
+ alert('系统错误');
|
|
|
+ });
|
|
|
+ },
|
|
|
+ isAuthed : function() {
|
|
|
+ return SessionService.get('authenticated');
|
|
|
+ },
|
|
|
+ getAuthentication : function() {
|
|
|
+ var config = {
|
|
|
+ cache: false,
|
|
|
+ headers: {
|
|
|
+ 'Cache-Control': 'no-cache',
|
|
|
+ 'Pragma': 'no-cache'
|
|
|
+ }
|
|
|
+ };
|
|
|
+ var request = $http.get(rootPath + '/user/authentication', config);
|
|
|
+ request.success(function(data){
|
|
|
+ if(data)
|
|
|
+ cacheSession();
|
|
|
+ else
|
|
|
+ uncacheSession();
|
|
|
+ });
|
|
|
+ request.error(uncacheSession);
|
|
|
+ return request;
|
|
|
+ },
|
|
|
+ reSignin : function(enUU) {
|
|
|
+ return $http.get(rootPath + '/user/authentication/' + enUU);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }]).factory('TableService', ['$parse', 'ngTableParams', function($parse, ngTableParams){
|
|
|
+ var scope;
|
|
|
+ return {
|
|
|
+ init: function(initConfig, dataConfig) {
|
|
|
+ var service = this, data = service.getData();
|
|
|
+ scope = initConfig.scope;
|
|
|
+ // no paging tableParams
|
|
|
+ scope.tableParams = new ngTableParams({
|
|
|
+ page : 1,
|
|
|
+ total : 1,
|
|
|
+ count : 1000
|
|
|
+ }, {
|
|
|
+ counts : [],
|
|
|
+ getData: function($defer, params) {
|
|
|
+ $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // watch $data, set to scope model
|
|
|
+ if(dataConfig) {
|
|
|
+ service.setGenerator(dataConfig.rownumGenerator);
|
|
|
+ var setModelValue = $parse(dataConfig.model).assign;
|
|
|
+ scope.$watch(data, function(){
|
|
|
+ setModelValue && setModelValue(scope, data);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ //add item
|
|
|
+ scope.addItem = function(){
|
|
|
+ service.addItem(data);
|
|
|
+ };
|
|
|
+ scope.removeItem = function(item){
|
|
|
+ service.removeItem(data, item);
|
|
|
+ };
|
|
|
+ if(initConfig) {
|
|
|
+ // add default items
|
|
|
+ service.addItem(data, initConfig.defaultItemsSize);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ setGenerator: function(gen) {
|
|
|
+ this.generator = gen;
|
|
|
+ this.rownum = 0;
|
|
|
+ },
|
|
|
+ getGenerator: function() {
|
|
|
+ return this.generator;
|
|
|
+ },
|
|
|
+ getRownum: function() {
|
|
|
+ if(this.generator) {
|
|
|
+ this.rownum++;
|
|
|
+ }
|
|
|
+ return this.rownum;
|
|
|
+ },
|
|
|
+ getData: function(){
|
|
|
+ return this.data || [];
|
|
|
+ },
|
|
|
+ refresh: function(){
|
|
|
+ if(scope.tableParams.data.length > 0)
|
|
|
+ scope.tableParams.reload();
|
|
|
+ },
|
|
|
+ addItem: function(data, size) {
|
|
|
+ size = size || 1;
|
|
|
+ var s = this, gen = s.getGenerator(), i = 0;
|
|
|
+ for (;i < size; i++ ) {
|
|
|
+ var d = {};
|
|
|
+ d[gen] = s.getRownum();
|
|
|
+ (size == 1) && ( d.$edit = true);
|
|
|
+ data.push(d);
|
|
|
+ }
|
|
|
+ this.refresh();
|
|
|
+ },
|
|
|
+ removeItem: function(data, item){
|
|
|
+ var key = item.$$hashKey;
|
|
|
+ for (var i = 0; i < data.length; i++) {
|
|
|
+ if (key === data[i].$$hashKey)
|
|
|
+ data.splice(i, 1);
|
|
|
+ }
|
|
|
+ this.refresh();
|
|
|
+ },
|
|
|
+ removeAll: function(data){
|
|
|
+ data = [];
|
|
|
+ this.refresh();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }]).filter('summary', function(){
|
|
|
+ var isNumber = function (n) {
|
|
|
+ return !isNaN(parseFloat(n)) && isFinite(n);
|
|
|
+ };
|
|
|
+ var getSum = function(data, attr) {
|
|
|
+ var sum = 0;
|
|
|
+ angular.forEach(data, function(d){
|
|
|
+ if(isNumber(d[attr]))
|
|
|
+ sum += Number(d[attr]);
|
|
|
+ });
|
|
|
+ return sum;
|
|
|
+ };
|
|
|
+ return function(data, summaryField) {
|
|
|
+ if(data && summaryField)
|
|
|
+ return getSum(data, summaryField);
|
|
|
+ return 0;
|
|
|
+ };
|
|
|
+ }).factory('Loading', function(){
|
|
|
+ var loading = angular.element('#loading');
|
|
|
+ return {
|
|
|
+ show: function(){
|
|
|
+ loading.addClass('in');
|
|
|
+ },
|
|
|
+ hide: function() {
|
|
|
+ loading.removeClass('in');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).factory('ImgUrl', function(){
|
|
|
+ //获得不同尺寸的图片url
|
|
|
+ return {
|
|
|
+ handelByWidthHeigth: function( url, width_px, heigth_px ){
|
|
|
+ return url.substring(0, url.lastIndexOf('.')) + "_" + width_px + "x" + heigth_px + url.substr(url.lastIndexOf('.'));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).factory('dynamicInput', function(){
|
|
|
+ /**
|
|
|
+ * 将dynamicInput动态生成的东西处理为接口对应的数据格式
|
|
|
+ * @return Arr[Property, Property, Property, ...]
|
|
|
+ */
|
|
|
+ return {
|
|
|
+ resolveDynamicInput: function(properties){
|
|
|
+ var arrProperties = [];
|
|
|
+ function getClass(x){
|
|
|
+ var str = Object.prototype.toString.call(x);
|
|
|
+ return /^\[object (.*)\]$/.exec(str)[1];
|
|
|
+ }
|
|
|
+ var length = properties.length;
|
|
|
+ for( var i = 0; i < length; i++ ){
|
|
|
+ var objProperty = {};
|
|
|
+ objProperty.propertyId = properties[i].id;
|
|
|
+ objProperty.num = properties[i].num;
|
|
|
+ //根据值类型不一样选择不同处理方式
|
|
|
+ if( getClass(properties[i].value) === "Object" ){
|
|
|
+ //这里针对区间值要单独拼接
|
|
|
+ var arr = [];
|
|
|
+ if(properties[i].type == "text-text"){
|
|
|
+ arr = ["", ""];
|
|
|
+ for( var key in properties[i].value ){
|
|
|
+ if(key == "0"){
|
|
|
+ arr[0] = properties[i].value[0];
|
|
|
+ }
|
|
|
+ else if(key == "1"){
|
|
|
+ arr[1] = properties[i].value[1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ objProperty.stringValue = arr.join("-");
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ for(var key in properties[i].value){
|
|
|
+ //过滤掉值为false的
|
|
|
+ if(properties[i].value[key] != false){
|
|
|
+ arr.push(properties[i].value[key]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ objProperty.stringValue = arr.join("\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ objProperty.stringValue = properties[i].value;
|
|
|
+ }
|
|
|
+ arrProperties.push(objProperty);
|
|
|
+ }
|
|
|
+ return arrProperties;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).factory('ArrayUtil', function(){
|
|
|
+ //拼接数组
|
|
|
+ return {
|
|
|
+ buildArr: function(num){
|
|
|
+ var arr = [];
|
|
|
+ for(var i = 1; i <= num; i++){
|
|
|
+ arr.push(i);
|
|
|
+ }
|
|
|
+ return arr;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).filter('strongStr', function() {
|
|
|
+ return function(input, keyword) {
|
|
|
+ if(angular.isUndefined(keyword) || keyword == "") {
|
|
|
+ return "<span>" + input + "</span>";
|
|
|
+ }
|
|
|
+ var index = angular.lowercase(input).indexOf(angular.lowercase(keyword));
|
|
|
+ if(index < 0) {
|
|
|
+ return "<span>" + input + "</span>";
|
|
|
+ }
|
|
|
+ var bold = input.slice(index + keyword.length, input.length);
|
|
|
+ var prefix = input.slice(0, keyword.length);
|
|
|
+ return "<span>" + prefix + "<strong>" + bold + "</strong></span>";
|
|
|
+ }
|
|
|
+ }).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('NumberService', [function() {
|
|
|
+ return {
|
|
|
+ /*
|
|
|
+ js的除法运算
|
|
|
+ */
|
|
|
+ div : function (arg1, arg2) {
|
|
|
+
|
|
|
+ var t1 = 0, t2 = 0, t3 = 0, r1, r2;
|
|
|
+
|
|
|
+ try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
|
|
|
+
|
|
|
+ try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
|
|
|
+
|
|
|
+ r1 = Number(arg1.toString().replace(".", ""))
|
|
|
+
|
|
|
+ r2 = Number(arg2.toString().replace(".", ""))
|
|
|
+
|
|
|
+ if (r2 == 0)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+ var result = String(r1 / r2);
|
|
|
+
|
|
|
+ try { t3 = result.toString().split(".")[1].length } catch (e) { }
|
|
|
+
|
|
|
+ var index = t2 - t1 - t3;
|
|
|
+
|
|
|
+ if (index < 0) {
|
|
|
+ result = result.replace(".", "");
|
|
|
+
|
|
|
+ while (result.length <= Math.abs(index)) {
|
|
|
+ result = '0' + result;
|
|
|
+ }
|
|
|
+
|
|
|
+ var start = result.substring(0, result.length + index);
|
|
|
+ var end = result.substring(result.length + index, result.length);
|
|
|
+
|
|
|
+ result = start + '.' + end;
|
|
|
+
|
|
|
+ return Number(result);
|
|
|
+ }
|
|
|
+ else if (index > 0) {
|
|
|
+ result = result.replace(".", "");
|
|
|
+
|
|
|
+ while (result.length <= Math.abs(index)) {
|
|
|
+ result += '0';
|
|
|
+ }
|
|
|
+ return Number(result);
|
|
|
+ }
|
|
|
+ else return Number(result.replace(".", ""));
|
|
|
+
|
|
|
+ },
|
|
|
+ mul: 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)
|
|
|
+ },
|
|
|
+ add : function(arg1, arg2) {
|
|
|
+
|
|
|
+ var r1, r2, m, c;
|
|
|
+
|
|
|
+ try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
|
|
|
+
|
|
|
+ try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
|
|
|
+
|
|
|
+ c = Math.abs(r1 - r2);
|
|
|
+ m = Math.pow(10, Math.max(r1, r2))
|
|
|
+ if (c > 0) {
|
|
|
+ var cm = Math.pow(10, c);
|
|
|
+ if (r1 > r2) {
|
|
|
+ arg1 = Number(arg1.toString().replace(".", ""));
|
|
|
+ arg2 = Number(arg2.toString().replace(".", "")) * cm;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ arg1 = Number(arg1.toString().replace(".", "")) * cm;
|
|
|
+ arg2 = Number(arg2.toString().replace(".", ""));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ arg1 = Number(arg1.toString().replace(".", ""));
|
|
|
+ arg2 = Number(arg2.toString().replace(".", ""));
|
|
|
+ }
|
|
|
+ return (arg1 + arg2) / m
|
|
|
+ },
|
|
|
+ sub : 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));
|
|
|
+ //last modify by deeka
|
|
|
+ //动态控制精度长度
|
|
|
+ n=(r1>=r2)?r1:r2;
|
|
|
+ return ((arg1*m-arg2*m)/m).toFixed(n);
|
|
|
+ },
|
|
|
+ //进一发 例如 0.00002 保留两位,使用进一发 0.01
|
|
|
+ //num 是传入的数字,scacle保留几位,
|
|
|
+ toCeil : function (num, scacle) {
|
|
|
+ //Math.ceil(num * Math.pow(10, scacle)) / Math.pow(10, scacle)
|
|
|
+ console.log(new Big(num));
|
|
|
+ return new Big(num).rnd(scacle, 3);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }]);
|
|
|
+});
|