content.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/7/5
  6. * Time: 下午6:13
  7. */
  8. namespace ugc;
  9. use Exception;
  10. use tools;
  11. class content_config
  12. {
  13. const def_item_bg_color = '#F2F2F2';
  14. const def_item_bg_type = 'color';
  15. const def_divider_bg_img= '/mobile/defimg/divider_bg.png';
  16. }
  17. abstract class UGContent
  18. {
  19. public function __construct($content)
  20. {
  21. }
  22. public function base_info()
  23. {
  24. $param['item_type'] = 'home_ugc';
  25. $param['bg_type'] = content_config::def_item_bg_type;
  26. $param['bg_data'] = content_config::def_item_bg_color;
  27. $param['has_margin'] = 0;
  28. $param['bg_image'] = '';
  29. return $param;
  30. }
  31. public abstract function format_block();
  32. public abstract function type();
  33. protected function item_data($show_type, $show_data, $title='', $type='', $data='', $reserved='')
  34. {
  35. $result['home_title'] = $title;
  36. $result['image'] = $show_data;
  37. $result['show_type'] = $show_type;
  38. $result['show_data'] = $show_data;
  39. $result['type'] = $type;
  40. $result['data'] = $data;
  41. $result['title'] = $title;
  42. $result['reserved'] = $reserved;
  43. return $result;
  44. }
  45. }
  46. class images_item extends UGContent
  47. {
  48. private $mTitle;
  49. private $mImages;
  50. public function __construct($content)
  51. {
  52. parent::__construct($content);
  53. $this->mTitle = empty($content['title']) ? "" : $content['title'];
  54. $this->mImages = $this->add_size($content['images']);
  55. if(empty($content['images'])) {
  56. throw new Exception("上传的图片不能为空");
  57. }
  58. }
  59. private function add_size($images)
  60. {
  61. $result = [];
  62. foreach ($images as $uri) {
  63. $img = new tools\fast_image($uri);
  64. list($width, $height) = $img->getSize();
  65. if($width > 0 && $height > 0) {
  66. $uri .= "?{$width},{$height}";
  67. }
  68. $result[] = $uri;
  69. }
  70. return $result;
  71. }
  72. public function add_image($image) {
  73. $this->mImages[] = $image;
  74. }
  75. public function format_block()
  76. {
  77. $image = $this->image();
  78. if($image == false) return false;
  79. $params = $this->base_info();
  80. $params['item_data'] = $this->item_data('image',$image,$this->mTitle);
  81. return $params;
  82. }
  83. private function image()
  84. {
  85. if(empty($this->mImages)) return false;
  86. return $this->mImages[0];
  87. }
  88. public function type() {
  89. return 'image';
  90. }
  91. }
  92. class text_item extends UGContent
  93. {
  94. private $mText;
  95. public function __construct($content)
  96. {
  97. parent::__construct($content);
  98. $this->mText = trim($content['text']);
  99. if(empty($this->mText)) {
  100. throw new Exception("输入文字内容不能为空");
  101. }
  102. }
  103. public function format_block()
  104. {
  105. $data = $this->mText;
  106. if(empty($data)) return false;
  107. $params = $this->base_info();
  108. $params['item_data'] = $this->item_data('text',$data,'','text',$data);
  109. return $params;
  110. }
  111. public function text() {
  112. return $this->mText;
  113. }
  114. public function type() {
  115. return 'text';
  116. }
  117. }
  118. class goods_item extends UGContent
  119. {
  120. private $mGoodsID;
  121. private $mRecommend;
  122. public function __construct($content)
  123. {
  124. parent::__construct($content);
  125. $this->mGoodsID = intval($content['goods_id']);
  126. $this->mRecommend = $content['text'];
  127. if($this->mGoodsID <= 0) {
  128. throw new Exception("选择的商品不能为空");
  129. }
  130. }
  131. function format_block($item_sort = 0)
  132. {
  133. $params = $this->base_info();
  134. $params['item_data'] = $this->item_data('goods',$this->mGoodsID,'','goods',$this->mGoodsID,$this->mRecommend);
  135. return $params;
  136. }
  137. public function type() {
  138. return 'goods';
  139. }
  140. }
  141. class voice_item extends UGContent
  142. {
  143. public function __construct($content)
  144. {
  145. parent::__construct($content);
  146. }
  147. public function format_block()
  148. {
  149. return false;
  150. }
  151. public function type() {
  152. return 'voice';
  153. }
  154. }
  155. class video_item extends UGContent
  156. {
  157. private $mTitle;
  158. private $mVideo;
  159. private $mImage;
  160. public function __construct($content)
  161. {
  162. parent::__construct($content);
  163. $this->mTitle = empty($content['title']) ? "" : $content['title'];
  164. $this->mVideo = $content['video'];
  165. $this->mImage = $content['cover'];
  166. if(empty($content['video'])) {
  167. throw new Exception("上传视频内容不能为空");
  168. }
  169. }
  170. public function set_images($images)
  171. {
  172. $this->mVideo = $images;
  173. }
  174. public function add_image($image) {
  175. $this->mVideo[] = $image;
  176. }
  177. public function format_block()
  178. {
  179. $url = $this->video();
  180. if($url == false) return false;
  181. $params = $this->base_info();
  182. $params['item_data'] = $this->item_data('image',$this->mImage,$this->mTitle,'video',$this->mVideo);
  183. return $params;
  184. }
  185. private function video()
  186. {
  187. if(empty($this->mVideo)) return false;
  188. return $this->mVideo;
  189. }
  190. public function type() {
  191. return 'video';
  192. }
  193. }
  194. class vote_item extends UGContent
  195. {
  196. private $mTitle;
  197. private $mDeadline;
  198. private $mOptions;
  199. private $mType;
  200. private $mSingle;
  201. private $mVotekey;
  202. public function __construct($content,$vote_key)
  203. {
  204. parent::__construct($content);
  205. $this->mTitle = $content['title'];
  206. $this->mDeadline = intval($content['deadline']);
  207. $this->mType = intval($content['vote_type']);
  208. $this->mVotekey = $vote_key;
  209. if(isset($content['vote_single'])) {
  210. $this->mSingle = boolval($content['vote_single']);
  211. }
  212. else {
  213. $this->mSingle = true;
  214. }
  215. $options = $content['options'];
  216. $this->mOptions = [];
  217. $i = 0;
  218. foreach ($options as $val)
  219. {
  220. $val = trim($val);
  221. if(!empty($val)) {
  222. $this->mOptions[$i++] = $val;
  223. }
  224. }
  225. if(empty($this->mTitle)) {
  226. throw new Exception("投票必须设置标题");
  227. }
  228. if(empty($this->mOptions || count($this->mOptions) <= 1)) {
  229. throw new Exception("投票最少需要两个选项");
  230. }
  231. }
  232. public function format_block()
  233. {
  234. $options = $this->options();
  235. if(empty($options)) return false;
  236. $data = json_encode($options,JSON_UNESCAPED_UNICODE);
  237. $params = $this->base_info();
  238. $reserved = "vote_type={$this->mType}"; //#vote_key={$this->mVotekey}
  239. $params['item_data'] = $this->item_data('vote','',$this->mTitle,'vote',$data,$reserved);
  240. return $params;
  241. }
  242. public function vote_param()
  243. {
  244. return ['options' => $this->mOptions,'deadline' => $this->mDeadline,'vote_type' => $this->mType,'vote_single' => $this->mSingle];
  245. }
  246. private function options()
  247. {
  248. $result = [];
  249. foreach ($this->mOptions as $key => $val) {
  250. $item = [];
  251. $item['id'] = $key;
  252. $item['text'] = $val;
  253. $result[] = $item;
  254. }
  255. return $result;
  256. }
  257. public function type() {
  258. return 'vote';
  259. }
  260. public function vote_result()
  261. {
  262. $result = [];
  263. foreach ($this->mOptions as $key => $val) {
  264. $result[$key] = 0;
  265. }
  266. return $result;
  267. }
  268. public function vote_type() {
  269. return $this->mType;
  270. }
  271. }
  272. class question_item extends UGContent
  273. {
  274. private $mTitle;
  275. private $mOptions;
  276. private $mAnswers;
  277. public function __construct($content)
  278. {
  279. parent::__construct($content);
  280. $this->mTitle = $content['title'];
  281. $this->mOptions = $content['options'];
  282. $answer = $content['answers'];
  283. $this->mAnswers = [];
  284. foreach ($answer as $val) {
  285. $this->mAnswers[] = intval($val);
  286. }
  287. if(empty($this->mTitle)) {
  288. throw new Exception("选择题必须设置题目");
  289. }
  290. if(empty($this->mAnswers || count($this->mAnswers) <= 1)) {
  291. throw new Exception("选择题最少需要设置两个选项");
  292. }
  293. }
  294. private function options()
  295. {
  296. $result = [];
  297. $i = 0;
  298. foreach ($this->mOptions as $key => $val) {
  299. $item = [];
  300. $item['id'] = $i;
  301. $item['text'] = $val;
  302. $result[] = $item;
  303. $i++;
  304. }
  305. return $result;
  306. }
  307. private function answer_type() {
  308. return count($this->mAnswers) == 1 ? 0 : 1;
  309. }
  310. public function question()
  311. {
  312. $result['title'] = $this->mTitle;
  313. $result['options'] = $this->options();
  314. return $result;
  315. }
  316. public function answer()
  317. {
  318. return $this->mAnswers;
  319. }
  320. public function format_block()
  321. {
  322. $options = $this->options();
  323. if(empty($options)) return false;
  324. $data = json_encode($options,JSON_UNESCAPED_UNICODE);
  325. $params = $this->base_info();
  326. $answer_type = $this->answer_type();
  327. $reserved = "answer_type={$answer_type}";
  328. $params['item_data'] = $this->item_data('question','',$this->mTitle,'question',$data,$reserved);
  329. return $params;
  330. }
  331. public function type() {
  332. return 'question';
  333. }
  334. public function answers()
  335. {
  336. return $this->mAnswers;
  337. }
  338. }
  339. class href_item extends UGContent
  340. {
  341. private $mUrl;
  342. private $mImage;
  343. public function __construct($content)
  344. {
  345. parent::__construct($content);
  346. $this->mUrl = $content['url'];
  347. $this->mImage = $content['image'];
  348. if(empty($this->mUrl)) {
  349. throw new Exception("链接不能为空");
  350. }
  351. if(empty($this->mImage)) {
  352. throw new Exception("图片不能为空");
  353. }
  354. }
  355. public function format_block()
  356. {
  357. $data = $this->mUrl;
  358. if(empty($data)) return false;
  359. $params = $this->base_info();
  360. $params['item_data'] = $this->item_data('image',$this->mImage,"",'url',$this->mUrl);
  361. return $params;
  362. }
  363. public function type() {
  364. return 'href';
  365. }
  366. }