model = new IpUserModel(); } protected function indexWhere(&$where) { if ($where) { foreach ($where as $key => $w) { if ($w[0] == 'username') { $uid = User::where('nick_name', $w[1], $w[2])->column('uid'); $where[$key] = ['uid', 'in', $uid ?: [0]]; } if ($w[0] == 'ip') { $uid = IpModel::where('ip', $w[1], $w[2])->column('id'); $where[$key] = ['ip_id', 'in', $uid ?: [0]]; } if ($w[0] == 'ip_status') { $uid = IpModel::where('status', $w[1], $w[2])->column('id'); $where[$key] = ['ip_id', 'in', $uid ?: [0]]; } } } } public function index() { if ($this->request->isAjax()) { if (input('selectFields')) { return $this->selectList(); } list($page, $limit, $where) = $this->buildTableParames(); $this->indexWhere($where); $count = $this->model ->where($where) ->count(); $list = $this->model ->withoutField('update_time, delete_time') ->where($where) ->page($page, $limit) ->order('id desc') ->select() ->each(function ($item) { $item['username'] = User::getUserValue($item['uid']); $item['avatar'] = User::getUserValue($item['uid'], 'avatar'); $ipData = IpModel::where('id', $item['ip_id'])->field('ip, status')->find(); $item['ip'] = $ipData['ip'] ?? '-'; $item['ip_status'] = $ipData['status'] ?? '-'; return $item; }); $data = [ 'code' => 0, 'msg' => '', 'count' => $count, 'data' => $list ]; return json($data); } return $this->fetch(); } /** * @NodeAnotation(title="添加") */ public function add() { if ($this->request->isPost()) { $post = $this->request->post(); try { $rule = ['uid|用户' => 'require|number', 'ip_id|IP' => 'require|number']; validate()->rule($rule)->scene('add')->check($post); $query = IpUserModel::where(['uid' => $post['uid'], 'ip_id' => $post['ip_id']])->find(); if ($query) throw new ValidateException('用户关联IP已存在'); $save = $this->model->save($post); } catch (ValidateException $e) { $this->error($e->getMessage()); } catch (\Exception $e) { $this->error('保存失败'); } $save ? $this->success('保存成功') : $this->error('保存失败'); } return $this->fetch(); } /** * @NodeAnotation(title="删除") */ public function delete($id) { $this->checkPostRequest(); $row = $this->model->whereIn('id', $id)->select(); $row->isEmpty() && $this->error('数据不存在'); try { $save = false; foreach ($row as $value) { $save = $this->model->where('id', $value['id'])->useSoftDelete('delete_time', time())->delete(); } } catch (\Exception $e) { $this->error('删除失败'); } $save ? $this->success('删除成功') : $this->error('删除失败'); } /** * @NodeAnotation(title="属性修改") */ public function modify() { $this->checkPostRequest(); $post = $this->request->post(); $rule = [ 'id|ID' => 'require', 'field|字段' => 'require', 'value|值' => 'require', ]; $this->validate($post, $rule); if (!in_array($post['field'], $this->allowModifyFields)) { $this->error('该字段不允许修改:' . $post['field']); } $row = $this->model->find($post['id']); empty($row) && $this->error('数据不存在'); try { $row->save([ $post['field'] => $post['value'], ]); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->success('保存成功'); } }