From a556457abab5e6057e0d8e2cc0a0a075726f0469 Mon Sep 17 00:00:00 2001 From: ximplez Date: Sat, 30 Mar 2024 23:11:22 +0800 Subject: [PATCH] sso login --- .env | 6 +- .env.prod | 2 +- .env.test | 2 +- .github/workflows/release.yml | 20 ++++ build/plugins/router.ts | 2 +- docker/Dockerfile | 17 ++++ docker/nginx.conf | 50 ++++++++++ packages/axios/src/type.ts | 10 ++ src/constants/app.ts | 2 + src/enum/index.ts | 4 + src/hooks/common/router.ts | 2 +- .../global-header/components/user-avatar.vue | 38 ++++---- src/locales/langs/en-us.ts | 3 + src/locales/langs/zh-cn.ts | 3 + src/router/elegant/routes.ts | 2 +- src/router/elegant/transform.ts | 6 +- src/router/guard/route.ts | 13 ++- src/service/api/index.ts | 1 + src/service/api/sso.ts | 31 ++++++ src/service/request/action-type.ts | 7 ++ src/service/request/action.ts | 24 +++++ src/service/request/index.ts | 82 +++++++++++++--- src/store/modules/auth/index.ts | 96 ++++++++++++++++++- src/typings/api.d.ts | 3 + src/typings/app.d.ts | 3 + src/typings/elegant-router.d.ts | 10 +- src/typings/storage.d.ts | 1 + src/typings/union-key.d.ts | 9 +- src/views/_builtin/login/index.vue | 36 +++---- .../_builtin/login/modules/code-login.vue | 60 ++++++------ .../_builtin/login/modules/pwd-login.vue | 93 +++++++++--------- src/views/_builtin/login/modules/register.vue | 92 +++++++++--------- .../_builtin/login/modules/reset-pwd.vue | 78 +++++++-------- .../_builtin/login/modules/sso-callback.vue | 68 +++++++++++++ .../_builtin/login/modules/sso-login.vue | 43 +++++++++ 35 files changed, 688 insertions(+), 231 deletions(-) create mode 100644 docker/Dockerfile create mode 100644 docker/nginx.conf create mode 100644 src/service/api/sso.ts create mode 100644 src/service/request/action-type.ts create mode 100644 src/service/request/action.ts create mode 100644 src/views/_builtin/login/modules/sso-callback.vue create mode 100644 src/views/_builtin/login/modules/sso-login.vue diff --git a/.env b/.env index a9f85cf7..1c752984 100644 --- a/.env +++ b/.env @@ -1,8 +1,8 @@ VITE_BASE_URL=/ -VITE_APP_TITLE=SoybeanAdmin +VITE_APP_TITLE=COCO -VITE_APP_DESC=SoybeanAdmin is a fresh and elegant admin template +VITE_APP_DESC=COCO is a Config Center # the prefix of the icon name VITE_ICON_PREFIX=icon @@ -27,7 +27,7 @@ VITE_HTTP_PROXY=Y VITE_ROUTER_HISTORY_MODE=history # success code of backend service, when the code is received, the request is successful -VITE_SERVICE_SUCCESS_CODE=0000 +VITE_SERVICE_SUCCESS_CODE=0 # logout codes of backend service, when the code is received, the user will be logged out and redirected to login page VITE_SERVICE_LOGOUT_CODES=8888,8889 diff --git a/.env.prod b/.env.prod index 832d4d6c..71b4e8a9 100644 --- a/.env.prod +++ b/.env.prod @@ -1,5 +1,5 @@ # backend service base url, prod environment -VITE_SERVICE_BASE_URL=https://mock.apifox.com/m1/3109515-0-default +VITE_SERVICE_BASE_URL=https://api.959617.xyz/coco # other backend service base url, prod environment VITE_OTHER_SERVICE_BASE_URL= `{ diff --git a/.env.test b/.env.test index 0f3c35ce..180880aa 100644 --- a/.env.test +++ b/.env.test @@ -1,5 +1,5 @@ # backend service base url, test environment -VITE_SERVICE_BASE_URL=https://mock.apifox.com/m1/3109515-0-default +VITE_SERVICE_BASE_URL=http://127.0.0.1:8000 # other backend service base url, test environment VITE_OTHER_SERVICE_BASE_URL= `{ diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0bf7c92d..52127c6e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,3 +23,23 @@ jobs: - run: npx githublogen env: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - uses: softprops/action-gh-release@v2 + + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@latest + - name: Login to Docker Hub + uses: docker/login-action@latest + with: + registry: ghcr.io + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + - name: Build and push Docker image + uses: docker/build-push-action@latest + with: + context: . + push: true + tags: v1.0.0 diff --git a/build/plugins/router.ts b/build/plugins/router.ts index 952e15d7..e9e060cc 100644 --- a/build/plugins/router.ts +++ b/build/plugins/router.ts @@ -15,7 +15,7 @@ export function setupElegantRouter() { const key = routeName as RouteKey; if (key === 'login') { - const modules: UnionKey.LoginModule[] = ['pwd-login', 'code-login', 'register', 'reset-pwd', 'bind-wechat']; + const modules: UnionKey.LoginModule[] = ['pwd-login', 'code-login', 'register', 'reset-pwd', 'bind-wechat', 'sso-login', 'sso-callback']; const moduleReg = modules.join('|'); diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 00000000..3ba16509 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,17 @@ +# 安装Node.js +FROM node:20-alpine3.17 as build + +WORKDIR /app +COPY .. /app + +RUN npm install -g pnpm \ + && pnpm install \ + && pnpm run build + +FROM nginx:stable-alpine +COPY --from=build /app/dist /usr/share/nginx/html/ +# COPY nginx.conf /etc/nginx/conf.d/default.conf +COPY nginx.conf /etc/nginx/conf.d/ +EXPOSE 80 +CMD ["nginx","-g","daemon off;"] + diff --git a/docker/nginx.conf b/docker/nginx.conf new file mode 100644 index 00000000..a641c298 --- /dev/null +++ b/docker/nginx.conf @@ -0,0 +1,50 @@ +server { + listen 80; + server_name localhost; + + client_max_body_size 100m; + client_body_buffer_size 128k; + proxy_connect_timeout 5; + proxy_send_timeout 1800; + proxy_read_timeout 1800; + proxy_buffer_size 4k; + proxy_buffers 4 32k; + proxy_busy_buffers_size 64k; + proxy_temp_file_write_size 64k; + auth_basic "status"; + #开启gzip + gzip on; + #低于1kb的资源不压缩 + gzip_min_length 1k; + #压缩级别1-9,越大压缩率越高,同时消耗cpu资源也越多,建议设置在5左右。 + gzip_comp_level 5; + #需要压缩哪些响应类型的资源,多个空格隔开。不建议压缩图片. + gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css; + #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持) + gzip_disable "MSIE [1-6]\."; + #是否添加“Vary: Accept-Encoding”响应头 + gzip_vary on; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; #VUE项目,配置路由(必须) + } + + location ^~ /assessh5 { + alias /usr/share/nginx/html; # inflow uni-app H5编译文件的目录,index.html所在目录 + try_files $uri $uri/ /index.html last; + index index.html index.htm; + } + +# location /inflow { +# try_files $uri $uri/ /inflow/index.html; +# root /usr/share/nginx/html/inflow/; +# index index.html; +# } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } +} diff --git a/packages/axios/src/type.ts b/packages/axios/src/type.ts index 8a4461c2..a83a657b 100644 --- a/packages/axios/src/type.ts +++ b/packages/axios/src/type.ts @@ -35,12 +35,14 @@ export interface RequestOption { response: AxiosResponse, instance: AxiosInstance ) => Promise | Promise; + /** * transform backend response when the responseType is json * * @param response Axios response */ transformBackendResponse(response: AxiosResponse): any | Promise; + /** * The hook to handle error * @@ -51,6 +53,13 @@ export interface RequestOption { onError: (error: AxiosError) => void | Promise; } +export interface ErrorCodeHandle { + handle: ( + response: AxiosResponse, + instance: AxiosInstance + ) => Promise | Promise; +} + interface ResponseMap { blob: Blob; text: string; @@ -58,6 +67,7 @@ interface ResponseMap { stream: ReadableStream; document: Document; } + export type ResponseType = keyof ResponseMap | 'json'; export type MappedType = R extends keyof ResponseMap diff --git a/src/constants/app.ts b/src/constants/app.ts index ca4f9276..37b06cf3 100644 --- a/src/constants/app.ts +++ b/src/constants/app.ts @@ -10,6 +10,8 @@ export const themeSchemaOptions = transformRecordToOption(themeSchemaRecord); export const loginModuleRecord: Record = { 'pwd-login': 'page.login.pwdLogin.title', + 'sso-login': 'page.login.ssoLogin.title', + 'sso-callback': 'page.login.ssoLogin.title', 'code-login': 'page.login.codeLogin.title', register: 'page.login.register.title', 'reset-pwd': 'page.login.resetPwd.title', diff --git a/src/enum/index.ts b/src/enum/index.ts index 2739b3a4..e3536e63 100644 --- a/src/enum/index.ts +++ b/src/enum/index.ts @@ -5,3 +5,7 @@ export enum SetupStoreId { Route = 'route-store', Tab = 'tab-store' } + +export enum SsoAuthor { + Authing = 'authing' +} diff --git a/src/hooks/common/router.ts b/src/hooks/common/router.ts index bc5277d7..a7ffc2d6 100644 --- a/src/hooks/common/router.ts +++ b/src/hooks/common/router.ts @@ -52,7 +52,7 @@ export function useRouterPush(inSetup = true) { * @param redirectUrl The redirect url, if not specified, it will be the current route fullPath */ async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) { - const module = loginModule || 'pwd-login'; + const module = loginModule || 'sso-login'; const options: RouterPushOptions = { params: { diff --git a/src/layouts/modules/global-header/components/user-avatar.vue b/src/layouts/modules/global-header/components/user-avatar.vue index 24ef9892..577ce62f 100644 --- a/src/layouts/modules/global-header/components/user-avatar.vue +++ b/src/layouts/modules/global-header/components/user-avatar.vue @@ -1,18 +1,18 @@ diff --git a/src/views/_builtin/login/modules/pwd-login.vue b/src/views/_builtin/login/modules/pwd-login.vue index db6ee3e0..9b756fa9 100644 --- a/src/views/_builtin/login/modules/pwd-login.vue +++ b/src/views/_builtin/login/modules/pwd-login.vue @@ -1,18 +1,17 @@ diff --git a/src/views/_builtin/login/modules/register.vue b/src/views/_builtin/login/modules/register.vue index 95b19217..cc93c421 100644 --- a/src/views/_builtin/login/modules/register.vue +++ b/src/views/_builtin/login/modules/register.vue @@ -1,17 +1,17 @@ diff --git a/src/views/_builtin/login/modules/reset-pwd.vue b/src/views/_builtin/login/modules/reset-pwd.vue index fb8dff69..b6a30760 100644 --- a/src/views/_builtin/login/modules/reset-pwd.vue +++ b/src/views/_builtin/login/modules/reset-pwd.vue @@ -1,15 +1,15 @@ diff --git a/src/views/_builtin/login/modules/sso-callback.vue b/src/views/_builtin/login/modules/sso-callback.vue new file mode 100644 index 00000000..d9431692 --- /dev/null +++ b/src/views/_builtin/login/modules/sso-callback.vue @@ -0,0 +1,68 @@ + + + + + + + diff --git a/src/views/_builtin/login/modules/sso-login.vue b/src/views/_builtin/login/modules/sso-login.vue new file mode 100644 index 00000000..11471c76 --- /dev/null +++ b/src/views/_builtin/login/modules/sso-login.vue @@ -0,0 +1,43 @@ + + + + +