zoukankan      html  css  js  c++  java
  • js 公用插件,教科书级的写法

    项目有个地方要调用接口,几个地方都用到这个接口的数据。于是便要封装一个公共的js文件供使用者调用。

    // 车系全景图
    var vrImgForBitauto = {
        CheZhanClassID:55555,
        CsID:0,
        Interface:'http://webapi.photo.com/Cooperate/api/v1/pano/GetPanoList?',
        GetDataCallBack:'vrImgForBitauto.GetVRData',
        CallBack:'',
        IntiDataForEntry : function(csid,callback){
            this.CallBack = callback;
            if(csid>0)
            {this.CsID=csid;}
            var date = new Date();
            var fixMin = Math.floor(date.getMinutes()/5)
            var version = date.getFullYear().toString() + (date.getMonth() + 1).toString() + date.getDate().toString() + date.getHours().toString()+'v'+fixMin;
            document.write(unescape('%3Cscript type="text/javascript" src="'+this.Interface+'classId='+this.CheZhanClassID+'&serialBrandId='+this.CsID+'&callback='+this.GetDataCallBack+'&v='+version+'"%3E%3C/script%3E'));
        },
        GetVRData : function(data){
            if(data!=undefined&&data.Code!=undefined&&data.Code==0&&data.Data!=undefined)
            {
                if(this.CallBack!='')
                {
                    try{
                        this.CallBack(data.Data);
                    }catch(err){}
                }
            }
        }
    }
    
    // 使用者示例:
    // 取某个车系的车展是否有全景返回数组 单一入口用索引0 调用Demo 参数:车系Id和回调函数 
    if(vrImgForBitauto!=undefined&&vrImgForBitauto.IntiDataForEntry!=undefined)
    {
        vrImgForBitauto.IntiDataForEntry(3999,function (vrImgs){    
            if(vrImgs.length>0)
            {
                document.write('<br/>车系ID: '+vrImgs[0].ModelId);
                document.write('<br/>包ID: '+vrImgs[0].SceneBagId);
                document.write('<br/>包名字: '+vrImgs[0].SceneBagName);
                document.write('<br/>图片ID: '+vrImgs[0].ImageId);
                document.write('<br/>封面地址: '+vrImgs[0].CoverImageUrl);
                document.write('<br/>全景封面: '+vrImgs[0].PanoThumbUrl);
                document.write('<br/>全景地址: '+vrImgs[0].PanoUrl);
            }
        });
    }

    该写法方便客户端调用,不受接口变动的限制。只需要关心业务逻辑就可以了

  • 相关阅读:
    任正非用人:砍掉高层手脚、中层屁股、基层脑袋、全身赘肉!
    SourceTree的基本使用
    Spring学习(三)
    Spring学习(二)
    Spring学习(一)
    SpringBoot学习(五)
    SpringBoot学习(四)
    SpringBoot学习(二)
    SpringBoot学习(三)
    SpringBoot学习(一)
  • 原文地址:https://www.cnblogs.com/bwlang/p/6726184.html
Copyright © 2011-2022 走看看