123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace app\controller;
- use think\App;
- use think\facade\View;
- use app\model\Seller as SellerModel;
- use app\model\Department as DepartmentModel;
- class Seller extends Base
- {
- protected $model;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->model = new SellerModel();
- }
- public function index()
- {
- $list = $this->model->select();
- return View::fetch('',['list'=>$list]);
- }
- public function save()
- {
- $departmentModel = new DepartmentModel();
- $departments = $departmentModel->column('name', 'id');
- if ($this->request->isPost()) {
- $param = $this->request->param();
-
- try {
- if ($param['id']) {
- $seller = $this->model->find(intval($param['id']));
- } else {
- $seller = new SellerModel();
- $seller->create_time = time();
- }
- $seller->name = $param['name'];
- $seller->department_id = $param['department_id'];
- $seller->department_name = $departments[$param['department_id']];
-
- $seller->save();
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('操作成功', 'seller/index');
- } else {
- $id = $this->request->has('id','route') ? intval($this->request->param('id')) : 0;
- if ($id) {
- $data = $this->model->find($id);
- } else {
- $data = [
- 'id' => 0,
- 'name' => '',
- 'department_id' => 1,
- ];
- }
- return View::fetch('', [
- 'data' => $data,
- 'departments' => $departments
- ]);
- }
- }
- public function delete($id)
- {
- if ($this->model->destroy($id)) {
- return json(['code'=>2]);
- } else {
- return json(['code'=>0]);
- }
- }
- }
|