Goods.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\Goods as GoodsModel;
  4. use app\api\model\Cart as CartModel;
  5. use app\common\service\qrcode\Goods as GoodsPoster;
  6. /**
  7. * 商品控制器
  8. * Class Goods
  9. * @package app\api\controller
  10. */
  11. class Goods extends Controller
  12. {
  13. /**
  14. * 商品列表
  15. * @return array
  16. * @throws \app\common\exception\BaseException
  17. * @throws \think\exception\DbException
  18. */
  19. public function lists()
  20. {
  21. // 整理请求的参数
  22. $param = array_merge($this->request->param(), [
  23. 'status' => 10
  24. ]);
  25. // 获取列表数据
  26. $model = new GoodsModel;
  27. $list = $model->getList($param, $this->getUser(false));
  28. return $this->renderSuccess(compact('list'));
  29. }
  30. /**
  31. * 获取商品详情
  32. * @param $goods_id
  33. * @return array
  34. * @throws \app\common\exception\BaseException
  35. * @throws \think\exception\DbException
  36. */
  37. public function detail($goods_id)
  38. {
  39. // 用户信息
  40. $user = $this->getUser(false);
  41. // 商品详情
  42. $model = new GoodsModel;
  43. $goods = $model->getDetails($goods_id, $this->getUser(false));
  44. if ($goods === false) {
  45. return $this->renderError($model->getError() ?: '商品信息不存在');
  46. }
  47. // 分类处理,是否升级产品
  48. $goods['is_upgrade'] = 0;
  49. if($goods['category_id'] == '10001'){
  50. $goods['is_upgrade'] = 1;
  51. }
  52. // 多规格商品sku信息, todo: 已废弃 v1.1.25
  53. $specData = $goods['spec_type'] == 20 ? $model->getManySpecData($goods['spec_rel'], $goods['sku']) : null;
  54. return $this->renderSuccess([
  55. // 商品详情
  56. 'detail' => $goods,
  57. // 购物车商品总数量
  58. 'cart_total_num' => $user ? (new CartModel($user))->getGoodsNum() : 0,
  59. // 多规格商品sku信息
  60. 'specData' => $specData,
  61. ]);
  62. }
  63. /**
  64. * 生成商品海报
  65. * @param $goods_id
  66. * @return array
  67. * @throws \app\common\exception\BaseException
  68. * @throws \think\exception\DbException
  69. * @throws \Exception
  70. */
  71. public function poster($goods_id)
  72. {
  73. // 商品详情
  74. $detail = GoodsModel::detail($goods_id);
  75. $Qrcode = new GoodsPoster($detail, $this->getUser(false));
  76. return $this->renderSuccess([
  77. 'qrcode' => $Qrcode->getImage(),
  78. ]);
  79. }
  80. }