CommonController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * CommonController.php UTF-8
  4. *
  5. *
  6. * @date : 2020/9/14 15:10
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : H5IOS 1.0
  11. */
  12. namespace huosdk\h5ios\admin\controller;
  13. use cmf\controller\AdminBaseController;
  14. use cmf\view\Filter;
  15. use huolib\constant\CommonConst;
  16. use huolib\status\CommonStatus;
  17. use huolib\tool\Page;
  18. class CommonController extends AdminBaseController {
  19. protected $logic_class_name = '';
  20. protected $model_class_name = '';
  21. protected $validate_class_name = '';
  22. protected $class_name = '';
  23. protected $order = '-id';
  24. protected $field_arr = [];
  25. protected $index_url = '';
  26. protected $add_url = '';
  27. protected $edit_url = '';
  28. protected $delete_url = '';
  29. protected $delete_index_url = '';
  30. protected $detail_url = '';
  31. protected $base_url = '/adminh5ios/';
  32. protected $app = 'adminh5ios';
  33. function _initialize() {
  34. $this->lang_index = 'ADMINH5IOS_'.$this->class_name.'_INDEX';
  35. $this->lang_add = 'ADMINH5IOS_'.$this->class_name.'_ADD';
  36. $this->lang_edit = 'ADMINH5IOS_'.$this->class_name.'_EDIT';
  37. $this->assign('field_arr', $this->field_arr);
  38. $_url = $this->base_url.$this->class_name;
  39. $this->index_url = $_url.'/index';
  40. $this->add_url = $_url.'/add';
  41. $this->edit_url = $_url.'/edit';
  42. $this->delete_url = $_url.'/delete';
  43. $this->detail_url = $_url.'/detail';
  44. $this->delete_index_url = $_url.'/deleteindex';
  45. $this->assign('class_name', $this->class_name);
  46. $this->assign('index_url', $this->index_url);
  47. $this->assign('add_url', $this->add_url);
  48. $this->assign('edit_url', $this->edit_url);
  49. $this->assign('delete_url', $this->delete_url);
  50. $this->assign('detail_url', $this->detail_url);
  51. $this->assign('base_url', $_url);
  52. $this->assign('delete_index_url', $this->delete_index_url);
  53. parent::_initialize();
  54. }
  55. public function _initializeView() {
  56. $_theme_path = dirname(__DIR__).'/view';
  57. $_view_replace_str = [
  58. '__ROOT__' => '/',
  59. '__TMPL__' => "{$_theme_path}",
  60. '__STATIC__' => STATICSITE.'/admin',
  61. '__WEB_ROOT__' => ''
  62. ];
  63. $_view_replace_str = array_merge(config('view_replace_str'), $_view_replace_str);
  64. config('template.view_base', "$_theme_path/");
  65. config('view_replace_str', $_view_replace_str);
  66. }
  67. /**
  68. * 列表
  69. *
  70. * @param string $template
  71. *
  72. * @return mixed
  73. */
  74. public function index($template = 'common/index') {
  75. if (CommonConst::CONST_EXPORT == $this->request->param('export/d', 0)) {
  76. return $this->exportIndex();
  77. }
  78. $_param = $this->getSearchParam();
  79. $_param = $this->trimAll($_param); //移除所有空格
  80. /*获取列表数据与分页*/
  81. $_logic = new $this->logic_class_name();
  82. $_datas = $_logic->getAdminList(
  83. $_param, $this->page.','.$this->list_rows, $this->order
  84. );
  85. $_page_data = Page::paginate($_datas['count'], $_datas['list'], $this->page, $this->list_rows);
  86. $this->assign('items', $_page_data);
  87. $this->assign('page', $_page_data->render());
  88. return $this->fetch($template);
  89. }
  90. /**
  91. * 添加
  92. *
  93. * @param string $template 模板文件
  94. *
  95. * @return mixed
  96. */
  97. public function add($template = 'add') {
  98. return $this->fetch($template);
  99. }
  100. /**
  101. * 添加操作函数
  102. */
  103. public function addPost() {
  104. if ($this->request->isPost()) {
  105. /* 校验参数 */
  106. $_param = $this->request->param();
  107. /*校验参数*/
  108. $_validate = new $this->validate_class_name('add');
  109. if (!$_validate->check($_param)) {
  110. $this->adminError($_validate->getError());
  111. }
  112. /* 逻辑处理 */
  113. $_rs = (new $this->model_class_name())->addData($_param);
  114. if (false === $_rs) {
  115. return $this->adminError(lang('ADD_FAILED'));
  116. }
  117. return $this->adminSuccess(lang('ADD_SUCCESS'));
  118. } else {
  119. $_code = CommonStatus::INVALID_PARAMS;
  120. return $this->adminError(CommonStatus::getMsg($_code));
  121. }
  122. }
  123. /**
  124. * 编辑
  125. *
  126. * @param string $template
  127. *
  128. * @return mixed
  129. */
  130. public function edit($template = 'edit') {
  131. $_id = $this->request->param('id/d');
  132. if (empty($_id)) {
  133. $_code = CommonStatus::INVALID_PARAMS;
  134. return $this->adminError(CommonStatus::getMsg($_code));
  135. }
  136. $_data = (new $this->model_class_name())->getInfoById($_id);
  137. if (empty($_data)) {
  138. $_code = CommonStatus::INVALID_PARAMS;
  139. return $this->adminError(CommonStatus::getMsg($_code));
  140. }
  141. $this->assign('data', $_data);
  142. return $this->fetch($template);
  143. }
  144. /**
  145. * 编辑操作函数
  146. */
  147. public function editPost() {
  148. if ($this->request->isPost()) {
  149. /* 校验参数 */
  150. $_param = $this->request->param();
  151. /*校验参数*/
  152. $_validate = new $this->validate_class_name('edit');
  153. if (!$_validate->check($_param)) {
  154. return $this->adminError($_validate->getError());
  155. }
  156. /* 逻辑处理 */
  157. $_rs = (new $this->model_class_name())->updateData($_param, $_param['id']);
  158. if (false === $_rs) {
  159. return $this->adminError(lang('EDIT_FAILED'));
  160. }
  161. return $this->adminSuccess(lang('EDIT_SUCCESS'));
  162. } else {
  163. $_code = CommonStatus::INVALID_PARAMS;
  164. return $this->adminError(CommonStatus::getMsg($_code));
  165. }
  166. }
  167. /**
  168. * 删除
  169. */
  170. public function delete() {
  171. $_id = $this->request->param('id/d', 0);
  172. if (empty($_id)) {
  173. $_code = CommonStatus::INVALID_PARAMS;
  174. return $this->adminError(CommonStatus::getMsg($_code));
  175. }
  176. $_rs = (new $this->model_class_name())->deleteData($_id);
  177. if (false === $_rs) {
  178. return $this->adminError(lang('DELETE_FAILED'));
  179. }
  180. return $this->adminSuccess(lang('DELETE_SUCCESS'));
  181. }
  182. /**
  183. * 排序 排序字段为list_orders数组 POST 排序字段为:list_order
  184. *
  185. * @param $model
  186. * @param string $field
  187. *
  188. */
  189. public function listOrders($model = null, $field = 'list_order') {
  190. $_rs = parent::listOrders(new $this->model_class_name(), $field);
  191. if (false == $_rs) {
  192. $_code = CommonStatus::INNER_ERROR;
  193. return $this->adminError(CommonStatus::getMsg($_code));
  194. }
  195. $_code = CommonStatus::NO_ERROR;
  196. return $this->adminSuccess(CommonStatus::getMsg($_code));
  197. }
  198. /**
  199. * 单个设置状态
  200. */
  201. public function setStatus() {
  202. $_id = $this->request->param('id/d', 0);
  203. $_ids = $this->request->param('ids/a', []);
  204. if (!empty($_id)) {
  205. $_ids = [$_id];
  206. }
  207. $_field = $this->request->param('field/s', 'status');
  208. $_status = $this->request->param($_field.'/d', 0);
  209. if ((empty($_ids)) || empty($_status)) {
  210. $this->adminError(lang('PARAM_ERROR'));
  211. }
  212. foreach ($_ids as $_id) {
  213. $_data[$_field] = $_status;
  214. $_rs = (new $this->model_class_name())->updateData($_data, $_id);
  215. if (false === $_rs) {
  216. $this->adminError(lang('EDIT_FAILED'));
  217. }
  218. }
  219. $this->adminSuccess(lang('EDIT_SUCCESS'));
  220. }
  221. /**
  222. * 导出数据
  223. */
  224. protected function exportIndex() {
  225. }
  226. protected function getSearchField() {
  227. $_fields_type = (new $this->model_class_name())->getFieldType();
  228. $_param = $this->request->param();
  229. $_search_param = [];
  230. foreach ($_fields_type as $_field => $_type) {
  231. // if (false == isset($_param[$_field]) || empty($_param[$_field])) {
  232. // continue;
  233. // }
  234. if (true == is_array($_type)) {
  235. list($_type, $param) = $_type;
  236. } elseif (strpos($_type, ':')) {
  237. list($_type, $param) = explode(':', $_type, 2);
  238. }
  239. switch ($_type) {
  240. case 'integer':
  241. case 'timestamp':
  242. $_search_param[$_field] = $this->request->param($_field.'/d', '');
  243. break;
  244. case 'float':
  245. $_search_param[$_field] = $this->request->param($_field.'/f', '');
  246. break;
  247. default:
  248. $_search_param[$_field] = $this->request->param($_field.'/s', '', 'trim');
  249. $_input = Filter::text($_field, $_search_param[$_field], lang('MSG_INPUT').lang($_field));
  250. $this->assign($_field.'_input', $_input);
  251. break;
  252. }
  253. }
  254. return $_search_param;
  255. }
  256. /**
  257. * 搜索参数
  258. * return array
  259. */
  260. protected function getSearchParam() {
  261. $_param = $this->getSearchField();
  262. $_param = $this->trimAll($_param);
  263. return $_param;
  264. }
  265. }