Log.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\admin\controller\shopro\commission;
  3. use app\admin\controller\shopro\Base;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use Exception;
  8. /**
  9. * 分销动态
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Log extends Base
  14. {
  15. /**
  16. * Order模型对象
  17. * @var \app\admin\model\shopro\commission\Log
  18. */
  19. protected $model = null;
  20. protected $noNeedRight = ['getEventAll'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\shopro\commission\Log;
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags']);
  38. if ($this->request->isAjax()) {
  39. $nobuildfields = [
  40. 'agent_nickname', 'agent_mobile',
  41. 'oper_nickname', 'oper_mobile',
  42. ];
  43. list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
  44. $total = $this->buildSearch()
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->count();
  48. // 查询分页数据
  49. $list = $this->buildSearch()
  50. ->with(['agent', 'oper']) // 默认oper 只关联用户,下面循环关联管理员
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->limit($offset, $limit)
  54. ->select();
  55. $list = collection($list)->toArray();
  56. foreach ($list as $key => $log) {
  57. if ($log['oper_type'] == 'admin') {
  58. $list[$key]['oper'] = $log['oper_id'] ? \app\admin\model\Admin::field('id,username,nickname,avatar')->where('id', $log['oper_id'])->find() : null;
  59. }
  60. }
  61. $result = [
  62. "total" => $total,
  63. "rows" => $list,
  64. ];
  65. $this->success('分销动态', null, $result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. // 获取所有 event
  70. public function getEventAll() {
  71. $event = $this->model->getEventList();
  72. $newData[] = ['name' => '全部', 'type' => 'all'];
  73. foreach ($event as $k => $v) {
  74. $newData[] = [
  75. 'name' => $v,
  76. 'type' => $k
  77. ];
  78. }
  79. return $this->success('获取成功', null, $newData);
  80. }
  81. private function buildSearch()
  82. {
  83. $filter = $this->request->get("filter", '');
  84. $filter = (array)json_decode($filter, true);
  85. $filter = $filter ? $filter : [];
  86. // 操作人
  87. $oper_type = isset($filter['oper_type']) ? $filter['oper_type'] : 'all';
  88. $oper_nickname = isset($filter['oper_nickname']) ? $filter['oper_nickname'] : '';
  89. $oper_mobile = isset($filter['oper_mobile']) ? $filter['oper_mobile'] : '';
  90. // 分销商
  91. $agent_nickname = isset($filter['agent_nickname']) ? $filter['agent_nickname'] : '';
  92. $agent_mobile = isset($filter['agent_mobile']) ? $filter['agent_mobile'] : '';
  93. // 当前表名
  94. $tableName = $this->model->getQuery()->getTable();
  95. $logs = $this->model;
  96. // 分销商
  97. if ($agent_nickname || $agent_mobile) {
  98. $logs = $logs->whereExists(function ($query) use ($agent_nickname, $agent_mobile, $tableName) {
  99. $userTableName = (new \app\admin\model\User())->getQuery()->getTable();
  100. $query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.agent_id');
  101. if ($agent_nickname) {
  102. $query = $query->where('nickname', 'like', "%{$agent_nickname}%");
  103. }
  104. if ($agent_mobile) {
  105. $query = $query->where('mobile', 'like', "%{$agent_mobile}%");
  106. }
  107. return $query;
  108. });
  109. }
  110. if ($oper_type != 'all') {
  111. if ($oper_type == 'user') {
  112. if ($oper_nickname || $oper_mobile) {
  113. $logs = $logs->whereExists(function ($query) use ($oper_nickname, $oper_mobile, $tableName) {
  114. $userTableName = (new \app\admin\model\User())->getQuery()->getTable();
  115. $query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.oper_id');
  116. if ($oper_nickname) {
  117. $query = $query->where('nickname', 'like', "%{$oper_nickname}%");
  118. }
  119. if ($oper_mobile) {
  120. $query = $query->where('mobile', 'like', "%{$oper_mobile}%");
  121. }
  122. return $query;
  123. });
  124. }
  125. } else if ($oper_type == 'admin') {
  126. if ($oper_nickname) {
  127. $logs = $logs->whereExists(function ($query) use ($oper_nickname, $tableName) {
  128. $userTableName = (new \app\admin\model\User())->getQuery()->getTable();
  129. $query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.oper_id');
  130. if ($oper_nickname) {
  131. $query = $query->where(function ($query) use ($oper_nickname) {
  132. $query->where('nickname', 'like', "%{$oper_nickname}%")->whereOr('username', 'like', "%{$oper_nickname}%");
  133. });
  134. }
  135. return $query;
  136. });
  137. }
  138. }
  139. }
  140. return $logs;
  141. }
  142. }