AgentService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Models\AgentModel;
  4. use App\Models\ActionLogModel;
  5. use App\Services\BaseService;
  6. use App\Services\RedisService;
  7. /**
  8. * 代理服务
  9. */
  10. class AgentService extends BaseService
  11. {
  12. public function __construct()
  13. {
  14. $this->model = new AgentModel();
  15. }
  16. /**
  17. * 获取代理列表
  18. */
  19. public function getDataList($params, $pageSize = 15)
  20. {
  21. $query = $this->model->where('mark', 1);
  22. // 审核状态筛选:1-已审核,2-待审核,3-驳回,4-冻结
  23. if (isset($params['status']) && $params['status'] > 0) {
  24. $query->where('status', $params['status']);
  25. }
  26. // 关键词搜索(姓名、电话)
  27. if (isset($params['keyword']) && $params['keyword']) {
  28. $keyword = $params['keyword'];
  29. $query->where(function ($q) use ($keyword) {
  30. $q->where('real_name', 'like', '%' . $keyword . '%')
  31. ->orWhere('phone', 'like', '%' . $keyword . '%');
  32. });
  33. }
  34. $list = $query->with(['user'])
  35. ->withCount(['invites'])
  36. ->orderBy('create_time', 'desc')
  37. ->orderBy('id', 'desc')
  38. ->paginate($pageSize);
  39. $list = $list ? $list->toArray() : [];
  40. if ($list && isset($list['data'])) {
  41. foreach ($list['data'] as &$item) {
  42. // 确保 status 是整数类型
  43. $item['status'] = (int)$item['status'];
  44. // 时间格式化:如果是时间戳则直接格式化,否则先转时间戳
  45. $item['create_time'] = $item['create_time'] ? (is_numeric($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : date('Y-m-d H:i:s', strtotime($item['create_time']))) : '';
  46. $item['update_time'] = $item['update_time'] ? (is_numeric($item['update_time']) ? date('Y-m-d H:i:s', $item['update_time']) : date('Y-m-d H:i:s', strtotime($item['update_time']))) : '';
  47. $item['user'] = $item['user'] ?? [];
  48. // 计算团队人数(这里简化处理,实际应该查询下级用户)
  49. $item['team_count'] = 0;
  50. if (isset($item['user_id']) && $item['user_id']) {
  51. $item['team_count'] = \App\Models\MemberModel::where('parent_id', $item['user_id'])
  52. ->where('mark', 1)
  53. ->count();
  54. }
  55. }
  56. }
  57. return [
  58. 'msg' => '操作成功',
  59. 'code' => 0,
  60. 'data' => $list['data'] ?? [],
  61. 'count' => $list['total'] ?? 0,
  62. ];
  63. }
  64. /**
  65. * 获取代理详情
  66. */
  67. public function getInfo($id)
  68. {
  69. $info = $this->model->where('id', $id)->where('mark', 1)
  70. ->with(['user'])
  71. ->first();
  72. if (!$info) {
  73. return ['code' => 1, 'msg' => '代理不存在'];
  74. }
  75. $info = $info->toArray();
  76. // 确保 status 是整数类型
  77. $info['status'] = (int)$info['status'];
  78. // 时间格式化:如果是时间戳则直接格式化,否则先转时间戳
  79. $info['create_time'] = $info['create_time'] ? (is_numeric($info['create_time']) ? date('Y-m-d H:i:s', $info['create_time']) : date('Y-m-d H:i:s', strtotime($info['create_time']))) : '';
  80. $info['update_time'] = $info['update_time'] ? (is_numeric($info['update_time']) ? date('Y-m-d H:i:s', $info['update_time']) : date('Y-m-d H:i:s', strtotime($info['update_time']))) : '';
  81. // 计算团队人数
  82. $info['team_count'] = 0;
  83. if (isset($info['user_id']) && $info['user_id']) {
  84. $info['team_count'] = \App\Models\MemberModel::where('parent_id', $info['user_id'])
  85. ->where('mark', 1)
  86. ->count();
  87. }
  88. return ['code' => 0, 'msg' => '操作成功', 'data' => $info];
  89. }
  90. /**
  91. * 删除代理
  92. */
  93. public function delete()
  94. {
  95. $id = request()->post('id');
  96. if (!$id) {
  97. return ['code' => 1, 'msg' => '参数错误'];
  98. }
  99. if (is_array($id)) {
  100. $result = $this->model->whereIn('id', $id)->update([
  101. 'mark' => 0,
  102. 'update_time' => time()
  103. ]);
  104. } else {
  105. $result = $this->model->where('id', $id)->update([
  106. 'mark' => 0,
  107. 'update_time' => time()
  108. ]);
  109. }
  110. if ($result) {
  111. ActionLogModel::setTitle("删除代理");
  112. ActionLogModel::record();
  113. RedisService::keyDel("caches:agents:*");
  114. return ['code' => 0, 'msg' => '删除成功'];
  115. }
  116. return ['code' => 1, 'msg' => '删除失败'];
  117. }
  118. /**
  119. * 更新状态
  120. */
  121. public function status()
  122. {
  123. $id = request()->post('id');
  124. $status = request()->post('status');
  125. if (!$id || !isset($status)) {
  126. return ['code' => 1, 'msg' => '参数错误'];
  127. }
  128. $result = $this->model->where('id', $id)->update([
  129. 'status' => $status,
  130. 'update_time' => time()
  131. ]);
  132. if ($result !== false) {
  133. ActionLogModel::setTitle("更新代理状态");
  134. ActionLogModel::record();
  135. RedisService::keyDel("caches:agents:*");
  136. return ['code' => 0, 'msg' => '操作成功'];
  137. }
  138. return ['code' => 1, 'msg' => '操作失败'];
  139. }
  140. /**
  141. * 审核代理
  142. */
  143. public function audit()
  144. {
  145. $id = request()->post('id');
  146. $status = request()->post('status'); // 1-已审核(通过),3-驳回
  147. $confirmRemark = request()->post('confirm_remark', '');
  148. if (!$id || !$status) {
  149. return ['code' => 1, 'msg' => '参数错误'];
  150. }
  151. $agent = $this->model->find($id);
  152. if (!$agent) {
  153. return ['code' => 1, 'msg' => '代理不存在'];
  154. }
  155. if ($agent->status != 2) {
  156. return ['code' => 1, 'msg' => '该代理不是待审核状态'];
  157. }
  158. if ($status == 3 && !$confirmRemark) {
  159. return ['code' => 1, 'msg' => '请填写驳回原因'];
  160. }
  161. $updateData = [
  162. 'status' => $status,
  163. 'confirm_remark' => $confirmRemark,
  164. 'update_time' => time()
  165. ];
  166. $result = $this->model->where('id', $id)->update($updateData);
  167. if ($result) {
  168. $statusText = $status == 1 ? '通过' : '驳回';
  169. ActionLogModel::setTitle("审核代理-{$statusText}");
  170. ActionLogModel::record();
  171. RedisService::keyDel("caches:agents:*");
  172. return ['code' => 0, 'msg' => "审核{$statusText}"];
  173. }
  174. return ['code' => 1, 'msg' => '操作失败'];
  175. }
  176. /**
  177. * 冻结/解冻代理
  178. */
  179. public function freeze()
  180. {
  181. $id = request()->post('id');
  182. $status = request()->post('status'); // 4-冻结,1-解冻
  183. if (!$id || !$status) {
  184. return ['code' => 1, 'msg' => '参数错误'];
  185. }
  186. $agent = $this->model->find($id);
  187. if (!$agent) {
  188. return ['code' => 1, 'msg' => '代理不存在'];
  189. }
  190. $updateData = [
  191. 'status' => $status,
  192. 'update_time' => time()
  193. ];
  194. $result = $this->model->where('id', $id)->update($updateData);
  195. if ($result) {
  196. $statusText = $status == 4 ? '冻结' : '解冻';
  197. ActionLogModel::setTitle("{$statusText}代理");
  198. ActionLogModel::record();
  199. RedisService::keyDel("caches:agents:*");
  200. return ['code' => 0, 'msg' => "{$statusText}成功"];
  201. }
  202. return ['code' => 1, 'msg' => '操作失败'];
  203. }
  204. }