taiji_caozhaorui 2 nedēļas atpakaļ
vecāks
revīzija
01edc0d90d

+ 1 - 1
.env.development

@@ -8,7 +8,7 @@ VITE_TITLE = 北之星
 VITE_BASE = 'study-ipad'
 
 # 是否需要登录  Y/N
-VITE_LOGIN_MUST = N
+VITE_LOGIN_MUST = Y
 # token标识
 VITE_TOKEN = siToken
 # 基础代理

+ 1 - 1
.env.production

@@ -8,7 +8,7 @@ VITE_TITLE = 北之星
 VITE_BASE = 'study-ipad'
 
 # 是否需要登录  Y/N
-VITE_LOGIN_MUST = N
+VITE_LOGIN_MUST = Y
 # token标识
 VITE_TOKEN = siToken
 # 基础代理

+ 2 - 0
.gitignore

@@ -22,3 +22,5 @@ dist-ssr
 *.njsproj
 *.sln
 *.sw?
+study-ipad/
+stats.html

+ 132 - 0
src/api/interceptors.ts

@@ -0,0 +1,132 @@
+import axios from 'axios'
+import { ElButton, ElMessage, ElNotification } from 'element-plus'
+import { h } from 'vue'
+import { YMDHms } from '@/utils/czr-util'
+
+// import {toLogin} from "@/utils/permissions";
+export class Interceptors {
+  public instance: any
+
+  constructor() {
+    this.instance = axios.create({
+      baseURL: '', // api base_url
+      timeout: 1000 * 300,
+    })
+    this.initInterceptors()
+  }
+
+  public getInterceptors() {
+    return this.instance
+  }
+
+  public initInterceptors() {
+    /**
+     * 请求拦截器
+     * 每次请求前,如果存在token则在请求头中携带token
+     */
+    this.instance.interceptors.request.use(
+      (config: any) => {
+        if (!config.headers.Authorization) {
+          const token = localStorage.getItem(
+            (import.meta as any).env.VITE_TOKEN,
+          )
+          if (token) {
+            config.headers.Authorization = token
+          } else {
+            // @ts-ignore
+            delete config.headers.Authorization
+          }
+        }
+        return config
+      },
+      (error: any) => {},
+    )
+
+    // 响应拦截器
+    this.instance.interceptors.response.use(
+      // 请求成功
+      (res: any) => {
+        // if (res.data.success) {
+        if (res.status === 200) {
+          return Promise.resolve(res.data)
+        } else {
+          // this.errorHandle(res)
+          return Promise.reject(res.data)
+        }
+      },
+      // 请求失败
+      (error: { response: any }) => {
+        const { response } = error
+        if (response) {
+          // 请求已发出,但是不在2xx的范围
+          // this.errorHandle(response)
+          return Promise.reject(response.data)
+        } else {
+          ElMessage.warning('网络连接异常,请稍后再试!')
+          // 抛出报错信息,在页面里需要接收
+          return Promise.reject(error)
+        }
+      },
+    )
+  }
+
+  private errorHandle(res: any) {
+    const hasLogin = (import.meta as any).env.VITE_LOGIN_MUST === 'Y'
+    if (hasLogin) {
+      console.error('错误接口:' + res.request.responseURL)
+      const exportLog = () => {
+        let str = ''
+        const arr = [
+          `错误日期:${YMDHms(res.headers.date)}`,
+          `请求方式:${res.config.method.toUpperCase()}`,
+          `接口地址:${res.request.responseURL}`,
+          `接口状态:${res.status} ${res.statusText}`,
+          `请求Token:${res.config.headers.Authorization}`,
+          `请求参数:${res.config.data || ''}`,
+          `返回结果:${res.request.response}`,
+        ]
+        arr.forEach((v) => {
+          str += v
+          str += '\n\n'
+        })
+        const blob = new Blob([str], { type: 'text/plain' })
+        const url = URL.createObjectURL(blob)
+        const a = document.createElement('a')
+        a.href = url
+        a.download = `错误日志(${YMDHms(res.headers.date)}).txt`
+        document.body.appendChild(a)
+        a.click()
+        setTimeout(() => {
+          document.body.removeChild(a)
+          URL.revokeObjectURL(url)
+        }, 0)
+      }
+      ElNotification({
+        title: res.data.message,
+        message: h('div', null, [
+          h(
+            ElButton,
+            {
+              type: 'warning',
+              size: 'small',
+              plain: true,
+              onClick: () => exportLog(),
+            },
+            { default: () => '错误日志' },
+          ),
+          h(
+            'div',
+            { class: 'mt-2 overflow-y-auto', style: 'max-height: 50vh;' },
+            res.data.data
+              ? typeof res.data.data === 'string'
+                ? res.data.data
+                : JSON.stringify(res.data.data)
+              : '',
+          ),
+        ]),
+        type: 'error',
+        duration: 0,
+      })
+    }
+  }
+}

+ 5 - 0
src/api/modules/global/login.ts

@@ -0,0 +1,5 @@
+import { get, post } from '@/api/request'
+// 验证码
+export const authCode = () => get(`/auth/code`, {}, {})
+// 登录
+export const loginSubmit = (params) => post(`/login/submit`, params, {})

+ 133 - 0
src/api/request.ts

@@ -0,0 +1,133 @@
+import { Interceptors } from '@/api/interceptors'
+
+const request: any = new Interceptors().getInterceptors()
+
+export const get = (
+  url = '',
+  params: any = {},
+  config: any = {},
+  proxy = '',
+) => {
+  return new Promise((resolve, reject) => {
+    const rUrl = (proxy || (import.meta as any).env.VITE_BASE_API_PROXY) + url
+    request
+      .get(rUrl, {
+        params,
+        ...config,
+      })
+      .then((res: any) => {
+        resolve(res)
+      })
+      .catch((res: any) => {
+        reject(res)
+      })
+  })
+}
+export const del = (
+  url = '',
+  params: any = {},
+  config: any = {},
+  proxy = '',
+) => {
+  return new Promise((resolve, reject) => {
+    const rUrl = (proxy || (import.meta as any).env.VITE_BASE_API_PROXY) + url
+    request
+      .delete(rUrl, {
+        params,
+        ...config,
+      })
+      .then((res: any) => {
+        resolve(res)
+      })
+      .catch((res: any) => {
+        reject(res)
+      })
+  })
+}
+export const post = (
+  url = '',
+  params: any = {},
+  config: any = {},
+  proxy = '',
+) => {
+  return new Promise((resolve, reject) => {
+    const rUrl = (proxy || (import.meta as any).env.VITE_BASE_API_PROXY) + url
+    request
+      .post(rUrl, params, {
+        ...config,
+      })
+      .then((res: any) => {
+        resolve(res)
+      })
+      .catch((res: any) => {
+        reject(res)
+      })
+  })
+}
+export const put = (
+  url = '',
+  params: any = {},
+  config: any = {},
+  proxy = '',
+) => {
+  return new Promise((resolve, reject) => {
+    const rUrl = (proxy || (import.meta as any).env.VITE_BASE_API_PROXY) + url
+    request
+      .put(rUrl, params, {
+        ...config,
+      })
+      .then((res: any) => {
+        resolve(res)
+      })
+      .catch((res: any) => {
+        reject(res)
+      })
+  })
+}
+export const patch = (
+  url = '',
+  params: any = {},
+  config: any = {},
+  proxy = '',
+) => {
+  return new Promise((resolve, reject) => {
+    const rUrl = (proxy || (import.meta as any).env.VITE_BASE_API_PROXY) + url
+    request
+      .patch(rUrl, params, {
+        ...config,
+      })
+      .then((res: any) => {
+        resolve(res)
+      })
+      .catch((res: any) => {
+        reject(res)
+      })
+  })
+}
+export const upload = (
+  url = '',
+  params: any = {},
+  config: any = {},
+  proxy = '',
+  onProcess = (p) => {},
+) => {
+  return new Promise((resolve, reject) => {
+    const rUrl = (proxy || (import.meta as any).env.VITE_BASE_API_PROXY) + url
+    request
+      .post(rUrl, params, {
+        ...config,
+        onUploadProgress: (progressEvent) => {
+          const percent = Math.round(
+            (progressEvent.loaded / progressEvent.total) * 100,
+          )
+          onProcess(percent)
+        },
+      })
+      .then((res: any) => {
+        resolve(res)
+      })
+      .catch((res: any) => {
+        reject(res)
+      })
+  })
+}

+ 9 - 1
src/views/global/login/index.vue

@@ -111,9 +111,11 @@
 </template>
 
 <script setup lang="ts">
-import { reactive, ref } from 'vue'
+import { onMounted, reactive, ref } from 'vue'
 import { useRouter } from 'vue-router'
 import { Notify } from 'quasar'
+import { authCode } from '@/api/modules/global/login'
+import axios from 'axios'
 
 const router = useRouter()
 const state: any = reactive({
@@ -125,6 +127,9 @@ const state: any = reactive({
   loading: false,
 })
 const ref_form = ref()
+const initCode = () => {
+  authCode().then()
+}
 const onLogin = () => {
   if (!state.loading) {
     ref_form.value
@@ -166,6 +171,9 @@ const onLogin = () => {
       })
   }
 }
+onMounted(() => {
+  initCode()
+})
 </script>
 
 <style lang="scss" scoped>

+ 8 - 1
vite.config.ts

@@ -59,7 +59,14 @@ export default defineConfig(({ mode, command }) => {
       host: '0.0.0.0',
       open: true,
       strictPort: false,
-      proxy: {},
+      proxy: {
+        [env.VITE_BASE_API_PROXY]: {
+          // target: 'https://bzx.85coding.com/prod-api/',
+          target: 'https://bzx.85coding.com:8083/prod-api/',
+          changeOrigin: true,
+          rewrite: (path) => path.replace(env.VITE_BASE_API_PROXY, ''),
+        },
+      },
     },
     build: {
       cssTarget: 'chrome83', // 将编译的css版本进行chrome83版本适应