仿乐优商城后台管理-前端vue+后端thinkphp5.1+数据库mysql项目开发----后端第2天

仿乐优商城后端thinkphp5.1开发文档


目录


内容

3、商品管理

3.1、品牌管理

3.1.1、规划
  • 控制器创建:php think make:controller api/Brand
  • 模型创建: php think make:model api/Brandm
  • 功能:
    • 品牌列表(分页,搜索)
    • 品牌新增
    • 品牌修改
    • 品牌删除
  • 路由:遵循Restful风格
功能 请求方法 路由 函数 参数 返回值
列表(分页、搜索) get ‘api/item/brand/page’ page 分页信息 分类列表
新增 post ‘api/item/brand’ addBrand 品牌信息 布尔值
根据ID查询分类信息 get ‘api/item/brand/categories’ getCategoriesByBid 品牌ID 分类层级列表
文件上传 post ‘api/item/brand/uploadImage’ uploadImage 文件信息 文件网络地址
修改 put ’api/item/brand’ editBrand 品牌信息 布尔值
删除 delete ’api/item/brand’ deleteBrand 品牌ID,logo地址 布尔值
3.1.2、控制器Brand.php
  • 源代码@2.2-1:
<?php

namespace app\api\controller;

use think\App;
use think\Controller;
use think\Db;
use think\facade\Env;
use think\Request;
use app\api\model\Brandm;
use think\facade\Config;

class Brand extends Controller
{
    //获取品牌分页列表
    public function page(Request $request)
    {
//        return json($request->param());
        // 获取参数
        $page = $request->param('page', 1); // 当前页
        $rows = $request->param('rows', 10); // 每页记录数
        $search = $request->param('search', ''); // 搜索条件
        $sortBy = $request->param('sortBy', 'id'); // 排序字段
//        $sortBy = implode(',', $sortBy);
//        return json($sortBy);
        $sortDesc = $request->param('sortDesc', 'true'); // 是否倒序
//        $sortDesc = implode(',', $sortDesc);
        $sortDesc = ($sortDesc == true) ? 'DESC' : 'ASC';
//        return json($sortDesc);
//        $sortDesc = $sortDesc.preg_replace('true', 'DESC', $sortDesc);
//        $sortDesc = $sortDesc.preg_replace('false', 'ASC', $sortDesc);
//        return json($sortDesc);
        // 查询数据库
        // 1、每页数据
        $itemList = Brandm::whereLike('initial', "%{$search}%")
            ->order($sortBy, $sortDesc)
            ->page($page, $rows)
            ->select();
        // 2、总记录数
        $total = count(Brandm::whereLike('initial', "%{$search}%")->select());
        // 3、总页数
        $totalPage = ceil($total / $rows);
        $data = [
            'total' => $total,
            'totalPage' => $totalPage,
            'items' => $itemList
        ];
        return json($data);
    }

    // 新增品牌
    public function addBrand(Request $request)
    {
        // 1、解析请求数据
        // 2、向品牌表添加品牌,并接收返回的品牌ID
        $brand = Brandm::create($request->param());
        // 3、向分类品牌第三方表添加数据
        $cids = $request->param('cids');
        foreach (explode(',', $cids) as $cid) {
            $data = ['category_id' => $cid, 'brand_id' => $brand->id];
            Db::table('tb_category_brand')->insert($data);
        }

        // 4、响应数据
        return json($brand, 201);
//        return  json($request->param(), 201);
    }

    // 根据品牌路由查找分类信息
    public function findCategoriesByBid(Request $request, $bid)
    {
//        // 1、解析请求参数品牌ID
//        $bid = $request->param('bid');
        if (empty($bid)) {
            return json('请求品牌不能为空', 404);
        }
        // 2、根据品牌ID查询分类品牌表,获取分类信息
//        $cids = DB::table('tb_category_brand')
//                    ->field('category_id')
//                    ->where('brand_id', $bid)
//                    ->select();
//        foreach ($cids as $key => $value) {
////            echo $key.'----'.$value['category_id'].'<br />';
//            $cids[$key]['value'] = $value['category_id'];
//        }
////        echo dump($cids);
        $brand = Brandm::get($bid);
        $categories = $brand->categories;
//        echo dump($categories);
        $cids = array();
        foreach ($categories as $index => $cate) {
            $cids[$index]['id'] = $cate->id;
            $cids[$index]['name'] = $cate->name;
        }
        // 3、响应请求
        return json($cids, 200);
    }

    // 图片上传
    public function uploadImage(Request $request)
    {
        // 1、获取上传文件对象
        $file = $request->file('file');
        // 2、移动上传文件到网站根目录下的uploads文件夹
        $info = $file->move('./uploads');
        if ($info) { // 判断文件是否移动成功
            // 返回文件路径
            $filePath = Config::get('file_storage_path') . $info->getSaveName();
            return str_replace('\\', '/', $filePath);
        } else {
            return $file->getError();
        }
    }

    // 修改品牌
    public function editBrand(Request $request)
    {
        $newBrand = $request->param('brand');
        if (!$newBrand) {
            return json('数据不能为空', 404);
        }

        $brand = Brandm::update($newBrand);
        $newCat = $request->param('categories');
        // 根据分类关联更新中间表
        $originCat = Db::table('tb_category_brand')
            ->where('brand_id', $brand->id)
            ->column('category_id');

        $insert = array_diff($newCat, $originCat);
        $delete = array_diff($originCat, $newCat);

        if ($insert) {
            $brand->categories()->saveAll($insert);
        }
        if ($delete) {
            $brand->categories()->detach($delete);
        }

        $last = Db::table('tb_category_brand')
            ->where('brand_id', $brand->id)
            ->select();
        return json('修改完成', 200);
    }

    // 删除指定ID的品牌
    public function deleteBrand(Request $request)
    {
//        return json($request->param());
        $bid = $request->param('bid', '');
        if (empty($bid)) {
            return json('请求品牌不能为空', 400);
        }
//        echo $bid;

//         删除品牌表中对应品牌数据
        Brandm::where('id', $bid)
            ->delete();
        // 删除关联中间表数据
        Db::table('tb_category_brand')
            ->where('brand_id', $bid)
            ->delete();
        // 删除品牌logo图片
        $image = $request->param('image', '');
        if (!empty($image)) {
            $rmLen = strlen(Config::get('file_storage_path'));
            $image = Env::get('root_path').'public\\uploads\\'.str_replace('/', '\\',substr($image, $rmLen));
//            return json(realpath($image));
//            return json($image);
            if (file_exists($image)) {
//                return json('文件存在<br />');
                unlink($image);
            }
        }
        return json('删除成功', 204);
    }
}

3.1.3、模型类Brandm.php
  • 源代码@2.3-1:
<?php

namespace app\api\model;

use think\Model;

class Brandm extends Model
{
    //定义主键:id
    protected $pk = 'id';
    // 定义表名:tb_brand
    protected $table = 'tb_brand';
   ...
 }

tips : 品牌管理功能完成


后记
    本项目为参考某马视频thinkphp5.1-乐优商城前后端项目开发,相关视频及配套资料可自行度娘或者联系本人。上面为自己编写的开发文档,持续更新。欢迎交流,本人QQ:806797785

    前端项目源代码地址:https://gitee.com/gaogzhen/vue-leyou
    后端thinkphp源代码地址:https://gitee.com/gaogzhen/leyou-backend-thinkphp

发布了17 篇原创文章 · 获赞 3 · 访问量 2681

猜你喜欢

转载自blog.csdn.net/gaogzhen/article/details/103915510