ProductController.php
2.5 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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/4/17
* Time: 1:15
*/
namespace app\user\controller;
use cmf\controller\AdminBaseController;
use think\Db;
class ProductController extends AdminBaseController
{
/*
* 获取产品列表
*/
public function getList() {
$request = input('request.');
$where ='';
$type = '';
if (!empty($request['type'])) {
$where['p.type'] = $request['type'];
$type =$request['type'];
}
$keywordComplex = [];
if (!empty($request['keyword'])) {
$keyword = $request['keyword'];
$keywordComplex['p.name|c.name'] = ['like', "%$keyword%"];
}
$usersQuery = Db::name('product');
$list = $usersQuery
->alias('p')
->join('user u','p.user_id = u.id','left')
->join('card c','c.user_id = u.id','left')
->whereOr($keywordComplex)
->where($where)
->field('p.*,c.name pname')
->order("p.create_time DESC")
->paginate(10);
// 获取分页显示
$page = $list->render();
$this->assign('list', $list);
$this->assign('page', $page);
$this->assign('type', $type);
// 渲染模板输出
return $this->fetch('list');
}
/*
* 获取产品详情
*/
public function getInfo() {
$id = input('param.id', 0, 'intval');
$info = Db::name('product')
->alias('p')
->join('user u','p.user_id = u.id','left')
->join('card c','c.user_id = u.id','left')
->field('p.*,c.name pname')
->where(['p.id' => $id])
->find();
$this->assign('info', $info);
// 渲染模板输出
return $this->fetch('info');
}
/*
* 产品上下架
*/
public function setState() {
$id = $this->request->param('id', 0, 'intval');
$info = Db::name('product')->where(["id" => $id])->field('')->find();
if ($info['type'] == 1) {
$type = 2;
} else {
$type = 1;
}
if (!empty($id)) {
$result = Db::name('product')->where(["id" => $id])->setField('type', $type);
if ($result !== false) {
$this->success("操作成功");
} else {
$this->error('操作失败!');
}
} else {
$this->error('数据传入失败!');
}
}
}