model = new CustomerSpecModel(); $this->assign('typeList', StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title','id')); $this->relationField=['customer']; } //查看 #[Route("GET,JSON","index")] public function index() { return $this->_index(); } /** * 添加 */ #[Route('GET,POST','add')] public function add() { if (false === $this->request->isPost()) { return $this->fetch(); } $customer_id =$this->request->post('customer_id/d', 0); $type_box =$this->request->post('type_box'); $type_list =$this->request->post('type_list'); if(empty($customer_id) || empty($type_box) || empty($type_list)) return resp_json(0,'请填写完整信息'); //判断是否有发货价格 foreach ($type_box as $key => $item) { if(!(isset($item['price']) && $item['price'] > 0)){ unset($type_box[$key]); } } if(empty($type_box)) return resp_json(0, '请填写发货价格'); if($this->model->where('customer_id', $customer_id)->count()>0) return resp_json(0,'客户信息已存在'); $result = false; Db::startTrans(); try { $result = $this->model->save([ 'customer_id'=> $customer_id, 'variety' => json_encode($type_list, JSON_UNESCAPED_UNICODE), 'specs' => json_encode($type_box, JSON_UNESCAPED_UNICODE), ]); if($this->callback){ $callback=$this->callback; $callback($this->model); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('没有新增任何数据')); } return resp_json(200,'操作成功'); } /** * 编辑 */ #[Route('GET,POST','edit')] public function edit(mixed $row=null) { if (false === $this->request->isPost()) { $ids = $this->request->get('ids'); if(!$row || is_array($row)){ $row = $this->model->find($ids); } if (!$row) { $this->error(__('没有找到记录')); } $this->assign('row', $row); return $this->fetch(); } $params = $this->request->post(""); if(empty($params['ids']) || empty($params['type_box']) || empty($params['type_list'])) return resp_json(0,'请填写完整信息'); //判断是否有发货价格 if(SpecService::getIsZeroSpecsPrice($params['type_box']) == false) return resp_json(0, '请填写发货价格'); $result = false; Db::startTrans(); try { $result = $this->model->where('id', $params['ids'])->save([ 'variety' => json_encode($params['type_list'], JSON_UNESCAPED_UNICODE), 'specs' => json_encode($params['type_box'], JSON_UNESCAPED_UNICODE), ]); if($this->callback){ $callback=$this->callback; $callback($row); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('没有数据被更新')); } return resp_json(200,'操作成功'); } //删除 #[Route("GET,POST","del")] public function del() { //通过定义callback回调函数来执行删除后的操作 $this->callback=function ($ids){}; return $this->_del(); } //更新 #[Route("GET,POST","multi")] public function multi() { //通过定义callback回调函数来执行更新后的操作 $this->callback=function ($ids,$field,$value){}; return $this->_multi(); } #[Route('GET','get_box')] public function get_box() { if($this->request->isAjax()){ $type_id = $this->request->get('type_id/d', 0); if(empty($type_id)) return resp_json(0, '参数错误'); $list = ProductConfig::where('type_id', $type_id)->order('sort desc')->field('id, title')->select(); return json($list, 200); } } }