AgentService.php 7.6 KB

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