ToolService.js 19.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
(function () {
    'use strict';

    angular.module("app")
    .factory("ToolService",["$rootScope","$timeout","UtilService",toolservice]);

    function toolservice($rootScope,$timeout,UtilService){
    	var service = {},that = $rootScope,
        rootmenu = {"dataview":"数据概览","action":"行为分析","user":"用户分析","event":"埋点管理","tool":"工具"},
        menu = {"custommenu": "行业看单","eventstats": "事件分析","funnel": "漏斗转化","retention": "留存分析",
                "intelligentpath": "智能路径","usergroup": "用户分群","event": "APP事件管理","profile": "用户属性管理",
                "logtool": "日志流-IO","report": "下载报表-IO","app": "产品中心-IO","auth": "成员管理-IO"};
    	service.setName = function(name){
    		that.name = name;
    	}
    	service.getName = function(){
    		return that.name;
    	}
        service.isDemoUser = function(){
            return (UtilService.getUser().email == 'demo@reyun.com');
        }
        service.getChannelCampaignList = function(){
            var list = [
                {id:"channel",name:"渠道"},
                {id:"campgroup",name:"活动组"},
                {id:"campaign",name:"活动"}
            ];
            return UtilService.cloneJSON(list);
        }
        service.getPackTypeList = function(){
            var list = [
                {id:"all",name:"全部"},
                {id:"APPMARKET",name:"分包"},
                {id:"ADVERTISING",name:"分链接"}
            ];
            return UtilService.cloneJSON(list);
        }
        service.getDeviceTypeList = function(){
            var list = [
                {id:"device",name:"按设备看"},
                {id:"user",name:"按账号看"}
            ];
            return UtilService.cloneJSON(list);
        }
        /**
         * [getChannelCampaignUrl description]
         * @param  type
         * @param  appid
         * @param cid 渠道ID
         * @return
         */
        service.getChannelCampaignParams = function(type,appid,cid){
            var url = "",txt = "";
            if(type == 'channel'){ //渠道
                url = appid + "/channel/findall";
                txt = "渠道";
            }
            else if(type == 'campgroup'){//活动组
                url = appid + "/campgroup/findall";
                txt = "活动组";
            }
            else if(type == 'campbychannel'){//应用渠道下的活动
                url = appid + "/campaign/findbychannel?channelid="+cid;
                txt = "活动";
            }
            else{
                url = appid + "/campaign/findall";
                txt = "活动";
            }
            return {
                url :url,
                txt:txt
            };
        }
        /**
         * [getParams description]
         * @param  {[type]} obj  一般为$scope
         * @param  {[type]} flag  1:有渠道、活动组、活动筛选
         * @return {[type]}      [description]
         */
        service.getParams = function(obj,flag){
            var params = {appid:obj.appid};
            if(obj.date){
                params.startdate = obj.date.startDate;
                params.enddate = obj.date.endDate;
            }
            if(flag && flag == 1 && obj.typeInfoIds != "" && obj.typeInfoIds != -1){{
                switch(obj.typeId){
                    case "campgroup":
                        params.cgroupid = obj.typeInfoIds;
                        break;
                    case "campaign":
                        params.campaignid = obj.typeInfoIds;
                        break;
                    case "channel":
                    default:
                        params.cid = obj.typeInfoIds;
                        break;
                }
            }}
            return params;
        }
        service.getOneColors = function(){
            return ["#1fb9e9"];
        }
        service.getTwoColors = function(){
            return ["#1fb9e9","#f8a20f"];
        }
        service.getThreeColors = function(){
            return ["#1fb9e9","#f8a20f","#8e6eb6"];
        }
        service.getSixColors = function(){
            return ["#1fb9e9","#f8a20f","#8e6eb6","#9cc272","#f36e55","#00b37f"];
        }
        service.getTenColors = function(){
            return ["#1fb9e9","#f8a20f","#00b37f","#f36e55","#5c9eff","#fccc01","#9cc272","#ec407a","#8e6eb6","#cddc39"];
        }
        service.getToday = function(){
            return {
                startDate:UtilService.getDay(0),
                endDate:UtilService.getDay(0)
            }
        }
        service.getYesDay = function(){
            return {
                startDate:UtilService.getDay(-1),
                endDate:UtilService.getDay(-1)
            }
        }
        service.getLastDays = function(d,flag){
            if(angular.isUndefined(flag)){ //默认最大显示昨天
                flag = -1;
            }
            else{
                if(d>0){
                    d -= 1;
                }
                else{
                    d += 1;
                }
            }

            return {
                startDate:UtilService.getDay(d),
                endDate:UtilService.getDay(flag)
            }
        }
        service.getLastWeeks = function(d){
            var w = UtilService.getNowWeekNum(),
                date = new Date(),
                year = date.getFullYear();

            var range = {end:year+UtilService.getStr(w)},pd = d + w +1;
            if(pd > 0 ){
                range.start = year + UtilService.getStr(pd);
            }else{
                var pr = UtilService.getYearWeekCount(year-1);
                range.start = (year-1) + UtilService.getStr(pr + pd);
            }
            return range;
        }
        service.getLastMonths = function(d){
            var date = new Date(),year = date.getFullYear(),month = date.getMonth()+1;

            var range = {end:year+UtilService.getStr(month)},pm = month+d+1;
            if(pm > 0 ){
                range.start = year + UtilService.getStr(pm);
            }else{
                range.start = (year-1) + UtilService.getStr(12 + pm);
            }
            return range;
        }

        service.getStringTypes = function(){
            return [
                {"id":"=","name":"="},
                {"id":"!=","name":"≠"},
                {"id":"in","name":"in","more":true},
                {"id":"not in","name":"not in","more":true}
            ]
        }
        service.getNumberTypes = function(){
            return [
                {"id":">","name":">"},
                {"id":">=","name":">="},
                {"id":"=","name":"="},
                {"id":"<=","name":"<="},
                {"id":"<","name":"<"},
                {"id":"between","name":"between","both":true}
            ]
        }

        //loading加载
        service.showLoading = function(){
            document.getElementById("sysLoad").style.display = "block";
            setTimeout(function(){
              service.hideLoading();
            },60*100);
        }
        service.hideLoading = function(){
            document.getElementById("sysLoad").style.display = "none";
        }
        //显示灰色背景
        service.showAlp = function(){
            document.getElementById("alpLoad").style.display = "block";
        }
        service.hideAlp = function(){
            document.getElementById("alpLoad").style.display = "none";
        }

        //删除等操作提示
        service.showTips = function(msg,time){
            service.hideLoading();
            var t = 1200;
            if(msg.length>10){
                t  = 2000;
            }
            time = time || t;
            $rootScope.operShow = true;
            $rootScope.operMsg = msg;
            $timeout(function(){
                service.hideTips();
            },time);
        }
        service.hideTips = function(){
            $rootScope.operShow = false;
            $rootScope.operMsg = "";
        }

        service.getMenuTip = function(name){
            var tip = $rootScope.currentPageTips[name];
            if(!tip){
                tip = "无";
            }
            return tip;
        }

        //计算漏斗数据格式
        service.getFunnelData = function(fdata,chartKey,chartKeyName){
            var rmd = new Date().getTime();
            if(angular.isUndefined(fdata)){
                fdata = {};
            }
            var users = [],rates = [],marks = [],preu = -1;
            chartKeyName.filter(function(key){
                var u = fdata[key],r = fdata['rate_'+key];
                u = angular.isUndefined(u) ? 0 : u;
                r = angular.isUndefined(r) ? 0 : r;
                users.push(u);
                rates.push(r);

                if(preu>-1){
                    if(preu==0){
                        marks.push(0);
                    }else{
                        marks.push(UtilService.decimal2(u/preu*100));
                    }
                }
                preu = u;
            });

            var data = {
                val : [rates,users],
                trans4last : marks,
                key : chartKey,
                rmd : ++rmd
            };
            var rateData = data.val[0],rateData1 = [],preRate = 0;
            for(var i=0;i<rateData.length;i++){
                var r = preRate - rateData[i];
                if(preRate>0){
                    // r = r / preRate  * preRate;
                }else{
                    r = 0;
                }
                rateData1.push(r); //转化率
                preRate = rateData[i];
            }
            data.val.splice(1,0,rateData1);

            return data;
        }

        service.noDataChart = function(){
            var rmd = new Date().getTime();
            return {
                val : [],
                trans4last : [],
                key : [],
                rmd : ++rmd,
                name :[]
            };
        }
        /**
         *
         * @param  {[type]} type   funnel:漏斗
         *                         customretention:自定义留存
         *                         detailcustomretention:自定义留存明细,
         *                         commonretention:固定留存
         *                         eventstats:事件
         *                         normal:普通
         */
        service.getChartConditions = function(name,reportname,params,type,id){
            if(UtilService.isNullStr(type)){type = "normal";}

            var appid = params.appid,newparams = UtilService.cloneJSON(params);
            var subType = params.viewtype || "";
            delete newparams.appid;
            var condition = {
                reportName:reportname,
                conditions:JSON.stringify(newparams),
                functionType:type,
                subType:subType,
                app:appid,
                name:name
            };
            if(!UtilService.isNullStr(id)){
                condition.functionId = id;
            }
            return condition;
        }
        //计算字节长度
        service.getByteLen = function(col){
            var templen=0;
            for(var i=0; i < col.length; i++){
                var a = col.charAt(i);
                if (a.match(/[a-zA-Z\u4e00-\u9fa5]/) != null) {
                    templen += 2;
                }
                else {
                    templen += 1;
                }
            }
            return templen;
        }
        //计算table列宽
        service.getTableColsWidth = function(cols,flag,count){
            var colsStyle = [];
            var leftw = $rootScope.unfoldFlag?90:230;
            var tablew = flag?(($(window).width()-10-leftw)/(count?count:2)-(count==3?48:45)):($(window).width()-leftw), colsw = 0;
            var specialCols = "推广活动,推广活动组,渠道,子渠道,子账号,关键字,创意,付费激活周期";
            angular.forEach(cols,function(col){
                var templen = service.getByteLen(col);
                var colw = specialCols.indexOf(col)>-1?240:col=="日期"?(flag!='day'?160:90):col=='分组'?150:(8 * templen+(flag?13:27));
                var colStyle = {'width': colw +'px','max-width': colw +'px'};
                colsw += colw;
                colsStyle.push(colStyle);
            });
            if(colsw < tablew){
                // var leftw = $rootScope.unfoldFlag?90:$rootScope.unfoldFlag==false?230:270;
                // tablew = $(window).width()-leftw;
                // if(flag){
                //     tablew = (tablew-10)/2-45;
                // }
                var diffw = tablew - 2 - colsw,
                    collen = colsStyle.length,
                    coldiffw = (diffw - 12*collen)/collen;
                angular.forEach(colsStyle,function(col){
                    var ocolw = parseInt(col.width.slice(0,-2));
                    col.width = col['max-width'] = ocolw + coldiffw +'px';
                });
            }
            return colsStyle;
        }

        service.getCMidInfo = function(){
            var cid = localStorage.getItem("rytf_cid"),
                campaignid = localStorage.getItem("rytf_campid");

            if(UtilService.isNullStr(cid)){
                cid = -1;
                campaignid = '_default_';
            }
            return {
                cid : cid,
                campaignid : campaignid
            };
        }
        service.getRoleName = function(){
            if($rootScope.isSuper){
                return "主账号";
            }
            else if($rootScope.isManage){
                return "管理员";
            }
            else if($rootScope.isAppManage){
                return "子应用管理员";
            }
        }
        service.setUser = function(user){
           window.localStorage.setItem("io_ry_user",JSON.stringify(user));
        }
        service.getUser = function(){
            var u = localStorage.getItem("io_ry_user");

            if(UtilService.isNullStr(u)){
                return {};
            }
            else{
                return JSON.parse(u);
            }
        }

        //菜单报送数据
        service.getMenuSubmitData = function(submenu){
            if($rootScope.pagePath=="userinfo")return;

            var cmids = service.getCMidInfo();
            var company = service.getUser().company;
            if(UtilService.isNullStr(company)){
                company = "";
            }

            var appkey = "",appcategory = "",appname = "",rootmenukey;
            if($rootScope.appInstance){
                appkey = $rootScope.appInstance.appkey;
                appcategory = $rootScope.appInstance.appGenreName;
                appname = $rootScope.appInstance.name;
            }
            if(!submenu || submenu==''){submenu = menu[$rootScope.pagePath];}
            for (var m in that.meauTree){
               if(that.meauTree[m].indexOf($rootScope.pagePath)>-1){
                 rootmenukey = m;
               }
            }
            var Uuser = UtilService.getUser();
            var params = {
                appid: UtilService.getTKAppkey(),
                who:Uuser.email,
                what:"pageview",
                context:{
                    rootmenu:rootmenu[rootmenukey], //菜单分类
                    menu:menu[$rootScope.pagePath], //当前菜单
                    submenu:submenu, //子菜单-看单
                    sessionid:UtilService.getCookie("JSESSIONID"),

                    user_appkey:appkey, //所选APP的id,
                    appcategory:appcategory,//应用类型  中文名
                    appname:appname, //应用名称
                    rolename:service.getRoleName(),
                    company:company,

                    "_cid":cmids.cid,
                    "_campaignid":cmids.campaignid,
                    "_apptype":"wap",

                    email:Uuser.email,
                    _deviceid:UtilService.randDeviceId(), //设备ID
                    operatesystem: UtilService.operatorSystem(),//操作系统  _ryos
                    browser: UtilService.getBrowser() //浏览器类型
                }
            };
            $.post(UtilService.getLogRoot()+"/receive/analyze/event",JSON.stringify(params));
        }
        //功能报送数据
        service.getOperSubmitData = function(opername){
            var appkey = "",appcategory = "",appname = "";
            if($rootScope.appInstance){
                appkey = $rootScope.appInstance.appkey;
                appcategory = $rootScope.appInstance.appGenreName;
                appname = $rootScope.appInstance.name;
            }

            var cmids = service.getCMidInfo();
            var company = service.getUser().company;
            if(UtilService.isNullStr(company)){
                company = "";
            }

            var Uuser = UtilService.getUser();
            var params = {
                appid:UtilService.getTKAppkey(),
                who:Uuser.email,
                what:"operation",
                context:{
                    menu:menu[$rootScope.pagePath], //菜单分类
                    opername:opername, //操作名字

                    email:Uuser.email,
                    user_appkey:appkey, //所选APP的id,
                    appcategory:appcategory,//应用类型  中文名
                    appname:appname, //应用名称
                    rolename:service.getRoleName(),
                    company:company,

                    "_cid":cmids.cid,
                    "_campaignid":cmids.campaignid,
                    "_apptype":"wap",

                    _deviceid:UtilService.randDeviceId(), //设备ID
                    operatesystem: UtilService.operatorSystem(),//操作系统  _ryos
                    browser: UtilService.getBrowser() //浏览器类型
                }
            };
            $.post(UtilService.getLogRoot()+"/receive/analyze/event",JSON.stringify(params));
        }

      //自定义报送
      service.sendWhatEvtData = function(what,jsonparams){
          var appkey = "",appcategory = "",appname = "";
          if($rootScope.appInstance){
              appkey = $rootScope.appInstance.appkey;
              appcategory = $rootScope.appInstance.appGenreName;
              appname = $rootScope.appInstance.name;
          }

          var cmids = service.getCMidInfo();
          var company = service.getUser().company;
          if(UtilService.isNullStr(company)){
              company = "";
          }

          var Uuser = UtilService.getUser();
          var params = {
              appid:UtilService.getTKAppkey(),
              who:Uuser.email,
              what:what,
              context:{
                  email:Uuser.email,
                  user_appkey:appkey, //所选APP的id,
                  appcategory:appcategory,//应用类型  中文名
                  appname:appname, //应用名称
                  rolename:service.getRoleName(),
                  company:company,

                  "_cid":cmids.cid,
                  "_campaignid":cmids.campaignid,
                  "_apptype":"wap",

                  _deviceid:UtilService.randDeviceId(), //设备ID
                  operatesystem: UtilService.operatorSystem(),//操作系统  _ryos
                  browser: UtilService.getBrowser() //浏览器类型
              }
          };
          if(jsonparams){
            for(var k in jsonparams){
              params.context[k] = jsonparams[k];
            }
          }

          $.post(UtilService.getLogRoot()+"/receive/analyze/event",JSON.stringify(params));
      }
    	return service;
    }
})();