UserLabel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\user;
  12. use app\controller\store\AuthController;
  13. use app\services\user\label\UserLabelCateServices;
  14. use app\services\user\label\UserLabelRelationServices;
  15. use app\services\user\label\UserLabelServices;
  16. use think\facade\App;
  17. /**
  18. * 用户标签
  19. * Class UserLabel
  20. * @package app\controller\store\user
  21. */
  22. class UserLabel extends AuthController
  23. {
  24. /**
  25. * UserLabel constructor.
  26. * @param App $app
  27. * @param UserLabelServices $service
  28. */
  29. public function __construct(App $app, UserLabelServices $service)
  30. {
  31. parent::__construct($app);
  32. $this->service = $service;
  33. }
  34. /**
  35. * 标签列表
  36. * @param int $label_cate
  37. * @return mixed
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function index($label_cate = 0)
  43. {
  44. return app('json')->success($this->service->getList(['label_cate' => $label_cate, 'type' => 1, 'relation_id' => $this->storeId]));
  45. }
  46. /**
  47. * 添加标签表单
  48. * @return mixed
  49. * @throws \FormBuilder\Exception\FormBuilderException
  50. */
  51. public function create()
  52. {
  53. return app('json')->success($this->service->add(0, 1, (int)$this->storeId));
  54. }
  55. /**
  56. * 修改标签表单
  57. * @return mixed
  58. * @throws \FormBuilder\Exception\FormBuilderException
  59. */
  60. public function edit()
  61. {
  62. [$id] = $this->request->getMore([
  63. ['id', 0],
  64. ], true);
  65. return app('json')->success($this->service->add((int)$id, 1, (int)$this->storeId));
  66. }
  67. /**
  68. * 保存标签表单数据
  69. * @param int $id
  70. * @return mixed
  71. */
  72. public function save()
  73. {
  74. $data = $this->request->postMore([
  75. ['id', 0],
  76. ['label_cate', 0],
  77. ['label_name', ''],
  78. ]);
  79. if (!$data['label_name'] = trim($data['label_name'])) return app('json')->fail('会员标签不能为空!');
  80. $this->service->save((int)$data['id'], $data, 1, (int)$this->storeId);
  81. return app('json')->success('保存成功');
  82. }
  83. /**
  84. * 删除
  85. * @param $id
  86. * @return
  87. * @throws \Exception
  88. */
  89. public function delete()
  90. {
  91. [$id] = $this->request->getMore([
  92. ['id', 0],
  93. ], true);
  94. if (!$id) return app('json')->fail('数据不存在');
  95. $this->service->delLabel((int)$id);
  96. return app('json')->success('刪除成功!');
  97. }
  98. /**
  99. * 标签分类
  100. * @param UserLabelCateServices $services
  101. * @param $uid
  102. * @return mixed
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\DbException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. */
  107. public function getUserLabel(UserLabelCateServices $services, $uid)
  108. {
  109. return app('json')->success($services->getUserLabel((int)$uid, 1, (int)$this->storeId));
  110. }
  111. /**
  112. * 设置用户标签
  113. * @param UserLabelRelationServices $services
  114. * @param $uid
  115. * @return mixed
  116. */
  117. public function setUserLabel(UserLabelRelationServices $services, $uid)
  118. {
  119. [$labels, $unLabelIds] = $this->request->postMore([
  120. ['label_ids', []],
  121. ['un_label_ids', []]
  122. ], true);
  123. if (!count($labels) && !count($unLabelIds)) {
  124. return app('json')->fail('请先添加标签');
  125. }
  126. if ($services->setUserLable($uid, $labels, 1, (int)$this->storeId) && $services->unUserLabel($uid, $unLabelIds, 1, (int)$this->storeId)) {
  127. return app('json')->success('设置成功');
  128. } else {
  129. return app('json')->fail('设置失败');
  130. }
  131. }
  132. }