model = new CouponAreaModel(); } use Curd; protected $allowModifyFields = [ 'can_buy', 'status' ]; public function index () { if ($this->request->isAjax()) { if (input('selectFields')) { return $this->selectList(); } list($page, $limit, $where) = $this->buildTableParames(); $count = $this->model ->where($where) ->count(); // return 11; // return json_decode('[{"price":10, "fee":1},{"price":50, "fee":0.5},{"price":100, "fee":0}]'); $list = $this->model ->withoutField('password') ->withAttr('buy_time', function ($val, $data){ if (!$val){return '还未配置时间';} $curdata = json_decode($val); $cu_data['day'] = sr_getcurtime($curdata->day, 'Y-m-d'); $arr_time = $curdata->time; for ($i = 0; $i < count($arr_time); $i++){ $val1 = $curdata->time[$i]; $key = 'time'.($i+1); $cu_data[$key] = (sr_getcurtime($val1->start_time, 'H:i:s') . ' - ' . sr_getcurtime($val1->end_time, 'H:i:s')); } return ($cu_data['day']. '日'. ($cu_data['time1']?('【'.$cu_data['time1'].'】'):'' ). '|'. (isset($cu_data['time2'])?('【'.$cu_data['time2'].'】'):''). '|' .(isset($cu_data['time3'])?('【'.$cu_data['time3'].'】'):'')); }) ->where($where) ->page($page, $limit) ->order($this->sort) ->select(); $data = [ 'code' => 0, 'msg' => '', 'count' => $count, 'data' => $list, ]; return json($data); } return $this->fetch(); } /** * @NodeAnotation(title="添加") */ public function add() { if ($this->request->isAjax()){ $row = request()->post(); $row['create_time'] = sr_getcurtime(time()); unset($row['file']); $modelplan = new CouponPlanModel(); Db::startTrans(); try { $a_id = $this->model->insertGetId($row); $modelplan->insert([ 'area_id'=>$a_id, 'target_num'=>$row['begin_num'], 'create_time'=>sr_getcurtime(time()), 'less_num'=>$row['begin_num'], 'buy_least'=>10, 'buy_most'=>1000 ]); Db::commit(); }catch (\Exception $e){ Db::rollback(); $this->error('添加失败'. $e->getMessage()); } $this->success('添加成功'); } return $this->fetch(); } /** * @NodeAnotation(title="修改") */ public function edit ($id) { $row = $this->model->find($id); empty($row) && $this->error('数据不存在'); if ($this->request->isAjax()) { $post = $this->request->post(); $rule = []; $this->validate($post, $rule); try { if (!empty($post['content'])) { $post['content'] = htmlspecialchars_decode($post['content']); } $save = $row->save($post); } catch (\Exception $e) { $this->error('保存失败'); } $save ? $this->success('保存成功') : $this->error('保存失败'); } $this->assign('row', $row); return $this->fetch(); } /** * @NodeAnotation(title="配置时间") */ public function addtime($id){ $row = $this->model->find($id); empty($row) && $this->error('数据不存在'); if ($this->request->isAjax()) { $post = $this->request->post(); $day = strtotime($post['day']); $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); $endToday=mktime(24,0,0,date('m'),date('d'),date('Y')); $is_today = false; if ($beginToday<=$day && $endToday>$day){ $is_today = true; } $timearr = []; if ($post['time1']){ $arrtime1 = explode(' - ', $post['time1']); // sr_log('aaa'.$post['day'].' '. $arrtime1[0]); $arrtime1 = [strtotime($post['day'].' '. $arrtime1[0]), strtotime($post['day'].' '. $arrtime1[1])]; array_push($timearr, ['start_time' => $arrtime1[0], 'end_time' => $arrtime1[1]]); } if ($post['time2']){ $arrtime2 = explode(' - ', $post['time2']); $arrtime2 = [strtotime($post['day'].' '.$arrtime2[0]), strtotime($post['day'].' '.$arrtime2[1])]; array_push($timearr, ['start_time' => $arrtime2[0], 'end_time' => $arrtime2[1]]); } if ($post['time3']){ $arrtime3 = explode(' - ', $post['time3']); $arrtime3 = [strtotime($post['day'].' '.$arrtime3[0]), strtotime($post['day'].' '.$arrtime3[1])]; array_push($timearr, ['start_time' => $arrtime3[0], 'end_time' => $arrtime3[1]]); } if (count($timearr) == 0){ $this->error('请选择时间段'); } foreach ($timearr as $key=>$value){ if ($timearr[$key]['start_time'] > $timearr[$key]['end_time']){ $this->error('时间段开始时间比如小于结束时间'); } } if (!compareDate($timearr)){ $this->error('时间段交叉,保存失败'); } try { $save = $row->save([ 'buy_time' => json_encode(['day'=>$day, 'time'=>$timearr]), 'is_today'=>($is_today?1:0) ]); } catch (\Exception $e) { $this->error('保存失败'); } $save ? $this->success('保存成功') : $this->error('保存失败'); } $curdata = json_decode($row['buy_time']); if ($curdata){ $cu_data['day'] = sr_getcurtime($curdata->day, 'Y-m-d'); $arr_time = $curdata->time; // dd($arr_time); for ($i = 0; $i < count($arr_time); $i++){ $val = $curdata->time[$i]; $key = 'time'.($i+1); $cu_data[$key] = (sr_getcurtime($val->start_time, 'H:i:s') . ' - ' . sr_getcurtime($val->end_time, 'H:i:s')); } $this->assign('row', $cu_data); } return $this->fetch(); } }