Goods.php 2.7 KB

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