慕桂英546537
自己摸索了一上午,写了一个能运行的方案。
// +---------------------------------------------
// | 体重记录表
// +---------------------------------------------
public function weighe()
{
if($this->request->isPost()) {
$data['weight'] = $this->request->param('weighe', '' , 'trim');
if(!preg_match("/^\d*$/", $data['weight'])) return $this->error('提交体重格式不正确!');
$time = time();
$nowTime = date('H', $time);
$_time = $nowTime <= 12 ? '早上' : ($nowTime >= 18 ? '晚上' : '');
$today = Time::today();
$today[2] = strtotime(date('Y-m-d', $time).' 12:00:00'); // 添加当日12点中午的时间戳到数组中
if ($nowTime >= 13 && $nowTime < 18) return $this->error('当前时间段不允许提交体重记录');
// 自动判断当前时间段内是否已经提交记录
if ($nowTime <= 12) {
$map['add_at'] = ['between', [$today[0], $today[2]]];
} elseif (18 <= $nowTime && $nowTime <= 23) {
$map['add_at'] = ['between', [$today[2], $today[1]]];
}
$map['wx_openid'] = $this->userInfo['openid'];
$row = Db::name('weights')->where($map)->count();
unset($map);
if ($row > 0) return $this->error($_time.'已经提交过体重记录了');
// 执行插入前先判断单日是否已经存在提交
$map['wx_openid'] = $this->userInfo['openid'];
$map['add_at'] = ['between', [$today[0], $today[1]]];
if (1 > Db::name('weights')->where($map)->count()) {
unset($map);
$map['customer_weixin_openid'] = $this->userInfo['openid'];
Db::name('customers')->where($map)->setInc('custmer_add_weight_days'); // 当日第一次提交,更新提交次数到客户表
}
$data['wx_openid'] = $this->userInfo['openid'];
$data['add_at'] = time();
if (Db::name('weights')->insert($data) > 0) return $this->success('体重记录提交成功!');
return $this->error('体重记录提交失败');
}
// 提交满指点天数才可发表满意评价
$map['customer_weixin_openid'] = $this->userInfo['openid'];
$rows = Db::name('customers')->where($map)->field('custmer_add_weight_days')->find();
$this->assign('disabled', $rows['custmer_add_weight_days'] > 25 ? false : true);
unset($map);
$map['wx_openid'] = $this->userInfo['openid'];
$rows = Db::name('weights')->where($map)->order('add_at DESC')->field('wx_openid', true)->select();
$this->assign('list', $this->getData($rows));
return $this->fetch();
}
能达到预期的效果,但不知道有没有未知的BUG。哪位大神还有比较好的方案可以改进下吗?