Goods.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. return $this->renderSuccess([
  48. // 商品详情
  49. 'detail' => $goods,
  50. // 购物车商品总数量
  51. 'cart_total_num' => $user ? (new CartModel($user))->getTotalNum() : 0,
  52. ]);
  53. }
  54. /**
  55. * 生成商品海报
  56. * @param $goods_id
  57. * @return array
  58. * @throws \app\common\exception\BaseException
  59. * @throws \think\exception\DbException
  60. * @throws \Exception
  61. */
  62. public function poster($goods_id)
  63. {
  64. // 商品详情
  65. $detail = GoodsModel::detail($goods_id);
  66. $Qrcode = new GoodsPoster($detail, $this->getUser(false));
  67. return $this->renderSuccess([
  68. 'qrcode' => $Qrcode->getImage(),
  69. ]);
  70. }
  71. }