record-wv-bak.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel='stylesheet' href='../luckysheet/dist/plugins/plugins.css' />
  9. <link rel='stylesheet' href='../luckysheet/dist/plugins/css/pluginsCss.css' />
  10. <link rel='stylesheet' href='../luckysheet/dist/css/luckysheet.css' />
  11. <link rel='stylesheet' href='../luckysheet/dist/assets/iconfont/iconfont.css' />
  12. <link rel="stylesheet" href="../dist/css/layui.css">
  13. <style>
  14. .la-btn {
  15. position: absolute;
  16. bottom: 20px;
  17. left: 50%;
  18. margin-left: -35px
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="luckysheet"
  24. style="margin:0px;padding:0px;position:absolute;width:100%;height:100%;left: 0px;top: 0px;"></div>
  25. <script src="../jquery.min.js"></script>
  26. <script src="../dist/layui.js"></script>
  27. <script src="../luckysheet/dist/luckysheet.umd.js"></script>
  28. <script src="../luckysheet/dist/plugins/js/plugin.js"></script>
  29. <script src="./echarts.js"></script>
  30. <script src="./cqcyCode.js"></script>
  31. <script src="./canvertChart.js"></script>
  32. <!-- uni 的 SDK -->
  33. <script src="./wvIP.js"></script>
  34. <script type="text/javascript" src="../luckysheet/dist/plugins/js/uni-webview-js0.0.3_index.js"></script>
  35. <script>
  36. document.addEventListener('UniAppJSBridgeReady', function() {
  37. uni.getEnv(function(res) {
  38. console.log('当前环境:' + JSON.stringify(res));
  39. window.sessionName = 'device_report_index'
  40. sessionStorage.removeItem(window.sessionName)
  41. const id = window.location.search.split('?id=')[1]
  42. const token = window.localStorage.getItem('C_TOKEN')
  43. $.ajax({
  44. url: "" + id,
  45. type: 'GET',
  46. beforeSend: function(xhr) {
  47. // this.url =
  48. // "http://192.168.0.40:8081/reportTable/getReportTableById?id=" +
  49. // id
  50. this.url = window.location.origin +
  51. '/reportTable/getReportTableById?id=' + id
  52. xhr.setRequestHeader("Authorization", "Bearer " +
  53. token);
  54. xhr.setRequestHeader("token", token);
  55. },
  56. success: function(res) {
  57. console.log(res);
  58. const data = res.data
  59. if (res.code === 200) {
  60. // 报表数据
  61. let tableData = JSON.parse(data.reportTableData)
  62. let option = tableData.option
  63. // 基础数据项值
  64. let baseItem = tableData.baseItem
  65. // 数据项
  66. let reportTableItemList = data.reportTableItemList
  67. let reportChartList = data.reportChartList
  68. // 报表类型
  69. let reportTableType = data.reportTableType
  70. let luckysheetOption = {
  71. container: 'luckysheet', // 设定 DOM 容器的 id
  72. title: '报表模板', // 设定表格名称
  73. lang: 'zh', // 设定表格语言
  74. showinfobar: false, // 是否显示顶部信息栏
  75. showtoolbar: false, // 是否显示工具栏
  76. showtoolbarConfig: {
  77. paintFormat: true, //格式刷
  78. moreFormats: true, // 单元格格式
  79. font: true, // 字体
  80. fontSize: true, // 字号大小
  81. bold: true, // 粗体 (Ctrl+B)
  82. italic: true, // 斜体 (Ctrl+I)
  83. strikethrough: true, // 删除线 (Alt+Shift+5)
  84. underline: true, // 下划线 (Alt+Shift+6)
  85. textColor: true, // 文本颜色
  86. fillColor: true, // 单元格颜色
  87. border: true, // 边框
  88. mergeCell: true, // 合并单元格
  89. horizontalAlignMode: true, // 水平对齐方式
  90. verticalAlignMode: true, // 垂直对齐方式
  91. function: true, // 公式
  92. // image: true
  93. // chart: true
  94. },
  95. showsheetbar: false, // 是否显示底部 sheet 页按钮
  96. sheetFormulaBar: false, // 是否显示公式
  97. row: 120, // 是否显示底部 sheet 页按钮
  98. data: [{
  99. "name": "统计报表", //工作表名称
  100. }],
  101. cellRightClickConfig: { // 自定义配置单元格右击菜单
  102. copy: true, // 复制
  103. copyAs: false, // 复制为
  104. paste: true, // 粘贴
  105. insertRow: true, // 插入行
  106. insertColumn: true, // 插入列
  107. deleteRow: true, // 删除选中行
  108. deleteColumn: true, // 删除选中列
  109. deleteCell: false, // 删除单元格
  110. hideRow: false, // 隐藏选中行和显示选中行
  111. hideColumn: false, // 隐藏选中列和显示选中列
  112. rowHeight: true, // 行高
  113. columnWidth: true, // 列宽
  114. clear: false, // 清除内容
  115. matrix: false, // 矩阵操作选区
  116. sort: false, // 排序选区
  117. filter: false, // 筛选选区
  118. chart: true, // 图表生成
  119. image: false, // 插入图片
  120. link: false, // 插入链接
  121. data: false, // 数据验证
  122. cellFormat: false // 设置单元格格式
  123. },
  124. plugins: ['chart']
  125. }
  126. if (!option) {
  127. option = JSON.parse(JSON.stringify(luckysheetOption))
  128. }
  129. option.data = tableData.data
  130. // 设置工作表保护
  131. option.data[0].config.authority = {
  132. sheet: 1, // 如果为 1 或 true,则该工作表受到保护;如果为 0 或 false,则该工作表不受保护。
  133. hintText: '该工作表受到保护,无法操作', // 弹窗提示的文字
  134. }
  135. // 关闭右键菜单
  136. option.cellRightClickConfig.chart = false
  137. option.cellRightClickConfig.columnWidth = false
  138. option.cellRightClickConfig.rowHeight = false
  139. option.cellRightClickConfig.deleteColumn = false
  140. option.cellRightClickConfig.deleteRow = false
  141. option.cellRightClickConfig.insertColumn = false
  142. option.cellRightClickConfig.insertRow = false
  143. // 关闭工具栏
  144. option.showtoolbar = false
  145. option.enableAddRow = false
  146. option.showtoolbarConfig = {
  147. bold: false,
  148. border: false,
  149. fillColor: false,
  150. font: false,
  151. fontSize: false,
  152. function: false,
  153. horizontalAlignMode: false,
  154. italic: false,
  155. mergeCell: false,
  156. moreFormats: false,
  157. paintFormat: false,
  158. strikethrough: false,
  159. textColor: false,
  160. underline: false,
  161. verticalAlignMode: false
  162. }
  163. // 钩子函数
  164. option.hook = {
  165. workbookCreateAfter() {
  166. // 图表操作
  167. for (let i in reportChartList) {
  168. insertEChartInfo(reportChartList[i])
  169. }
  170. // option.data.forEach((data, i) => {
  171. // if (data.chart && data.chart.length > 0) {
  172. // data.chart.forEach((chart, j) => {
  173. // console.log(chart.chart_id)
  174. // let dom = document.getElementById(chart.chart_id + '_c')
  175. // if (dom) dom.style.display = 'none'
  176. // })
  177. // }
  178. // })
  179. // 绘制基础数据项
  180. drawBaseInfo(baseItem)
  181. // 绘制数据值
  182. drawTableData(reportTableItemList, reportTableType, data)
  183. luckysheet.setRangeShow('BH1')
  184. // convertChart(tableData)
  185. // printExcel()
  186. }
  187. }
  188. luckysheet.create(option)
  189. } else {
  190. }
  191. }
  192. })
  193. /** 绘制基础数据项布局信息 */
  194. function drawBaseInfo(baseData) {
  195. if (!baseData) return
  196. let option = luckysheet.getAllSheets()[0]
  197. option.celldata.map(item => {
  198. if (item.v.v) {
  199. item.v.v = String(item.v.v).trim()
  200. if (item.v.v.match(/\${([^}]+)}/g)) { // 替换${xxx}内部数据
  201. if (item.v.v.indexOf('${currDate}') > -1) {
  202. let val = item.v.v
  203. val = val.replace('${currDate}', baseData.currDate)
  204. luckysheet.setCellValue(item.r, item.c, val)
  205. }
  206. if (item.v.v.indexOf('${currDateTime}') > -1) {
  207. let val = item.v.v
  208. val = val.replace('${currDateTime}', baseData
  209. .currDateTime)
  210. luckysheet.setCellValue(item.r, item.c, val)
  211. }
  212. if (item.v.v.indexOf('${userName}') > -1) {
  213. let val = item.v.v
  214. val = val.replace('${userName}', baseData.userName)
  215. luckysheet.setCellValue(item.r, item.c, val)
  216. }
  217. if (item.v.v.indexOf('${winUserName}') > -1) {
  218. let val = item.v.v
  219. val = val.replace('${winUserName}', baseData
  220. .winUserName)
  221. luckysheet.setCellValue(item.r, item.c, val)
  222. }
  223. }
  224. }
  225. })
  226. }
  227. /** 向 Excel 插入图表 */
  228. function insertEChartInfo(chart) {
  229. let _self = this
  230. let info = JSON.parse(chart.standby)
  231. let reportChartItemList = chart.reportChartItemList
  232. let chartType = chart.chartType
  233. // 系列
  234. let series = info.option.series
  235. for (let i in series) {
  236. let temp = series[i]
  237. temp.data = []
  238. for (let j in reportChartItemList) {
  239. let _name = reportChartItemList[j].describe ?
  240. reportChartItemList[j].describe : reportChartItemList[j]
  241. .itemName
  242. if (chartType == 'pie') {
  243. info.option.legend = null
  244. info.option.tooltip = {
  245. trigger: 'item',
  246. formatter: '{b0}<br /> <b>{c0}</b>'
  247. }
  248. temp.name = _name
  249. let names = reportChartItemList[i].valueTimeList ? reportChartItemList[i].valueTimeList.split(',') : [];
  250. names = withDateFormatLength(names);
  251. let vals = reportChartItemList[i].valueList ? reportChartItemList[i].valueList.split(',') : [];
  252. names.forEach((name, j) => {
  253. temp.data.push({
  254. 'name': name,
  255. 'value': vals[j]
  256. })
  257. })
  258. } else {
  259. let _t = []
  260. let t = reportChartItemList[i].valueList ?
  261. reportChartItemList[i].valueList.split(',') : []
  262. t.forEach((temp) => {
  263. _t.push(parseFloat(temp))
  264. })
  265. temp.data = _t
  266. }
  267. }
  268. }
  269. if (chartType != 'pie') {
  270. // x 轴
  271. let xAxis = info.option.xAxis
  272. let legend = info.option.legend
  273. // xAxis.data = []
  274. legend.data = []
  275. let axisLabel = {};
  276. axisLabel.rotate = 20;
  277. xAxis.axisLabel = axisLabel
  278. let times = (reportChartItemList[0].valueTimeList) ? reportChartItemList[0].valueTimeList.split(",") : []
  279. xAxis.data = (reportChartItemList && reportChartItemList.length > 0) ?
  280. withDateFormatLength(times) : []
  281. // xAxis.data = xAxis.data.slice(0, 7)
  282. for (let i in reportChartItemList) {
  283. let name = reportChartItemList[i].describe ? reportChartItemList[i].describe : reportChartItemList[i].itemName
  284. // xAxis.data.push(name)
  285. legend.data.push(name)
  286. }
  287. }
  288. setTimeout(() => {
  289. const sheet = luckysheet.getLuckysheetfile()[0]
  290. let optionData = sheet.data
  291. try {
  292. let flag = false
  293. insertLuckysheetEChart({
  294. selector: '#luckysheet',
  295. info,
  296. sheet,
  297. optionData,
  298. echarts,
  299. luckysheet,
  300. $,
  301. _self,
  302. flag
  303. })
  304. } catch (e) {
  305. console.log(999, e == 'echarts is not defined')
  306. console.error(e)
  307. }
  308. }, 200)
  309. }
  310. /** 绘制表格数据 */
  311. function drawTableData(tableItemList, type, tableInfo) {
  312. if (!tableItemList || tableItemList.length == 0) {
  313. updateLocalExcelContent()
  314. return
  315. }
  316. // 事件驱动报表
  317. if (type == 2 || type == 4) {
  318. tableItemList.forEach((tableItem, i) => {
  319. if (i == 0) {
  320. let valueTimeList = tableItem.valueTimeList ? tableItem.valueTimeList.split(',') : []
  321. valueTimeList = withDateFormatLength(valueTimeList)
  322. let xAxis = tableItem.xaxis
  323. let yAxis = tableItem.yaxis - 1
  324. if (valueTimeList.length == 0) {
  325. luckysheet.setCellValue(xAxis, yAxis, '')
  326. } else {
  327. valueTimeList.forEach((v, j) => {
  328. luckysheet.setCellValue(xAxis + j, yAxis, v)
  329. })
  330. }
  331. }
  332. let valueList = tableItem.valueList ? tableItem.valueList
  333. .split(',') : []
  334. let xAxis = tableItem.xaxis
  335. let yAxis = tableItem.yaxis
  336. if (valueList.length == 0) {
  337. luckysheet.setCellValue(xAxis, yAxis, '')
  338. } else {
  339. valueList.forEach((v, j) => {
  340. luckysheet.setCellValue(xAxis + j, yAxis,
  341. v)
  342. })
  343. }
  344. })
  345. updateLocalExcelContent()
  346. return
  347. }
  348. // 设备报表
  349. if (type == 5 || type == 6) {
  350. tableItemList.forEach((tableItem, i) => {
  351. let standby = tableItem.standby ? JSON.parse(tableItem.standby) : {}
  352. let dataIndex = standby.index != null ? standby.index : -1
  353. let valueList = tableItem.valueList ? tableItem.valueList.split(',') : []
  354. let xAxis = tableItem.xaxis
  355. let yAxis = tableItem.yaxis
  356. // 序号、时间处理
  357. if (tableItem.timeItemType == 0) { // 序号、开始时间
  358. let valueIndexList = tableItem.valueIndexList ? tableItem.valueIndexList.split(',') : []
  359. deviceReportUpdateData('${index}', valueIndexList, false)
  360. let valueTimeList = tableItem.valueTimeList ? tableItem.valueTimeList.split(',') : []
  361. valueTimeList = withDateFormatLength(valueTimeList)
  362. deviceReportUpdateData('${startTime}', valueTimeList, false)
  363. }
  364. if (tableItem.timeItemType == 1) { // 结束时间
  365. let valueTimeList = tableItem.valueTimeList ? tableItem.valueTimeList.split(',') : []
  366. valueTimeList = withDateFormatLength(valueTimeList)
  367. deviceReportUpdateData('${stopTime}', valueTimeList, tableInfo.isGenCountTime == 1)
  368. }
  369. if (valueList.length == 0) {
  370. luckysheet.setCellValue(xAxis, yAxis, '')
  371. } else {
  372. valueList.forEach((v, j) => {
  373. luckysheet.setCellValue(xAxis + j, yAxis, v)
  374. })
  375. }
  376. })
  377. updateLocalExcelContent()
  378. return
  379. }
  380. // 其余报表信息
  381. tableItemList.forEach((tableItem, i) => {
  382. // let standby = tableItem.standby ? JSON.parse(tableItem.standby) : {}
  383. // let dataIndex = standby.index != null ? standby.index : -1
  384. let valueList = tableItem.valueList ? tableItem.valueList.split(
  385. ',') : []
  386. // let val = (dataIndex == -1 || (dataIndex + 1) > valueList
  387. // .length) ?
  388. // cqcyCode['invalidData'] : valueList[dataIndex]
  389. let xAxis = tableItem.xaxis
  390. let yAxis = tableItem.yaxis
  391. if (valueList.length == 0) {
  392. luckysheet.setCellValue(xAxis, yAxis, '')
  393. } else {
  394. valueList.forEach((v, j) => {
  395. luckysheet.setCellValue(xAxis + j, yAxis, v)
  396. })
  397. }
  398. })
  399. updateLocalExcelContent()
  400. }
  401. /** 设备报表时间特殊处理 */
  402. function deviceReportUpdateData(field, dataList, hasCalc) {
  403. let indexPos = sessionStorage.getItem(window.sessionName)
  404. if (indexPos) {
  405. let pos = indexPos.split(',')
  406. if (field == '${index}') {
  407. let yAxis = parseInt(pos[0])
  408. let xAxis = parseInt(pos[1])
  409. dataList.forEach((v, j) => {
  410. luckysheet.setCellValue(yAxis + j, xAxis, j + 1)
  411. })
  412. } else if (field == '${startTime}') {
  413. let yAxis = parseInt(pos[0])
  414. let xAxis = parseInt(pos[1]) + 1
  415. dataList.forEach((v, j) => {
  416. luckysheet.setCellValue(yAxis + j, xAxis, v)
  417. })
  418. } else if (field == '${stopTime}') {
  419. let yAxis = parseInt(pos[0])
  420. let xAxis = parseInt(pos[1]) + 2
  421. dataList.forEach((v, j) => {
  422. luckysheet.setCellValue(yAxis + j, xAxis, v)
  423. if (hasCalc) {
  424. let startDate = luckysheet.getCellValue(yAxis + j, xAxis - 1)
  425. let minutes = calculateMinutes(startDate, v)
  426. luckysheet.setCellValue(yAxis + j, xAxis + 1, minutes)
  427. }
  428. })
  429. }
  430. return
  431. }
  432. let option = luckysheet.getAllSheets()[0]
  433. option.celldata.map(item => {
  434. if (item.v.v) {
  435. item.v.v = String(item.v.v).trim()
  436. // 替换${xxx}内部数据
  437. if (item.v.v.match(/\${([^}]+)}/g)) {
  438. if (item.v.v.indexOf(field) > -1) {
  439. let yAxis = item.r
  440. let xAxis = item.c
  441. if (field == '${index}') {
  442. sessionStorage.setItem(window.sessionName, yAxis + ',' + xAxis)
  443. dataList.forEach((v, j) => {
  444. luckysheet.setCellValue(yAxis + j, xAxis, j + 1)
  445. })
  446. } else {
  447. dataList.forEach((v, j) => {
  448. luckysheet.setCellValue(yAxis + j, xAxis, v)
  449. if (hasCalc) {
  450. let startDate = luckysheet.getCellValue(yAxis + j, xAxis - 1)
  451. let minutes = calculateMinutes(startDate, v)
  452. luckysheet.setCellValue(yAxis + j, xAxis + 1, minutes)
  453. }
  454. })
  455. }
  456. }
  457. }
  458. }
  459. })
  460. }
  461. /** 删除不合理数据值 */
  462. function updateLocalExcelContent() {
  463. let option = luckysheet.getAllSheets()[0]
  464. option.celldata.map(item => {
  465. if (item.v.v) {
  466. item.v.v = String(item.v.v).trim()
  467. // 替换${xxx}内部数据
  468. if (item.v.v.match(/\${([^}]+)}/g)) {
  469. luckysheet.setCellValue(item.r, item.c, '')
  470. }
  471. }
  472. })
  473. }
  474. /** 计算分钟数 */
  475. function calculateMinutes(time1, time2) {
  476. // 将时间字符串转换为Date对象
  477. let date1 = new Date(time1);
  478. let date2 = new Date(time2);
  479. // 计算时间差(以毫秒为单位)
  480. let diffInMilliseconds = Math.abs(date2.getTime() - date1.getTime());
  481. // 将时间差转换为分钟
  482. let minutes = Math.floor(diffInMilliseconds / 1000 / 60);
  483. return minutes;
  484. }
  485. function withDateFormatLength(listDate) {
  486. if (!listDate || listDate.length == 0) {
  487. return []
  488. }
  489. let arr = []
  490. listDate.forEach((date) => {
  491. arr.push(date.substring(0, 19))
  492. })
  493. return arr
  494. }
  495. });
  496. });
  497. </script>
  498. </body>
  499. </html>