GoodsController.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;
class GoodsController extends AdminBaseController
{
public $table_name_tag = 'goods';
//列表页
public function data_list()
{
//获取筛选参数
$param = $this->request->param();
//1
$keyword = empty($param['keyword']) ? '' : $param['keyword'];
if (!empty($keyword)) {
$where['name'] = ['like', "%$keyword%"];
}
//2
$type = empty($param['type']) ? '' : $param['type'];
if (!empty($type)) {
$where['type'] = ['eq', $type];
}
$where['delete_time'] = 0;
$list = DB::name($this->table_name_tag)->where($where)->select()->toArray();
$this->assign('list', $list);
return $this->fetch();
}
//列表编辑
public function data_edit()
{
$param = $this->request->param();
//提交数据
if ($this->request->isPost()) {
//添加提交
$datain['name'] = $param['name'];
$datain['price'] = $param['price'];
$datain['des'] = $param['des'];
$datain['hetong'] = $param['hetong'];
$datain['create_time'] = time();
//提交修改
if (!empty($param['id'])) {
$datain['id'] = $param['id'];
$result = DB::name($this->table_name_tag)->update($datain);
if ($result) {
$this->success('修改成功!', url('data_list'));
} else {
$this->error('添加失败!');
}
}else{
$this->error('暂时无法添加数据!');
$result = DB::name($this->table_name_tag)->insert($datain);
if ($result) {
$this->success('添加成功!', url('data_list'));
} else {
$this->error('添加失败!');
}
}
} else {
//展示表单
if (!empty($param['id'])) {
$info = DB::name($this->table_name_tag)->where(['id' => $param['id']])->find();
}else{
$info=[];
}
$this->assign('info', $info);
return $this->fetch();
}
}
//列表状态修改
public function data_status()
{
$param = $this->request->param();
$id = $param['id'];
$type = $param['type'];
if ($type == 'ban') {
$field = 'status';
$field_value = '0';
$field_msg = '停用';
} elseif ($type == 'cancelban') {
$field = 'status';
$field_value = '1';
$field_msg = '启用';
} elseif ($type == 'del') {
$field = 'delete_time';
$field_value = time();
$field_msg = '删除';
}
if($field){
$result = Db::name('goods')->where(["id" => $id])->setField($field, $field_value);
if ($result != false) {
$this->success("{$field_msg}成功!");
} else {
$this->error("{$field_msg}失败!");
}
}else{
$this->error("数据错误!");
}
}
}