glhcp/server/app/api/controller/Goods.php

120 lines
4.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\api\controller;
use app\common\basics\Api;
use app\api\logic\GoodsLogic;
use app\common\server\JsonServer;
use think\facade\Validate;
class Goods extends Api
{
public $like_not_need_login = ['getGoodsDetail', 'getHotList', 'getGoodsList', 'getGoodsListTemplate',
'getGoodsListByColumnId', 'getRecommendGoods'];
/**
* 商品详情
*/
public function getGoodsDetail()
{
if($this->request->isGet()) {
$goodsId = $this->request->get('goods_id', '', 'trim');
$validate = Validate::rule('goods_id', 'require|integer|gt:0');
if(!$validate->check(['goods_id'=>$goodsId])) {
return JsonServer::error($validate->getError());
}
$goodsDetail = GoodsLogic::getGoodsDetail($goodsId, $this->user_id);
if(false === $goodsDetail) {
$error = GoodsLogic::getError() ?? '获取商品详情失败';
return JsonServer::error($error);
}
return JsonServer::success('获取商品详情成功', $goodsDetail);
}else{
return JsonServer::error('请求方式错误');
}
}
/**
* 热销榜单
*/
public function getHotList()
{
return $this->getGoodsListTemplate($this->request, 'getHotList');
}
/**
* 商品列表
*/
public function getGoodsList(){
return $this->getGoodsListTemplate($this->request, 'getGoodsList');
}
/**
* 商品列表模板
* 作用:代码复用
*/
public function getGoodsListTemplate($request, $methodName)
{
if($request->isGet()) {
$get = $this->request->get();
$get['user_id'] = $this->user_id;
$get['page_no'] = $this->page_no;
$get['page_size'] = $this->page_size;
$data = GoodsLogic::$methodName($get); // 可变方法
return JsonServer::success('获取成功', $data);
}else{
return JsonServer::error('请求方式错误');
}
}
/**
* 根据商品栏目获取商品列表
*/
public function getGoodsListByColumnId()
{
if($this->request->isGet()) {
$columnId = $this->request->get('column_id', '', 'trim');
$validate = Validate::rule('column_id', 'require|integer|gt:0');
if(!$validate->check(['column_id'=>$columnId])) {
return JsonServer::error($validate->getError());
}
$data = GoodsLogic::getGoodsListByColumnId($columnId,$this->page_no, $this->page_size);
return JsonServer::success('获取成功', $data);
}else{
return JsonServer::error('请求方式错误');
}
}
// 获取商品的推荐商品
public function getRecommendGoods()
{
if($this->request->isGet()) {
$goodsId = input('goods_id/d',0);
if (empty($goodsId)) {
return JsonServer::error('参数错误');
}
$data = GoodsLogic::getRecommendGoodsById($goodsId);
return JsonServer::success('获取成功', $data);
}else{
return JsonServer::error('请求方式错误');
}
}
}