Browse Source

完善写法,避免重复请求

luyanan 6 years ago
parent
commit
f6ebf5a56c

+ 0 - 9
src/api/collectionbox.js

@ -71,12 +71,3 @@ export function checkDeviceInternalCode(params) {
71 71
    params
72 72
  })
73 73
}
74
75
/*  查询服务器名字 */
76
export function queryServer(params) {
77
  return request({
78
    url: '/ajax/server/qo',
79
    method: 'get',
80
    params
81
  })
82
}

+ 0 - 10
src/api/sensor.js

@ -71,13 +71,3 @@ export function checkDeviceInternalCode(params) {
71 71
    params
72 72
  })
73 73
}
74
75
/*  查询采集盒名字 */
76
export function queryServer(params) {
77
  return request({
78
    url: '/ajax/device/qo',
79
    method: 'get',
80
    params
81
  })
82
}
83

+ 0 - 9
src/api/server.js

@ -71,12 +71,3 @@ export function checkDeviceInternalCode(params) {
71 71
    params
72 72
  })
73 73
}
74
75
/*  查询服务器名字 */
76
export function queryServer(params) {
77
  return request({
78
    url: '/ajax/bridge/qo',
79
    method: 'get',
80
    params
81
  })
82
}

+ 0 - 7
src/router/index.js

@ -1,18 +1,11 @@
1 1
import Vue from 'vue'
2 2
import Router from 'vue-router'
3 3
4
// in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading;
5
// detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading
6
7 4
Vue.use(Router)
8 5
9 6
/* Layout */
10 7
import Layout from '../views/layout/Layout'
11 8
12
/** note: submenu only apppear when children.length>=1
13
*   detail see  https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
14
**/
15
16 9
/**
17 10
* hidden: true                   if `hidden:true` will not show in the sidebar(default is false)
18 11
* alwaysShow: true               if set true, will always show the root menu, whatever its child routes length

+ 138 - 0
src/utils/queryBase.js

@ -0,0 +1,138 @@
1
/**
2
 * Created by luyanan on 18/8/27.
3
 * bridge、server、device
4
 */
5
/* eslint-disable one-var */
6
import request from '@/utils/request'
7
var $req = {
8
  get: function(url, data, sh, eh) {
9
    request({
10
      method: 'get',
11
      url: url,
12
      params: data
13
    }).then(res => {
14
      sh(res)
15
    }).catch(err => {
16
      console.log(err)
17
      eh(err)
18
    })
19
  }
20
}
21
var objCache = {
22
  bridge: {},
23
  server: {},
24
  device: {}
25
}
26
var objHcache = {
27
  bridge: {},
28
  server: {},
29
  device: {}
30
}
31
var objCacheHandler = {
32
  bridge: function(id) {
33
    var hc = objHcache.bridge[id]
34
    $req.get('/ajax/bridge/qo?id=' + id, null, function(data) {
35
      delete objHcache.bridge[id]
36
      if (data.success) {
37
        objCache.bridge[id] = data.data
38
        for (let i = 0; i < hc.length; ++i) {
39
          hc[i](true, data.data)
40
        }
41
      } else {
42
        for (let i = 0; i < hc.length; ++i) {
43
          hc[i](false)
44
        }
45
      }
46
    }, function() {
47
      for (let i = 0; i < hc.length; ++i) {
48
        hc[i](false)
49
      }
50
    })
51
  },
52
  server: function(id) {
53
    var hc = objHcache.server[id]
54
    $req.get('/ajax/server/qo?id=' + id, null, function(data) {
55
      delete objHcache.server[id]
56
      if (data.success) {
57
        objCache.server[id] = data.data
58
        for (let i = 0; i < hc.length; ++i) {
59
          hc[i](true, data.data)
60
        }
61
      } else {
62
        for (let i = 0; i < hc.length; ++i) {
63
          hc[i](false)
64
        }
65
      }
66
    }, function() {
67
      for (let i = 0; i < hc.length; ++i) {
68
        hc[i](false)
69
      }
70
    })
71
  },
72
  device: function(id) {
73
    var hc = objHcache.device[id]
74
    $req.get('/ajax/device/qo?id=' + id, null, function(data) {
75
      delete objHcache.device[id]
76
      if (data.success) {
77
        objCache.device[id] = data.data
78
        for (let i = 0; i < hc.length; ++i) {
79
          hc[i](true, data.data)
80
        }
81
      } else {
82
        for (let i = 0; i < hc.length; ++i) {
83
          hc[i](false)
84
        }
85
      }
86
    }, function() {
87
      for (let i = 0; i < hc.length; ++i) {
88
        hc[i](false)
89
      }
90
    })
91
  }
92
}
93
var cacheModel = {
94
  getBridge: function(id, handler) {
95
    var data = objCache.bridge[id]
96
    if (data) {
97
      handler(true, data)
98
    } else {
99
      if (objHcache.bridge[id]) {
100
        objHcache.bridge[id].push(handler)
101
      } else {
102
        objHcache.bridge[id] = []
103
        objHcache.bridge[id].push(handler)
104
        objCacheHandler.bridge(id)
105
      }
106
    }
107
  },
108
  getServer: function(id, handler) {
109
    var data = objCache.server[id]
110
    if (data) {
111
      handler(true, data)
112
    } else {
113
      if (objHcache.server[id]) {
114
        objHcache.server[id].push(handler)
115
      } else {
116
        objHcache.server[id] = []
117
        objHcache.server[id].push(handler)
118
        objCacheHandler.server(id)
119
      }
120
    }
121
  },
122
  getDevice: function(id, handler) {
123
    var data = objCache.device[id]
124
    if (data) {
125
      handler(true, data)
126
    } else {
127
      if (objHcache.device[id]) {
128
        objHcache.device[id].push(handler)
129
      } else {
130
        objHcache.device[id] = []
131
        objHcache.device[id].push(handler)
132
        objCacheHandler.device(id)
133
      }
134
    }
135
  }
136
}
137
138
export default cacheModel

+ 1 - 0
src/utils/queryInfo.js

@ -1,5 +1,6 @@
1 1
/**
2 2
 * Created by luyanan on 18/8/23.
3
 * All information about bridges
3 4
 */
4 5
/* eslint-disable one-var */
5 6
import request from '@/utils/request'

+ 1 - 27
src/utils/request.js

@ -1,14 +1,11 @@
1 1
import axios from 'axios'
2 2
import qs from 'qs'
3 3
import { comUrl } from '@/utils/index'
4
// import { Message } from 'element-ui'
5
// import store from '@/store'
6
// import { getSession } from '@/utils/auth'
7 4
8 5
// 创建axios实例
9 6
const service = axios.create({
10 7
  baseURL: comUrl, // api的base_url
11
  // timeout: 5000 // 请求超时时间
8
  timeout: 5000, // 请求超时时间
12 9
  paramsSerializer: function(params) {
13 10
    return qs.stringify(params, { arrayFormat: 'repeat' })
14 11
  }
@ -28,30 +25,11 @@ service.interceptors.request.use(config => {
28 25
  }
29 26
  return config
30 27
}, error => {
31
  // Do something with request error
32
  console.log(error) // for debug
33 28
  Promise.reject(error)
34 29
})
35 30
36 31
// respone拦截器
37 32
service.interceptors.response.use(response => {
38
  // response => {
39
  // /**
40
  // * code为非20000是抛错 可结合自己业务进行修改
41
  // */
42
  //   const res = response.data
43
  //   if (res.code !== 20000) {
44
  //     Message({
45
  //       message: res.message,
46
  //       type: 'error',
47
  //       duration: 5 * 1000
48
  //     })
49
50
  //     return Promise.reject('error')
51
  //   } else {
52
  //     return response.data
53
  //   }
54
  // },
55 33
  let data = response.data
56 34
  const status = response.status
57 35
  if (status === 200) {
@ -67,13 +45,9 @@ service.interceptors.response.use(response => {
67 45
    }
68 46
    return response.data
69 47
  } else {
70
    // 业务异常
71
    console.log(response)
72 48
    return Promise.resolve(response)
73 49
  }
74 50
}, error => {
75
  // 系统异常(后期统一处理)
76
  console.log(error)
77 51
  return Promise.reject(error)
78 52
})
79 53

+ 24 - 27
src/views/baseInfoManage/boxesConfig/index.vue

@ -11,26 +11,10 @@
11 11
12 12
    <el-table :key='tableKey' :data="list" v-loading="listLoading" border fit highlight-current-row
13 13
      style="width: 100%;min-height:550px;">
14
      <el-table-column width="150px" align="center" label="采集盒编号">
15
        <template slot-scope="scope">
16
          <span>{{scope.row.code}}</span>
17
        </template>
18
      </el-table-column>
19
      <el-table-column width="150px" align="center" label="采集盒信道数量">
20
        <template slot-scope="scope">
21
          <span>{{scope.row.channels}}</span>
22
        </template>
23
      </el-table-column>
24
      <el-table-column min-width="150px" align="center" label="所属服务器编号">
25
        <template slot-scope="scope">
26
          <span>{{scope.row.serverName}}</span>
27
        </template>
28
      </el-table-column>
29
      <el-table-column min-width="200px" align="center" label="备注信息">
30
        <template slot-scope="scope">
31
          <span>{{scope.row.remark}}</span>
32
        </template>
33
      </el-table-column>
14
      <el-table-column width="150px" align="center" label="采集盒编号" prop="code"></el-table-column>
15
      <el-table-column width="150px" align="center" label="采集盒信道数量" prop="channels"></el-table-column>
16
      <el-table-column min-width="150px" align="center" label="所属服务器编号" prop="serverName"></el-table-column>
17
      <el-table-column min-width="200px" align="center" label="备注信息" prop="remark"></el-table-column>
34 18
      <el-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">
35 19
        <template slot-scope="scope"> 
36 20
          <el-button v-waves v-waves type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button> 
@ -89,8 +73,9 @@
89 73
</template>
90 74
91 75
<script>
92
import { addDevice, updateDevice, deleteDevice, pageQueryDevice, DeviceOfservice, checkDeviceCode, checkDeviceInternalCode, queryServer } from '@/api/collectionbox'
76
import { addDevice, updateDevice, deleteDevice, pageQueryDevice, DeviceOfservice, checkDeviceCode, checkDeviceInternalCode } from '@/api/collectionbox'
93 77
import waves from '@/directive/waves'
78
import queryBase from '@/utils/queryBase'
94 79
export default {
95 80
  name: 'complexTable',
96 81
  directives: {
@ -256,19 +241,31 @@ export default {
256 241
      this.edit = ''
257 242
    },
258 243
    getList() {
244
      var that = this
259 245
      this.listLoading = true
260 246
      pageQueryDevice(this.listQuery).then(response => {
261 247
        const $data = response.data.data
248
        var hdata = { num: 1, data: $data }
262 249
        for (let i = 0; i < $data.length; i++) {
263
          $data[i].serverName = ''
264
          queryServer({ id: $data[i].serverId }).then(response => {
265
            $data[i].serverName = response.data.code
250
          hdata.num++
251
          const str = $data[i]
252
          queryBase.getServer(str.serverId, function(sc, value) {
253
            if (sc) {
254
              str.serverName = value.code
255
              hdata.num--
256
              if (hdata.num === 0) {
257
                that.list = hdata.data
258
              }
259
            }
266 260
          })
267 261
        }
268
        this.list = $data
269
        this.total = response.data.total
262
        hdata.num--
263
        if (hdata.num === 0) {
264
          that.list = hdata.data
265
        }
266
        that.total = response.data.total
270 267
        setTimeout(() => {
271
          this.listLoading = false
268
          that.listLoading = false
272 269
        }, 1.5 * 1000)
273 270
      })
274 271
    },

+ 39 - 44
src/views/baseInfoManage/sensorsConfig/index.vue

@ -9,33 +9,13 @@
9 9
      <el-button v-waves class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">添加传感器</el-button>
10 10
    </div>
11 11
12
    <el-table :key='tableKey' :data="list" v-loading="listLoading" border fit highlight-current-row
12
    <el-table :key='tableKey' :data="listG" v-loading="listLoading" border fit highlight-current-row
13 13
      style="width: 100%;min-height:550px;">
14
      <el-table-column width="150px" align="center" label="传感器编号">
15
        <template slot-scope="scope">
16
          <span>{{scope.row.code}}</span>
17
        </template>
18
      </el-table-column>
19
      <el-table-column width="150px" align="center" label="传感器所在主缆">
20
        <template slot-scope="scope">
21
          <span>{{cableMain[scope.row.cableType]}}</span>
22
        </template>
23
      </el-table-column>
24
      <el-table-column width="150px" align="center" label="传感器位置">
25
        <template slot-scope="scope">
26
          <span>{{addr[scope.row.locType]}}</span>
27
        </template>
28
      </el-table-column>
29
      <el-table-column min-width="150px" align="center" label="所属采集盒编号">
30
        <template slot-scope="scope">
31
          <span>{{scope.row.deviceName}}</span>
32
        </template>
33
      </el-table-column>
34
      <el-table-column min-width="200px" align="center" label="备注信息">
35
        <template slot-scope="scope">
36
          <span>{{scope.row.remark}}</span>
37
        </template>
38
      </el-table-column>
14
      <el-table-column width="150px" align="center" label="传感器编号" prop="code"></el-table-column>
15
      <el-table-column width="150px" align="center" label="传感器所在主缆" prop="cableType"></el-table-column>
16
      <el-table-column width="150px" align="center" label="传感器位置" prop="locType"></el-table-column>
17
      <el-table-column min-width="150px" align="center" label="所属采集盒编号" prop="deviceName"></el-table-column>
18
      <el-table-column min-width="200px" align="center" label="备注信息" prop="remark"></el-table-column>
39 19
      <el-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">
40 20
        <template slot-scope="scope"> 
41 21
          <el-button v-waves type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button> 
@ -113,9 +93,10 @@
113 93
</template>
114 94
115 95
<script>
116
import { addDevice, updateDevice, deleteDevice, pageQueryDevice, DeviceOfservice, checkDeviceCode, checkDeviceInternalCode, queryServer } from '@/api/sensor'
96
import { addDevice, updateDevice, deleteDevice, pageQueryDevice, DeviceOfservice, checkDeviceCode, checkDeviceInternalCode } from '@/api/sensor'
117 97
import waves from '@/directive/waves'
118 98
import queryDict from '@/utils/queryDict'
99
import queryBase from '@/utils/queryBase'
119 100
export default {
120 101
  name: 'complexTable',
121 102
  directives: {
@ -217,7 +198,7 @@ export default {
217 198
      timeout: null,
218 199
      dialogTableVisible: false,
219 200
      tableKey: 0,
220
      list: null,
201
      listG: null,
221 202
      total: null,
222 203
      listLoading: true,
223 204
      listQuery: {
@ -240,19 +221,19 @@ export default {
240 221
  methods: {
241 222
    getDictoryData() {
242 223
      const that = this
243
      queryDict.applyDict('ZLLX', function(dictData) {
224
      queryDict.applyDict('ZLLX', function(dictData) { // 主缆
244 225
        dictData.map(item => {
245 226
          that.options.push({ value: item.code, label: item.caption })
246 227
          that.cableMain[item.code] = item.caption
247 228
        })
248
      }) // 主缆
249
      queryDict.applyDict('ZLWZ', function(dictData) {
250
        dictData.map(item => {
251
          that.options1.push({ value: item.code, label: item.caption })
252
          that.addr[item.code] = item.caption
229
        queryDict.applyDict('ZLWZ', function(dictData) { // 位置
230
          dictData.map(item => {
231
            that.options1.push({ value: item.code, label: item.caption })
232
            that.addr[item.code] = item.caption
233
          })
234
          that.getList()
253 235
        })
254
      }) // 位置
255
      that.getList()
236
      })
256 237
    },
257 238
    submitForm(formName) {
258 239
      const that = this
@ -306,19 +287,33 @@ export default {
306 287
      this.edit = ''
307 288
    },
308 289
    getList() {
290
      var that = this
309 291
      this.listLoading = true
310
      pageQueryDevice(this.listQuery).then(response => {
311
        const $data = response.data.data
292
      pageQueryDevice(that.listQuery).then(response => {
293
        var $data = response.data.data
294
        var hdata = { num: 1, data: $data }
312 295
        for (let i = 0; i < $data.length; i++) {
313
          $data[i].deviceName = ''
314
          queryServer({ id: $data[i].deviceId }).then(response => {
315
            $data[i].deviceName = response.data.code
296
          hdata.num++
297
          $data[i].cableType = that.cableMain[$data[i].cableType]
298
          $data[i].locType = that.addr[$data[i].locType]
299
          const str = $data[i]
300
          queryBase.getDevice(str.deviceId, function(sc, value) {
301
            if (sc) {
302
              str.deviceName = value.code
303
              hdata.num--
304
              if (hdata.num === 0) {
305
                that.listG = hdata.data
306
              }
307
            }
316 308
          })
317 309
        }
318
        this.list = $data
319
        this.total = response.data.total
310
        hdata.num--
311
        if (hdata.num === 0) {
312
          that.listG = hdata.data
313
        }
314
        that.total = response.data.total
320 315
        setTimeout(() => {
321
          this.listLoading = false
316
          that.listLoading = false
322 317
        }, 1.5 * 1000)
323 318
      })
324 319
    },

+ 24 - 27
src/views/baseInfoManage/serversConfig/index.vue

@ -11,26 +11,10 @@
11 11
12 12
    <el-table :key='tableKey' :data="list" v-loading="listLoading" border fit highlight-current-row
13 13
      style="width: 100%;min-height:550px;">
14
      <el-table-column width="150px" align="center" label="服务器编号">
15
        <template slot-scope="scope">
16
          <span>{{scope.row.code}}</span>
17
        </template>
18
      </el-table-column>
19
      <el-table-column width="150px" align="center" label="采集盒数量">
20
        <template slot-scope="scope">
21
          <span>{{scope.row.devices}}</span>
22
        </template>
23
      </el-table-column>
24
      <el-table-column min-width="150px" align="center" label="所属桥梁">
25
        <template slot-scope="scope">
26
          <span>{{scope.row.bridgeName}}</span>
27
        </template>
28
      </el-table-column>
29
      <el-table-column min-width="200px" align="center" label="备注信息">
30
        <template slot-scope="scope">
31
          <span>{{scope.row.remark}}</span>
32
        </template>
33
      </el-table-column>
14
      <el-table-column width="150px" align="center" label="服务器编号" prop="code"></el-table-column>
15
      <el-table-column width="150px" align="center" label="采集盒数量" prop="devices"></el-table-column>
16
      <el-table-column min-width="150px" align="center" label="所属桥梁" prop="bridgeName"></el-table-column>
17
      <el-table-column min-width="200px" align="center" label="备注信息" prop="remark"></el-table-column>
34 18
      <el-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">
35 19
        <template slot-scope="scope"> 
36 20
          <el-button v-waves type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button> 
@ -89,8 +73,9 @@
89 73
</template>
90 74
91 75
<script>
92
import { addDevice, updateDevice, deleteDevice, pageQueryDevice, DeviceOfservice, checkDeviceCode, checkDeviceInternalCode, queryServer } from '@/api/server'
76
import { addDevice, updateDevice, deleteDevice, pageQueryDevice, DeviceOfservice, checkDeviceCode, checkDeviceInternalCode } from '@/api/server'
93 77
import waves from '@/directive/waves'
78
import queryBase from '@/utils/queryBase'
94 79
export default {
95 80
  name: 'complexTable',
96 81
  directives: {
@ -256,19 +241,31 @@ export default {
256 241
      this.edit = ''
257 242
    },
258 243
    getList() {
244
      var that = this
259 245
      this.listLoading = true
260 246
      pageQueryDevice(this.listQuery).then(response => {
261 247
        const $data = response.data.data
248
        var hdata = { num: 1, data: $data }
262 249
        for (let i = 0; i < $data.length; i++) {
263
          $data[i].bridgeName = ''
264
          queryServer({ id: $data[i].bridgeId }).then(response => {
265
            $data[i].bridgeName = response.data.shortName
250
          hdata.num++
251
          const str = $data[i]
252
          queryBase.getBridge(str.bridgeId, function(sc, value) {
253
            if (sc) {
254
              str.bridgeName = value.shortName
255
              hdata.num--
256
              if (hdata.num === 0) {
257
                that.list = hdata.data
258
              }
259
            }
266 260
          })
267 261
        }
268
        this.list = $data
269
        this.total = response.data.total
262
        hdata.num--
263
        if (hdata.num === 0) {
264
          that.list = hdata.data
265
        }
266
        that.total = response.data.total
270 267
        setTimeout(() => {
271
          this.listLoading = false
268
          that.listLoading = false
272 269
        }, 1.5 * 1000)
273 270
      })
274 271
    },

+ 0 - 22
src/views/layout/components/TopNavbar.vue

@ -158,28 +158,6 @@ export default {
158 158
      cursor: pointer;
159 159
    }
160 160
  }
161
  .avatar-container {
162
    height: 50px;
163
    display: inline-block;
164
    position: absolute;
165
    right: 35px;
166
    .avatar-wrapper {
167
      cursor: pointer;
168
      position: relative;
169
      color: #fff;
170
      .user-avatar {
171
        width: 40px;
172
        height: 40px;
173
        border-radius: 10px;
174
      }
175
      .el-icon-caret-bottom {
176
        position: absolute;
177
        right: -20px;
178
        top: 25px;
179
        font-size: 12px;
180
      }
181
    }
182
  }
183 161
}
184 162
.drop-menu-list{
185 163
  .el-dropdown-menu__item{