content.php 9.8 KB

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