StoreProductLabel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\admin\v1\product\label;
  12. use app\controller\admin\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\admin\v1\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. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function allLabel()
  53. {
  54. return $this->success($this->services->getAllLabel());
  55. }
  56. /**
  57. * 添加、编辑商品标签
  58. * @param Request $request
  59. * @param $id
  60. * @return mixed
  61. */
  62. public function save(Request $request, $id)
  63. {
  64. $data = $request->postMore([
  65. ['label_cate', 0],
  66. ['label_name', ''],
  67. ]);
  68. if (!$data['label_cate']) {
  69. return $this->fail('请选择标签组');
  70. }
  71. if (!trim($data['label_name'])) {
  72. return $this->fail('请输入标签名称');
  73. }
  74. $label = $this->services->getOne(['label_cate' => $data['label_cate'], 'label_name' => $data['label_name'], 'type' => 0]);
  75. if ($id) {
  76. if ($label && $id != $label['id']) {
  77. return $this->fail('标签已经存在');
  78. }
  79. if ($this->services->update($id, $data)) {
  80. $data['id'] = $id;
  81. $this->services->cacheUpdate($data);
  82. return $this->success('编辑成功');
  83. } else {
  84. return $this->fail('编辑失败');
  85. }
  86. } else {
  87. if ($label) {
  88. return $this->fail('标签已经存在');
  89. }
  90. $data['type'] = 0;
  91. $data['relation_id'] = 0;
  92. $data['add_time'] = time();
  93. if ($this->services->save($data)) {
  94. $data['id'] = $id;
  95. $this->services->cacheUpdate($data);
  96. return $this->success('保存成功');
  97. } else {
  98. return $this->fail('保存失败');
  99. }
  100. }
  101. }
  102. /**
  103. * 删除指定资源
  104. *
  105. * @param int $id
  106. * @return \think\Response
  107. */
  108. public function delete($id)
  109. {
  110. if (!$id || !$this->services->count(['id' => $id])) {
  111. return $this->fail('删除的数据不存在');
  112. }
  113. $info = $this->services->get((int)$id);
  114. if (in_array($id, [1, 2, 3, 4, 5]) && $info['label_cate'] == 1) {//系统默认
  115. return $this->fail('默认标签不允许删除');
  116. }
  117. $this->services->delete($id);
  118. $this->services->cacheDelById($id);
  119. return $this->success('删除成功');
  120. }
  121. /**
  122. * 获取表单
  123. * @return mixed
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\DbException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. */
  128. public function getLabelForm()
  129. {
  130. return $this->success($this->services->getLabelForm());
  131. }
  132. }