Skip to content

Commit

Permalink
111
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyu8866 committed Nov 7, 2024
1 parent 73034ae commit d88328a
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
184 changes: 184 additions & 0 deletions dian/dian.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link href="https://www.yiwuzhishu.cn/Public/layui/layui_2.9/css/layui.css" rel="stylesheet">
<style>
@media screen and (min-width: 1400px) {
.layui-container {
width: 1400px;
}
}
</style>
</head>

<body>
<div class="layui-container">
<table class="layui-hide" id="ID-table-demo-editmodes"></table>
</div>
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-group">
<button class="layui-btn layui-btn-sm" lay-on="add">添加</button>
<button class="layui-btn layui-btn-sm" lay-on="pull">拉取</button>
<button class="layui-btn layui-btn-sm" lay-on="push">推送</button>
<button class="layui-btn layui-btn-sm" lay-on="clear">清除本地</button>
</div>
</script>
<script src="https://www.yiwuzhishu.cn/Public/layui/layui_2.9/layui.js"></script>
<script>
layui.use(function () {
var $ = layui.$, table = layui.table, laydate = layui.laydate, util = layui.util, layer = layui.layer;

let global_data = layui.data('electricity').v || [];
if (global_data.length == 0) {
getData();
}

// 渲染
table.render({
elem: '#ID-table-demo-editmodes',
height: 'full-50',
css: [
'.layui-table-cell{height: 46px; line-height: 36px;}',
'.layui-table-cell input{height: 36px; padding: 0 5px;}'
].join(''),
toolbar: '#toolbarDemo',
defaultToolbar: ['filter'],
data: global_data,
cols: [
[
{ field: 'date', title: '日期', rowspan: 2, minWidth: 130, align: 'center' },
{ field: 'balance', title: '24点', minWidth: 100, align: 'center', edit: 'text' },
{ field: 'morning', title: '早上', minWidth: 100, align: 'center', edit: 'text' },
{ field: 'noon', title: '中午', minWidth: 100, align: 'center', edit: 'text' },
{ field: 'night', title: '晚上', minWidth: 100, align: 'center', edit: 'text' },
{ field: 'usage', title: '一天使用量', minWidth: 140, align: 'center' },
{ field: 'usage_night', title: '24点-08点使用量', minWidth: 140, align: 'center' },
{ field: 'usage_day', title: '08点-19点使用量', minWidth: 140, align: 'center' },
{ field: 'usage_fromWork', title: '19点-24点使用量', minWidth: 140, align: 'center' },
{ field: 'usage_noon', title: '08点-中午使用量', minWidth: 140, align: 'center' },
{ field: 'usage_noon_1', title: '中午-19点使用量', minWidth: 140, align: 'center' },
]
],
done: function (res, curr, count) {
table.on('edit(ID-table-demo-editmodes)', function (obj) {
var value = obj.value, field = obj.field;

var update = {};
update[field] = value;
obj.update(update);
saveTable();

calculateUsage();
});
}
});

util.on({
add: function () {
addTable();
},
push: function () {
if (!layui.data('electricity').v) {
layer.msg('没有数据');
return;
}
$.post('/dian.php', {data: JSON.stringify(layui.data('electricity').v)}, function(r) {
let res = JSON.parse(r);
if (res.code == 1) {
layer.msg(res.msg);
}
})
},
pull: function () {
getData()
},
clear: function () {
localStorage.clear();
}
})

function addTable(date) {
let d = table.cache['ID-table-demo-editmodes'];
if (d.length) {
date = new Date(d[d.length - 1].date);
} else {
date = new Date();
date.setDate(date.getDate() - 1);
}

d.push({ date: addOneDay(date), balance: '', morning: '', night: '', noon: '' });
table.renderData('ID-table-demo-editmodes');
}

function addOneDay(date, days = 1) {
let newDate = new Date(date.getTime());
let day = newDate.getDate();
newDate.setDate(day + days);
return util.toDateString(newDate, 'yyyy-MM-dd');
}

setTimeout(() => {
calculateUsage();
}, 500);
function calculateUsage(d) {
if (d) {
table.cache['ID-table-demo-editmodes'] = d
} else {
d = table.cache['ID-table-demo-editmodes']
}
let len = d.length;
for (let i = 0; i < len; i++) {
let now_balance = parseFloat(d[i].balance),
next_balance = i < len - 1 ? parseFloat(d[i + 1].balance) : 0,
morning = parseFloat(d[i].morning),
night = parseFloat(d[i].night),
noon = parseFloat(d[i].noon);
d[i].usage = _calc(now_balance, next_balance);
d[i].usage_day = _calc(morning, night);
d[i].usage_night = _calc(now_balance, morning);
d[i].usage_fromWork = _calc(night, next_balance);
d[i].usage_noon = _calc(morning, noon);
d[i].usage_noon_1 = _calc(noon, night);
}

table.renderData('ID-table-demo-editmodes');

function _calc(arg, arg1) {
return arg > 0 && arg1 > 0 ? (arg - arg1).toFixed(2) : '--';
}
}

function getData() {
$.get('/dian.php', function(res) {
if (res) {
let data = JSON.parse(res);
calculateUsage(data);
}
})
}

function saveTable() {
let d = table.cache['ID-table-demo-editmodes'];
let data = d.map((item) => {
return {
date: item.date,
balance: item.balance,
morning: item.morning,
night: item.night,
noon: item.noon,
}
});
layui.data('electricity', {
key: 'v',
value: data
});
}
});
</script>
</body>

</html>
19 changes: 19 additions & 0 deletions dian/dian.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
$p = $_POST['data'];
if (isset($p) && !empty($p)) {
file_put_contents('dian.txt', $p);
echo json_encode(array('code' => 1, 'msg' => '保存成功'));
} else {
echo file_get_contents('dian.txt');
}

// $userAgent = $_SERVER['HTTP_USER_AGENT'];
// if (strpos($userAgent, 'MicroMessenger') !== false) {
// header('Location: https://www.yiwuzhishu.cn/jz.php?type=wx&id=746');
// } elseif (strpos($userAgent, 'AlipayClient') !== false) {
// header('Location: https://www.yiwuzhishu.cn/jz.php?type=zfb&id=746');
// } else {
// // 其他方式访问
// echo '用户通过其他方式访问';
// }
?>
1 change: 1 addition & 0 deletions dian/dian.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"date":"2024-10-21","balance":"35.76","morning":"35.55","night":"35.01","noon":""},{"date":"2024-10-22","balance":"34.56","morning":"34.33","night":"33.86","noon":""},{"date":"2024-10-23","balance":"33.59","morning":"33.4","night":"32.83","noon":""},{"date":"2024-10-24","balance":"32.44","morning":"32.24","night":"31.84","noon":""},{"date":"2024-10-25","balance":"31.45","morning":"31.26","night":"29.95","noon":""},{"date":"2024-10-26","balance":"29.75","morning":"29.36","night":"28.73","noon":""},{"date":"2024-10-27","balance":"28.02","morning":"","night":"","noon":""},{"date":"2024-10-28","balance":"26.04","morning":"25.8","night":"","noon":""},{"date":"2024-10-29","balance":"24.58","morning":"24.36","night":"23.94","noon":""},{"date":"2024-10-30","balance":"23.06","morning":"22.8","night":"22.36","noon":""},{"date":"2024-10-31","balance":"21.47","morning":"","night":"","noon":""},{"date":"2024-11-01","balance":"20.05","morning":"19.83","night":"19.51","noon":""},{"date":"2024-11-02","balance":"18.58","morning":"17.86","night":"","noon":"17.86"},{"date":"2024-11-03","balance":"16.98","morning":"","night":"","noon":""},{"date":"2024-11-04","balance":"15.38","morning":"15.16","night":"","noon":""},{"date":"2024-11-05","balance":"14.43","morning":"14.09","night":"13.75","noon":""},{"date":"2024-11-06","balance":"13.25","morning":"13.03","night":"","noon":""},{"date":"2024-11-07","balance":"12.09","morning":"11.86","night":"","noon":""}]

0 comments on commit d88328a

Please sign in to comment.