Goods.php 2.6 KB

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