Commit 6db3ce8f33c06a0de97175168541946ec112dfc8

Authored by wuxw
1 parent 468b84d2

优化 post 显示动画

src/api/community/communityManageApi.js
... ... @@ -30,7 +30,7 @@ export function listAreas(params) {
30 30 }).then(response => {
31 31 const res = response.data
32 32  
33   - resolve(res)
  33 + resolve(res)
34 34  
35 35 }).catch(error => {
36 36 reject(error)
... ... @@ -67,11 +67,8 @@ export function updateCommunity(data) {
67 67 data
68 68 }).then(response => {
69 69 const res = response.data
70   - if (res.code === 0) {
71   - resolve(res)
72   - } else {
73   - reject(new Error(res.msg || '更新小区失败'))
74   - }
  70 + resolve(res)
  71 +
75 72 }).catch(error => {
76 73 reject(error)
77 74 })
... ... @@ -87,11 +84,8 @@ export function deleteCommunity(data) {
87 84 data
88 85 }).then(response => {
89 86 const res = response.data
90   - if (res.code === 0) {
91   - resolve(res)
92   - } else {
93   - reject(new Error(res.msg || '删除小区失败'))
94   - }
  87 + resolve(res)
  88 +
95 89 }).catch(error => {
96 90 reject(error)
97 91 })
... ... @@ -107,11 +101,8 @@ export function sendCommunityToIot(data) {
107 101 data
108 102 }).then(response => {
109 103 const res = response.data
110   - if (res.code === 0) {
111   - resolve(res)
112   - } else {
113   - reject(new Error(res.msg || '同步到IOT失败'))
114   - }
  104 + resolve(res)
  105 +
115 106 }).catch(error => {
116 107 reject(error)
117 108 })
... ... @@ -127,11 +118,9 @@ export function auditEnterCommunity(data) {
127 118 data
128 119 }).then(response => {
129 120 const res = response.data
130   - if (res.code === 0) {
131   - resolve(res)
132   - } else {
133   - reject(new Error(res.msg || '撤回审核失败'))
134   - }
  121 +
  122 + resolve(res)
  123 +
135 124 }).catch(error => {
136 125 reject(error)
137 126 })
... ...
src/components/community/communityDataToIot.vue
1 1 <template>
2   - <el-dialog
3   - :title="$t('communityDataToIot.title')"
4   - :visible.sync="visible"
5   - width="30%"
6   - @close="closeCommunityDataToIotModel"
7   - >
  2 + <el-dialog :title="$t('communityDataToIot.title')" :visible.sync="visible" width="30%"
  3 + @close="closeCommunityDataToIotModel">
8 4 <div style="text-align: center">
9 5 <p>{{ $t('communityDataToIot.confirmText') }}</p>
10 6 </div>
... ...
src/utils/request.js
1 1 import axios from 'axios'
2   -import { Message } from 'element-ui'
  2 +import { Message, Loading } from 'element-ui'
3 3 import config from '@/conf/config'
4 4 import { getHeader } from './header'
5 5  
  6 +// 创建loading实例
  7 +let loadingInstance = null
  8 +
6 9 // 创建axios实例
7 10 const service = axios.create({
8 11 baseURL: config.baseApi, // 基础URL
9 12 timeout: config.apiTimeout // 请求超时时间
10 13 })
11 14  
  15 +// 显示加载动画
  16 +const showLoading = () => {
  17 + loadingInstance = Loading.service({
  18 + lock: true,
  19 + text: '加载中...',
  20 + spinner: 'el-icon-loading',
  21 + background: 'rgba(0, 0, 0, 0.7)'
  22 + })
  23 +}
  24 +
  25 +// 隐藏加载动画
  26 +const hideLoading = () => {
  27 + if (loadingInstance) {
  28 + loadingInstance.close()
  29 + loadingInstance = null
  30 + }
  31 +}
12 32  
13 33 // 请求拦截器
14 34 service.interceptors.request.use(
... ... @@ -19,19 +39,26 @@ service.interceptors.request.use(
19 39 config.headers['Accept'] = 'application/json'
20 40 config.headers['app-id'] = _header['app-id']
21 41 config.headers['TRANSACTION-ID'] = _header['TRANSACTION-ID']
22   - config.headers['REQ-TIME'] = _header['REQ-TIME']
23   - config.headers['SIGN'] = _header['SIGN']
24   - config.headers['user-id'] = _header['user-id']
  42 + config.headers['REQ-TIME'] = _header['REQ-TIME']
  43 + config.headers['SIGN'] = _header['SIGN']
  44 + config.headers['user-id'] = _header['user-id']
25 45 config.headers['X-Requested-With'] = 'XMLHttpRequest'
26 46 // 这里可以添加token等认证信息
27 47 config.headers['Authorization'] = _header['Authorization']
28   - if(config.url.indexOf('/app/') == -1 && config.url.indexOf('/callComponent/') == -1){
  48 + if (config.url.indexOf('/app/') == -1 && config.url.indexOf('/callComponent/') == -1) {
29 49 config.baseURL = "/app"
30 50 }
  51 + // GET请求不显示加载动画,其他请求显示
  52 + if (config.method !== 'get' && config.showLoading !== false) {
  53 + showLoading()
  54 + }
  55 +
31 56 return config
32 57 },
33 58 error => {
34 59 console.log(error)
  60 + // 请求错误时也要隐藏加载动画
  61 + hideLoading()
35 62 return Promise.reject(error)
36 63 }
37 64 )
... ... @@ -39,6 +66,9 @@ service.interceptors.request.use(
39 66 // 响应拦截器
40 67 service.interceptors.response.use(
41 68 response => {
  69 + // 隐藏加载动画
  70 + hideLoading()
  71 +
42 72 const res = response.data
43 73  
44 74 if (res.code && res.code != 0) {
... ... @@ -47,28 +77,31 @@ service.interceptors.response.use(
47 77 type: 'error',
48 78 duration: 5 * 1000
49 79 })
50   - return Promise.reject(new Error(res.message || 'Error'))
  80 + return Promise.reject()
51 81 } else {
52 82 return response
53 83 }
54 84 },
55 85 error => {
56 86 console.log(error)
57   -
  87 +
  88 + // 隐藏加载动画
  89 + hideLoading()
  90 +
58 91 // 判断是否为401未授权错误
59 92 if (error.response && error.response.status === 401) {
60 93 // 清除本地存储的token
61 94 localStorage.removeItem('token')
62 95 // 跳转到登录页面
63 96 window.location.href = '/#/views/user/login'
64   -
  97 +
65 98 // Message({
66 99 // message: '登录已过期,请重新登录',
67 100 // type: 'error',
68 101 // duration: 5 * 1000
69 102 // })
70 103 }
71   -
  104 +
72 105 return Promise.reject(error)
73 106 }
74 107 )
... ...
src/views/community/communityManageList.vue
... ... @@ -80,9 +80,9 @@
80 80 @current-change="handleCurrentChange" />
81 81 </el-card>
82 82 <!-- 组件 -->
83   - <add-community ref="addCommunity" @listData="_queryCommunityMethod"/>
84   - <edit-community ref="editCommunity" @listData="_queryCommunityMethod"/>
85   - <delete-community ref="deleteCommunity" @listData="_queryCommunityMethod"/>
  83 + <add-community ref="addCommunity" @listData="_queryCommunityMethod" />
  84 + <edit-community ref="editCommunity" @listData="_queryCommunityMethod" />
  85 + <delete-community ref="deleteCommunity" @listData="_queryCommunityMethod" />
86 86 <community-data-to-iot ref="communityDataToIot" />
87 87 </div>
88 88 </template>
... ... @@ -202,7 +202,7 @@ export default {
202 202 const { data } = await getAttrSpecList({
203 203 page: 1,
204 204 row: 100,
205   - tableName:'building_community_attr'
  205 + tableName: 'building_community_attr'
206 206 })
207 207 this.communityManageInfo.listColumns = []
208 208 data.forEach(item => {
... ...