Index.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\api\controller;
  3. use ba\Tree;
  4. use Throwable;
  5. use think\facade\Db;
  6. use think\facade\Config;
  7. use app\common\controller\Frontend;
  8. use app\common\library\token\TokenExpirationException;
  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. } elseif ($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 过期异常
  40. try {
  41. $token = get_auth_token(['ba', 'user', 'token']);
  42. $this->auth->init($token);
  43. } catch (TokenExpirationException) {
  44. $this->error(__('Token expiration'), [], 409);
  45. }
  46. $this->error(__('Please login first'), [
  47. 'type' => $this->auth::NEED_LOGIN
  48. ], $this->auth::LOGIN_RESPONSE_CODE);
  49. }
  50. $rules = Db::name('user_rule')
  51. ->where('status', '1')
  52. ->where('no_login_valid', 1)
  53. ->where('type', 'in', ['route', 'nav', 'button'])
  54. ->order('weigh', 'desc')
  55. ->select()
  56. ->toArray();
  57. $rules = Tree::instance()->assembleChild($rules);
  58. }
  59. $this->success('', [
  60. 'site' => [
  61. 'siteName' => get_sys_config('site_name'),
  62. 'recordNumber' => get_sys_config('record_number'),
  63. 'version' => get_sys_config('version'),
  64. 'cdnUrl' => full_url(),
  65. 'upload' => get_upload_config(),
  66. ],
  67. 'openMemberCenter' => Config::get('buildadmin.open_member_center'),
  68. 'userInfo' => $this->auth->getUserInfo(),
  69. 'rules' => $rules,
  70. 'menus' => $menus,
  71. ]);
  72. }
  73. }