Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
78c25497d6 | ||
![]() |
4a09a203c4 | ||
![]() |
164aa40738 | ||
![]() |
3bae9d7761 | ||
![]() |
759aa3fcdf | ||
![]() |
bb4422e36b | ||
![]() |
2c035c7441 | ||
![]() |
a36fa86d8d | ||
![]() |
66f19d8ef1 | ||
![]() |
560e1bab5b | ||
![]() |
be1307db94 | ||
![]() |
a6c879ce09 | ||
![]() |
ff14f46fe0 | ||
![]() |
03ea4759af |
@@ -18,7 +18,7 @@
|
||||
"guzzlehttp/guzzle": "^7.2",
|
||||
"laravel/framework": "^10.0",
|
||||
"laravel/tinker": "^2.8",
|
||||
"catchadmin/core": "^0.1.14"
|
||||
"catchadmin/core": "^0.2.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"fakerphp/faker": "^1.9.1",
|
||||
|
@@ -8,7 +8,6 @@ use Exception;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Develop\Support\Generate\Create\Schema;
|
||||
use Illuminate\Support\Facades\Schema as SchemaFacade;
|
||||
|
||||
class Schemas extends CatchModel
|
||||
{
|
||||
@@ -108,22 +107,4 @@ class Schemas extends CatchModel
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete
|
||||
*
|
||||
* @param $id
|
||||
* @param bool $force
|
||||
* @return bool|null
|
||||
*/
|
||||
public function deleteBy($id, bool $force = false): ?bool
|
||||
{
|
||||
$schema = parent::firstBy($id);
|
||||
|
||||
if ($schema->delete()) {
|
||||
SchemaFacade::dropIfExists($schema->name);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -13,6 +13,8 @@ return new class extends Migration
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
if (Schema::hasTable('{table}')) { return; }
|
||||
|
||||
Schema::{method}('{table}', function (Blueprint $table) {
|
||||
{content}
|
||||
});
|
||||
|
@@ -22,6 +22,16 @@
|
||||
required: true,
|
||||
message: '模块名称必须填写',
|
||||
},
|
||||
{
|
||||
validator: (rule: any, value: any, callback: any) => {
|
||||
if (! /^[A-Za-z]+$/.test(value)) {
|
||||
callback('模块名称只允许大小字母组合')
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
},
|
||||
trigger: 'blur',
|
||||
},
|
||||
]"
|
||||
>
|
||||
<el-input v-model="formData.title" />
|
||||
|
@@ -64,7 +64,7 @@ const schemaVisible = ref<boolean>(false)
|
||||
const api = 'schema'
|
||||
|
||||
const { data, query, search, reset, loading } = useGetList(api)
|
||||
const { destroy, deleted } = useDestroy('确认删除吗? 将会删除数据库的 Schema,请提前做好备份,一旦删除,将无法恢复!')
|
||||
const { destroy, deleted } = useDestroy('确认删除吗? 删除后数据表将会保留,如需删除相关表,请手动进行删除!')
|
||||
const { open, close, title, visible, id } = useOpen()
|
||||
|
||||
const tableData = computed(() => data.value?.data)
|
||||
|
@@ -41,11 +41,9 @@ class RolesController extends Controller
|
||||
public function store(RoleRequest $request)
|
||||
{
|
||||
$data = $request->all();
|
||||
|
||||
if ($request->get('data_range') && ! DataRange::Personal_Choose->assert($data['data_range'])) {
|
||||
$data['data_range'] = (int) $data['data_range'];
|
||||
if (!$data['data_range'] || !DataRange::Personal_Choose->assert($data['data_range'])) {
|
||||
$data['departments'] = [];
|
||||
} else {
|
||||
$data['data_range'] = 0;
|
||||
}
|
||||
|
||||
return $this->model->storeBy($data);
|
||||
@@ -80,11 +78,9 @@ class RolesController extends Controller
|
||||
public function update($id, RoleRequest $request)
|
||||
{
|
||||
$data = $request->all();
|
||||
|
||||
if ($request->get('data_range') && ! DataRange::Personal_Choose->assert($data['data_range'])) {
|
||||
$data['data_range'] = (int) $data['data_range'];
|
||||
if (!$data['data_range'] || !DataRange::Personal_Choose->assert($data['data_range'])) {
|
||||
$data['departments'] = [];
|
||||
} else {
|
||||
$data['data_range'] = 0;
|
||||
}
|
||||
|
||||
return $this->model->updateBy($id, $data);
|
||||
|
@@ -159,4 +159,16 @@ class UserController extends Controller
|
||||
return $builder;
|
||||
})->getList();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
return User::query()
|
||||
->select('id', 'username', 'email', 'created_at')
|
||||
->without('roles')
|
||||
->get()
|
||||
->download(['id', '昵称', '邮箱', '创建时间']);
|
||||
}
|
||||
}
|
||||
|
@@ -58,16 +58,16 @@ trait UserRelations
|
||||
|
||||
/* @var Permissions $permissionsModel */
|
||||
$permissionsModel = app($this->getPermissionsModel());
|
||||
|
||||
if ($this->isSuperAdmin()) {
|
||||
$permissions = $permissionsModel->get();
|
||||
} else {
|
||||
$permissions = Collection::make();
|
||||
$permissionIds = Collection::make();
|
||||
$this->roles()->with('permissions')->get()
|
||||
->each(function ($role) use (&$permissions) {
|
||||
$permissions = $permissions->concat($role->permissions);
|
||||
->each(function ($role) use (&$permissionIds) {
|
||||
$permissionIds = $permissionIds->concat($role->permissions?->pluck('id'));
|
||||
});
|
||||
$permissions = $permissions->unique();
|
||||
|
||||
$permissions = $permissionsModel->whereIn('id', $permissionIds->unique())->get();
|
||||
}
|
||||
|
||||
$this->setAttribute('permissions', $permissions->each(fn ($permission) => $permission->setAttribute('hidden', $permission->isHidden())));
|
||||
|
@@ -96,7 +96,7 @@ class User extends Model implements AuthenticatableContract
|
||||
*/
|
||||
public function updateBy($id, array $data): mixed
|
||||
{
|
||||
if (isset($data['password']) && ! $data['password']) {
|
||||
if (empty($data['password'])) {
|
||||
unset($data['password']);
|
||||
}
|
||||
|
||||
|
@@ -14,4 +14,8 @@ Route::put('users/enable/{id}', [UserController::class, 'enable']);
|
||||
Route::match(['post', 'get'], 'user/online', [UserController::class, 'online']);
|
||||
Route::get('user/login/log', [UserController::class, 'loginLog']);
|
||||
Route::get('user/operate/log', [UserController::class, 'operateLog']);
|
||||
Route::get('user/operate/log', [UserController::class, 'operateLog']);
|
||||
Route::get('user/export', [UserController::class, 'export']);
|
||||
|
||||
|
||||
|
||||
|
@@ -16,7 +16,11 @@
|
||||
</template>
|
||||
</Search>
|
||||
<div class="table-default">
|
||||
<Operate :show="open" />
|
||||
<Operate :show="open">
|
||||
<template #operate>
|
||||
<el-button @click="download('/user')">导出</el-button>
|
||||
</template>
|
||||
</Operate>
|
||||
<el-table :data="tableData" class="mt-3" v-loading="loading">
|
||||
<el-table-column prop="username" label="用户名" width="150" />
|
||||
<el-table-column prop="avatar" label="头像">
|
||||
@@ -61,6 +65,7 @@ import Department from './components/department.vue'
|
||||
import { useUserStore } from '/admin/stores/modules/user'
|
||||
import { isUndefined } from '/admin/support/helper'
|
||||
import { UserFilled } from '@element-plus/icons-vue'
|
||||
import { useExcelDownload } from '/resources/admin/composables/curd/useExcelDownload'
|
||||
|
||||
const userStore = useUserStore()
|
||||
|
||||
@@ -68,6 +73,7 @@ const api = 'users'
|
||||
const { data, query, search, reset, loading } = useGetList(api)
|
||||
const { destroy, deleted } = useDestroy()
|
||||
const { open, close, title, visible, id } = useOpen()
|
||||
const { download } = useExcelDownload()
|
||||
|
||||
const tableData = computed(() => data.value?.data)
|
||||
|
||||
|
50
resources/admin/composables/curd/useExcelDownload.ts
Normal file
50
resources/admin/composables/curd/useExcelDownload.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import Request from '/admin/support/request'
|
||||
import { ref, watch } from 'vue'
|
||||
import Message from '/admin/support/message'
|
||||
|
||||
export function useExcelDownload() {
|
||||
const http = new Request()
|
||||
const isSuccess = ref(false)
|
||||
const loading = ref<boolean>(false)
|
||||
const afterDownload = ref()
|
||||
function download(path: string, data: object = {}) {
|
||||
loading.value = true
|
||||
http
|
||||
.setResponseType('blob')
|
||||
.init()
|
||||
.get(path + '/export', data)
|
||||
.then(r => {
|
||||
if (r.headers['content-type'] === 'application/json') {
|
||||
const blob = new Blob([r.data], { type: r.headers['content-type'] })
|
||||
const blobReader = new Response(blob).json()
|
||||
blobReader.then(res => {
|
||||
if (res.code === 1e4) {
|
||||
Message.success(res.message)
|
||||
} else {
|
||||
Message.error(res.message)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
const downloadLink = document.createElement('a')
|
||||
const blob = new Blob([r.data], { type: r.headers['content-type'] })
|
||||
downloadLink.href = URL.createObjectURL(blob)
|
||||
downloadLink.download = r.headers.filename
|
||||
document.body.appendChild(downloadLink)
|
||||
downloadLink.click()
|
||||
document.body.removeChild(downloadLink)
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
loading.value = false
|
||||
})
|
||||
}
|
||||
|
||||
const success = (func: Function) => {
|
||||
watch(isSuccess, function () {
|
||||
isSuccess.value = false
|
||||
func()
|
||||
})
|
||||
}
|
||||
|
||||
return { download, success, loading, afterDownload }
|
||||
}
|
@@ -13,13 +13,16 @@
|
||||
<el-icon>
|
||||
<Icon :name="menu?.meta?.icon" v-if="menu?.meta?.icon" class="text-sm" />
|
||||
</el-icon>
|
||||
<span>{{ menu?.meta?.title }}</span>
|
||||
<span v-if="menu?.path.indexOf('https://') !== -1 || menu?.path.indexOf('http://') !== -1">
|
||||
<span @click="openUrl(menu?.path as string)">{{ menu?.meta?.title }}</span>
|
||||
</span>
|
||||
<span v-else>{{ menu?.meta?.title }}</span>
|
||||
</el-menu-item>
|
||||
</template>
|
||||
|
||||
<script lang="ts" name="MenuItem" setup>
|
||||
<script lang="ts" setup>
|
||||
import { Menu } from '/admin/types/Menu'
|
||||
import { onMounted, PropType, ref } from 'vue'
|
||||
import { PropType } from 'vue'
|
||||
import { useAppStore } from '/admin/stores/modules/app'
|
||||
import { isMiniScreen } from '/admin/support/helper'
|
||||
|
||||
@@ -37,6 +40,12 @@ defineProps({
|
||||
require: true,
|
||||
},
|
||||
})
|
||||
|
||||
const openUrl = (path: string) => {
|
||||
const start = path.indexOf('https://') || path.indexOf('http://')
|
||||
window.open(path.substring(start))
|
||||
return false
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
|
@@ -7,22 +7,15 @@
|
||||
:collapse="!appStore.isExpand"
|
||||
:collapse-transition="false"
|
||||
:router="true"
|
||||
@select="selectMenu"
|
||||
:unique-opened="true"
|
||||
>
|
||||
<slot />
|
||||
</el-menu>
|
||||
</template>
|
||||
<script lang="ts" setup name="menus">
|
||||
<script lang="ts" setup>
|
||||
import { useAppStore } from '/admin/stores/modules/app'
|
||||
|
||||
const appStore = useAppStore()
|
||||
|
||||
const selectMenu = (index: string) => {
|
||||
if (index.startsWith('http') || index.startsWith('https')) {
|
||||
window.open(index)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
|
@@ -1,216 +1,4 @@
|
||||
import { Code } from '/admin/enum/app'
|
||||
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
|
||||
import { getAuthToken, getBaseUrl, removeAuthToken } from './helper'
|
||||
import Message from './message'
|
||||
import router from '/admin/router'
|
||||
import ResponseData from '/admin/types/responseData'
|
||||
import Request from './request'
|
||||
|
||||
/**
|
||||
* http util
|
||||
*/
|
||||
class Http {
|
||||
/**
|
||||
* axios config
|
||||
* @protected
|
||||
*/
|
||||
protected config: AxiosRequestConfig = {}
|
||||
|
||||
/**
|
||||
* base url
|
||||
* @protected
|
||||
*/
|
||||
protected baseURL: string = ''
|
||||
|
||||
/**
|
||||
* http request timeout
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected timeout: number = 0
|
||||
|
||||
/**
|
||||
* http request headers
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected headers: { [k: string]: string } = {}
|
||||
|
||||
/**
|
||||
* axios instance
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected request: AxiosInstance
|
||||
|
||||
/**
|
||||
* instance
|
||||
*/
|
||||
constructor() {
|
||||
this.request = axios.create(this.getConfig())
|
||||
}
|
||||
|
||||
/**
|
||||
* get request
|
||||
*
|
||||
* @param path
|
||||
* @param params
|
||||
*/
|
||||
public get(path: string, params: object = {}) {
|
||||
return this.request.get(this.baseURL + path, {
|
||||
params,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* post request
|
||||
*
|
||||
* @param path
|
||||
* @param data
|
||||
*/
|
||||
public post(path: string, data: object = {}) {
|
||||
return this.request.post(this.baseURL + path, data)
|
||||
}
|
||||
|
||||
/**
|
||||
* put request
|
||||
*
|
||||
* @param path
|
||||
* @param data
|
||||
*/
|
||||
public put(path: string, data: object = {}) {
|
||||
return this.request.put(this.baseURL + path, data)
|
||||
}
|
||||
|
||||
/**
|
||||
* delete request
|
||||
*
|
||||
* @param path
|
||||
*/
|
||||
public delete(path: string) {
|
||||
return this.request.delete(this.baseURL + path)
|
||||
}
|
||||
|
||||
/**
|
||||
* set timeout
|
||||
*
|
||||
* @param timeout
|
||||
* @returns
|
||||
*/
|
||||
public setTimeout(timeout: number): Http {
|
||||
this.timeout = timeout
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* set baseurl
|
||||
*
|
||||
* @param url
|
||||
* @returns
|
||||
*/
|
||||
public setBaseUrl(url: string): Http {
|
||||
this.baseURL = url
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* set headers
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @returns
|
||||
*/
|
||||
public setHeader(key: string, value: string): Http {
|
||||
this.headers.key = value
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* get axios 配置
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
protected getConfig(): AxiosRequestConfig {
|
||||
// set base url
|
||||
this.config.baseURL = getBaseUrl()
|
||||
|
||||
// set timeout
|
||||
this.config.timeout = this.timeout ? this.timeout : 10000
|
||||
|
||||
// set ajax request
|
||||
this.headers['X-Requested-With'] = 'XMLHttpRequest'
|
||||
// set dashboard request
|
||||
this.headers['Request-from'] = 'Dashboard'
|
||||
this.config.headers = this.headers
|
||||
|
||||
return this.config
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加请求拦截器
|
||||
*
|
||||
*/
|
||||
public interceptorsOfRequest(): void {
|
||||
// @ts-ignore
|
||||
this.request.interceptors.request.use((config: AxiosRequestConfig) => {
|
||||
const token = getAuthToken()
|
||||
if (token) {
|
||||
if (!config.headers) {
|
||||
config.headers = {}
|
||||
}
|
||||
|
||||
config.headers.authorization = 'Bearer ' + token
|
||||
}
|
||||
|
||||
return config
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加响应拦截器
|
||||
*
|
||||
*/
|
||||
public interceptorsOfResponse(): void {
|
||||
this.request.interceptors.response.use(
|
||||
response => {
|
||||
const r: ResponseData = response.data
|
||||
const code = r.code
|
||||
const message = r.message
|
||||
if (code === 1e4) {
|
||||
return response
|
||||
}
|
||||
|
||||
if (code === 10004) {
|
||||
Message.error(message || 'Error')
|
||||
} else if (code === Code.LOST_LOGIN || code === Code.LOGIN_EXPIRED) {
|
||||
// to re-login
|
||||
Message.confirm(message + ',需要重新登陆', function () {
|
||||
removeAuthToken()
|
||||
router.push('/login')
|
||||
})
|
||||
} else if (code === Code.LOGIN_BLACKLIST || code === Code.USER_FORBIDDEN) {
|
||||
Message.error(message || 'Error')
|
||||
removeAuthToken()
|
||||
// to login page
|
||||
router.push('/login')
|
||||
} else {
|
||||
Message.error(message || 'Error')
|
||||
}
|
||||
|
||||
return Promise.reject(new Error(message || 'Error'))
|
||||
},
|
||||
error => {
|
||||
Message.error(error.message)
|
||||
return Promise.reject(error)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const http = new Http()
|
||||
http.interceptorsOfRequest()
|
||||
|
||||
http.interceptorsOfResponse()
|
||||
const http = new Request()
|
||||
export default http
|
||||
|
235
resources/admin/support/request.ts
Normal file
235
resources/admin/support/request.ts
Normal file
@@ -0,0 +1,235 @@
|
||||
import { Code } from '/admin/enum/app'
|
||||
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
|
||||
import { getAuthToken, getBaseUrl, removeAuthToken } from './helper'
|
||||
import Message from './message'
|
||||
import router from '/admin/router'
|
||||
import ResponseData from '/admin/types/responseData'
|
||||
|
||||
type responseType = 'arraybuffer' | 'document' | 'json' | 'text' | 'stream' | 'blob'
|
||||
|
||||
/**
|
||||
* http util
|
||||
*/
|
||||
class Request {
|
||||
/**
|
||||
* axios config
|
||||
* @protected
|
||||
*/
|
||||
protected config: AxiosRequestConfig = {}
|
||||
|
||||
/**
|
||||
* base url
|
||||
* @protected
|
||||
*/
|
||||
protected baseURL: string = ''
|
||||
|
||||
/**
|
||||
* http request timeout
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected timeout: number = 0
|
||||
|
||||
/**
|
||||
* http request headers
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected headers: { [k: string]: string } = {}
|
||||
|
||||
/**
|
||||
* axios instance
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
// @ts-ignore
|
||||
protected request: AxiosInstance
|
||||
|
||||
/**
|
||||
* 响应结构
|
||||
*
|
||||
* @protected
|
||||
*/
|
||||
protected responseType: responseType = 'json'
|
||||
|
||||
public init() {
|
||||
this.request = axios.create(this.getConfig())
|
||||
this.interceptorsOfRequest()
|
||||
this.interceptorsOfResponse()
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* get request
|
||||
*
|
||||
* @param path
|
||||
* @param params
|
||||
*/
|
||||
public get(path: string, params: object = {}) {
|
||||
this.init()
|
||||
return this.request.get(this.baseURL + path, {
|
||||
params,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* post request
|
||||
*
|
||||
* @param path
|
||||
* @param data
|
||||
*/
|
||||
public post(path: string, data: object = {}) {
|
||||
this.init()
|
||||
return this.request.post(this.baseURL + path, data)
|
||||
}
|
||||
|
||||
/**
|
||||
* put request
|
||||
*
|
||||
* @param path
|
||||
* @param data
|
||||
*/
|
||||
public put(path: string, data: object = {}) {
|
||||
this.init()
|
||||
return this.request.put(this.baseURL + path, data)
|
||||
}
|
||||
|
||||
/**
|
||||
* delete request
|
||||
*
|
||||
* @param path
|
||||
*/
|
||||
public delete(path: string) {
|
||||
this.init()
|
||||
return this.request.delete(this.baseURL + path)
|
||||
}
|
||||
|
||||
/**
|
||||
* set timeout
|
||||
*
|
||||
* @param timeout
|
||||
* @returns
|
||||
*/
|
||||
public setTimeout(timeout: number): Request {
|
||||
this.timeout = timeout
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* set baseurl
|
||||
*
|
||||
* @param url
|
||||
* @returns
|
||||
*/
|
||||
public setBaseUrl(url: string): Request {
|
||||
this.baseURL = url
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* set headers
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @returns
|
||||
*/
|
||||
public setHeader(key: string, value: string): Request {
|
||||
this.headers.key = value
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* get axios 配置
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
protected getConfig(): AxiosRequestConfig {
|
||||
// set base url
|
||||
this.config.baseURL = getBaseUrl()
|
||||
//
|
||||
this.config.responseType = this.responseType
|
||||
// set timeout
|
||||
this.config.timeout = this.timeout ? this.timeout : 10000
|
||||
|
||||
// set ajax request
|
||||
this.headers['X-Requested-With'] = 'XMLHttpRequest'
|
||||
// set dashboard request
|
||||
this.headers['Request-from'] = 'Dashboard'
|
||||
this.config.headers = this.headers
|
||||
return this.config
|
||||
}
|
||||
|
||||
public setResponseType(type: responseType) {
|
||||
this.responseType = type
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加请求拦截器
|
||||
*
|
||||
*/
|
||||
public interceptorsOfRequest(): void {
|
||||
// @ts-ignore
|
||||
this.request.interceptors.request.use((config: AxiosRequestConfig) => {
|
||||
const token = getAuthToken()
|
||||
if (token) {
|
||||
if (!config.headers) {
|
||||
config.headers = {}
|
||||
}
|
||||
|
||||
config.headers.authorization = 'Bearer ' + token
|
||||
}
|
||||
|
||||
return config
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加响应拦截器
|
||||
*
|
||||
*/
|
||||
public interceptorsOfResponse(): void {
|
||||
this.request.interceptors.response.use(
|
||||
response => {
|
||||
// 如果是 blob response type, 直接返回 response
|
||||
if (response.request.responseType === 'blob') {
|
||||
return response
|
||||
}
|
||||
|
||||
const r: ResponseData = response.data
|
||||
const code = r.code
|
||||
const message = r.message
|
||||
if (code === 1e4) {
|
||||
return response
|
||||
}
|
||||
|
||||
if (code === 10004) {
|
||||
Message.error(message || 'Error')
|
||||
} else if (code === Code.LOST_LOGIN || code === Code.LOGIN_EXPIRED) {
|
||||
// to re-login
|
||||
Message.confirm(message + ',需要重新登陆', function () {
|
||||
removeAuthToken()
|
||||
router.push('/login')
|
||||
})
|
||||
} else if (code === Code.LOGIN_BLACKLIST || code === Code.USER_FORBIDDEN) {
|
||||
Message.error(message || 'Error')
|
||||
removeAuthToken()
|
||||
// to login page
|
||||
router.push('/login')
|
||||
} else {
|
||||
Message.error(message || 'Error')
|
||||
}
|
||||
return Promise.reject(new Error(message || 'Error'))
|
||||
},
|
||||
error => {
|
||||
Message.error(error.message)
|
||||
return Promise.reject(error)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
export default Request
|
Reference in New Issue
Block a user