StoreProductLabel.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\store\product\label;
  12. use app\controller\store\AuthController;
  13. use app\services\product\label\StoreProductLabelCateServices;
  14. use app\services\product\label\StoreProductLabelServices;
  15. use think\facade\App;
  16. use app\Request;
  17. /**
  18. * 商品标签
  19. * Class StoreProductLabel
  20. * @package app\controller\store\product\label
  21. */
  22. class StoreProductLabel extends AuthController
  23. {
  24. /**
  25. * StoreProductLabel constructor.
  26. * @param App $app
  27. * @param StoreProductLabelServices $services
  28. */
  29. public function __construct(App $app, StoreProductLabelServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 带标签的标签组树形结构
  36. * @return mixed
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function tree_list()
  42. {
  43. return $this->success($this->services->getProductLabelTreeList());
  44. }
  45. /**
  46. * 获取商品标签列表
  47. * @return mixed
  48. */
  49. public function allLabel()
  50. {
  51. return $this->success($this->services->getAllLabel(1, (int)$this->storeId));
  52. }
  53. /**
  54. * 添加、编辑商品标签
  55. * @param Request $request
  56. * @param $id
  57. * @return mixed
  58. */
  59. public function save(Request $request, $id)
  60. {
  61. $data = $request->postMore([
  62. ['label_cate', 0],
  63. ['label_name', ''],
  64. ]);
  65. if (!$data['label_cate']) {
  66. return $this->fail('请选择标签组');
  67. }
  68. if (!trim($data['label_name'])) {
  69. return $this->fail('请输入标签名称');
  70. }
  71. $label = $this->services->getOne(['label_cate' => $data['label_cate'], 'label_name' => $data['label_name'], 'type' => 1, 'relation_id' => $this->storeId]);
  72. if ($id) {
  73. if ($label && $id != $label['id']) {
  74. return $this->fail('标签已经存在');
  75. }
  76. if ($this->services->update($id, $data)) {
  77. return $this->success('编辑成功');
  78. } else {
  79. return $this->fail('编辑失败');
  80. }
  81. } else {
  82. if ($label) {
  83. return $this->fail('标签已经存在');
  84. }
  85. $data['type'] = 1;
  86. $data['relation_id'] = $this->storeId;
  87. $data['add_time'] = time();
  88. if ($this->services->save($data)) {
  89. return $this->success('保存成功');
  90. } else {
  91. return $this->fail('保存失败');
  92. }
  93. }
  94. }
  95. /**
  96. * 删除指定资源
  97. *
  98. * @param int $id
  99. * @return \think\Response
  100. */
  101. public function delete($id)
  102. {
  103. if (!$id || !$this->services->count(['id' => $id])) {
  104. return $this->fail('删除的数据不存在');
  105. }
  106. $this->services->delete($id);
  107. return $this->success('删除成功');
  108. }
  109. /**
  110. * 获取表单
  111. * @return mixed
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. public function getLabelForm()
  117. {
  118. return $this->success($this->services->getLabelForm());
  119. }
  120. }