Goods.php 2.4 KB

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