device.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. import store from '@/store'
  2. import request, { tenantRequest } from '@/utils/request'
  3. import {
  4. add,
  5. update,
  6. del,
  7. confirm,
  8. messageSend,
  9. send,
  10. confirmAndSend,
  11. addTenant,
  12. addTenantOrOrg,
  13. addUser
  14. } from './base'
  15. import {
  16. AssetType,
  17. SupportedAlarmStrategies
  18. } from '@/constant'
  19. export function getRatiosWithUser () {
  20. return tenantRequest({
  21. url: '/device/resolutionRatio',
  22. method: 'GET',
  23. params: addTenantOrOrg({})
  24. }).then(({ data }) => {
  25. return {
  26. data: Object.keys(data).map(key => {
  27. return {
  28. value: key,
  29. label: `${key} ${data[key].slice(0, 3).map(device => device.name).join(', ')}`
  30. }
  31. })
  32. }
  33. })
  34. }
  35. export function getRatios () {
  36. return tenantRequest({
  37. url: '/device/resolutionRatio',
  38. method: 'GET',
  39. params: addTenant({})
  40. }).then(({ data }) => {
  41. return {
  42. data: Object.keys(data).map(key => {
  43. return {
  44. value: key,
  45. label: key
  46. }
  47. })
  48. }
  49. })
  50. }
  51. export function getTimeline (deviceId, options) {
  52. return request({
  53. url: `/content/deviceCalender/${deviceId}`,
  54. method: 'GET',
  55. ...options
  56. }).then(({ data }) => JSON.parse(data.eventDetail) || [])
  57. }
  58. export function getTimelineByRange (deviceId, startTime, endTime, options) {
  59. return request({
  60. url: `/content/deviceCalender/${deviceId}`,
  61. method: 'GET',
  62. params: {
  63. startTime,
  64. endTime
  65. },
  66. ...options
  67. }).then(({ data }) => JSON.parse(data.eventDetail) || [])
  68. }
  69. export function addDevice (data) {
  70. return add({
  71. url: '/device',
  72. method: 'POST',
  73. data
  74. })
  75. }
  76. export function updateDevice (data) {
  77. return update({
  78. url: '/device',
  79. method: 'PUT',
  80. data
  81. })
  82. }
  83. function deleteDeviceById (id) {
  84. return messageSend({
  85. url: `/device/${id}`,
  86. method: 'DELETE'
  87. }, '删除')
  88. }
  89. export function deleteDevice ({ id, name, masterId }) {
  90. if (__SUB_DEVICE__) {
  91. if (masterId) {
  92. return confirm(`删除备份设备【${name}】?`).then(() => deleteDeviceById(id))
  93. }
  94. return send({
  95. url: `/device/${id}/standbyDevice`,
  96. method: 'GET',
  97. params: { pageNum: 1, pageSize: 1 }
  98. }).then(({ data }) => confirm(
  99. data.length
  100. ? `删除主设备【${name}】后备份设备也将删除`
  101. : `删除设备【${name}】?`
  102. )).then(() => deleteDeviceById(id))
  103. }
  104. return confirm(`删除设备【${name}】?`).then(() => deleteDeviceById(id))
  105. }
  106. // isAttentionFlag 是否带关注标识位,开启值为1
  107. // attentionValue 过滤值,2为不过滤
  108. export function getDevices (query, options) {
  109. return getDevicesByQuery(addTenantOrOrg(query), options)
  110. }
  111. export function getDevicesByQuery (query, options) {
  112. const { tenant, org, ...params } = query
  113. if (tenant || org && org === store.getters.tenant) {
  114. return getDevicesByTenant(tenant || org, params, options)
  115. }
  116. return getDevicesByOrg(org, params, options)
  117. }
  118. export function getDevicesByTenant (tenant, query, options) {
  119. const { pageNum: pageIndex, pageSize, ...params } = query
  120. return tenantRequest({
  121. url: '/device/tenant/page',
  122. method: 'GET',
  123. params: {
  124. tenant,
  125. pageIndex,
  126. pageSize,
  127. ...params
  128. },
  129. ...options
  130. })
  131. }
  132. export function getDevicesByOrg (org, query, options) {
  133. const { pageNum: pageIndex, pageSize, ...params } = query
  134. return tenantRequest({
  135. url: '/device/relation/page',
  136. method: 'GET',
  137. params: {
  138. org,
  139. pageIndex,
  140. pageSize,
  141. ...params
  142. },
  143. ...options
  144. })
  145. }
  146. export function activateDevice ({ id, name }) {
  147. return confirmAndSend('激活', name, {
  148. url: '/device/batch/activate',
  149. method: 'PUT',
  150. data: [id]
  151. })
  152. }
  153. export function deactivateDevice ({ id, name }) {
  154. return confirmAndSend('停用', name, {
  155. url: '/device/batch/deactivate',
  156. method: 'PUT',
  157. data: [id]
  158. })
  159. }
  160. export function getDevice (id) {
  161. return request({
  162. url: `/device/${id}`,
  163. method: 'GET'
  164. })
  165. }
  166. export function getSubDevices (query) {
  167. const { id, pageNum: pageIndex, pageSize, ...params } = query
  168. return request({
  169. url: `/device/${id}/standbyDevice`,
  170. method: 'GET',
  171. params: {
  172. pageIndex, pageSize,
  173. ...params
  174. }
  175. })
  176. }
  177. export function addSubDevice ({ id }, data) {
  178. return add({
  179. url: `/device/${id}/addStandby`,
  180. method: 'POST',
  181. data
  182. })
  183. }
  184. export function addProductType (data) {
  185. return add({
  186. url: '/productType',
  187. method: 'POST',
  188. data
  189. })
  190. }
  191. export function updateProductType (data) {
  192. return update({
  193. url: '/productType',
  194. method: 'put',
  195. data
  196. })
  197. }
  198. export function deleteProductType ({ id, name }) {
  199. return del({
  200. url: `/productType/${id}`,
  201. method: 'DELETE'
  202. }, name)
  203. }
  204. export function getProductTypes (query) {
  205. const { pageNum: pageIndex, pageSize, ...params } = query
  206. return tenantRequest({
  207. url: '/productType/list',
  208. method: 'GET',
  209. params: addTenant({
  210. pageIndex, pageSize,
  211. ...params
  212. })
  213. })
  214. }
  215. export function addProduct (data) {
  216. return add({
  217. url: '/product',
  218. method: 'POST',
  219. data
  220. })
  221. }
  222. export function updateProduct (data) {
  223. return update({
  224. url: '/product',
  225. method: 'put',
  226. data
  227. })
  228. }
  229. export function deleteProduct ({ id, name }) {
  230. return del({
  231. url: `/product/${id}`,
  232. method: 'DELETE'
  233. }, name)
  234. }
  235. const productMap = {}
  236. export function getProduct (id) {
  237. if (productMap[id]) {
  238. return Promise.resolve({ data: productMap[id] })
  239. }
  240. return request({
  241. url: `/product/${id}`,
  242. method: 'GET'
  243. }).then(({ data }) => {
  244. if (data) {
  245. productMap[id] = data
  246. }
  247. return { data }
  248. })
  249. }
  250. export function getProducts (query) {
  251. const { pageNum: pageIndex, pageSize, ...params } = query
  252. return tenantRequest({
  253. url: '/product/list',
  254. method: 'GET',
  255. params: addTenant({
  256. pageIndex, pageSize,
  257. ...params
  258. })
  259. })
  260. }
  261. export function addDeviceGroup (data) {
  262. return add({
  263. url: '/deviceGroup',
  264. method: 'POST',
  265. data
  266. })
  267. }
  268. export function updateDeviceGroup (data) {
  269. return update({
  270. url: '/deviceGroup',
  271. method: 'PUT',
  272. data
  273. })
  274. }
  275. export function deleteDeviceGroup ({ id, name }) {
  276. return del({
  277. url: `/deviceGroup/${id}`,
  278. method: 'DELETE'
  279. }, name)
  280. }
  281. export function getDeviceGroups (query) {
  282. const { pageNum: pageIndex, pageSize, ...params } = query
  283. return tenantRequest({
  284. url: '/deviceGroup/list',
  285. method: 'GET',
  286. params: addUser({
  287. pageIndex, pageSize,
  288. ...params
  289. })
  290. })
  291. }
  292. export function getDevicesByGroup (id) {
  293. return tenantRequest({
  294. url: `/deviceGroup/${id}/device`,
  295. method: 'GET',
  296. params: addTenantOrOrg({})
  297. })
  298. }
  299. export function getDeviceTree () {
  300. return tenantRequest({
  301. url: '/deviceGroup/deviceTree',
  302. method: 'GET',
  303. params: addUser(addTenantOrOrg({}))
  304. })
  305. }
  306. export function addDevicesToGroup (id, deviceIds) {
  307. return add({
  308. url: `/deviceGroup/${id}/device`,
  309. method: 'POST',
  310. data: deviceIds
  311. })
  312. }
  313. export function deleteDeviceFromGroup (id, { id: deviceId, name }) {
  314. return confirmAndSend('移除', name, {
  315. url: `/deviceGroup/${id}/device`,
  316. method: 'DELETE',
  317. params: { deviceId }
  318. })
  319. }
  320. export function getDeviceStatistics () {
  321. return getDeviceStatisticsByPath(store.getters.org)
  322. }
  323. export function getDeviceStatisticsByPath (path, options) {
  324. return tenantRequest({
  325. url: '/device/listDeviceTotal',
  326. method: 'GET',
  327. params: path === store.getters.tenant
  328. ? { tenant: path }
  329. : { org: path },
  330. ...options
  331. })
  332. }
  333. const handeEnum = ['应用重启', '设备重启', '恢复出厂', '未干预']
  334. const typeEnum = ['primary', 'success', 'danger']
  335. const labelEnum = ['处理中', '成功', '失败']
  336. export function getDeviceAlarms (query) {
  337. const { pageNum: pageIndex, pageSize, ...params } = query
  338. return request({
  339. url: '/deviceException/list',
  340. method: 'GET',
  341. params: {
  342. pageIndex, pageSize,
  343. ...params
  344. }
  345. }).then(({ data, totalCount }) => {
  346. return {
  347. data: data.map(({ id, messageId, deviceName, level, pic, picUrl, type, handle, status, happenTime, bugMessage, ...item }) => {
  348. const alarm = {
  349. id, messageId, deviceName, level, happenTime,
  350. file: pic && picUrl
  351. ? {
  352. type: AssetType.IMAGE,
  353. url: picUrl,
  354. origin: true
  355. }
  356. : null,
  357. type: bugMessage || `预警码【${type}】`,
  358. handle: handeEnum[handle] || '-',
  359. status: handle <= 2 && status <= 2
  360. ? {
  361. type: typeEnum[status],
  362. label: labelEnum[status]
  363. }
  364. : { label: '-' }
  365. }
  366. SupportedAlarmStrategies.forEach(({ key }) => {
  367. alarm[key] = getTag(item[key])
  368. })
  369. return alarm
  370. }),
  371. totalCount
  372. }
  373. })
  374. }
  375. function getTag (status) {
  376. switch (status) {
  377. case 0:
  378. return {
  379. type: 'primary',
  380. label: '待发送'
  381. }
  382. case 1:
  383. return {
  384. type: 'success',
  385. label: '已发送'
  386. }
  387. case 2:
  388. return {
  389. type: 'warning',
  390. label: '发送中'
  391. }
  392. case 3:
  393. return {
  394. type: 'danger',
  395. label: '失败'
  396. }
  397. case 4:
  398. return {
  399. type: 'danger',
  400. label: '无目标'
  401. }
  402. case 5:
  403. return {
  404. type: 'warning',
  405. label: '未开启'
  406. }
  407. default:
  408. return null
  409. }
  410. }
  411. export function getUsersByInform ({ messageId }) {
  412. return send({
  413. url: '/deviceException/user/list',
  414. method: 'GET',
  415. params: { messageId }
  416. })
  417. }
  418. export function getTasks (query) {
  419. const { pageNum: pageIndex, pageSize, ...params } = query
  420. return request({
  421. url: '/device/functionTask',
  422. method: 'GET',
  423. params: {
  424. pageIndex, pageSize,
  425. ...params
  426. }
  427. })
  428. }
  429. export function addTask (task) {
  430. return add({
  431. url: '/device/functionTask',
  432. method: 'POST',
  433. data: task
  434. })
  435. }
  436. export function updateTask (task) {
  437. return update({
  438. url: '/device/functionTask',
  439. method: 'PUT',
  440. data: task
  441. })
  442. }
  443. export function deleteTask ({ taskId }) {
  444. return del({
  445. url: `/device/functionTask/${taskId}`,
  446. method: 'DELETE'
  447. })
  448. }
  449. export function activateTask ({ taskId }) {
  450. return confirmAndSend('启用', null, {
  451. url: '/device/functionTask/resume',
  452. method: 'PUT',
  453. data: [taskId]
  454. })
  455. }
  456. export function deactivateTask ({ taskId }) {
  457. return confirmAndSend('停用', null, {
  458. url: '/device/functionTask/pause',
  459. method: 'PUT',
  460. data: [taskId]
  461. })
  462. }
  463. export function getShadow (deviceId) {
  464. return request({
  465. url: `/device/shadow/${deviceId}`,
  466. method: 'GET'
  467. })
  468. }
  469. export function getRecordConfig (deviceId, config) {
  470. return request({
  471. url: '/deviceStream/config',
  472. method: 'GET',
  473. params: { deviceId },
  474. ...config
  475. })
  476. }
  477. export function addRecordConfig (data, config) {
  478. return request({
  479. url: '/deviceStream/config',
  480. method: 'POST',
  481. data,
  482. ...config
  483. })
  484. }
  485. export function updateRecordConfig (data, config) {
  486. return request({
  487. url: '/deviceStream/config',
  488. method: 'PUT',
  489. data,
  490. ...config
  491. })
  492. }
  493. export function authCode (stream, options) {
  494. return request({
  495. url: `/deviceStream/${stream}/authCode`,
  496. method: 'GET',
  497. ...options
  498. })
  499. }
  500. export function addDeviceAttention (deviceId) {
  501. return update({
  502. url: `/device/attention/add/${deviceId}`,
  503. method: 'POST',
  504. data: { deviceId }
  505. }, '关注')
  506. }
  507. export function cancelDeviceAttention (deviceId) {
  508. return update({
  509. url: `/device/attention/cancel/${deviceId}`,
  510. method: 'DELETE'
  511. }, '取关')
  512. }
  513. export function getDeviceAttentionList (options) {
  514. return request({
  515. url: '/device/user/attention/list',
  516. method: 'GET',
  517. ...options
  518. })
  519. }
  520. export function getStatusReport (id, options) {
  521. return request({
  522. url: '/device/screenPower/latestStatusReport',
  523. method: 'POST',
  524. data: { deviceIds: id },
  525. ...options
  526. })
  527. }
  528. export function getDevicesWithPower (params) {
  529. return request({
  530. url: '/device/bond/multiFunction/list',
  531. method: 'GET',
  532. params
  533. })
  534. }
  535. export function getDepartmentDeviceTree (options) {
  536. return getDepartmentDeviceTreeByGroup(store.getters.org, options)
  537. }
  538. export function getDepartmentDeviceTreeByGroup (path, options) {
  539. const data = addTenant()
  540. if (path && path !== data.tenant) {
  541. data.org = path
  542. }
  543. return tenantRequest({
  544. url: '/admin/department/device/list',
  545. method: 'GET',
  546. params: data,
  547. ...options
  548. })
  549. }