diff --git a/application/admin/controller/Item.php b/application/admin/controller/Item.php index ed026f9..f841937 100644 --- a/application/admin/controller/Item.php +++ b/application/admin/controller/Item.php @@ -68,7 +68,7 @@ class Item extends Backend // dd($ruleList); $this->itemdata = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title'); foreach ($this->itemdata as &$v) { - $v['status'] = $v['status'] ?'normal' : 'aqua'; + $v['status'] = $v['status'] ?'normal' : 'hidden'; } unset($v); diff --git a/application/admin/controller/Order.php b/application/admin/controller/Order.php index 9e374fb..97d53be 100644 --- a/application/admin/controller/Order.php +++ b/application/admin/controller/Order.php @@ -3,6 +3,7 @@ namespace app\admin\controller; use app\common\controller\Backend; +use fast\Tree; use think\Db; use think\exception\PDOException; use think\exception\ValidateException; @@ -20,12 +21,40 @@ class Order extends Backend * @var \app\admin\model\Order */ protected $model = null; - + protected $sources = null; public function _initialize() { parent::_initialize(); $this->model = new \app\admin\model\Order; $this->view->assign("statusList", $this->model->getStatusList()); + + $sources = Db::name('source') + ->where('status',1) + ->field(['id','title','key_word','pid']) + ->order('pid','asc') + ->order('sort','desc') + ->select(); + $this->sources = $sources; + $filtered = array_filter($sources, function($item) { + return $item['pid'] == 0; + }); + + $pid_map = array_column($filtered,null,'id'); + $res = []; + foreach ($sources as $item){ + if ($item['pid'] != 0 && isset($pid_map[$item['pid']])){ + $res [] = [ + ...$item,'ptitle' => $pid_map[$item['pid']]['title'] + ]; + } + } +// dd($res); + +// Tree::instance()->init($sources); +// $data = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title'); +// dd($data); + $this->view->assign("statusList", $this->model->getStatusList()); + $this->view->assign("sources", $res); //$this->view->assign("collectList", $this->model->getCollectList()); //$this->view->assign("dispatchTypeList", $this->model->getDispatchTypeList()); } @@ -53,7 +82,7 @@ class Order extends Backend [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->field(['id','order_no','customer','tel','status','area_id','address', - 'source','source_uid','source','item_title','item_id', + 'source','source_shop','source_uid','source','item_title','item_id', 'detail','remark','images','create_time','update_time']) ->where($where) ->order($sort, $order) @@ -86,7 +115,10 @@ class Order extends Backend $this->model->validateFailException()->validate($validate); } - $params['enter_admin_id'] = $this->auth->id; + $sources = $this->sources; + $sources = array_column($sources,'title','id'); + $params['source_shop'] = $sources[$params['source']] ?? null; + $params['user_id'] = $this->auth->id; $params['status'] = 10; $params['order_no'] = $this->generateOrderNumber(); $params['create_time'] = date('Y-m-d H:i:s'); @@ -129,6 +161,9 @@ class Order extends Backend // 获取表单提交的数据 $data = input('post.row/a'); $data['update_time'] = date('Y-m-d H:i:s'); + $sources = $this->sources; + $sources = array_column($sources,'title','id'); + $data['source_shop'] = $sources[$data['source']] ?? null; // 更新订单信息 $order->save($data); diff --git a/application/admin/controller/Source.php b/application/admin/controller/Source.php new file mode 100644 index 0000000..09e41be --- /dev/null +++ b/application/admin/controller/Source.php @@ -0,0 +1,237 @@ +model = new \app\admin\model\Source; + $this->view->assign("statusList", $this->model->getStatusList()); + } + + + + /** + * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 + * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 + * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 + */ + public function index() + { + if ($this->request->isAjax()) { + // 必须将结果集转换为数组 + $search = request()->get('search') ?? false; +// dd($where); + $build = Db::name('source') + ->where('status',1); + if ($search){ + $build->where(function ($query) use ($search){ + $query->where('title','like','%'.$search.'%') + ->whereOr('key_word','like','%'.$search.'%'); + }); + } + + $ruleList = $build->field('id,pid,level,title,key_word,sort,status') +// ->order('sort','desc') +// ->fetchSql(true) + ->select(); + if ($search){ + $pids = array_column($ruleList,'pid') ; + $pidData = Db::name('source') + ->where('status',1)->whereIn('id',$pids) + ->field('id,pid,level,title,key_word,sort,status')->select(); + $ruleList = array_values(array_reduce(array_merge($ruleList,$pidData), function ($carry, $source) { + $carry[$source['id']] = $source; + return $carry; + }, [])); + } + + Tree::instance()->init($ruleList)->icon = [' ', ' ', ' ']; +// dd($ruleList); + $this->sourcedata = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title'); + foreach ($this->sourcedata as &$v) { + $v['status'] = $v['status'] ?'normal' : 'aqua'; + } + unset($v); + + $list = $this->sourcedata; + $total = count($this->sourcedata); + $result = array("total" => $total, "rows" => $list); + + return json($result); + } + return $this->view->fetch(); + } + + + /** + * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 + * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 + * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 + */ + + public function add() + { + if (false === $this->request->isPost()) { + // 必须将结果集转换为数组 + $ruleList = \think\Db::name("source") + ->where('pid',0) + ->field('id,pid,level,title,key_word') + ->order('sort DESC,id ASC')->select(); + + Tree::instance()->init($ruleList)->icon = [' ', ' ', ' ']; +// dd($ruleList); + $this->sourcedata = Tree::instance()->getTreeList(Tree::instance() + ->getTreeArray(0), 'title'); + $sourcedata = [0 => __('None')]; + foreach ($this->sourcedata as $k => $v) { + $sourcedata[$v['id']] = $v['title']; + unset($v['spacer']); + } + unset($v); + + $this->view->assign('sourcedata', $sourcedata); + return $this->view->fetch(); + } + $params = $this->request->post('row/a'); + if (empty($params)) { + $this->error(__('Parameter %s can not be empty', '')); + } + $params = $this->preExcludeFields($params); + + if ($this->dataLimit && $this->dataLimitFieldAutoFill) { + $params[$this->dataLimitField] = $this->auth->id; + } + $result = false; + Db::startTrans(); + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; + $this->model->validateFailException()->validate($validate); + } + + $pid = $params['pid']; + if ($pid > 0){ + $parent = $this->model->where('id',$pid)->find(); + if ($parent){ + $params['level'] = $parent->value('level') + 1; + } + }else{ + $params['level'] = 0; + } + + + $result = $this->model->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if ($result === false) { + $this->error(__('No rows were inserted')); + } + $this->success(); + } + + public function search(){ + + $keyword = request()->get('keyword'); + if (!$keyword){ + $this->success(data:[]); + }else{ + $data = model('source') + ->where('level','=',2) + ->where(function ($query)use ($keyword){ + $query->where('title','like','%'.$keyword.'%') + ->whereOr('key_word', 'like', "%{$keyword}%"); + }) + ->order('level','desc') + ->field('title,id,key_word') + ->limit(0,10) + ->select(); + $this->success(data:$data); + } + + } + + public function edit($ids = null) + { + $row = $this->model->get($ids); + if (!$row) { + $this->error(__('No Results were found')); + } + $adminIds = $this->getDataLimitAdminIds(); + if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { + $this->error(__('You have no permission')); + } + if (false === $this->request->isPost()) { + // 必须将结果集转换为数组 + $ruleList = \think\Db::name("source") + ->where('pid',0) + ->field('id,pid,level,title,key_word') + ->order('sort DESC,id ASC')->select(); + + Tree::instance()->init($ruleList)->icon = [' ', ' ', ' ']; +// dd($ruleList); + $this->sourcedata = Tree::instance()->getTreeList(Tree::instance() + ->getTreeArray(0), 'title'); + $sourcedata = [0 => __('None')]; + foreach ($this->sourcedata as $k => $v) { + $sourcedata[$v['id']] = $v['title']; + unset($v['spacer']); + } + unset($v); + + $this->view->assign('sourcedata', $sourcedata); + $this->view->assign('row', $row); + return $this->view->fetch(); + } + $params = $this->request->post('row/a'); + if (empty($params)) { + $this->error(__('Parameter %s can not be empty', '')); + } + $params = $this->preExcludeFields($params); + $result = false; + Db::startTrans(); + try { + //是否采用模型验证 + if ($this->modelValidate) { + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; + $row->validateFailException()->validate($validate); + } + $result = $row->allowField(true)->save($params); + Db::commit(); + } catch (ValidateException|PDOException|Exception $e) { + Db::rollback(); + $this->error($e->getMessage()); + } + if (false === $result) { + $this->error(__('No rows were updated')); + } + $this->success(); + } + +} diff --git a/application/admin/lang/zh-cn/source.php b/application/admin/lang/zh-cn/source.php new file mode 100644 index 0000000..fd5908f --- /dev/null +++ b/application/admin/lang/zh-cn/source.php @@ -0,0 +1,16 @@ + 'ID', + 'Pid' => 'PID', + 'Level' => '层级', + 'Title' => '服务名称', + 'Key_word' => '关键字', + 'Image' => '图标', + 'Sort' => '排序', + 'Status' => '状态', + 'Status 1' => '启用', + 'Set status to 1'=> '设为启用', + 'Status 0' => '关闭', + 'Set status to 0'=> '设为关闭' +]; diff --git a/application/admin/model/Source.php b/application/admin/model/Source.php new file mode 100644 index 0000000..8c5d186 --- /dev/null +++ b/application/admin/model/Source.php @@ -0,0 +1,49 @@ + __('Status 1'), '0' => __('Status 0')]; + } + + + public function getStatusTextAttr($value, $data) + { + $value = $value ?: ($data['status'] ?? ''); + $list = $this->getStatusList(); + return $list[$value] ?? ''; + } + + + + +} diff --git a/application/admin/validate/Source.php b/application/admin/validate/Source.php new file mode 100644 index 0000000..bf63a14 --- /dev/null +++ b/application/admin/validate/Source.php @@ -0,0 +1,27 @@ + [], + 'edit' => [], + ]; + +} diff --git a/application/admin/view/order/add.html b/application/admin/view/order/add.html index e15cf2e..f6de8fc 100644 --- a/application/admin/view/order/add.html +++ b/application/admin/view/order/add.html @@ -42,19 +42,26 @@