Index.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\api\controller;
  3. use ba\Tree;
  4. use Throwable;
  5. use think\facade\Db;
  6. use think\facade\Cookie;
  7. use think\facade\Config;
  8. use app\common\controller\Frontend;
  9. class Index extends Frontend
  10. {
  11. protected array $noNeedLogin = ['index'];
  12. public function initialize(): void
  13. {
  14. parent::initialize();
  15. }
  16. /**
  17. * 前台和会员中心的初始化请求
  18. * @throws Throwable
  19. */
  20. public function index(): void
  21. {
  22. $menus = [];
  23. if ($this->auth->isLogin()) {
  24. $rules = [];
  25. $userMenus = $this->auth->getMenus();
  26. // 首页加载的规则,验权,但过滤掉会员中心菜单
  27. foreach ($userMenus as $item) {
  28. if ($item['type'] == 'menu_dir') {
  29. $menus[] = $item;
  30. } else if ($item['type'] != 'menu') {
  31. $rules[] = $item;
  32. }
  33. }
  34. $rules = array_values($rules);
  35. } else {
  36. // 若是从前台会员中心内发出的请求,要求必须登录,否则会员中心异常
  37. $requiredLogin = $this->request->get('requiredLogin/b', false);
  38. if ($requiredLogin) {
  39. $token = $this->request->server('HTTP_BA_USER_TOKEN', $this->request->request('ba-user-token', Cookie::get('ba-user-token') ?: false));
  40. // 若过期可触发过期判定
  41. $this->auth->init($token);
  42. // 未过期或无token,直接要求登录
  43. $this->error(__('Please login first'), [
  44. 'type' => $this->auth::NEED_LOGIN
  45. ], $this->auth::LOGIN_RESPONSE_CODE);
  46. }
  47. $rules = Db::name('user_rule')
  48. ->where('status', '1')
  49. ->where('no_login_valid', 1)
  50. ->where('type', 'in', ['route', 'nav', 'button'])
  51. ->order('weigh', 'desc')
  52. ->select()
  53. ->toArray();
  54. $rules = Tree::instance()->assembleChild($rules);
  55. }
  56. $this->success('', [
  57. 'site' => [
  58. 'siteName' => get_sys_config('site_name'),
  59. 'recordNumber' => get_sys_config('record_number'),
  60. 'version' => get_sys_config('version'),
  61. 'cdnUrl' => full_url(),
  62. 'upload' => get_upload_config(),
  63. ],
  64. 'openMemberCenter' => Config::get('buildadmin.open_member_center'),
  65. 'userInfo' => $this->auth->getUserInfo(),
  66. 'rules' => $rules,
  67. 'menus' => $menus,
  68. ]);
  69. }
  70. }