* @version : H5IOS 1.0 */ namespace huosdk\h5ios\admin\controller; use cmf\controller\AdminBaseController; use cmf\view\Filter; use huolib\constant\CommonConst; use huolib\status\CommonStatus; use huolib\tool\Page; class CommonController extends AdminBaseController { protected $logic_class_name = ''; protected $model_class_name = ''; protected $validate_class_name = ''; protected $class_name = ''; protected $order = '-id'; protected $field_arr = []; protected $index_url = ''; protected $add_url = ''; protected $edit_url = ''; protected $delete_url = ''; protected $delete_index_url = ''; protected $detail_url = ''; protected $base_url = '/adminh5ios/'; protected $app = 'adminh5ios'; function _initialize() { $this->lang_index = 'ADMINH5IOS_'.$this->class_name.'_INDEX'; $this->lang_add = 'ADMINH5IOS_'.$this->class_name.'_ADD'; $this->lang_edit = 'ADMINH5IOS_'.$this->class_name.'_EDIT'; $this->assign('field_arr', $this->field_arr); $_url = $this->base_url.$this->class_name; $this->index_url = $_url.'/index'; $this->add_url = $_url.'/add'; $this->edit_url = $_url.'/edit'; $this->delete_url = $_url.'/delete'; $this->detail_url = $_url.'/detail'; $this->delete_index_url = $_url.'/deleteindex'; $this->assign('class_name', $this->class_name); $this->assign('index_url', $this->index_url); $this->assign('add_url', $this->add_url); $this->assign('edit_url', $this->edit_url); $this->assign('delete_url', $this->delete_url); $this->assign('detail_url', $this->detail_url); $this->assign('base_url', $_url); $this->assign('delete_index_url', $this->delete_index_url); parent::_initialize(); } public function _initializeView() { $_theme_path = dirname(__DIR__).'/view'; $_view_replace_str = [ '__ROOT__' => '/', '__TMPL__' => "{$_theme_path}", '__STATIC__' => STATICSITE.'/admin', '__WEB_ROOT__' => '' ]; $_view_replace_str = array_merge(config('view_replace_str'), $_view_replace_str); config('template.view_base', "$_theme_path/"); config('view_replace_str', $_view_replace_str); } /** * 列表 * * @param string $template * * @return mixed */ public function index($template = 'common/index') { if (CommonConst::CONST_EXPORT == $this->request->param('export/d', 0)) { return $this->exportIndex(); } $_param = $this->getSearchParam(); $_param = $this->trimAll($_param); //移除所有空格 /*获取列表数据与分页*/ $_logic = new $this->logic_class_name(); $_datas = $_logic->getAdminList( $_param, $this->page.','.$this->list_rows, $this->order ); $_page_data = Page::paginate($_datas['count'], $_datas['list'], $this->page, $this->list_rows); $this->assign('items', $_page_data); $this->assign('page', $_page_data->render()); return $this->fetch($template); } /** * 添加 * * @param string $template 模板文件 * * @return mixed */ public function add($template = 'add') { return $this->fetch($template); } /** * 添加操作函数 */ public function addPost() { if ($this->request->isPost()) { /* 校验参数 */ $_param = $this->request->param(); /*校验参数*/ $_validate = new $this->validate_class_name('add'); if (!$_validate->check($_param)) { $this->adminError($_validate->getError()); } /* 逻辑处理 */ $_rs = (new $this->model_class_name())->addData($_param); if (false === $_rs) { return $this->adminError(lang('ADD_FAILED')); } return $this->adminSuccess(lang('ADD_SUCCESS')); } else { $_code = CommonStatus::INVALID_PARAMS; return $this->adminError(CommonStatus::getMsg($_code)); } } /** * 编辑 * * @param string $template * * @return mixed */ public function edit($template = 'edit') { $_id = $this->request->param('id/d'); if (empty($_id)) { $_code = CommonStatus::INVALID_PARAMS; return $this->adminError(CommonStatus::getMsg($_code)); } $_data = (new $this->model_class_name())->getInfoById($_id); if (empty($_data)) { $_code = CommonStatus::INVALID_PARAMS; return $this->adminError(CommonStatus::getMsg($_code)); } $this->assign('data', $_data); return $this->fetch($template); } /** * 编辑操作函数 */ public function editPost() { if ($this->request->isPost()) { /* 校验参数 */ $_param = $this->request->param(); /*校验参数*/ $_validate = new $this->validate_class_name('edit'); if (!$_validate->check($_param)) { return $this->adminError($_validate->getError()); } /* 逻辑处理 */ $_rs = (new $this->model_class_name())->updateData($_param, $_param['id']); if (false === $_rs) { return $this->adminError(lang('EDIT_FAILED')); } return $this->adminSuccess(lang('EDIT_SUCCESS')); } else { $_code = CommonStatus::INVALID_PARAMS; return $this->adminError(CommonStatus::getMsg($_code)); } } /** * 删除 */ public function delete() { $_id = $this->request->param('id/d', 0); if (empty($_id)) { $_code = CommonStatus::INVALID_PARAMS; return $this->adminError(CommonStatus::getMsg($_code)); } $_rs = (new $this->model_class_name())->deleteData($_id); if (false === $_rs) { return $this->adminError(lang('DELETE_FAILED')); } return $this->adminSuccess(lang('DELETE_SUCCESS')); } /** * 排序 排序字段为list_orders数组 POST 排序字段为:list_order * * @param $model * @param string $field * */ public function listOrders($model = null, $field = 'list_order') { $_rs = parent::listOrders(new $this->model_class_name(), $field); if (false == $_rs) { $_code = CommonStatus::INNER_ERROR; return $this->adminError(CommonStatus::getMsg($_code)); } $_code = CommonStatus::NO_ERROR; return $this->adminSuccess(CommonStatus::getMsg($_code)); } /** * 单个设置状态 */ public function setStatus() { $_id = $this->request->param('id/d', 0); $_ids = $this->request->param('ids/a', []); if (!empty($_id)) { $_ids = [$_id]; } $_field = $this->request->param('field/s', 'status'); $_status = $this->request->param($_field.'/d', 0); if ((empty($_ids)) || empty($_status)) { $this->adminError(lang('PARAM_ERROR')); } foreach ($_ids as $_id) { $_data[$_field] = $_status; $_rs = (new $this->model_class_name())->updateData($_data, $_id); if (false === $_rs) { $this->adminError(lang('EDIT_FAILED')); } } $this->adminSuccess(lang('EDIT_SUCCESS')); } /** * 导出数据 */ protected function exportIndex() { } protected function getSearchField() { $_fields_type = (new $this->model_class_name())->getFieldType(); $_param = $this->request->param(); $_search_param = []; foreach ($_fields_type as $_field => $_type) { // if (false == isset($_param[$_field]) || empty($_param[$_field])) { // continue; // } if (true == is_array($_type)) { list($_type, $param) = $_type; } elseif (strpos($_type, ':')) { list($_type, $param) = explode(':', $_type, 2); } switch ($_type) { case 'integer': case 'timestamp': $_search_param[$_field] = $this->request->param($_field.'/d', ''); break; case 'float': $_search_param[$_field] = $this->request->param($_field.'/f', ''); break; default: $_search_param[$_field] = $this->request->param($_field.'/s', '', 'trim'); $_input = Filter::text($_field, $_search_param[$_field], lang('MSG_INPUT').lang($_field)); $this->assign($_field.'_input', $_input); break; } } return $_search_param; } /** * 搜索参数 * return array */ protected function getSearchParam() { $_param = $this->getSearchField(); $_param = $this->trimAll($_param); return $_param; } }