index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <template>
  2. <wrapper
  3. fill
  4. margin
  5. padding
  6. background
  7. horizontal
  8. >
  9. <department-tree
  10. class="c-sibling-item c-sidebar u-width--md"
  11. @change="onGroupChanged"
  12. />
  13. <table-dialog
  14. ref="attentionList"
  15. title="关注列表"
  16. :schema="attentionSchema"
  17. />
  18. <schema-table
  19. ref="table"
  20. class="c-sibling-item far"
  21. row-key="id"
  22. :schema="schema"
  23. />
  24. <confirm-dialog
  25. ref="editDialog"
  26. title="编辑"
  27. @confirm="onSave"
  28. >
  29. <div class="c-grid-form u-align-self--center">
  30. <span class="c-grid-form__label u-required">名称</span>
  31. <el-input
  32. v-model.trim="info.name"
  33. placeholder="最多30个字符"
  34. maxlength="30"
  35. clearable
  36. />
  37. <div class="c-grid-form__label u-required">开关机时间</div>
  38. <el-time-picker
  39. v-model="info.range"
  40. class="u-width u-pointer"
  41. is-range
  42. value-format="HH:mm:ss"
  43. :clearable="false"
  44. />
  45. <span class="c-grid-form__label u-required">地址</span>
  46. <el-input
  47. v-model.trim="info.address"
  48. type="textarea"
  49. placeholder="最多100个字符"
  50. maxlength="100"
  51. :rows="4"
  52. resize="none"
  53. show-word-limit
  54. />
  55. <span class="c-grid-form__label">坐标</span>
  56. <div class="l-flex--row c-grid-form__option">
  57. <span class="c-sibling-item">{{ info.longitude }},{{ info.latitude }}</span>
  58. <i
  59. class="c-sibling-item el-icon-edit u-color--blue has-active"
  60. @click="onEditCoordinate"
  61. />
  62. </div>
  63. </div>
  64. </confirm-dialog>
  65. <coordinate-dialog
  66. ref="coordinateDialog"
  67. @confirm="onChangeCoordinate"
  68. />
  69. </wrapper>
  70. </template>
  71. <script>
  72. import {
  73. validLongitude,
  74. validLatitude
  75. } from '@/utils/validate'
  76. import {
  77. getDevicesByQuery,
  78. getSubDevices,
  79. activateDevice,
  80. deactivateDevice,
  81. updateDevice,
  82. addDeviceAttention,
  83. cancelDeviceAttention,
  84. getDeviceAttentionList
  85. } from '@/api/device'
  86. export default {
  87. name: 'DeviceManagement',
  88. data () {
  89. return {
  90. attentionSchema: {
  91. nonPagination: true,
  92. list: getDeviceAttentionList,
  93. cols: [
  94. { type: 'refresh', render: (data, h) => h(
  95. 'i',
  96. {
  97. staticClass: `${data.ifAttention === 1 ? 'el-icon-star-on u-font-size--xl' : 'el-icon-star-off u-font-size--lg'} u-color--warning`,
  98. on: {
  99. click: () => {
  100. data.ifAttention === 1 ? this.cancelDeviceAttention(data) : this.addDeviceAttention(data)
  101. }
  102. }
  103. }
  104. ) },
  105. { prop: 'name', label: '设备名称' }
  106. ]
  107. },
  108. schema: {
  109. keepalive: true,
  110. list: this.getDevicesByQuery,
  111. transform: this.transform,
  112. transformData: __SUB_DEVICE__ && this.transformTableData,
  113. // listeners: {
  114. // 'row-click': this.onRowClick
  115. // },
  116. filters: [
  117. { key: 'name', type: 'search', placeholder: '设备名称' }
  118. ],
  119. buttons: [
  120. { label: '关注列表', on: this.onShowAttentionList }
  121. ],
  122. cols: [
  123. // { type: 'refresh', render: __SUB_DEVICE__
  124. // ? (data, h) => data.isMaster
  125. // ? h('i', {
  126. // staticClass: `o-expand-icon u-pointer ${data.loading ? 'el-icon-loading' : 'el-icon-arrow-right'}`,
  127. // class: { expand: data.expand }
  128. // })
  129. // : null
  130. // : null },
  131. // __SUB_DEVICE__
  132. // ? { label: '设备名称', render: (data, h) => data.empty ? h('span', { staticClass: 'u-color--info' }, '暂无备份设备') : data.name, 'min-width': 120 }
  133. // : { prop: 'name', label: '设备名称', 'min-width': 120 },
  134. { type: 'refresh', render: (data, h) => h(
  135. 'i',
  136. {
  137. staticClass: `${data.ifAttention === 1 ? 'el-icon-star-on u-font-size--xl' : 'el-icon-star-off u-font-size--lg'} u-color--warning`,
  138. on: {
  139. click: () => {
  140. data.ifAttention === 1 ? this.cancelDeviceAttention(data) : this.addDeviceAttention(data)
  141. }
  142. }
  143. }
  144. ) },
  145. { prop: 'name', 'min-width': 120 },
  146. { prop: 'serialNumber', label: '序列号' },
  147. { prop: 'mac', label: 'MAC' },
  148. { label: '运行时间', render: ({ empty, openTime, closeTime }) => empty
  149. ? ''
  150. : openTime && closeTime
  151. ? `${openTime} - ${closeTime}`
  152. : '-' },
  153. { label: '坐标', type: 'tag', render: ({ empty, longitude, latitude }) => empty
  154. ? null
  155. : longitude && latitude
  156. ? {
  157. type: 'success',
  158. label: '已设置'
  159. }
  160. : {
  161. type: 'warning',
  162. label: '未设置'
  163. } },
  164. { type: 'tag', render: ({ empty, activate, onlineStatus }) => empty
  165. ? null
  166. : activate
  167. ? onlineStatus === 0
  168. ? { type: 'primary', label: '待接入' }
  169. : onlineStatus === 1
  170. ? { type: 'success', label: '在线' }
  171. : { type: 'danger', label: '离线' }
  172. : { type: 'warning', label: '未激活' },
  173. on: this.onTagClick },
  174. { type: 'invoke', render: [
  175. { label: '编辑', render ({ empty }) { return !empty }, on: this.onEditDevice },
  176. { label: '详情', render ({ empty }) { return !empty }, on: this.onViewDevice },
  177. { label: '配置', render ({ isMaster }) { return isMaster }, on: this.onSettingDevice }
  178. ], width: 160 }
  179. ]
  180. },
  181. info: {}
  182. }
  183. },
  184. methods: {
  185. onGroupChanged (group) {
  186. this.$group = group
  187. this.$refs.table?.pageTo(1)
  188. },
  189. getDevicesByQuery (params) {
  190. if (!this.$group) {
  191. return Promise.resolve({ data: [] })
  192. }
  193. return getDevicesByQuery({
  194. org: this.$group.path,
  195. isAttentionFlag: 1,
  196. attentionValue: 2,
  197. ...params
  198. })
  199. },
  200. transform (data) {
  201. return {
  202. ...data,
  203. loaded: false,
  204. loading: false,
  205. expand: false,
  206. subs: [],
  207. isMaster: true
  208. }
  209. },
  210. transformTableData (data) {
  211. const arr = []
  212. data.forEach(item => {
  213. arr.push(item)
  214. if (item.loaded && item.expand) {
  215. arr.push(...item.subs)
  216. }
  217. })
  218. return arr
  219. },
  220. onViewDevice (item) {
  221. this.$router.push({
  222. name: 'device-management-detail',
  223. params: {
  224. id: item.id
  225. }
  226. })
  227. },
  228. reloadSubDevices (item) {
  229. item.loaded = false
  230. item.children = []
  231. this.getSubDevices(item)
  232. },
  233. getSubDevices (item, pageSize = 999) {
  234. item.loading = true
  235. getSubDevices({
  236. id: item.id,
  237. pageNum: 1,
  238. pageSize
  239. }).then(({ data, totalCount }) => {
  240. if (totalCount > pageSize) {
  241. this.getSubDevices(item, totalCount)
  242. } else {
  243. item.loading = false
  244. item.loaded = true
  245. item.expand = true
  246. if (data.length === 0) {
  247. item.subs = [{ id: `${Math.random()}`, empty: true }]
  248. } else {
  249. item.subs = data.map(device => {
  250. return {
  251. ...device,
  252. isMaster: false,
  253. empty: false,
  254. parent: item
  255. }
  256. })
  257. }
  258. }
  259. }, () => {
  260. item.loading = false
  261. })
  262. },
  263. onRowClick (data) {
  264. if (__SUB_DEVICE__ && data.isMaster) {
  265. if (data.loaded) {
  266. data.expand = !data.expand
  267. } else if (!data.loading) {
  268. this.getSubDevices(data)
  269. }
  270. }
  271. },
  272. onTagClick (data) {
  273. (data.activate ? deactivateDevice : activateDevice)(data).then(() => {
  274. if (data.isMaster) {
  275. this.$refs.table.pageTo()
  276. } else {
  277. this.reloadSubDevices(data.parent)
  278. }
  279. })
  280. },
  281. onEditDevice (device) {
  282. const { id, name, openTime, closeTime, address, longitude, latitude } = device
  283. this.$device = device
  284. this.info = { id, name, range: [openTime || '', closeTime || ''], address, longitude, latitude }
  285. this.$refs.editDialog.show()
  286. },
  287. onEditCoordinate () {
  288. const { longitude, latitude, address } = this.info
  289. this.$refs.coordinateDialog.show({
  290. longitude,
  291. latitude,
  292. address
  293. })
  294. },
  295. onChangeCoordinate ({ value: { longitude, latitude, address }, done }) {
  296. this.info.longitude = longitude
  297. this.info.latitude = latitude
  298. this.info.address = address
  299. done()
  300. },
  301. onSave (done) {
  302. const { id, name, range, longitude, address, latitude } = this.info
  303. if (!name) {
  304. this.$message({
  305. type: 'warning',
  306. message: '名称不能为空'
  307. })
  308. return
  309. }
  310. if (!range || !range[0] || !range[1]) {
  311. this.$message({
  312. type: 'warning',
  313. message: '请选择开关机时间'
  314. })
  315. return
  316. }
  317. if (range[0] >= range[1]) {
  318. this.$message({
  319. type: 'warning',
  320. message: '开机时间必须小于关机时间'
  321. })
  322. return
  323. }
  324. if (!address) {
  325. this.$message({
  326. type: 'warning',
  327. message: '地址不能为空'
  328. })
  329. return
  330. }
  331. if (longitude && !validLongitude(longitude)) {
  332. this.$message({
  333. type: 'warning',
  334. message: '经度格式错误,-180 ~ +180'
  335. })
  336. return
  337. }
  338. if (latitude && !validLatitude(latitude)) {
  339. this.$message({
  340. type: 'warning',
  341. message: '纬度格式错误,-90 ~ +90'
  342. })
  343. return
  344. }
  345. const info = { id, name, openTime: range[0], closeTime: range[1], address, longitude, latitude }
  346. if (Object.keys(info).every(key => info[key] === this.$device[key])) {
  347. done()
  348. return
  349. }
  350. updateDevice(info).then(() => {
  351. done()
  352. Object.assign(this.$device, info)
  353. })
  354. },
  355. onSettingDevice ({ id }) {
  356. this.$router.push({
  357. name: 'device-management-settings',
  358. params: { id }
  359. })
  360. },
  361. addDeviceAttention (row) {
  362. addDeviceAttention(row.id).then(() => {
  363. row.ifAttention = 1
  364. })
  365. },
  366. cancelDeviceAttention (row) {
  367. cancelDeviceAttention(row.id).then(() => {
  368. row.ifAttention = 0
  369. })
  370. },
  371. cancelDeviceAttentionByDialog (row) {
  372. cancelDeviceAttention(row.id).then(() => {
  373. this.$refs.table.pageTo()
  374. })
  375. },
  376. onShowAttentionList () {
  377. this.$refs.attentionList.show()
  378. }
  379. }
  380. }
  381. </script>