Goods.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\CategoryService;
  5. use app\common\service\GoodsService;
  6. use app\common\model\Category;
  7. use fast\Tree;
  8. use think\Exception;
  9. use think\exception\HttpResponseException;
  10. use think\Validate;
  11. class Goods extends Api
  12. {
  13. protected $noNeedLogin = ['index', 'detail'];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 分类列表
  17. *
  18. * @ApiMethod (POST)
  19. * @param string $desc 需求描述
  20. * @param array $images 图片数组
  21. * @param array $files 文件数组
  22. * @param string $contact 联系方式
  23. */
  24. public function list()
  25. {
  26. try {
  27. $page = $this->request->get('page', 1);
  28. $page_size = $this->request->get('pageSize', 10);
  29. $category_id = $this->request->get('categoryId', 0);
  30. $keywords = $this->request->get('keywords', 0);
  31. $where = [];
  32. if ($category_id) {
  33. $subCateIds = Category::where('pid', $category_id)->column('id');
  34. $where['category_id'] = ['in', array_merge([$category_id], $subCateIds)];
  35. }
  36. if ($keywords) {
  37. $where['title|etitle'] = ['like', "%$keywords%"];
  38. }
  39. $goodsList = GoodsService::getList($where, $page_size, 'weigh desc,sold_num desc,id desc')->toArray();
  40. foreach ($goodsList['data'] as $key => &$value) {
  41. $value['skuList'] = [];
  42. $value['thumb'] = $value['primary_image'];
  43. }
  44. //转换格式
  45. // print_r($goodsList);
  46. $result = [
  47. 'algId' => 0,
  48. 'pageNum' => $page,
  49. 'pageSize' => $page_size,
  50. 'saasId' => null,
  51. 'spuList' => convert_keys_to_camelcase($goodsList['data']),
  52. 'storeId' => null,
  53. 'totalCount' => $goodsList['total'],
  54. ];
  55. $this->success('', $result);
  56. } catch (\Exception $e) {
  57. if ($e instanceof HttpResponseException) {
  58. throw $e;
  59. }
  60. $this->error($e->getMessage());
  61. }
  62. }
  63. public function detail()
  64. {
  65. try {
  66. $id = $this->request->get('id', 0);
  67. $where = [
  68. 'id' => $id
  69. ];
  70. $info = GoodsService::getOne($where);
  71. if (!$info) {
  72. throw new Exception('该商品不存在或已删除');
  73. }
  74. //格式转换
  75. $info = convert_keys_to_camelcase($info->append(['images_arr', 'desc'])->hidden(['video'])->toArray());
  76. $info['skuList'] = [];
  77. $info['thumb'] = $info['primaryImage'];
  78. $info['images'] = $info['imagesArr'];
  79. $info['isPutOnSale'] = $info['isPutOnSaleSwitch'];
  80. $this->success('', $info);
  81. } catch (\Exception $e) {
  82. if ($e instanceof HttpResponseException) {
  83. throw $e;
  84. }
  85. $this->error($e->getMessage());
  86. }
  87. }
  88. }