Topic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace app\api\controller;
  3. use think\Controller;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Request;
  7. use think\Validate;
  8. class Topic extends Base
  9. {
  10. use PublicApi;
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->check_config();
  15. }
  16. public function index()
  17. {
  18. }
  19. /**
  20. * 获取列表
  21. *
  22. * @param Request $request
  23. * @return \think\response\Json
  24. */
  25. public function get_list(Request $request)
  26. {
  27. // 参数校验
  28. $param = $request->param();
  29. $validate = validate($request->controller());
  30. if (!$validate->scene($request->action())->check($param)) {
  31. return json([
  32. 'code' => 1001,
  33. 'msg' => '参数错误: ' . $validate->getError(),
  34. ]);
  35. }
  36. $offset = isset($param['offset']) ? (int)$param['offset'] : 0;
  37. $limit = isset($param['limit']) ? (int)$param['limit'] : 20;
  38. // 查询条件组装
  39. $where = [];
  40. $where['topic_status'] = ['eq', 1];
  41. if (isset($param['time_end']) && isset($param['time_start'])) {
  42. $where['topic_time'] = ['between', [(int)$param['time_start'], (int)$param['time_end']]];
  43. }elseif (isset($param['time_end'])) {
  44. $where['topic_time'] = ['<', (int)$param['time_end']];
  45. }elseif (isset($param['time_start'])) {
  46. $where['topic_time'] = ['>', (int)$param['time_start']];
  47. }
  48. // 数据获取
  49. $total = model('Topic')->getCountByCond($where);
  50. $list = [];
  51. if ($total > 0) {
  52. // 排序
  53. $order = "topic_time DESC";
  54. if (!empty($param['orderby'])) {
  55. $order = 'topic_' . $param['orderby'] . " DESC";
  56. }
  57. $field = 'topic_id,topic_name,topic_en,topic_sub,topic_pic,topic_pic_slide,topic_blurb,topic_rel_vod,topic_content,topic_time,topic_hits,topic_up,topic_down';
  58. $list = model('Topic')->getListByCond($offset, $limit, $where, $order, $field, []);
  59. foreach ($list as &$v) {
  60. $v['topic_pic'] = mac_url_img($v['topic_pic'] ?? '');
  61. $v['topic_link'] = mac_url_topic_detail($v);
  62. $rel = isset($v['topic_rel_vod']) ? (string) $v['topic_rel_vod'] : '';
  63. $v['topic_rel_count'] = $rel === '' ? 0 : count(array_filter(explode(',', $rel)));
  64. }
  65. unset($v);
  66. }
  67. // 返回
  68. return json([
  69. 'code' => 1,
  70. 'msg' => '获取成功',
  71. 'info' => [
  72. 'offset' => $offset,
  73. 'limit' => $limit,
  74. 'total' => $total,
  75. 'rows' => $list,
  76. ],
  77. ]);
  78. }
  79. /**
  80. * 获取列表与推荐信息视频文章
  81. *
  82. * @param Request $request
  83. * @return \think\response\Json
  84. */
  85. public function get_detail(Request $request)
  86. {
  87. // 参数校验
  88. $param = $request->param();
  89. $validate = validate($request->controller());
  90. if (!$validate->scene($request->action())->check($param)) {
  91. return json([
  92. 'code' => 1001,
  93. 'msg' => '参数错误: ' . $validate->getError(),
  94. ]);
  95. }
  96. $result = Db::table('mac_topic')->where(['topic_id' => $param['topic_id']])->find();
  97. if ($result)
  98. {
  99. // 处理图片 URL
  100. $result['topic_pic'] = mac_url_img($result['topic_pic'] ?? '');
  101. $topic_rel_vod = [];
  102. $topic_rel_art = [];
  103. if (!empty($result['topic_rel_vod']))
  104. {
  105. $topic_rel_vod_arr = explode(',',$result['topic_rel_vod']);
  106. foreach ($topic_rel_vod_arr as $index => $item) {
  107. $vod = Db::table('mac_vod')->where(['vod_id' => $item])->field('vod_id,vod_name,vod_en,vod_pic,vod_actor,vod_director,vod_blurb,vod_remarks,vod_score,vod_year,vod_area,vod_class,type_id,type_id_1')->find();
  108. if ($vod) {
  109. $vod['vod_pic'] = mac_url_img($vod['vod_pic']);
  110. $vod['vod_link'] = mac_url_vod_detail($vod);
  111. array_push($topic_rel_vod,$vod);
  112. }
  113. }
  114. $result['topic_rel_vod'] = $topic_rel_vod;
  115. }
  116. if (!empty($result['topic_rel_art']))
  117. {
  118. $topic_rel_art_arr = explode(',',$result['topic_rel_art']);
  119. foreach ($topic_rel_art_arr as $index => $item) {
  120. $art = Db::table('mac_art')->where(['art_id' => $item])->field('art_id,type_id,art_name,art_sub,art_en,art_pic,art_blurb,art_remarks,art_time')->find();
  121. if ($art) {
  122. $art['art_pic'] = mac_url_img($art['art_pic'] ?? '');
  123. $art['art_link'] = mac_url_art_detail($art);
  124. array_push($topic_rel_art,$art);
  125. }
  126. }
  127. $result['topic_rel_art'] = $topic_rel_art;
  128. }
  129. $uid = (int) ($GLOBALS['user']['user_id'] ?? 0);
  130. $tid = (int) ($result['topic_id'] ?? 0);
  131. $fav = mac_user_fav_state($uid, 3, $tid);
  132. $result['is_fav'] = $fav['is_fav'];
  133. $result['fav_ulog_id'] = $fav['fav_ulog_id'];
  134. $result['user_has_up'] = mac_user_has_digg(3, $tid);
  135. }
  136. // 返回
  137. return json([
  138. 'code' => 1,
  139. 'msg' => '获取成功',
  140. 'info' => $result,
  141. ]);
  142. }
  143. /**
  144. * 获取推荐/精选专题
  145. * 对应首页精选专题区块
  146. *
  147. * @param Request $request
  148. * @return \think\response\Json
  149. *
  150. * 参数说明:
  151. * ids - 可选,专题 ID 列表(逗号分隔),与 num 个格子顺序一一对应;0 或无效表示该格占位
  152. * num - 可选,数量,默认5;传 ids 时表示期望返回行数(与首页七巧板一致)
  153. * by - 可选,排序字段,默认 time,可选: time,hits(仅未传 ids 时生效)
  154. */
  155. public function get_recommend(Request $request)
  156. {
  157. $param = $request->param();
  158. $num = isset($param['num']) ? (int)$param['num'] : 5;
  159. $num = max(1, min(20, $num));
  160. $idsRaw = isset($param['ids']) ? trim((string) $param['ids']) : '';
  161. if ($idsRaw !== '') {
  162. $parts = explode(',', $idsRaw);
  163. $orderedIds = [];
  164. foreach ($parts as $p) {
  165. if (count($orderedIds) >= $num) {
  166. break;
  167. }
  168. $orderedIds[] = max(0, (int) trim($p));
  169. }
  170. while (count($orderedIds) < $num) {
  171. $orderedIds[] = 0;
  172. }
  173. $orderedIds = array_slice($orderedIds, 0, $num);
  174. $wantIds = array_values(array_unique(array_filter($orderedIds, function ($id) {
  175. return $id > 0;
  176. })));
  177. $byId = [];
  178. if (!empty($wantIds)) {
  179. $dbList = Db::table('mac_topic')
  180. ->field('topic_id,topic_name,topic_en,topic_sub,topic_pic,topic_pic_slide,topic_blurb,topic_rel_vod,topic_time,topic_hits,topic_status')
  181. ->where('topic_id', 'in', $wantIds)
  182. ->select();
  183. foreach ($dbList as $row) {
  184. $byId[(int) $row['topic_id']] = $row;
  185. }
  186. }
  187. $placeholder = function () {
  188. return [
  189. 'topic_empty' => 1,
  190. 'topic_id' => 0,
  191. 'topic_name' => '',
  192. 'topic_en' => '',
  193. 'topic_sub' => '',
  194. 'topic_pic' => '',
  195. 'topic_pic_slide' => '',
  196. 'topic_blurb' => '',
  197. 'topic_rel_vod' => '',
  198. 'topic_time' => 0,
  199. 'topic_hits' => 0,
  200. 'topic_link' => '',
  201. ];
  202. };
  203. $list = [];
  204. foreach ($orderedIds as $tid) {
  205. if ($tid <= 0) {
  206. $list[] = $placeholder();
  207. continue;
  208. }
  209. $row = isset($byId[$tid]) ? $byId[$tid] : null;
  210. if (!$row || (int) $row['topic_status'] !== 1) {
  211. $list[] = $placeholder();
  212. continue;
  213. }
  214. unset($row['topic_status']);
  215. $row['topic_pic'] = mac_url_img($row['topic_pic']);
  216. $row['topic_pic_slide'] = mac_url_img($row['topic_pic_slide']);
  217. $row['topic_link'] = mac_url_topic_detail($row);
  218. $row['topic_empty'] = 0;
  219. $list[] = $row;
  220. }
  221. return json([
  222. 'code' => 1,
  223. 'msg' => '获取成功',
  224. 'info' => [
  225. 'total' => count($list),
  226. 'rows' => $list,
  227. ],
  228. ]);
  229. }
  230. $start = isset($param['start']) ? max(0, (int)$param['start']) : 0;
  231. $by = isset($param['by']) ? trim($param['by']) : 'time';
  232. $allowBy = ['time', 'hits'];
  233. if (!in_array($by, $allowBy)) {
  234. $by = 'time';
  235. }
  236. $where = [];
  237. $where['topic_status'] = ['eq', 1];
  238. $list = Db::table('mac_topic')
  239. ->field('topic_id,topic_name,topic_en,topic_sub,topic_pic,topic_pic_slide,topic_blurb,topic_rel_vod,topic_time,topic_hits')
  240. ->where($where)
  241. ->order('topic_' . $by . ' desc')
  242. ->limit($start, $num)
  243. ->select();
  244. foreach ($list as &$v) {
  245. $v['topic_pic'] = mac_url_img($v['topic_pic']);
  246. $v['topic_pic_slide'] = mac_url_img($v['topic_pic_slide']);
  247. $v['topic_link'] = mac_url_topic_detail($v);
  248. $v['topic_empty'] = 0;
  249. }
  250. unset($v);
  251. return json([
  252. 'code' => 1,
  253. 'msg' => '获取成功',
  254. 'info' => [
  255. 'total' => count($list),
  256. 'rows' => $list,
  257. ],
  258. ]);
  259. }
  260. }