1
0

Auth.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. /**
  6. * Auth 认证与权限控制器
  7. *
  8. * 提供当前用户状态和资源级权限判断接口,
  9. * 用于前端全站 AJAX 化时统一做登录态、VIP态、可播/可读/可下载判断。
  10. */
  11. class Auth extends Base
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. }
  17. /**
  18. * 获取当前登录用户基础信息
  19. * GET /api.php/auth/me
  20. *
  21. * 用途:前端全局状态、头像、昵称、会员到期等。
  22. * 前端调用时机:页面首屏初始化时调用一次;登录成功、退出登录后各再调一次。
  23. * 未登录也返回 code=1,但 info.is_login=0(不返回 HTML 错页)。
  24. *
  25. * @param Request $request
  26. * @return \think\response\Json
  27. */
  28. public function me(Request $request)
  29. {
  30. $check = model('User')->checkLogin();
  31. if ($check['code'] > 1) {
  32. // 未登录,返回稳定结构
  33. return json([
  34. 'code' => 1,
  35. 'msg' => 'ok',
  36. 'info' => [
  37. 'is_login' => 0,
  38. 'user_id' => 0,
  39. 'user_name' => '',
  40. 'nick_name' => '',
  41. 'group_id' => 0,
  42. 'group_name' => '',
  43. 'points' => 0,
  44. 'user_portrait' => '',
  45. 'vip_expire_time' => 0,
  46. ],
  47. ]);
  48. }
  49. $user = $check['info'];
  50. $groupName = '';
  51. if (!empty($user['group'])) {
  52. $groupName = $user['group']['group_name'] ?? '';
  53. }
  54. // 获取主要 group_id(取第一个)
  55. $groupIds = explode(',', $user['group_id']);
  56. $primaryGroupId = intval($groupIds[0]);
  57. return json([
  58. 'code' => 1,
  59. 'msg' => 'ok',
  60. 'info' => [
  61. 'is_login' => 1,
  62. 'user_id' => intval($user['user_id']),
  63. 'user_name' => $user['user_name'] ?? '',
  64. 'nick_name' => $user['user_nick_name'] ?? '',
  65. 'group_id' => $primaryGroupId,
  66. 'group_name' => $groupName,
  67. 'points' => intval($user['user_points'] ?? 0),
  68. 'user_portrait' => mac_get_user_portrait($user['user_id']),
  69. 'vip_expire_time' => intval($user['user_end_time'] ?? 0),
  70. ],
  71. ]);
  72. }
  73. /**
  74. * 按"用户 + 资源"返回可操作权限位
  75. * GET /api.php/auth/permission
  76. *
  77. * 用途:用于按钮显示和点击拦截。
  78. *
  79. * 入参:
  80. * mid - 资源模块(1=视频 / 2=文章 / 6=漫画)
  81. * id - 资源 id(vod_id / art_id / manga_id)
  82. * action - 可选,play|read|download|comment|favorite(不传则返回全部权限位)
  83. *
  84. * @param Request $request
  85. * @return \think\response\Json
  86. */
  87. public function permission(Request $request)
  88. {
  89. $param = $request->param();
  90. $mid = intval($param['mid'] ?? 0);
  91. $id = intval($param['id'] ?? 0);
  92. $action = trim($param['action'] ?? '');
  93. // 基础参数验证
  94. if ($mid < 1 || $id < 1) {
  95. return json([
  96. 'code' => 1001,
  97. 'msg' => '参数错误: mid 和 id 必须',
  98. ]);
  99. }
  100. // 允许的 action 值
  101. $allowedActions = ['play', 'read', 'download', 'comment', 'favorite'];
  102. if (!empty($action) && !in_array($action, $allowedActions)) {
  103. return json([
  104. 'code' => 1001,
  105. 'msg' => '参数错误: action 值无效',
  106. ]);
  107. }
  108. // 1. 读取当前登录用户
  109. $check = model('User')->checkLogin();
  110. $isLogin = ($check['code'] == 1) ? 1 : 0;
  111. $user = $isLogin ? $check['info'] : null;
  112. // 判断是否为 VIP(group_id >= 3 且未过期)
  113. $isVip = 0;
  114. $userPoints = 0;
  115. $userGroupIds = [];
  116. if ($isLogin && $user) {
  117. $userPoints = intval($user['user_points'] ?? 0);
  118. $userGroupIds = array_map('intval', explode(',', $user['group_id']));
  119. if (max($userGroupIds) >= 3 && intval($user['user_end_time'] ?? 0) > time()) {
  120. $isVip = 1;
  121. }
  122. }
  123. // 2. 按 mid + id 查询资源基础信息
  124. $resource = null;
  125. $resourcePoints = 0;
  126. $resourceStatus = 0;
  127. $denyReason = '';
  128. switch ($mid) {
  129. case 1: // 视频
  130. $resource = Db::name('vod')->field('vod_id,vod_status,vod_points_play,vod_points_down,vod_pwd_play,vod_pwd_down,type_id')->where('vod_id', $id)->find();
  131. if ($resource) {
  132. $resourceStatus = intval($resource['vod_status'] ?? 0);
  133. $resourcePoints = intval($resource['vod_points_play'] ?? 0);
  134. }
  135. break;
  136. case 2: // 文章
  137. $resource = Db::name('art')->field('art_id,art_status,art_points,art_points_detail,type_id')->where('art_id', $id)->find();
  138. if ($resource) {
  139. $resourceStatus = intval($resource['art_status'] ?? 0);
  140. // 优先 art_points_detail(详情阅读积分),回退 art_points
  141. $detailPoints = intval($resource['art_points_detail'] ?? 0);
  142. $resourcePoints = $detailPoints > 0 ? $detailPoints : intval($resource['art_points'] ?? 0);
  143. }
  144. break;
  145. case 6: // 漫画
  146. $resource = Db::name('manga')->field('manga_id,manga_status,manga_points,type_id')->where('manga_id', $id)->find();
  147. if ($resource) {
  148. $resourceStatus = intval($resource['manga_status'] ?? 0);
  149. $resourcePoints = intval($resource['manga_points'] ?? 0);
  150. }
  151. break;
  152. default:
  153. return json([
  154. 'code' => 1001,
  155. 'msg' => '参数错误: mid 不支持',
  156. ]);
  157. }
  158. // 资源不存在
  159. if (empty($resource)) {
  160. return json([
  161. 'code' => 1,
  162. 'msg' => 'ok',
  163. 'info' => [
  164. 'is_login' => $isLogin,
  165. 'is_vip' => $isVip,
  166. 'resource' => ['mid' => $mid, 'id' => $id],
  167. 'can_play' => 0,
  168. 'can_read' => 0,
  169. 'can_download' => 0,
  170. 'can_comment' => 0,
  171. 'can_favorite' => 0,
  172. 'deny_reason' => 'RESOURCE_NOT_FOUND',
  173. ],
  174. ]);
  175. }
  176. // 资源下线
  177. if ($resourceStatus != 1) {
  178. return json([
  179. 'code' => 1,
  180. 'msg' => 'ok',
  181. 'info' => [
  182. 'is_login' => $isLogin,
  183. 'is_vip' => $isVip,
  184. 'resource' => ['mid' => $mid, 'id' => $id],
  185. 'can_play' => 0,
  186. 'can_read' => 0,
  187. 'can_download' => 0,
  188. 'can_comment' => 0,
  189. 'can_favorite' => 0,
  190. 'deny_reason' => 'RESOURCE_OFFLINE',
  191. ],
  192. ]);
  193. }
  194. // 3. 结合用户会员组/VIP有效期/积分,计算 can_* 权限位
  195. // 是否需要 VIP(检查分类的 type_is_vip_exclusive)
  196. $typeId = intval($resource['type_id'] ?? 0);
  197. $typeIsVipExclusive = 0;
  198. if ($typeId > 0) {
  199. $typeInfo = Db::name('type')->field('type_id,type_extend')->where('type_id', $typeId)->find();
  200. if ($typeInfo && !empty($typeInfo['type_extend'])) {
  201. $typeExtend = json_decode($typeInfo['type_extend'], true);
  202. if (is_array($typeExtend)) {
  203. $typeIsVipExclusive = intval($typeExtend['type_is_vip_exclusive'] ?? 0);
  204. }
  205. }
  206. }
  207. $canPlay = 1;
  208. $canRead = 1;
  209. $canDownload = 1;
  210. $canComment = 1;
  211. $canFavorite = 1;
  212. // 未登录限制
  213. if (!$isLogin) {
  214. $canComment = 0;
  215. $canFavorite = 0;
  216. $denyReason = 'NOT_LOGIN';
  217. // 需要积分的资源未登录不可用
  218. if ($resourcePoints > 0) {
  219. $canPlay = 0;
  220. $canRead = 0;
  221. $canDownload = 0;
  222. }
  223. // VIP 专属资源未登录不可用
  224. if ($typeIsVipExclusive) {
  225. $canPlay = 0;
  226. $canRead = 0;
  227. $canDownload = 0;
  228. $denyReason = 'VIP_REQUIRED';
  229. }
  230. } else {
  231. // 已登录
  232. // VIP 专属资源检查
  233. if ($typeIsVipExclusive && !$isVip) {
  234. $canPlay = 0;
  235. $canRead = 0;
  236. $canDownload = 0;
  237. $denyReason = 'VIP_REQUIRED';
  238. }
  239. // 积分检查(VIP 用户免积分)
  240. if ($resourcePoints > 0 && !$isVip) {
  241. if ($userPoints < $resourcePoints) {
  242. $canPlay = 0;
  243. $canRead = 0;
  244. $denyReason = 'POINTS_NOT_ENOUGH';
  245. }
  246. }
  247. // 下载积分检查(视频模块有单独的 vod_points_down)
  248. if ($mid == 1 && !$isVip) {
  249. $downPoints = intval($resource['vod_points_down'] ?? 0);
  250. if ($downPoints > 0 && $userPoints < $downPoints) {
  251. $canDownload = 0;
  252. if (empty($denyReason)) {
  253. $denyReason = 'POINTS_NOT_ENOUGH';
  254. }
  255. }
  256. }
  257. // 用户组权限检查(通过 popedom)
  258. if ($typeId > 0) {
  259. $groupIdStr = $user['group_id'] ?? '1';
  260. // popedom 2=播放/阅读, 3=下载
  261. if (!model('User')->popedom($typeId, 2, $groupIdStr)) {
  262. $canPlay = 0;
  263. $canRead = 0;
  264. if (empty($denyReason)) {
  265. $denyReason = 'GROUP_PERMISSION_DENIED';
  266. }
  267. }
  268. if (!model('User')->popedom($typeId, 3, $groupIdStr)) {
  269. $canDownload = 0;
  270. if (empty($denyReason)) {
  271. $denyReason = 'GROUP_PERMISSION_DENIED';
  272. }
  273. }
  274. }
  275. }
  276. // 如果全部可用,清空 deny_reason
  277. if ($canPlay && $canRead && $canDownload && $canComment && $canFavorite) {
  278. $denyReason = '';
  279. }
  280. // 构建结果
  281. $result = [
  282. 'is_login' => $isLogin,
  283. 'is_vip' => $isVip,
  284. 'resource' => ['mid' => $mid, 'id' => $id],
  285. 'can_play' => $canPlay,
  286. 'can_read' => $canRead,
  287. 'can_download' => $canDownload,
  288. 'can_comment' => $canComment,
  289. 'can_favorite' => $canFavorite,
  290. 'deny_reason' => $denyReason,
  291. ];
  292. // 如果指定了 action,只返回对应权限位
  293. if (!empty($action)) {
  294. $actionMap = [
  295. 'play' => 'can_play',
  296. 'read' => 'can_read',
  297. 'download' => 'can_download',
  298. 'comment' => 'can_comment',
  299. 'favorite' => 'can_favorite',
  300. ];
  301. $key = $actionMap[$action] ?? '';
  302. if ($key) {
  303. $filtered = [
  304. 'is_login' => $isLogin,
  305. 'is_vip' => $isVip,
  306. 'resource' => ['mid' => $mid, 'id' => $id],
  307. $key => $result[$key],
  308. 'deny_reason' => $result[$key] ? '' : $denyReason,
  309. ];
  310. $result = $filtered;
  311. }
  312. }
  313. return json([
  314. 'code' => 1,
  315. 'msg' => 'ok',
  316. 'info' => $result,
  317. ]);
  318. }
  319. }