123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/7/5
- * Time: 下午6:13
- */
- namespace ugc;
- use Exception;
- use tools;
- class content_config
- {
- const def_item_bg_color = '#F2F2F2';
- const def_item_bg_type = 'color';
- const def_divider_bg_img= '/mobile/defimg/divider_bg.png';
- }
- abstract class UGContent
- {
- public function __construct($content)
- {
- }
- public function base_info()
- {
- $param['item_type'] = 'home_ugc';
- $param['bg_type'] = content_config::def_item_bg_type;
- $param['bg_data'] = content_config::def_item_bg_color;
- $param['has_margin'] = 0;
- $param['bg_image'] = '';
- return $param;
- }
- public abstract function format_block();
- public abstract function type();
- protected function item_data($show_type, $show_data, $title='', $type='', $data='', $reserved='')
- {
- $result['home_title'] = $title;
- $result['image'] = $show_data;
- $result['show_type'] = $show_type;
- $result['show_data'] = $show_data;
- $result['type'] = $type;
- $result['data'] = $data;
- $result['title'] = $title;
- $result['reserved'] = $reserved;
- return $result;
- }
- }
- class images_item extends UGContent
- {
- private $mTitle;
- private $mImages;
- public function __construct($content)
- {
- parent::__construct($content);
- $this->mTitle = empty($content['title']) ? "" : $content['title'];
- $this->mImages = $this->add_size($content['images']);
- if(empty($content['images'])) {
- throw new Exception("上传的图片不能为空");
- }
- }
- private function add_size($images)
- {
- $result = [];
- foreach ($images as $uri) {
- $img = new tools\fast_image($uri);
- list($width, $height) = $img->getSize();
- if($width > 0 && $height > 0) {
- $uri .= "?{$width},{$height}";
- }
- $result[] = $uri;
- }
- return $result;
- }
- public function add_image($image) {
- $this->mImages[] = $image;
- }
- public function format_block()
- {
- $image = $this->image();
- if($image == false) return false;
- $params = $this->base_info();
- $params['item_data'] = $this->item_data('image',$image,$this->mTitle);
- return $params;
- }
- private function image()
- {
- if(empty($this->mImages)) return false;
- return $this->mImages[0];
- }
- public function type() {
- return 'image';
- }
- }
- class text_item extends UGContent
- {
- private $mText;
- public function __construct($content)
- {
- parent::__construct($content);
- $this->mText = trim($content['text']);
- if(empty($this->mText)) {
- throw new Exception("输入文字内容不能为空");
- }
- }
- public function format_block()
- {
- $data = $this->mText;
- if(empty($data)) return false;
- $params = $this->base_info();
- $params['item_data'] = $this->item_data('text',$data,'','text',$data);
- return $params;
- }
- public function text() {
- return $this->mText;
- }
- public function type() {
- return 'text';
- }
- }
- class goods_item extends UGContent
- {
- private $mGoodsID;
- private $mRecommend;
- public function __construct($content)
- {
- parent::__construct($content);
- $this->mGoodsID = intval($content['goods_id']);
- $this->mRecommend = $content['text'];
- if($this->mGoodsID <= 0) {
- throw new Exception("选择的商品不能为空");
- }
- }
- function format_block($item_sort = 0)
- {
- $params = $this->base_info();
- $params['item_data'] = $this->item_data('goods',$this->mGoodsID,'','goods',$this->mGoodsID,$this->mRecommend);
- return $params;
- }
- public function type() {
- return 'goods';
- }
- }
- class voice_item extends UGContent
- {
- public function __construct($content)
- {
- parent::__construct($content);
- }
- public function format_block()
- {
- return false;
- }
- public function type() {
- return 'voice';
- }
- }
- class video_item extends UGContent
- {
- private $mTitle;
- private $mVideo;
- private $mImage;
- public function __construct($content)
- {
- parent::__construct($content);
- $this->mTitle = empty($content['title']) ? "" : $content['title'];
- $this->mVideo = $content['video'];
- $this->mImage = $content['cover'];
- if(empty($content['video'])) {
- throw new Exception("上传视频内容不能为空");
- }
- }
- public function set_images($images)
- {
- $this->mVideo = $images;
- }
- public function add_image($image) {
- $this->mVideo[] = $image;
- }
- public function format_block()
- {
- $url = $this->video();
- if($url == false) return false;
- $params = $this->base_info();
- $params['item_data'] = $this->item_data('image',$this->mImage,$this->mTitle,'video',$this->mVideo);
- return $params;
- }
- private function video()
- {
- if(empty($this->mVideo)) return false;
- return $this->mVideo;
- }
- public function type() {
- return 'video';
- }
- }
- class vote_item extends UGContent
- {
- private $mTitle;
- private $mDeadline;
- private $mOptions;
- private $mType;
- private $mSingle;
- private $mVotekey;
- public function __construct($content,$vote_key)
- {
- parent::__construct($content);
- $this->mTitle = $content['title'];
- $this->mDeadline = intval($content['deadline']);
- $this->mType = intval($content['vote_type']);
- $this->mVotekey = $vote_key;
- if(isset($content['vote_single'])) {
- $this->mSingle = boolval($content['vote_single']);
- }
- else {
- $this->mSingle = true;
- }
- $options = $content['options'];
- $this->mOptions = [];
- $i = 0;
- foreach ($options as $val)
- {
- $val = trim($val);
- if(!empty($val)) {
- $this->mOptions[$i++] = $val;
- }
- }
- if(empty($this->mTitle)) {
- throw new Exception("投票必须设置标题");
- }
- if(empty($this->mOptions || count($this->mOptions) <= 1)) {
- throw new Exception("投票最少需要两个选项");
- }
- }
- public function format_block()
- {
- $options = $this->options();
- if(empty($options)) return false;
- $data = json_encode($options,JSON_UNESCAPED_UNICODE);
- $params = $this->base_info();
- $reserved = "vote_type={$this->mType}"; //#vote_key={$this->mVotekey}
- $params['item_data'] = $this->item_data('vote','',$this->mTitle,'vote',$data,$reserved);
- return $params;
- }
- public function vote_param()
- {
- return ['options' => $this->mOptions,'deadline' => $this->mDeadline,'vote_type' => $this->mType,'vote_single' => $this->mSingle];
- }
- private function options()
- {
- $result = [];
- foreach ($this->mOptions as $key => $val) {
- $item = [];
- $item['id'] = $key;
- $item['text'] = $val;
- $result[] = $item;
- }
- return $result;
- }
- public function type() {
- return 'vote';
- }
- public function vote_result()
- {
- $result = [];
- foreach ($this->mOptions as $key => $val) {
- $result[$key] = 0;
- }
- return $result;
- }
- public function vote_type() {
- return $this->mType;
- }
- }
- class question_item extends UGContent
- {
- private $mTitle;
- private $mOptions;
- private $mAnswers;
- public function __construct($content)
- {
- parent::__construct($content);
- $this->mTitle = $content['title'];
- $this->mOptions = $content['options'];
- $answer = $content['answers'];
- $this->mAnswers = [];
- foreach ($answer as $val) {
- $this->mAnswers[] = intval($val);
- }
- if(empty($this->mTitle)) {
- throw new Exception("选择题必须设置题目");
- }
- if(empty($this->mAnswers || count($this->mAnswers) <= 1)) {
- throw new Exception("选择题最少需要设置两个选项");
- }
- }
- private function options()
- {
- $result = [];
- $i = 0;
- foreach ($this->mOptions as $key => $val) {
- $item = [];
- $item['id'] = $i;
- $item['text'] = $val;
- $result[] = $item;
- $i++;
- }
- return $result;
- }
- private function answer_type() {
- return count($this->mAnswers) == 1 ? 0 : 1;
- }
- public function question()
- {
- $result['title'] = $this->mTitle;
- $result['options'] = $this->options();
- return $result;
- }
- public function answer()
- {
- return $this->mAnswers;
- }
- public function format_block()
- {
- $options = $this->options();
- if(empty($options)) return false;
- $data = json_encode($options,JSON_UNESCAPED_UNICODE);
- $params = $this->base_info();
- $answer_type = $this->answer_type();
- $reserved = "answer_type={$answer_type}";
- $params['item_data'] = $this->item_data('question','',$this->mTitle,'question',$data,$reserved);
- return $params;
- }
- public function type() {
- return 'question';
- }
- public function answers()
- {
- return $this->mAnswers;
- }
- }
- class href_item extends UGContent
- {
- private $mUrl;
- private $mImage;
- public function __construct($content)
- {
- parent::__construct($content);
- $this->mUrl = $content['url'];
- $this->mImage = $content['image'];
- if(empty($this->mUrl)) {
- throw new Exception("链接不能为空");
- }
- if(empty($this->mImage)) {
- throw new Exception("图片不能为空");
- }
- }
- public function format_block()
- {
- $data = $this->mUrl;
- if(empty($data)) return false;
- $params = $this->base_info();
- $params['item_data'] = $this->item_data('image',$this->mImage,"",'url',$this->mUrl);
- return $params;
- }
- public function type() {
- return 'href';
- }
- }
|