Index.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. 'version' => get_sys_config('version'),
  63. 'cdnUrl' => full_url(),
  64. 'upload' => keys_to_camel_case(get_upload_config(), ['max_size', 'save_name', 'allowed_suffixes', 'allowed_mime_types']),
  65. 'recordNumber' => get_sys_config('record_number'),
  66. 'cdnUrlParams' => Config::get('buildadmin.cdn_url_params'),
  67. ],
  68. 'openMemberCenter' => Config::get('buildadmin.open_member_center'),
  69. 'userInfo' => $this->auth->getUserInfo(),
  70. 'rules' => $rules,
  71. 'menus' => $menus,
  72. ]);
  73. }
  74. }