table, $id); if (!$data) { Helper::error('数据不存在'); } Helper::ok($data); } public function actionList() { $filter = [ 'r.is_del' => 0, 'r.phone' => Helper::getPostString('phone'), 'r.school_id' => Helper::getPostInt('school_id') ?: null, ]; if ($name = Helper::getPostString('name')) { $filter['r.name'] = '%'.$name; } $cri = DbCriteria::simpleCompareWithPage($filter) ->setAlias('r') ->setSelect('r.*, s.name as school_name') ->setJoin('LEFT JOIN wx_school s ON s.id=r.school_id') ->setOrder('r.id desc'); $data = DB::getListWithCriteria($this->table, $cri); if (!empty($data['records'])) { $users = Helper::arrayColumn( DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare([])->setSelect('id, username')), 'username', 'id' ); $data['records'] = array_map(function ($item) use ($users) { $item['last_user_name'] = $users[$item['last_user_id']] ?? '-'; return $item; }, $data['records']); } Helper::ok($data); } /** * 下拉列表获取 * @return void */ public function actionGetSelectList() { $cri = DbCriteria::simpleCompare(['is_del' => 0])->setSelect('id, name'); $schools = Helper::arrayColumn(DB::getListWithCriteria('school', $cri), null, 'id'); if (empty($schools)) { Helper::ok(); } $cri1 = DbCriteria::simpleCompare(['is_del' => 0])->setSelect('id, name, school_id'); $relations = DB::getListWithCriteria($this->table, $cri1); foreach ($relations['records'] as $relation) { $sid = $relation['school_id']; if (!isset($schools[$sid])) { continue; } if (!isset($schools[$sid]['children'])) { $schools[$sid]['children'] = []; } $schools[$sid]['children'][] = [ 'id' => $relation['id'], 'name' => $relation['name'], ]; } Helper::ok(array_values($schools)); } public function actionDelete() { $id = Helper::getPostInt('id'); if ($id < 1) { Helper::error('参数错误'); } Db::updateById($this->table, ['is_del' => 1], $id); Helper::ok(); } public function actionAdd() { $this->_save(); } public function actionEdit() { $id = Helper::getPostInt('id'); if (!$id) { Helper::error('参数错误'); } $this->_save($id); } private function _save($id = 0) { $data = [ 'name' => Helper::getPostString('name'), 'school_id' => Helper::getPostInt('school_id'), 'phone' => Helper::getPostString('phone'), 'weixin' => Helper::getPostString('weixin'), 'position' => Helper::getPostString('position'), 'memo' => Helper::getPostString('memo'), ]; $notNullField = ["name", "school_id", "phone", "weixin", "position"]; $allowEmptyField = []; // 空字段检测 if (!Helper::checkEmptyKey($data, $notNullField, $allowEmptyField)) { Helper::error('参数错误'); } if ($id) { DB::updateById($this->table, $data, $id); } else { DB::addData($this->table, $data); } Helper::ok(); } public function actionUpdateAttr() { $id = Helper::getPostInt('id'); $attr = Helper::getPostString('attr'); $value = Helper::getPostString('value'); if ($id <= 0 || !$attr) { Helper::error('参数错误'); } if (!in_array($attr, [])) { Helper::error('参数错误2'); } if (DB::updateById($this->table, [$attr => $value], $id) === false) { Helper::error('更新失败'); } Helper::ok(); } }