1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\service\CategoryService;
- use app\common\service\GoodsService;
- use app\common\model\Category;
- use fast\Tree;
- use think\Exception;
- use think\exception\HttpResponseException;
- use think\Validate;
- class Goods extends Api
- {
- protected $noNeedLogin = ['index', 'detail'];
- protected $noNeedRight = ['*'];
- /**
- * 分类列表
- *
- * @ApiMethod (POST)
- * @param string $desc 需求描述
- * @param array $images 图片数组
- * @param array $files 文件数组
- * @param string $contact 联系方式
- */
- public function list()
- {
- try {
- $page = $this->request->get('page', 1);
- $page_size = $this->request->get('pageSize', 10);
- $category_id = $this->request->get('categoryId', 0);
- $keywords = $this->request->get('keywords', 0);
- $where = [];
- if ($category_id) {
- $subCateIds = Category::where('pid', $category_id)->column('id');
- $where['category_id'] = ['in', array_merge([$category_id], $subCateIds)];
- }
- if ($keywords) {
- $where['title|etitle'] = ['like', "%$keywords%"];
- }
- $goodsList = GoodsService::getList($where, $page_size, 'weigh desc,sold_num desc,id desc')->toArray();
- foreach ($goodsList['data'] as $key => &$value) {
- $value['skuList'] = [];
- $value['thumb'] = $value['primary_image'];
- }
- //转换格式
- // print_r($goodsList);
- $result = [
- 'algId' => 0,
- 'pageNum' => $page,
- 'pageSize' => $page_size,
- 'saasId' => null,
- 'spuList' => convert_keys_to_camelcase($goodsList['data']),
- 'storeId' => null,
- 'totalCount' => $goodsList['total'],
- ];
- $this->success('', $result);
- } catch (\Exception $e) {
- if ($e instanceof HttpResponseException) {
- throw $e;
- }
- $this->error($e->getMessage());
- }
- }
- public function detail()
- {
- try {
- $id = $this->request->get('id', 0);
- $where = [
- 'id' => $id
- ];
- $info = GoodsService::getOne($where);
- if (!$info) {
- throw new Exception('该商品不存在或已删除');
- }
- //格式转换
- $info = convert_keys_to_camelcase($info->append(['images_arr', 'desc'])->hidden(['video'])->toArray());
- $info['skuList'] = [];
- $info['thumb'] = $info['primaryImage'];
- $info['images'] = $info['imagesArr'];
- $info['isPutOnSale'] = $info['isPutOnSaleSwitch'];
- $this->success('', $info);
- } catch (\Exception $e) {
- if ($e instanceof HttpResponseException) {
- throw $e;
- }
- $this->error($e->getMessage());
- }
- }
- }
|