UserMerchant.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\merchant\user;
  12. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  13. use app\common\repositories\store\order\StoreOrderRepository;
  14. use app\common\repositories\user\UserLabelRepository;
  15. use app\common\repositories\user\UserMerchantRepository;
  16. use crmeb\basic\BaseController;
  17. use FormBuilder\Exception\FormBuilderException;
  18. use think\App;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. /**
  23. * Class UserMerchant
  24. * @package app\controller\merchant\user
  25. * @author xaboy
  26. * @day 2020/10/20
  27. */
  28. class UserMerchant extends BaseController
  29. {
  30. /**
  31. * @var UserMerchantRepository
  32. */
  33. protected $repository;
  34. /**
  35. * UserMerchant constructor.
  36. * @param App $app
  37. * @param UserMerchantRepository $repository
  38. */
  39. public function __construct(App $app, UserMerchantRepository $repository)
  40. {
  41. parent::__construct($app);
  42. $this->repository = $repository;
  43. }
  44. /**
  45. * 获取列表
  46. *
  47. * @return \think\response\Json
  48. */
  49. public function getList()
  50. {
  51. $where = $this->request->params(['nickname', 'sex', 'is_promoter', 'user_time_type', 'user_time',
  52. 'pay_count', 'label_id', 'user_type', 'uid','phone','keyword']);
  53. // 获取分页参数
  54. [$page, $limit] = $this->getPage();
  55. // 添加商家ID查询条件
  56. $where['mer_id'] = $this->request->merId();
  57. // 调用仓库方法获取列表并返回JSON格式的成功响应
  58. return app('json')->success($this->repository->getList($where, $page, $limit));
  59. }
  60. /**
  61. * 显示标签修改表单
  62. *
  63. * @param int $id 用户ID
  64. * @return \think\response\Json
  65. */
  66. public function changeLabelForm($id)
  67. {
  68. // 判断用户是否存在
  69. if (!$this->repository->exists($id))
  70. return app('json')->fail('数据不存在');
  71. // 调用仓库方法获取标签修改表单数据并返回JSON格式的成功响应
  72. return app('json')->success(formToData($this->repository->changeLabelForm($this->request->merId(), $id)));
  73. }
  74. /**
  75. * 修改用户标签
  76. *
  77. * @param int $id 用户ID
  78. * @param UserLabelRepository $labelRepository 用户标签仓库
  79. * @return \think\response\Json
  80. */
  81. public function changeLabel($id, UserLabelRepository $labelRepository)
  82. {
  83. // 获取要修改的标签ID
  84. $label_id = (array)$this->request->param('label_id', []);
  85. // 判断用户是否存在
  86. if (!$this->repository->exists($id))
  87. return app('json')->fail('数据不存在');
  88. // 获取当前商家ID
  89. $merId = $this->request->merId();
  90. // 获取当前商家可用的标签ID
  91. $label_id = $labelRepository->intersection((array)$label_id, $merId, 0);
  92. // 合并原有标签和新增标签
  93. $label_id = array_unique(array_merge($label_id, $this->repository->get($id)->authLabel));
  94. // 转换为字符串形式
  95. $label_id = implode(',', $label_id);
  96. // 更新用户标签
  97. $this->repository->update($id, compact('label_id'));
  98. // 返回JSON格式的成功响应
  99. return app('json')->success('修改成功');
  100. }
  101. public function order($uid)
  102. {
  103. [$page, $limit] = $this->getPage();
  104. $data = app()->make(StoreOrderRepository::class)->userMerList($uid, $this->request->merId(), $page, $limit);
  105. return app('json')->success($data);
  106. }
  107. /**
  108. * 获取用户优惠券列表
  109. *
  110. * @param int $uid 用户ID
  111. * @return \Psr\Http\Message\ResponseInterface
  112. */
  113. public function coupon($uid)
  114. {
  115. // 获取分页参数
  116. [$page, $limit] = $this->getPage();
  117. // 调用 StoreCouponUserRepository 类的 userList 方法获取用户优惠券列表
  118. $data = app()->make(StoreCouponUserRepository::class)->userList(['mer_id' => $this->request->merId(), 'uid' => (int)$uid], $page, $limit);
  119. // 返回 JSON 格式的成功响应和数据
  120. return app('json')->success($data);
  121. }
  122. /**
  123. * 获取用户列表 - 公司员工/客服 对应的用户
  124. * @return \think\response\Json
  125. * @author Qinii
  126. */
  127. public function managerUserLst()
  128. {
  129. [$page, $limit] = $this->getPage();
  130. $where['mer_id'] = $this->request->merId();
  131. $where['status'] = 1;
  132. $where['uids'] = $this->repository->getManagerUid($where);
  133. return app('json')->success($this->repository->getList($where, $page, $limit));
  134. }
  135. }