ProductController.php 2.5 KB
<?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('数据传入失败!');
        }
    }
}