221 lines
6.3 KiB
PHP
221 lines
6.3 KiB
PHP
<?php
|
||
|
||
namespace app\models;
|
||
|
||
use Yii;
|
||
use app\components\SiteHelper;
|
||
use app\components\Utils;
|
||
use app\models\Model;
|
||
|
||
/**
|
||
* This is the model class for table "{{%banner}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $user_id 用户ID
|
||
* @property int $zone_id 位置ID
|
||
* @property int $url_type 链接类型,0=小程序页面,1=外部链接,2=小程序
|
||
* @property string|null $app_id APP_ID
|
||
* @property string|null $app_ext APP_EXT
|
||
* @property string $img_url 图片
|
||
* @property string|null $title 标题
|
||
* @property string|null $page_url 链接
|
||
* @property int $sort 排序
|
||
* @property int $is_delete 是否删除
|
||
* @property int $status 状态,0=隐藏,1=显示
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $deleted_at 更新时间
|
||
* @property int $media 媒体类型:0=图片,1=视频
|
||
* @property string $video_url 视频
|
||
* @property string $text 视频
|
||
* @property string $other_img_url 视频
|
||
*/
|
||
class Banner extends \yii\db\ActiveRecord
|
||
{
|
||
const URL_TYPE_MP_PAGE = 0;
|
||
const URL_TYPE_LINK = 1;
|
||
const URL_TYPE_MP = 2;
|
||
|
||
const STATUS_SHOW = 1;
|
||
const STATUS_HIDE = 0;
|
||
|
||
const MEDIA_IMG = 0;
|
||
const MEDIA_VIDEO = 1;
|
||
|
||
const ZONE_ID_WXAPP_INDEX = 0;
|
||
const ZONE_ID_WXOA_INDEX = 1;
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%banner}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['cx_mch_id', 'user_id', 'zone_id', 'url_type', 'sort', 'is_delete', 'status', 'created_at', 'updated_at', 'deleted_at', 'media'], 'integer'],
|
||
[['app_id'], 'string', 'max' => 64],
|
||
[['app_ext'], 'string', 'max' => 1024],
|
||
[['img_url', 'page_url', 'video_url','other_img_url'], 'string', 'max' => 2048],
|
||
[['title'], 'string', 'max' => 512],
|
||
[['text'], 'string'],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'user_id' => '用户ID',
|
||
'zone_id' => '位置ID',
|
||
'url_type' => '链接类型,0=小程序页面,1=外部链接,2=小程序',
|
||
'app_id' => 'APP_ID',
|
||
'app_ext' => 'APP_EXT',
|
||
'img_url' => '图片',
|
||
'title' => '标题',
|
||
'page_url' => '链接',
|
||
'sort' => '排序',
|
||
'is_delete' => '是否删除',
|
||
'status' => '状态,0=隐藏,1=显示',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'deleted_at' => '删除时间',
|
||
'media' => '媒体类型:0=图片,1=视频',
|
||
'video_url' => '视频',
|
||
];
|
||
}
|
||
|
||
public function beforeSave($insert)
|
||
{
|
||
if (parent::beforeSave($insert)) {
|
||
if ($this->isNewRecord) {
|
||
$this->created_at = time();
|
||
}
|
||
$this->updated_at = time();
|
||
if ($this->is_delete == 1)
|
||
$this->deleted_at = time();
|
||
$this->htmlTagFilter();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public function htmlTagFilter()
|
||
{
|
||
$this->img_url = Model::htmlTagFilter($this->img_url);
|
||
$this->title = Model::htmlTagFilter($this->title);
|
||
// $this->page_url = Model::htmlTagFilter($this->page_url);
|
||
$this->video_url = Model::htmlTagFilter($this->video_url);
|
||
}
|
||
|
||
|
||
public static function urlTypeLabels()
|
||
{
|
||
return [
|
||
'0' => '小程序页面',
|
||
'1' => '外部链接',
|
||
'2' => '小程序',
|
||
'3' => '公众号图片',
|
||
'4' => '视频号图片',
|
||
'5' => '抖音号图片',
|
||
'6' => '文章',
|
||
'7' => '不弹窗',
|
||
];
|
||
}
|
||
|
||
public static function getUrlType($type)
|
||
{
|
||
$labels = self::urlTypeLabels();
|
||
return isset($labels[$type]) ? $labels[$type] : "未知";
|
||
}
|
||
|
||
public static function statusLabels()
|
||
{
|
||
return [
|
||
'0' => '隐藏',
|
||
'1' => '显示'
|
||
];
|
||
}
|
||
|
||
public static function getStatus($status)
|
||
{
|
||
$labels = self::statusLabels();
|
||
return isset($labels[$status]) ? $labels[$status] : "未知";
|
||
}
|
||
|
||
public static function mediaLabels()
|
||
{
|
||
return [
|
||
'0' => '图片',
|
||
// '1' => '视频',
|
||
];
|
||
}
|
||
|
||
public static function type()
|
||
{
|
||
return [
|
||
// '1' => 'banner',
|
||
'2' => '轮播',
|
||
];
|
||
}
|
||
|
||
public static function getMedia($media)
|
||
{
|
||
$labels = self::statusLabels();
|
||
return isset($labels[$media]) ? $labels[$media] : "未知";
|
||
}
|
||
|
||
public static function getBannerList($zone_id, $cx_mch_id = 0)
|
||
{
|
||
$list = Banner::find()
|
||
->where([
|
||
'zone_id' => $zone_id,
|
||
'is_delete' => 0,
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'status' => 1,
|
||
])
|
||
->select('id,url_type,app_id,app_ext,img_url,title,page_url,media,video_url,type,other_img_url,text')
|
||
->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])->asArray()->all();
|
||
foreach ($list as $index => $item) {
|
||
if ($item['media'] == self::MEDIA_IMG) {
|
||
$item['img_url'] = SiteHelper::getFullUrl($item['img_url']);
|
||
}
|
||
if ($item['media'] == self::MEDIA_VIDEO) {
|
||
//获取视频时长
|
||
$item['duration'] = 0;
|
||
$video_url = trim($item['video_url'], ".");
|
||
$video_url = trim($item['video_url'], "/");
|
||
if (file_exists($video_url)) {
|
||
$video_info = Utils::getVideoInfo($video_url);
|
||
$item['duration'] = ceil($video_info['seconds']);
|
||
}
|
||
$item['video_url'] = SiteHelper::getFullUrl($item['video_url']);
|
||
}
|
||
$list[$index] = $item;
|
||
}
|
||
return $list;
|
||
}
|
||
|
||
/**
|
||
* 轮播图查询
|
||
*/
|
||
public function search()
|
||
{
|
||
$data = Banner::find()->where(['status' => 1, 'is_delete' => 0, 'media' => 0])->select('title,page_url,sort,status,')->orderBy(['sort' => SORT_DESC])->all();
|
||
return $data;
|
||
}
|
||
|
||
}
|