Question.php 1.4 KB
<?php
namespace app\mobile\model;

use think\Model;

class Question extends Model
{
	// 表名
    protected $name = 'mobile_question';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 获取视频解析封面和地址
    public function getAnalysisVideoAttr($value,$data){
    	if(empty($value)){
    		return [];
    	}
    	// 获取网络视频信息
    	if(preg_match("/^http(s)?:\\/\\/.+/",$value)){
    		return [
	    		'cover' => request()->domain().'/assets/img/bg-middle.jpg',
	    		'video' => $value
	    	];
    	}
    	// 获取七牛云视频信息
    	$video = cdnurl($value,true);
    	$video_info = json_decode(file_get_contents($video.'?avinfo'),true);
    	return [
    		'cover' => $this->getCoverImagesQiniu($video,$video_info),
    		'video' => $video
    	];
    }

    // 获取七牛云视频的封面图
    public function getCoverImagesQiniu($video_url,$video_info){
        if(empty($video_info['streams'][0]['width'])) {
            $width = $video_info['streams'][1]['width'];
            $height = $video_info['streams'][1]['height'];
        } else {
            $width = $video_info['streams'][0]['width'];
            $height = $video_info['streams'][0]['height'];
        }
        return $video_url.'?vframe/jpg/offset/1/w/'.$width.'/h/'.$height;
    }  
}