Content.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace Collection\Lib;
  3. use Dever;
  4. class Content
  5. {
  6. public function updateTypeId($id, $name, $data)
  7. {
  8. # 同步更新到content的type_id
  9. $update['type_id'] = $id;
  10. $content_id = Dever::param('content_id', $data);
  11. if ($content_id) {
  12. $update['where_id'] = $content_id;
  13. Dever::db('collection/content')->update($update);
  14. }
  15. }
  16. # 获取某一个合集的内容列表
  17. public function getList($info_id, $page_id)
  18. {
  19. $where['info_id'] = $info_id;
  20. $where['page_id'] = $page_id;
  21. $data = Dever::db('collection/content')->getAllPage($where);
  22. $result = array();
  23. if ($data) {
  24. $i = 0;
  25. foreach ($data as $k => $v) {
  26. $data[$k] = $this->getOne($v);
  27. if ($data[$k]['data']) {
  28. $result[$i] = $data[$k];
  29. $i++;
  30. }
  31. }
  32. }
  33. return $result;
  34. }
  35. public function getOne($data)
  36. {
  37. if ($data['page_id']) {
  38. $page = Dever::db('collection/page')->one($data['page_id']);
  39. $data['parent_page_id'] = $page['page_id'];
  40. }
  41. $type = Dever::db('collection/cate')->config['gettype'];
  42. $table = $type[$data['type']]['table'];
  43. if (!$table) {
  44. return $data;
  45. }
  46. $method = 'one';
  47. if (strstr($table, '-')) {
  48. $temp = explode('-', $table);
  49. $table = $temp[0];
  50. $method = $temp[1];
  51. }
  52. $data['data'] = Dever::db($table)->$method(array('content_id' => $data['id'], 'info_id' => $data['info_id']));
  53. if ($data['data']) {
  54. if (isset($data['data']['id'])) {
  55. $data['data'] = $this->getData($data['data']);
  56. } else {
  57. foreach ($data['data'] as $k => $v) {
  58. $data['data'][$k] = $this->getData($v);
  59. }
  60. }
  61. }
  62. return $data;
  63. }
  64. public function getData($data)
  65. {
  66. $data['num_view'] = 1;
  67. $data['date'] = '';
  68. if (isset($data['cdate'])) {
  69. $data['date'] = date('Y-m-d H:i:s', $data['cdate']);
  70. }
  71. if (isset($data['text'])) {
  72. $data['text'] = Dever::array_decode($data['text']);
  73. foreach ($data['text'] as $k => $v) {
  74. if (isset($v['name']) && $v['name']) {
  75. $data['text'][$k]['name_array'] = explode("\r\n", $v['name']);
  76. }
  77. if (isset($v['bgcolor_type']) && $v['bgcolor_type'][0] == 2) {
  78. $data['text'][$k]['bgcolor'] = '';
  79. }
  80. if (isset($v['content']) && $v['content']) {
  81. $data['text'][$k]['content_array'] = $this->getContent($v);
  82. }
  83. }
  84. }
  85. if (isset($data['author_id']) && $data['author_id'] > 0) {
  86. $data['author'] = Dever::db('store/author')->one($data['author_id']);
  87. }
  88. if (isset($data['content']) && $data['content']) {
  89. $data = $this->getContent($data);
  90. }
  91. if (isset($data['goods_id']) && $data['goods_id'] > 0) {
  92. $data['goods'] = Dever::load('goods/lib/info')->getInfo($data['goods_id']);
  93. $data['goods'] = $this->getContent($data['goods']);
  94. }
  95. unset($data['info_id']);
  96. unset($data['content_id']);
  97. unset($data['status']);
  98. unset($data['state']);
  99. unset($data['cdate']);
  100. unset($data['reorder']);
  101. unset($data['udate']);
  102. return $data;
  103. }
  104. public function getContent($data, $uid = false)
  105. {
  106. //embed
  107. $data['content_array'] = $data['content_pic'] = array();
  108. $data['content'] = Dever::filter($data['content']);
  109. $content = $data['content'];
  110. $replace = array();
  111. # 小程序
  112. if (strstr($data['content'], 'data-applet')) {
  113. preg_match_all('/<img(.*?)data-applet="(.*?)" \/>/', $content, $matches);
  114. if (isset($matches[2])) {
  115. foreach ($matches[2] as $k => $v) {
  116. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  117. $temp = explode('||', $v);
  118. $pic = $temp[0];
  119. $appid = $temp[1];
  120. $path = $temp[2];
  121. if ($appid) {
  122. $appinfo = Dever::db('content/applet')->one(array('appid' => $appid));
  123. } else {
  124. $appinfo['name'] = '';
  125. $appinfo['link'] = '';
  126. }
  127. if (isset($temp[3]) && $temp[3]) {
  128. $appinfo['link'] = $temp[3];
  129. }
  130. $replace[] = array('type' => 'applet', 'pic_cover' => $pic, 'appid' => $appid, 'path' => $path, 'name' => $appinfo['name'], 'link' => $appinfo['link']);
  131. }
  132. }
  133. }
  134. # 图片
  135. if (strstr($data['content'], '<img')) {
  136. preg_match_all('/<img(.*?)src="(.*?)" (.*?)\/>/', $content, $matches);
  137. if (isset($matches[2])) {
  138. foreach ($matches[2] as $k => $v) {
  139. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  140. $pic = $v;
  141. $replace[] = array('type' => 'pic', 'content' => $pic);
  142. $data['content_pic'][] = $pic;
  143. }
  144. }
  145. }
  146. # 音视频
  147. if (strstr($data['content'], 'data-file')) {
  148. preg_match_all('/<img src="(.*?)" style="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  149. if (!isset($matches[1][0])) {
  150. preg_match_all('/<img style="(.*?)" src="(.*?)" data-file="(.*?)" \/>/', $content, $matches);
  151. $temp = array();
  152. if (isset($matches[2][0])) {
  153. $temp = $matches;
  154. $matches[1] = $temp[2];
  155. unset($temp);
  156. }
  157. }
  158. if (isset($matches[1])) {
  159. foreach ($matches[1] as $k => $v) {
  160. if (isset($matches[3][$k])) {
  161. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  162. $file = $matches[3][$k];
  163. $temp = explode('||', $file);
  164. $file = $temp[0];
  165. if (isset($temp[1])) {
  166. $name = $temp[1];
  167. } else {
  168. $name = '';
  169. }
  170. $cover = $v;
  171. if (strstr($v, '.mp4') || strstr($v, '.mov') || strstr($v, '.m3u8')) {
  172. $replace[] = array('type' => 'video', 'content' => $file, 'cover' => $cover, 'name' => $name);
  173. } else {
  174. $replace[] = array('type' => 'audio', 'content' => $file, 'cover' => $cover, 'name' => $name);
  175. }
  176. }
  177. }
  178. }
  179. }
  180. # 视频+直播
  181. if (strstr($data['content'], 'data-id')) {
  182. preg_match_all('/<img(.*?)data-id="(.*?)" data-key="(.*?)" \/>/', $content, $matches);
  183. if (!isset($matches[2][0])) {
  184. preg_match_all('/<img(.*?)data-key="(.*?)" data-id="(.*?)" \/>/', $content, $matches);
  185. $temp = array();
  186. if (isset($matches[2][0]) && isset($matches[3][0])) {
  187. $temp = $matches;
  188. $matches[2] = $temp[3];
  189. $matches[3] = $temp[2];
  190. unset($temp);
  191. }
  192. }
  193. if (isset($matches[2][0]) && isset($matches[3][0])) {
  194. foreach ($matches[2] as $k => $v) {
  195. $content = str_replace($matches[0][$k], '{replace}'.count($replace).'{replace}', $content);
  196. if ($matches[3][$k] == 'video/lib/core.vod') {
  197. $method = 'video/lib/vod';
  198. $type = 'video';
  199. } else {
  200. $type = 'live';
  201. $method = 'video/lib/live';
  202. }
  203. $info = Dever::load($method)->get($v);
  204. if (isset($info['content'])) {
  205. unset($info['content']);
  206. }
  207. if ($type == 3) {
  208. # 查看是否有预约
  209. if ($uid > 0) {
  210. $info['user_act']['note'] = Dever::load('act/lib/note')->get($uid, $v, $type);
  211. } else {
  212. $info['user_act']['note'] = 2;
  213. }
  214. }
  215. $replace[] = array('id' => $v, 'type' => $type, 'content' => $info);
  216. }
  217. }
  218. }
  219. //$data['content'] = preg_replace('/<div class="dever-drop">([\s\S]*?)<\/div>/i', '', $data['content']);
  220. $content = explode('{replace}', $content);
  221. $data['content_array'] = array();
  222. //print_r($content);die;
  223. foreach ($content as $k => $v) {
  224. $v = trim($v);
  225. if (is_numeric($v) && $v >= 0 && isset($replace[$v])) {
  226. $data['content_array'][] = $replace[$v];
  227. } elseif ($v) {
  228. $data['content_array'][] = array
  229. (
  230. 'type' => 'html',
  231. 'content' => $v,
  232. );
  233. }
  234. }
  235. if (!$data['content_array']) {
  236. $data['content_array'][] = array
  237. (
  238. 'type' => 'html',
  239. 'content' => $data['content'],
  240. );
  241. }
  242. unset($data['content']);
  243. //print_r($data['content_array']);die;
  244. return $data;
  245. }
  246. }