Goods.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // 多规格商品sku信息, todo: 已废弃 v1.1.25
  48. $specData = $goods['spec_type'] == 20 ? $model->getManySpecData($goods['spec_rel'], $goods['sku']) : null;
  49. return $this->renderSuccess([
  50. // 商品详情
  51. 'detail' => $goods,
  52. // 购物车商品总数量
  53. 'cart_total_num' => $user ? (new CartModel($user))->getGoodsNum() : 0,
  54. // 多规格商品sku信息
  55. 'specData' => $specData,
  56. ]);
  57. }
  58. /**
  59. * 生成商品海报
  60. * @param $goods_id
  61. * @return array
  62. * @throws \app\common\exception\BaseException
  63. * @throws \think\exception\DbException
  64. * @throws \Exception
  65. */
  66. public function poster($goods_id)
  67. {
  68. // 商品详情
  69. $detail = GoodsModel::detail($goods_id);
  70. $Qrcode = new GoodsPoster($detail, $this->getUser(false));
  71. return $this->renderSuccess([
  72. 'qrcode' => $Qrcode->getImage(),
  73. ]);
  74. }
  75. }