app开屏广告

Posted 邹柯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了app开屏广告相关的知识,希望对你有一定的参考价值。

最近公司有个需求需要做app开屏广告(跳转到不同的页面)--下面是app开屏广告的处理

1.管理后台效果图

(1)广告链接--商品详情

 

(2)广告链接--关联模块

(3)广告链接--消息富文本

(4)广告链接--H5页面

(5)广告链接--蜂雷头条

2.数据表的设计

 CREATE TABLE `lc_open_ad` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `second` int(11) DEFAULT NULL COMMENT \'开屏广告停留时长(单位:秒)\',
  `ad_effective_start` datetime DEFAULT NULL COMMENT \'开屏广告有效时间--开始时间\',
  `ad_effective_end` datetime DEFAULT NULL COMMENT \'开屏广告有效时间--结束时间\',
  `ad_url` varchar(100) DEFAULT NULL COMMENT \' 开屏广告图片地址\',
  `ad_link_type` int(11) DEFAULT NULL COMMENT \'广告链接类型(1商品详情页、2关联模块、3消息富文本、4H5页面、5蜂雷头条详情页)\',
  `ad_link_content` text COMMENT \'广告链接内容 (json格式)\',
  `create_id` bigint(20) DEFAULT NULL COMMENT \' 创建人\',
  `create_time` datetime DEFAULT NULL COMMENT \'创建时间\',
  `update_id` bigint(20) DEFAULT NULL COMMENT \'修改人\',
  `update_time` datetime DEFAULT NULL COMMENT \'修改时间\',
  `status` tinyint(1) NOT NULL DEFAULT \'1\' COMMENT \'状态: 1 正常 2 禁用\',
  `is_deleted` tinyint(1) NOT NULL DEFAULT \'0\' COMMENT \'是否已删除 0正常  1 已删除\',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=122 DEFAULT CHARSET=utf8 COMMENT=\'开屏广告\\n{"ad_type":"一级分类(默认1)","ad_link_type":"1(商品详情页)","itemid":"商品sku_id","sno":"商品sku_no"}\\n{"ad_type":"一级分类(默认1)","ad_link_type":"2(关联模块)","plate_type":"1限时蜂抢、2新品、3蜂神榜、4蜂觅、5首页"}\\n{"ad_type":"一级分类(默认1)","ad_link_type":"3(消息富文本)","rich_text_id":"消息富文本id"}\\n{"ad_type":"一级分类(默认1)","ad_link_type":"4(H5页面)","url":"H5页面地址"}\\n{"ad_type":"一级分类(默认1)","ad_link_type":"5(蜂雷头条详情页)","id":"蜂雷头条id"}\';

3.数据表值的存储

4.管理后台添加(修改)的数据接收格式

{"id":"","second":"10","ad_url":"/Public/Uploads/ad/origin/5a1a68890eda5.jpg","ad_effective_start":"2016-10-12 00:00:00","ad_effective_end":"2016-10-13 00:00:00","ad_type":"1","ad_link_type":"1","ad_link_content":{"sku_id":"111111","sku_no":"P11111-01"}}
{"id":"","second":"10","ad_url":"/Public/Uploads/ad/origin/5a1a68890eda5.jpg","ad_effective_start":"2016-10-10 00:00:00","ad_effective_end":"2016-10-15 00:00:00","ad_type":"1","ad_link_type":"2","ad_link_content":{"plate_type":"1"}}
{"id":"","second":"10","ad_url":"/Public/Uploads/ad/origin/5a1a68890eda5.jpg","ad_effective_start":"2016-10-23 00:00:00","ad_effective_end":"2016-10-24 00:00:00","ad_type":"1","ad_link_type":"3","ad_link_content":{"rich_text_id":"富文本消息id--为空添加,不为空修改","content":"ssssssssssssss"}}
{"id":"","second":"10","ad_url":"/Public/Uploads/ad/origin/5a1a68890eda5.jpg","ad_effective_start":"2016-10-10 00:00:00","ad_effective_end":"2016-10-15 00:00:00","ad_type":"1","ad_link_type":"4","ad_link_content":{"url":"goods/detail?sno=P001219-01&itemid=1002975101}}
{"id":"","second":"10","ad_url":"/Public/Uploads/ad/origin/5a1a68890eda5.jpg","ad_effective_start":"2016-10-10 00:00:00","ad_effective_end":"2016-10-15 00:00:00","ad_type":"1","ad_link_type":"5","ad_link_content":{"id":"66"}}    

 6.php代码实现

(1)控制器

    /**
     * @title App开屏广告--添加(修改)
     * @param data 是 json 接收参数,格式:{"id":"为空添加,不为空修改","second":"开屏广告停留时间(单位:秒)","ad_url":"开屏广告图片地址(相对路径)","ad_effective_start":"开屏广告有效时间--开始时间","ad_effective_end":"开屏广告有效时间--结束时间","ad_type":"广告类型(不传默认1)","ad_link_type":"广告链接类型(1商品详情页、2关联模块、3消息富文本、4H5页面、5蜂雷头条详情页)","ad_link_content":"广告链接内容"}
     * @example app/kaipin_ad_save?
     * @method POST 
     * @author 邹柯
     */
    public function kaipin_ad_saveAction(){        
        load(\'Common.check\');
        $public=D(\'Public\');     
        $data=trim(I(\'post.data\'));
        $res=$public->dealJson($data);
        $id=trim($res[\'id\']);
        //广告类型
        $ad_type=trim($res[\'ad_type\']);
        if(empty($ad_type)){
            $ad_type=1;
        }
        //开屏广告停留时间
        $second=trim($res[\'second\']);
        if(empty($second)){
            $data = array(\'msg\' =>"开屏广告停留时长不能为空!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        if(!is_numeric($second) || $second <= 0){
            $data = array(\'msg\' =>"开屏广告停留时长必须为大于等于1的整形!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }  
        //开屏广告图片地址
        $ad_url=trim($res[\'ad_url\']);
        if(empty($ad_url)){
            $data = array(\'msg\' =>"开屏广告图片必须上传!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        //广告有效时间
        $ad_effective_start=trim($res[\'ad_effective_start\']);
        if(empty($ad_effective_start)){
            $data = array(\'msg\' =>"广告有效时间--开始时间不能为空!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        if(!IsDate($ad_effective_start,\'Y-m-d H:i:s\')){
            $data = array(\'msg\' =>"广告有效时间--结束时间格式错误!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        $ad_effective_end=trim($res[\'ad_effective_end\']);
        if(empty($ad_effective_end) && !empty($ad_effective_end)){
            $data = array(\'msg\' =>"广告有效时间--结束时间不能为空!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        if(!IsDate($ad_effective_end,\'Y-m-d H:i:s\')){
            $data = array(\'msg\' =>"广告有效时间--结束时间格式错误!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        if($ad_effective_start > $ad_effective_end){
            $data = array(\'msg\' =>"广告有效时间开始时间不能大于结束时间!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        //广告链接类型
        $ad_link_type=trim($res[\'ad_link_type\']);
        if(empty($ad_link_type)){
            $data = array(\'msg\' =>"广告链接类型不能为空!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        if(!is_numeric($ad_link_type) || $ad_link_type <= 0){
            $data = array(\'msg\' =>"广告链接类型必须为大于等于1的整形!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }  
        if(!in_array($ad_link_type,array(1,2,3,4,5))){
            $data = array(\'msg\' =>"广告链接类型值非法!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        //广告链接内容
        $ad_link_content=$res[\'ad_link_content\'];
        if(empty($ad_link_content)){
            $data = array(\'msg\' =>"广告链接内容不能为空!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }       
        $app=D(\'App\');
        $list=$app->kaipin_ad_add($id,$second,$ad_url,$ad_effective_start,$ad_effective_end,$ad_type,$ad_link_type,$ad_link_content); 
        if((int)$list==100){
            $data = array(\'msg\' =>"广告有效时间冲突!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        if($list==false){
            $data = array(\'msg\' =>"添加失败!" , \'status\'=>\'1\',\'result\'=>null);
            $this->ajaxReturn($data);
        }
        $data = array(\'msg\' =>"添加成功!" , \'status\'=>\'0\',\'result\'=>$list);
        $this->ajaxReturn($data);
    }
  /**
     * @title 开屏广告图片上传
     * @example app/img_upload?
     * @param ad_url 是 file 开屏广告图片名称(上传图片的尺寸1440_2560)
     * @return_param_explain  returnPath:相对路径(存储用) preview:全路径(展示用)
     * @method POST
     * @author 邹柯
     */
    public function img_uploadAction(){
        if(!empty($_FILES["ad_url"]["name"])){
             $width="1440";
             $height="2560";
             $app = D(\'App\');
             $data = $app->upload_file(\'ad_url\',$width,$height);
        }else{
             $data = array(
                \'msg\' => "参数错误",
                \'status\' => \'1\'
             );
        }
        
        $this->ajaxReturn($data);
    }  

(2)模型

     //App开屏广告--添加(修改)
     public function kaipin_ad_add($id,$second,$ad_url,$ad_effective_start,$ad_effective_end,$ad_type,$ad_link_type,$ad_link_content){  
         $open_ad=M(\'open_ad\');
         $create_time=date(\'Y-m-d H:i:s\',time());
         $create_user=$_SESSION[\'user\'][\'personnel_code\'];
         
         //判断时间是否冲突
         $where2="status=1 and is_deleted=0";
         if(!empty($id)){
             $where2 .=" and id !=\'".$id."\'";
         }
         $rs=$open_ad->field(\'ad_effective_start,ad_effective_end\')->where($where2)->select();
         $r_start=array_unique(array_column($rs,\'ad_effective_start\'));
         $r_end=array_unique(array_column($rs,\'ad_effective_end\'));
         if(in_array($ad_effective_start,$r_start)){
             return 100;
         }
         if(in_array($ad_effective_end,$r_end)){
             return 100;
         }
         foreach($r_end as $k=>$v){
             if($v > $ad_effective_start && $v <= $ad_effective_end){
                  return 100;
             }
             continue;
         }       
         foreach($rs as $k=>$v){
             if($v[\'ad_effective_start\'] <=$ad_effective_start && $ad_effective_end <=$v[\'ad_effective_end\']){
                  return 100;
             }
         }
         switch($ad_link_type){
             case 1: //商品详情页
                 $ad_link_content_info=array(
                     \'ad_type\'=>$ad_type,
                     \'ad_link_type\'=>$ad_link_type,
                     \'itemid\'=>$ad_link_content[\'sku_id\'],
                     \'sno\'=>$ad_link_content[\'sku_no\']
                 );
                 break; 
             case 2://关联模块
                 $ad_link_content_info=array(
                     \'ad_type\'=>$ad_type,
                     \'ad_link_type\'=>$ad_link_type,
                     \'plate_type\'=>$ad_link_content[\'plate_type\'],
                 );
                 break;
             case 3://广告富文本
                 $rich_text=M(\'rich_text\');
                 //添加
                 if(empty($ad_link_content[\'rich_text_id\'])){
                      $data=array(
                            \'content\'=>html_entity_decode($ad_link_content[\'content\']),
                            \'create_id\'=>$create_user,
                            \'create_time\'=>$create_time,
                            \'update_id\'=>$create_user,
                            \'update_time\'=>$create_time,                          
                            \'type\'=>2
                      );
                      $re=$rich_text->data($data)->add();
                      //获取自增id
                      $id2=$rich_text->getLastInsID();
                 }else{
                      $data=array(
                            \'content\'=>html_entity_decode($ad_link_content[\'content\']),
                            \'update_id\'=>$create_user,
                            \'update_time\'=>$create_time,
                      );
                      $re=$rich_text->where(array(\'id\'=>$ad_link_content[\'rich_text_id\']))->data($data)->save();
                      $id2=$ad_link_content[\'rich_text_id\'];
                 }     
                 
                 $ad_link_content_info=array(
                     \'ad_type\'=>$ad_type,
                     \'ad_link_type\'=>$ad_link_type,
                     \'rich_text_id\'=>$id2
                 );
                 break;
             case 4://H5页面
                 $ad_link_content_info=array(
                     \'ad_type\'=>$ad_type,
                     \'ad_link_type\'=>$ad_link_type,
                     \'url\'=>$ad_link_content[\'url\']
                 );
                 break;
             default://蜂雷头条详情页                 
                 $ad_link_content_info=array(
                     \'ad_type\'=>$ad_type,
                     \'ad_link_type\'=>$ad_link_type,
                     \'id\'=>$ad_link_content[\'id\']
                 );
                 break;                 
         }  
         //转换成json       
         $ad_link_content_json = json_encode($ad_link_content_info);
         //添加
         if(empty($id)){
             //添加开屏广告
            $data2=array(
                   \'second\'=>$second,
                   \'ad_url\'=>$ad_url,
                   \'ad_effective_start\'=>$ad_effective_start,
                   \'ad_effective_end\'=>$ad_effective_end,
                   \'ad_link_type\'=>$ad_link_type,
                   \'ad_link_content\'=>$ad_link_content_json,
                   \'create_id\'=>$create_user,
                   \'create_time\'=>$create_time,
                   \'update_id\'=>$create_user,
                   \'update_time\'=>$create_time,
            );
            $res2=$open_ad->data($data2)->add();
         }else{//修改
             //添加开屏广告
            $wh = "id=\'".$id."\'";
            $res_in=$open_ad->field(\'id,second,ad_effective_start,ad_effective_end,ad_url,ad_link_type,ad_link_content\')->where($wh)->find();
            $data2=array(
                   \'second\'=>$second,
                   \'ad_url\'=>$ad_url,
                   \'ad_effective_start\'=>$ad_effective_start,
                   \'ad_effective_end\'=>$ad_effective_end,
                   \'ad_link_type\'=>$ad_link_type,
                   \'ad_link_content\'=>$ad_link_content_json,
                   \'update_id\'=>$create_user,
                   \'update_time\'=>$create_time,
            );
            $res2=$open_ad->data($data2)->where(array

以上是关于app开屏广告的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的应用启动界面实现「开屏广告」?

舒服了!淘宝支付宝等阿里系App取消开屏广告:秒开

为什么国外的 App 很少会有开屏广告?

主流App开屏广告一夜消失!满屏找“跳过”的时代快结束了

iOS接入腾讯优量汇开屏广告教程

iOS启动图和开屏广告图,类似网易