diff --git a/packages/plugins/@nocobase/plugin-mobile/src/client/__e2e__/redirect.test.ts b/packages/plugins/@nocobase/plugin-mobile/src/client/__e2e__/redirect.test.ts new file mode 100644 index 0000000000..49a32e01aa --- /dev/null +++ b/packages/plugins/@nocobase/plugin-mobile/src/client/__e2e__/redirect.test.ts @@ -0,0 +1,36 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import { devices, expect, test } from '@nocobase/test/e2e'; + +test.use({ + ...devices['Galaxy S9+'], +}); + +test.describe('redirect to other page from mobile', () => { + test('redirect to signin page', async ({ page }) => { + const baseURL = process.env.APP_BASE_URL || `http://localhost:${process.env.APP_PORT || 20000}`; + + await page.goto('/m/signin'); + await page.waitForURL(`${baseURL}/signin`); + expect(page.url()).toBe(`${baseURL}/signin`); + + // do not redirect to mobile page + await page.waitForTimeout(5000); + expect(page.url()).toBe(`${baseURL}/signin`); + }); + + test('redirect to admin page', async ({ page }) => { + const baseURL = process.env.APP_BASE_URL || `http://localhost:${process.env.APP_PORT || 20000}`; + + await page.goto('/m/admin/settings/@nocobase/plugin-api-keys'); + await page.waitForURL(`${baseURL}/admin/settings/@nocobase/plugin-api-keys`); + expect(page.url()).toBe(`${baseURL}/admin/settings/@nocobase/plugin-api-keys`); + }); +}); diff --git a/packages/plugins/@nocobase/plugin-mobile/src/client/index.tsx b/packages/plugins/@nocobase/plugin-mobile/src/client/index.tsx index 9c485a5b87..701faf0d6e 100644 --- a/packages/plugins/@nocobase/plugin-mobile/src/client/index.tsx +++ b/packages/plugins/@nocobase/plugin-mobile/src/client/index.tsx @@ -179,22 +179,26 @@ export class PluginMobileClient extends Plugin { Component: 'MobileHomePage', }); - // 跳转到主应用的登录页 + // redirect to main app signin page + // e.g. /m/signin => /signin this.mobileRouter.add('signin', { path: '/signin', Component: () => { window.location.href = window.location.href - .replace(this.mobilePath, '') + .replace(this.mobilePath + '/', '/') .replace('redirect=', `redirect=${this.mobilePath}`); return null; }, }); - // 跳转到主应用的页面 + // redirect to main app admin page + // e.g. /m/admin/xxx => /admin/xxx this.mobileRouter.add('admin', { path: `/admin/*`, Component: () => { - window.location.replace(window.location.href.replace(this.mobilePath, '')); + if (window.location.pathname.startsWith(this.mobilePath + '/')) { + window.location.replace(window.location.href.replace(this.mobilePath + '/', '/')); + } return null; }, });