1)记录----日程安排
2)需求:
) 1.遍历月份到后台 实现切换月份 并显示当月的今天高亮 设计数据表
) 2. 添加日程 添加可见用户,只有可见用户才可以看到该发布的日程安排
) 3.日程提交后会在日历上显示高亮区域 点击可以看到详情 并可对其删除
)遍历月历:两种方法:1.封装日历类 2.控制器中写方法 我选择了第二种....
//前台代码:
<table class="gridtable">
<tr>
</tr>
<tr>
<td class="txt_center td_week">周一</td>
<td class="txt_center td_week">周二</td>
<td class="txt_center td_week">周三</td>
<td class="txt_center td_week">周四</td>
<td class="txt_center td_week">周五</td>
<td class="txt_center td_week">周六</td>
<td class="txt_center td_week">周日</td>
</tr>
<tr>
{for start="0" end='$week'}
<td class="txt_center td_week td_style"></td>
{/for}
{for start="1" end='$days+1'}
{if condition="($week+$i)%7 == 1"}
</tr>
{/if}
{if condition=" $i eq $day"}
<td class="txt_center td_week td_style" style="background-color: #6eddee">{$i}</td>
{else/}
<?php $time = strtotime("$year-$month-$i"); ?>
{if condition='in_array($time,$times)'}
<td title=" " class="txt_center td_week td_style" style="background-color: #ee2d08">
<a title="今日有提醒日程" id="{$new[$time]}" href="\admin/workschedule/show_workschedule?id={$new[$time]}">{$i}</a>
</td>
{else/}
<td class="txt_center td_week td_style">{$i}</td>
{/if}
{/if}
{/for}
<?php
while (($days+$week)%7 !== 0){
echo '<td class="txt_center td_week td_style"></td>';
$days++;
}
?>
</table>
public function schedule_list(Request $request)
{ #使用$request 是从视图中取出改变的$year $month
#判断当$year、$month是否为空 否则取时间戳中年月
$year= $request->param('year') ?? date('Y')+1;
$month = $request->param('month') ?? date('m');
#$true值判断年份增减
$true = $request->param('true');
#定义updateDay()方法取出渲染日历所需要的值
$arr = $this->updateDay($year,$month,$true);
$this->assign($arr);
return $this->fetch();
}
)该页年渲染视图的控制器 schedule_list()updateDay()方法将参数传入
)//queryAll()数据处理方法 根据当月时间范围和可见用户(用户id存储到数据库字段中) 判断该显示的那些日程安排能遍历
public function updateDay($year,$month,$true)
{
//该方法会return 到schedule_list 10个遍历日历的所以需要的数值
$nextmonth = $month + 1;
$prevmonth = $month - 1;
# 计算上个月
if ($month == 1) {
if($true==1){
$year++;
}
$prevmonth = 12;
}
# 计算下个月
if ($month == 12) {
if($true==0){
$year--;
}
$nextmonth = 1;
}
$day = date('d');
$days = date('t', strtotime("{$year}-{$month}-1"));
$week = date('w', strtotime("{$year}-{$month}-1")) -1 ; # 由于我是已week作为基数来判断的,采用0开始迭代,所以-1
if( $week == '-1'){
$week = 6;
}
# 计算上个月
if( $week == '-1'){
$week = 6;
}
$test=$this->queryAll($year,$month,$days);
$new = [];
$times= []; # 这个用来保存被标记的时间
foreach ($test as $val) {
$new[$val['remind_time']] = $val['id'];
array_push($times,$val['remind_time']);
}
return[
'week'=>$week,
'days'=>$days,
'year'=>$year,
'month'=>$month,
'test'=>$test,
'times'=>$times,
'new'=>$new,
'day'=>$day,
'prevmonth'=>$prevmonth,
'nextmonth'=>$nextmonth
];
}
public function queryAll($year,$month,$days)
{
#获取当前用户user_id 分配可看权限
$user_id=$this->user_id;
#拼接查询字段
$day1=0;
$day=1;
$a=$year."-".$month."-".$day1.$day;
$b=$year."-".$month."-".$days;
//确定时间戳范围
$dateMin=strtotime($a);
$dateMix=strtotime($b);
//根据时间戳取数据
try{
$arr=db('work_date')
->whereLike('shou_user_id',"%$user_id%")
->where('remind_time','between',[$dateMin,$dateMix])
->select();
}catch (\Exception $e){
$e->getMessage();
}
return $arr;
}
)日历遍历ok
)点击添加日程
)此处稍微有些杂乱 通过ajax post 提交数据 db类插入 判断时间日期逻辑 以及对数据进行处理后 存储
public function add_work()
{
if (request()->isPost()) {
try {
$param = json_decode(input('post.workData'), true);
if (count($param) == 0) {
return;
}
$param = $param[0];
//加入创建者 登录user_id到字段 拼接字段shou_user_id
$user_id=$this->user_id;
$param['shou_user_id']=$param['shou_user_id'].','.$user_id;
//取出发布人user_name
$user_name=db('user_all')
->where('user_id',$user_id)
->field('user_name')
->select();
$user_name=$user_name[0];
//验证时间范围
$param['create_time'] = strtotime(date('Y-m-d,H:i:s'));
$a = strtotime($param['start_time']);
$b = strtotime($param['over_time']);
$c = strtotime($param['remind_time']);
$d = strtotime(date('Y-m-d,H:i:s'));
//日程不合理、日程过期
if ($a > $b || $a > $c || $c > $b) {
return ['data' => 1, 'msg' => '时间范围有问题'];
}
if ($b < $d || $c<$d ) {
return ['data' => 2, 'msg' => '提醒 结束 时间已过期'];
}
//将时间戳 只取年月日 判断利于遍历
$a=(strtotime(date('Y-m-d',$a)));
$b=(strtotime(date('Y-m-d',$b)));
$c=(strtotime(date('Y-m-d',$c)));
$param['start_time'] = $a;
$param['over_time'] = $b;
$param['remind_time'] = $c;
$param['user_name']=$user_name['user_name'];
#tblName 乱码...去除该字段 这是一个bug 待解决.
unset($param['tblName']);
$result=db('work_date')->insert($param);
if($result===1){
return ['msg'=>'安排日程成功','data'=>'3'];
}else{
$this->error('添加日程安排失败!');
}
try {
} catch (\Exception $e) {
echo Db::name('work_date')->getLastSql();
halt($e->getMessage());
}
} catch (\Exception $e) {
error($e->getMessage());
}
}
return;
}
\\点击红色 日程安排
通过 a标签 href 进行传参 到show_workschesule()
<a title="今日有提醒日程" id="{$new[$time]}" href="\admin/workschedule/show_workschedule?id={$new[$time]}">{$i}</a>
public function show_workschedule(Request $request)
{
try {
$work_id = $request->param('id');
//发布人数数据:
$data=db('work_date')
->where('id',$work_id)
->field('id,start_time,over_time,title,content,remind_time,user_name')
->select();
$data=$data[0];
$a=$data['start_time'];
$b=$data['over_time'];
$c=$data['remind_time'];
$data['start_time']=date('Y-m-d',$a);
$data['remind_time']=date('Y-m-d H-i-s');
$data['over_time']=date('Y-m-d',$b);
} catch (\Exception $e) {
$e->getMessage();
}
$this->assign($data);
return $this->view->fetch('show_workschedule');
return $this->fetch();
}
)最后ok 有问题 或者有更好的方法可以子啊评论区给出哦~