demo.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /**
  2. * @keep
  3. * 人员管理中树
  4. * Copyright (c) 2015 大华科技
  5. */
  6. import fetch from './ajax.js'
  7. // 子系统需要此模块引用时开启
  8. // ztree
  9. import './ztree/js/jquery.ztree.core.js'
  10. import './ztree/js/jquery.ztree.excheck.js'
  11. import './ztree/js/jquery.ztree.exedit.js'
  12. import './ztree/js/jquery.ztree.exhide.js'
  13. var vue = new Vue()
  14. var zTree, //树
  15. /* zNodesJson, //树返回的json */
  16. clickCB = null,
  17. _searchKey = '',
  18. treeId = '',
  19. openV = null,
  20. _videoShopObj = null, //videoShop页面的components对象
  21. onlineChannels = [],
  22. // arg = '',
  23. handleAsyncSuccess = null,
  24. _getTreeNodeCountFn = null,
  25. objectNodeType = [2, 10, 11, 12],
  26. devNodes = [],
  27. setting = $.extend(true, { //表格设置
  28. view: {
  29. dblClickExpand: true,
  30. showLine: false,
  31. addHoverDom: null,
  32. removeHoverDom: null,
  33. addDiyDom: (treeId, treeNode) => {
  34. if (treeNode.nodeType === 3) {
  35. devNodes.push(treeNode)
  36. }
  37. if (treeNode && treeNode.uniqueCode === '001') {
  38. let aObj = $('#' + treeNode.tId + '_ico_item')
  39. // let nodes = zTree.transformToArray(zTree.getNodes())
  40. // let devNodes = []
  41. // nodes.forEach((item) => {
  42. // if (item.nodeType === 3) {
  43. // devNodes.push(item)
  44. // }
  45. // })
  46. aObj.after(`<span class='show-dev tree-item ${_videoShopObj.hideDev ? 'off-dev' : ''}' id='showDev_${treeNode.uniqueCode}' title="${_videoShopObj.hideDev ? '显示设备' : '隐藏设备'}"></span>`)
  47. $(`#showDev_${treeNode.uniqueCode}`).on('click', function () {
  48. _videoShopObj.hideDev = !_videoShopObj.hideDev
  49. if (_videoShopObj.hideDev) {
  50. zTree.showNodes(devNodes)
  51. $("#showDev_001").attr('title', '隐藏设备')
  52. $("#showDev_001").removeClass("off-dev")
  53. } else {
  54. zTree.hideNodes(devNodes)
  55. $("#showDev_001").attr('title', '显示设备')
  56. $("#showDev_001").addClass("off-dev")
  57. }
  58. _videoShopObj.initShopTree(null)
  59. })
  60. }
  61. }
  62. },
  63. // async: {
  64. // enable: false,
  65. // autoParam: ["id=parentId", "nodeType=nodeType"],
  66. // url: function () {
  67. // // arg = arguments[1]
  68. // if (_videoShopObj.storeIds.length != 0 && _videoShopObj.activeIndex == 2) {
  69. // return basePath + '/membership/tree/zTree'
  70. // }
  71. // return basePath + '/membership/tree/zTree'
  72. // // return '/membership/tree/zTree?type=6&searchKey=' + _searchKey
  73. // },
  74. // type: "get",
  75. // otherParam: {
  76. // "type": function() {
  77. // return (_videoShopObj.storeIds.length != 0 && _videoShopObj.activeIndex == 2) ? '1;0_' + _videoShopObj.storeIds + ':1:1:1;' + (_videoShopObj.hideDev ? 0 : 1) + ';1::0' : '1;0:0:1:1;' + (_videoShopObj.hideDev ? 0 : 1) + ';1::0'
  78. // },
  79. // "searchKey": function() {
  80. // return decodeURIComponent(_searchKey)
  81. // }
  82. // },
  83. // token: sessionStorage.token || localStorage.token,
  84. // dataFilter: function (treeId, parentNode, responseData) {
  85. // console.log('videoshop tree dataFilter')
  86. // if (responseData.success) {
  87. // responseData = responseData.data
  88. // console.log('success data:' + JSON.stringify(responseData))
  89. // if(!responseData.length) {
  90. // return
  91. // }
  92. // setIcon(responseData)
  93. // if(responseData[0].uniqueCode === '001') {
  94. // responseData[0].iconSkin = 'rootdept'
  95. // }
  96. // setShowName(responseData)
  97. // return responseData
  98. // }
  99. // console.log(JSON.stringify(responseData))
  100. // }
  101. // },
  102. data: {
  103. key: {
  104. name: 'showName'
  105. },
  106. simpleData: {
  107. enable: true,
  108. idKey: "id",
  109. pIdKey: "parentId",
  110. rootPId: "001"
  111. }
  112. },
  113. callback: {
  114. // onClick: function (event, treeId, treeNode) { //点击树
  115. // console.log('-------------------------onClick videoshop--------------------')
  116. // try{console.log(JSON.stringify(treeNode));}catch(e){e && console.log(e.stack);}
  117. // let node = treeNode
  118. // if(treeNode.nodeType === 3) {
  119. // node = treeNode.getParentNode()
  120. // }
  121. // clickCB && clickCB(node);
  122. // //refreshTable(treeNode);
  123. // },
  124. onDblClick: function (event, treeId, treeNode) {
  125. openV && openV([treeNode])
  126. },
  127. // onAsyncSuccess: function (event, treeId, treeNode, msg) {
  128. // let rootNode = zTree.getNodes()[0]
  129. // changeStatus(onlineChannels)
  130. // console.log('videoshop tree rootNode: ')
  131. // rootNode && console.log(rootNode.name)
  132. // _getTreeNodeCountFn(!rootNode)
  133. // if (_searchKey) {
  134. // _searchKey = ''
  135. // let nodes = zTree.transformToArray(zTree.getNodes())
  136. // zTree.expandAll(true)
  137. // nodes.forEach((item) => {
  138. // showDev(item)
  139. // })
  140. // } else {
  141. // if (rootNode) {
  142. // zTree.expandNode(rootNode, true, false)
  143. // }
  144. // showDev(treeNode)
  145. // }
  146. // vue.isFunction(handleAsyncSuccess) && handleAsyncSuccess(treeNode)
  147. // },
  148. // beforeAsync: function () {
  149. // console.log('------------------beforeAsync--------------------')
  150. // return true
  151. // },
  152. onExpand: function (e, treeId, treeNode) {
  153. console.log('------------------onExpand--------------------')
  154. console.log(JSON.stringify(treeNode))
  155. },
  156. // onAsyncError: function (argument) {
  157. // console.log('----------videoshop onAsyncError----------')
  158. // console.log(JSON.stringify(argument))
  159. // }
  160. }
  161. })
  162. function showDev(treeNode) {
  163. if (treeNode && objectNodeType.indexOf(treeNode.nodeType) !== -1) {
  164. if (_videoShopObj.hideDev) {
  165. zTree.expandNode(treeNode, true, true, true)
  166. $(`#${treeNode.tId}_ul>li>span`).addClass('hide-node')
  167. $(`#${treeNode.tId}_ul>li>a`).addClass('hide-node')
  168. $(`#${treeNode.tId}_ul>li>ul`).addClass('pdl0')
  169. } else {
  170. $(`#${treeNode.tId}_ul>li>span`).removeClass('hide-node')
  171. $(`#${treeNode.tId}_ul>li>a`).removeClass('hide-node')
  172. $(`#${treeNode.tId}_ul>li>ul`).removeClass('pdl0')
  173. }
  174. }
  175. }
  176. function setShowName(treeNodes) {
  177. for(let i = 0, length = treeNodes.length; i < length; i++) {
  178. let onLineChannelNum = treeNodes[i] && treeNodes[i].onLineChannelNum ? treeNodes[i].onLineChannelNum : 0
  179. let totalChannelNum = treeNodes[i] && treeNodes[i].totalChannelNum ? treeNodes[i].totalChannelNum : 0
  180. if(treeNodes[i] && (treeNodes[i].nodeType == 1 || objectNodeType.indexOf(treeNodes[i].nodeType) !== -1)) {
  181. treeNodes[i].showName = treeNodes[i].name + '(' + onLineChannelNum + '/' + totalChannelNum + ')'
  182. } else {
  183. treeNodes[i].showName = treeNodes[i].name
  184. }
  185. treeNodes[i].children && setShowName(treeNodes[i].children)
  186. }
  187. }
  188. /**
  189. * @method openChannel() 过滤通道播放通道
  190. */
  191. function openChannel(data, treeNode, isCloseAllVideo) {
  192. let channels = []
  193. let len = data.length
  194. for (var i = 0; i < len; i++) {
  195. if (data[i].nodeType == 3) {
  196. //设备
  197. for (var j = 0, jL = data[i].children.length; j < jL; j++) {
  198. if (data[i].children[j]) {
  199. //data[i].children[j].devName = data[i].name
  200. data[i].children[j].parentId = treeNode.uniqueCode
  201. data[i].children[j].parentName = treeNode.name
  202. channels.push(data[i].children[j])
  203. }
  204. }
  205. } else if (data[i].nodeType == 4) {
  206. // 通道
  207. data[i].parentId = treeNode.uniqueCode
  208. data[i].parentName = treeNode.parentName
  209. channels.push(data[i])
  210. }
  211. }
  212. openV && openV(channels, true, isCloseAllVideo)
  213. }
  214. function getChannels(treeNode, bl, isCloseAllVideo) { //获取所有通道
  215. fetch({
  216. method: 'get',
  217. url: '/membership/tree/zTree',
  218. isSNoTip: true,
  219. data: {
  220. params: {
  221. type: '1;0:0:1:1;1;1::0',
  222. nodeType: treeNode.nodeType,
  223. parentId: treeNode.uniqueCode
  224. }
  225. },
  226. onSuccess: function (res) {
  227. let data = res.data.data
  228. openChannel(data, treeNode, isCloseAllVideo)
  229. }
  230. });
  231. }
  232. // 给树数据设置iconSkin属性为'dept'
  233. function setIcon(data) {
  234. $.each(data, function (k, node) {
  235. if (node.nodeType === 2) {
  236. node.iconSkin = 'shopIcon'
  237. } else if (node.nodeType === 1) {
  238. node.iconSkin = 'dept'
  239. } else if (node.nodeType === 4) {
  240. if(node.isOnline === 1) {
  241. node.iconSkin = 'channelIcon'
  242. } else {
  243. node.iconSkin = 'channelIconD'
  244. }
  245. } else if(node.nodeType === 3) {
  246. if(node.isOnline === 1) {
  247. node.iconSkin = 'deviceIcon'
  248. } else {
  249. node.iconSkin = 'deviceIconD'
  250. }
  251. } else if (node.nodeType === 10) {
  252. node.iconSkin = 'buildingIcon'
  253. } else if (node.nodeType === 11) {
  254. node.iconSkin = 'unitIcon'
  255. } else if (node.nodeType === 12) {
  256. node.iconSkin = 'houseIcon'
  257. }
  258. node.children && setIcon(node.children)
  259. })
  260. }
  261. /*
  262. * @parameter searchKey 树查询参数
  263. * @parameter refreshTableFn 刷新页面数据的函数(目前都是刷新table)
  264. * @parameter getTreeNodeCountFn 告诉页面当前树节点数
  265. */
  266. export let getShopTree = (searchKey, getTreeNodeCountFn, clickCb, video, elm, id, onAsyncSuccess, videoShopObj) => { //树初始化及加载
  267. handleAsyncSuccess = onAsyncSuccess
  268. clickCB = clickCb
  269. openV = video
  270. _videoShopObj = videoShopObj
  271. _searchKey = searchKey || ''
  272. treeId = id
  273. _getTreeNodeCountFn = getTreeNodeCountFn || function () {return null}
  274. return $.Deferred(function (defer) {
  275. fetch({
  276. // url: '/gateway/device/api/page',
  277. url: '/admin/dahua/device',
  278. method: 'POST',
  279. isNoLock: true,
  280. isSNoTip: true,
  281. data: {pageNum: 1, pageSize: 200, searchKey: _searchKey},
  282. onSuccess: data => {
  283. let responseData = data.data.data.pageData
  284. // console.log('success data:' + JSON.stringify(responseData))
  285. let channels = []
  286. if (responseData.length) {
  287. responseData.forEach(item => {
  288. if (item.channelList) {
  289. let channelNameArr = []
  290. item.channelList.forEach(channel => {
  291. channel.deviceId = item.deviceId
  292. channel.devPassword = item.devPassword
  293. channel.devUsername = item.devUsername
  294. channel.showName = channel.channelName
  295. channel.ability = item.ability
  296. channel.deviceModel = item.deviceModel
  297. channel.storeId = item.storeId
  298. channel.storeName = item.storeName
  299. channel.brand = item.brand
  300. channel.accessSource = item.accessSource
  301. if(channel.onlineStatus == 0) {
  302. channel.iconSkin = 'channelIconD'
  303. } else {
  304. channel.iconSkin = 'channelIcon'
  305. }
  306. if (!channelNameArr.includes(channel.channelName)) {
  307. channels.push(channel)
  308. channelNameArr.push(channel.channelName)
  309. }
  310. })
  311. // channels = channels.concat(item.channelList)
  312. }
  313. })
  314. }
  315. let root = {
  316. id: "001",
  317. isChecked: false,
  318. isInherit: 0,
  319. isOpen: true,
  320. isParent: true,
  321. name: "根节点",
  322. nodeType: 1,
  323. uniqueCode: "001",
  324. iconSkin: 'rootdept',
  325. showName: '根节点'
  326. }
  327. const reg = new RegExp(decodeURIComponent(_searchKey))
  328. root.children = channels.filter(item => reg.test(item.channelName))
  329. // root.children = channels
  330. zTree = $.fn.zTree.init($(elm), setting, [root])
  331. defer.resolve(zTree)
  332. }
  333. })
  334. }).promise()
  335. }
  336. /**
  337. * @method GetLeToken 获取乐橙的token
  338. */
  339. export function GetLeToken() {
  340. return new Promise((resolve, reject) => {
  341. fetch({
  342. // url: '/gateway/device/api/lechangeToken',
  343. url: '/admin/dahua/lechange',
  344. method: 'post',
  345. isSNoTip: true,
  346. isNoLock: true,
  347. timeout: 1000000,
  348. onSuccess: data => {
  349. resolve(data)
  350. }
  351. })
  352. })
  353. }
  354. /*
  355. * @parameter searchKey 树查询参数
  356. * @parameter refreshTableFn 刷新页面数据的函数(目前都是刷新table)
  357. * @parameter getTreeNodeCountFn 告诉页面当前树节点数
  358. */
  359. export let changeStatus = (param) => {
  360. onlineChannels = param
  361. var treeObj = $.fn.zTree.getZTreeObj(treeId);
  362. for(var i = 0, len = param.length; i < len; i++) {
  363. var node = treeObj.getNodesByParam("deviceId", param[i].deviceID, null)
  364. if(node && node.length) {
  365. for(var j = 0, nlen = node.length; j < nlen; j++) {
  366. if(node[j].channelId == param[i].channelID) {
  367. if(param[i].status == 0) {
  368. node[j].iconSkin = 'channelIcon'
  369. } else {
  370. node[j].iconSkin = 'conline'
  371. }
  372. node[j].iconSkin = node[j].onlineStatus === 1 ? node[j].iconSkin : 'channelIconD'
  373. treeObj.updateNode(node[j])
  374. }
  375. }
  376. }
  377. }
  378. }
  379. export function onDataSuccess() {
  380. let rootNode = zTree.getNodes()[0]
  381. // changeStatus(onlineChannels)
  382. console.log('videoshop tree rootNode: ')
  383. rootNode && console.log(rootNode.name)
  384. _getTreeNodeCountFn(!rootNode)
  385. if (_searchKey) {
  386. _searchKey = ''
  387. zTree.expandAll(true)
  388. } else {
  389. if (rootNode) {
  390. zTree.expandNode(rootNode, true, false)
  391. }
  392. showDev(null)
  393. }
  394. vue.isFunction(handleAsyncSuccess) && handleAsyncSuccess()
  395. }
  396. export let setDev = showDev