UserLabel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\adminapi\controller\v1\user;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\user\UserLabelCateServices;
  14. use app\services\user\UserLabelRelationServices;
  15. use app\services\user\UserLabelServices;
  16. use think\facade\App;
  17. /**
  18. * 用户标签控制器
  19. * Class UserLabel
  20. * @package app\adminapi\controller\v1\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. * @return mixed
  37. */
  38. public function index($label_cate = 0)
  39. {
  40. return app('json')->success($this->service->getList(['label_cate' => $label_cate]));
  41. }
  42. /**
  43. * 添加修改标签表单
  44. * @return mixed
  45. * @throws \FormBuilder\Exception\FormBuilderException
  46. */
  47. public function add()
  48. {
  49. [$id] = $this->request->getMore([
  50. ['id', 0],
  51. ], true);
  52. return app('json')->success($this->service->add((int)$id));
  53. }
  54. /**
  55. * 保存标签表单数据
  56. * @param int $id
  57. * @return mixed
  58. */
  59. public function save()
  60. {
  61. $data = $this->request->postMore([
  62. ['id', 0],
  63. ['label_cate', 0],
  64. ['label_name', ''],
  65. ]);
  66. if (!$data['label_name'] = trim($data['label_name'])) return app('json')->fail('会员标签不能为空!');
  67. $this->service->save((int)$data['id'], $data);
  68. return app('json')->success('保存成功');
  69. }
  70. /**
  71. * 删除
  72. * @param $id
  73. * @throws \Exception
  74. */
  75. public function delete()
  76. {
  77. list($id) = $this->request->getMore([
  78. ['id', 0],
  79. ], true);
  80. if (!$id) return app('json')->fail('数据不存在');
  81. $this->service->delLabel((int)$id);
  82. return app('json')->success('刪除成功!');
  83. }
  84. /**
  85. * 标签分类
  86. * @param UserLabelCateServices $services
  87. * @return mixed
  88. */
  89. public function getUserLabel(UserLabelCateServices $services, $uid)
  90. {
  91. return app('json')->success($services->getUserLabel((int)$uid));
  92. }
  93. /**
  94. * 设置用户标签
  95. * @param UserLabelRelationServices $services
  96. * @param $uid
  97. * @return mixed
  98. */
  99. public function setUserLabel(UserLabelRelationServices $services, $uid)
  100. {
  101. [$labels, $unLabelIds] = $this->request->postMore([
  102. ['label_ids', []],
  103. ['un_label_ids', []]
  104. ], true);
  105. if (!count($labels) && !count($unLabelIds)) {
  106. return app('json')->fail('缺少标签id');
  107. }
  108. if ($services->setUserLable($uid, $labels) && $services->unUserLabel($uid, $unLabelIds)) {
  109. return app('json')->success('设置成功');
  110. } else {
  111. return app('json')->fail('设置失败');
  112. }
  113. }
  114. }