Role.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. /**
  6. * Role 角色前台 JSON 接口
  7. *
  8. * 提供 role/get_list 和 role/get_detail 接口,
  9. * 对应前台模板中 {maccms:role} 标签的数据需求。
  10. */
  11. class Role extends Base
  12. {
  13. use PublicApi;
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $this->check_config();
  18. }
  19. public function index()
  20. {
  21. }
  22. /**
  23. * 获取角色列表
  24. * GET /api.php/role/get_list
  25. *
  26. * 参数说明:
  27. * offset - 可选,偏移量,默认0
  28. * limit - 可选,每页条数,默认20
  29. * rid - 可选,关联视频ID(role_rid)
  30. * name - 可选,角色名称模糊搜索
  31. * letter - 可选,首字母筛选
  32. * level - 可选,推荐等级筛选,多个用逗号分隔
  33. * actor - 可选,配音/演员筛选
  34. * orderby - 可选,排序字段,默认 time
  35. * 可选: id,time,time_add,hits,hits_day,hits_week,hits_month,score,up,down,level
  36. *
  37. * @param Request $request
  38. * @return \think\response\Json
  39. */
  40. public function get_list(Request $request)
  41. {
  42. $param = $request->param();
  43. // 参数校验
  44. $validate = validate($request->controller());
  45. if (!$validate->scene($request->action())->check($param)) {
  46. return json([
  47. 'code' => 1001,
  48. 'msg' => '参数错误: ' . $validate->getError(),
  49. ]);
  50. }
  51. $offset = isset($param['offset']) ? (int)$param['offset'] : 0;
  52. $limit = isset($param['limit']) ? (int)$param['limit'] : 20;
  53. // 查询条件组装
  54. $where = [];
  55. $where['role_status'] = ['eq', 1];
  56. if (isset($param['rid']) && strlen($param['rid']) > 0) {
  57. $where['role_rid'] = (int)$param['rid'];
  58. }
  59. if (isset($param['name']) && strlen($param['name']) > 0) {
  60. $where['role_name|role_en'] = ['like', '%' . $this->format_sql_string($param['name']) . '%'];
  61. }
  62. if (isset($param['letter']) && strlen($param['letter']) > 0) {
  63. $where['role_letter'] = strtoupper(substr($this->format_sql_string($param['letter']), 0, 1));
  64. }
  65. if (isset($param['level']) && strlen($param['level']) > 0) {
  66. $where['role_level'] = ['in', $this->format_sql_string($param['level'])];
  67. }
  68. if (isset($param['actor']) && strlen($param['actor']) > 0) {
  69. $where['role_actor'] = ['like', '%' . $this->format_sql_string($param['actor']) . '%'];
  70. }
  71. // 数据获取
  72. $total = model('Role')->countData($where);
  73. $list = [];
  74. if ($total > 0) {
  75. // 排序
  76. $by = 'time';
  77. if (!empty($param['orderby'])) {
  78. $allowBy = ['id', 'time', 'time_add', 'hits', 'hits_day', 'hits_week', 'hits_month', 'score', 'up', 'down', 'level'];
  79. if (in_array($param['orderby'], $allowBy)) {
  80. $by = $param['orderby'];
  81. }
  82. }
  83. $order = 'role_' . $by . ' DESC';
  84. $field = 'role_id,role_name,role_en,role_pic,role_actor,role_remarks,role_content,role_rid,role_letter,role_level,role_hits,role_hits_day,role_hits_week,role_hits_month,role_score,role_time,role_time_add';
  85. $list = Db::name('role')
  86. ->field($field)
  87. ->where($where)
  88. ->order($order)
  89. ->limit($offset, $limit)
  90. ->select();
  91. foreach ($list as &$v) {
  92. $v['role_pic'] = mac_url_img($v['role_pic'] ?? '');
  93. $v['role_link'] = mac_url_role_detail($v);
  94. }
  95. unset($v);
  96. $rids = [];
  97. foreach ($list as $row) {
  98. if (!empty($row['role_rid'])) {
  99. $rids[] = (int)$row['role_rid'];
  100. }
  101. }
  102. $rids = array_unique(array_filter($rids));
  103. $vodTitleMap = [];
  104. if (!empty($rids)) {
  105. $vodTitleMap = Db::name('vod')->where('vod_id', 'in', $rids)->column('vod_name', 'vod_id');
  106. }
  107. foreach ($list as &$v) {
  108. $rid = (int)($v['role_rid'] ?? 0);
  109. $v['vod_name'] = ($rid && isset($vodTitleMap[$rid])) ? $vodTitleMap[$rid] : '';
  110. }
  111. unset($v);
  112. }
  113. return json([
  114. 'code' => 1,
  115. 'msg' => '获取成功',
  116. 'info' => [
  117. 'offset' => $offset,
  118. 'limit' => $limit,
  119. 'total' => $total,
  120. 'rows' => $list,
  121. ],
  122. ]);
  123. }
  124. /**
  125. * 获取角色详情
  126. * GET /api.php/role/get_detail
  127. *
  128. * 参数说明:
  129. * role_id - 必须,角色ID
  130. *
  131. * @param Request $request
  132. * @return \think\response\Json
  133. */
  134. public function get_detail(Request $request)
  135. {
  136. $param = $request->param();
  137. // 参数校验
  138. $validate = validate($request->controller());
  139. if (!$validate->scene($request->action())->check($param)) {
  140. return json([
  141. 'code' => 1001,
  142. 'msg' => '参数错误: ' . $validate->getError(),
  143. ]);
  144. }
  145. $roleId = intval($param['role_id']);
  146. $res = Db::table('mac_role')->where(['role_id' => $roleId])->find();
  147. if (empty($res)) {
  148. return json(['code' => 1001, 'msg' => '数据不存在']);
  149. }
  150. // 处理图片 URL
  151. $res['role_pic'] = mac_url_img($res['role_pic'] ?? '');
  152. $res['role_link'] = mac_url_role_detail($res);
  153. // 关联视频信息
  154. $res['vod_info'] = null;
  155. if (!empty($res['role_rid'])) {
  156. $vodInfo = Db::name('vod')
  157. ->field('vod_id,vod_name,vod_sub,vod_pic,vod_remarks,vod_score,vod_year,vod_area,vod_class,type_id,type_id_1')
  158. ->where('vod_id', intval($res['role_rid']))
  159. ->find();
  160. if ($vodInfo) {
  161. $vodInfo['vod_pic'] = mac_url_img($vodInfo['vod_pic'] ?? '');
  162. $vodInfo['vod_link'] = mac_url_vod_detail($vodInfo);
  163. $res['vod_info'] = $vodInfo;
  164. }
  165. }
  166. // 清理 HTML 标签
  167. if (!empty($res['role_content'])) {
  168. $res['role_content'] = str_replace('mac:', $GLOBALS['config']['upload']['protocol'] . ':', $res['role_content']);
  169. }
  170. return json([
  171. 'code' => 1,
  172. 'msg' => '获取成功',
  173. 'info' => $res,
  174. ]);
  175. }
  176. /**
  177. * 获取推荐角色
  178. * GET /api.php/role/get_recommend
  179. *
  180. * 参数说明:
  181. * rid - 可选,关联视频ID
  182. * num - 可选,数量,默认8
  183. * by - 可选,排序字段,默认 time
  184. * level - 可选,推荐等级
  185. *
  186. * @param Request $request
  187. * @return \think\response\Json
  188. */
  189. public function get_recommend(Request $request)
  190. {
  191. $param = $request->param();
  192. $num = isset($param['num']) ? (int)$param['num'] : 8;
  193. $start = isset($param['start']) ? max(0, (int)$param['start']) : 0;
  194. $by = isset($param['by']) ? trim($param['by']) : 'time';
  195. $rid = isset($param['rid']) ? (int)$param['rid'] : 0;
  196. $level = isset($param['level']) ? trim($param['level']) : '';
  197. $allowBy = ['hits', 'hits_day', 'hits_week', 'hits_month', 'time', 'score'];
  198. if (!in_array($by, $allowBy)) {
  199. $by = 'time';
  200. }
  201. $where = [];
  202. $where['role_status'] = ['eq', 1];
  203. if ($rid > 0) {
  204. $where['role_rid'] = $rid;
  205. }
  206. if (!empty($level)) {
  207. $where['role_level'] = ['in', $level];
  208. }
  209. $list = Db::name('role')
  210. ->field('role_id,role_name,role_en,role_pic,role_actor,role_remarks,role_rid,role_hits,role_hits_month,role_score,role_time')
  211. ->where($where)
  212. ->order('role_' . $by . ' desc')
  213. ->limit($start, $num)
  214. ->select();
  215. foreach ($list as &$v) {
  216. $v['role_pic'] = mac_url_img($v['role_pic'] ?? '');
  217. $v['role_link'] = mac_url_role_detail($v);
  218. }
  219. unset($v);
  220. return json([
  221. 'code' => 1,
  222. 'msg' => '获取成功',
  223. 'info' => [
  224. 'total' => count($list),
  225. 'rows' => $list,
  226. ],
  227. ]);
  228. }
  229. }