Backend.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace app\admin\library\traits;
  3. trait Backend
  4. {
  5. /**
  6. * 查看
  7. */
  8. public function index()
  9. {
  10. //设置过滤方法
  11. $this->request->filter(['strip_tags']);
  12. if ($this->request->isAjax()) {
  13. //如果发送的来源是Selectpage,则转发到Selectpage
  14. if ($this->request->request('keyField')) {
  15. return $this->selectpage();
  16. }
  17. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  18. $total = $this->model
  19. ->where($where)
  20. ->order($sort, $order)
  21. ->count();
  22. $list = $this->model
  23. ->where($where)
  24. ->order($sort, $order)
  25. ->limit($offset, $limit)
  26. ->select();
  27. $list = collection($list)->toArray();
  28. $result = array("total" => $total, "rows" => $list);
  29. return json($result);
  30. }
  31. return $this->view->fetch();
  32. }
  33. /**
  34. * 回收站
  35. */
  36. public function recyclebin()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags']);
  40. if ($this->request->isAjax()) {
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $total = $this->model
  43. ->onlyTrashed()
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->count();
  47. $list = $this->model
  48. ->onlyTrashed()
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->limit($offset, $limit)
  52. ->select();
  53. $result = array("total" => $total, "rows" => $list);
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 添加
  60. */
  61. public function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $params = $this->request->post("row/a");
  65. if ($params) {
  66. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  67. $params[$this->dataLimitField] = $this->auth->id;
  68. }
  69. try {
  70. //是否采用模型验证
  71. if ($this->modelValidate) {
  72. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  73. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  74. $this->model->validate($validate);
  75. }
  76. $result = $this->model->allowField(true)->save($params);
  77. if ($result !== false) {
  78. $this->success();
  79. } else {
  80. $this->error($this->model->getError());
  81. }
  82. } catch (\think\exception\PDOException $e) {
  83. $this->error($e->getMessage());
  84. } catch (\think\Exception $e) {
  85. $this->error($e->getMessage());
  86. }
  87. }
  88. $this->error(__('Parameter %s can not be empty', ''));
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 编辑
  94. */
  95. public function edit($ids = NULL)
  96. {
  97. $row = $this->model->get($ids);
  98. if (!$row)
  99. $this->error(__('No Results were found'));
  100. $adminIds = $this->getDataLimitAdminIds();
  101. if (is_array($adminIds)) {
  102. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  103. $this->error(__('You have no permission'));
  104. }
  105. }
  106. if ($this->request->isPost()) {
  107. $params = $this->request->post("row/a");
  108. if ($params) {
  109. try {
  110. //是否采用模型验证
  111. if ($this->modelValidate) {
  112. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  113. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  114. $row->validate($validate);
  115. }
  116. $result = $row->allowField(true)->save($params);
  117. if ($result !== false) {
  118. $this->success();
  119. } else {
  120. $this->error($row->getError());
  121. }
  122. } catch (\think\exception\PDOException $e) {
  123. $this->error($e->getMessage());
  124. } catch (\think\Exception $e) {
  125. $this->error($e->getMessage());
  126. }
  127. }
  128. $this->error(__('Parameter %s can not be empty', ''));
  129. }
  130. $this->view->assign("row", $row);
  131. return $this->view->fetch();
  132. }
  133. /**
  134. * 删除
  135. */
  136. public function del($ids = "")
  137. {
  138. if ($ids) {
  139. $pk = $this->model->getPk();
  140. $adminIds = $this->getDataLimitAdminIds();
  141. if (is_array($adminIds)) {
  142. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  143. }
  144. $list = $this->model->where($pk, 'in', $ids)->select();
  145. $count = 0;
  146. foreach ($list as $k => $v) {
  147. $count += $v->delete();
  148. }
  149. if ($count) {
  150. $this->success();
  151. } else {
  152. $this->error(__('No rows were deleted'));
  153. }
  154. }
  155. $this->error(__('Parameter %s can not be empty', 'ids'));
  156. }
  157. /**
  158. * 真实删除
  159. */
  160. public function destroy($ids = "")
  161. {
  162. $pk = $this->model->getPk();
  163. $adminIds = $this->getDataLimitAdminIds();
  164. if (is_array($adminIds)) {
  165. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  166. }
  167. if ($ids) {
  168. $this->model->where($pk, 'in', $ids);
  169. }
  170. $count = 0;
  171. $list = $this->model->onlyTrashed()->select();
  172. foreach ($list as $k => $v) {
  173. $count += $v->delete(true);
  174. }
  175. if ($count) {
  176. $this->success();
  177. } else {
  178. $this->error(__('No rows were deleted'));
  179. }
  180. $this->error(__('Parameter %s can not be empty', 'ids'));
  181. }
  182. /**
  183. * 还原
  184. */
  185. public function restore($ids = "")
  186. {
  187. $pk = $this->model->getPk();
  188. $adminIds = $this->getDataLimitAdminIds();
  189. if (is_array($adminIds)) {
  190. $this->model->where($this->dataLimitField, 'in', $adminIds);
  191. }
  192. if ($ids) {
  193. $this->model->where($pk, 'in', $ids);
  194. }
  195. $count = 0;
  196. $list = $this->model->onlyTrashed()->select();
  197. foreach ($list as $index => $item) {
  198. $count += $item->restore();
  199. }
  200. if ($count) {
  201. $this->success();
  202. }
  203. $this->error(__('No rows were updated'));
  204. }
  205. /**
  206. * 批量更新
  207. */
  208. public function multi($ids = "")
  209. {
  210. $ids = $ids ? $ids : $this->request->param("ids");
  211. if ($ids) {
  212. if ($this->request->has('params')) {
  213. parse_str($this->request->post("params"), $values);
  214. $values = array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  215. if ($values || $this->auth->isSuperAdmin()) {
  216. $adminIds = $this->getDataLimitAdminIds();
  217. if (is_array($adminIds)) {
  218. $this->model->where($this->dataLimitField, 'in', $adminIds);
  219. }
  220. $count = 0;
  221. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  222. foreach ($list as $index => $item) {
  223. $count += $item->allowField(true)->isUpdate(true)->save($values);
  224. }
  225. if ($count) {
  226. $this->success();
  227. } else {
  228. $this->error(__('No rows were updated'));
  229. }
  230. } else {
  231. $this->error(__('You have no permission'));
  232. }
  233. }
  234. }
  235. $this->error(__('Parameter %s can not be empty', 'ids'));
  236. }
  237. /**
  238. * 导入
  239. */
  240. protected function import()
  241. {
  242. $file = $this->request->request('file');
  243. if (!$file) {
  244. $this->error(__('Parameter %s can not be empty', 'file'));
  245. }
  246. $filePath = ROOT_PATH . DS . 'public' . DS . $file;
  247. if (!is_file($filePath)) {
  248. $this->error(__('No results were found'));
  249. }
  250. $PHPReader = new \PHPExcel_Reader_Excel2007();
  251. if (!$PHPReader->canRead($filePath)) {
  252. $PHPReader = new \PHPExcel_Reader_Excel5();
  253. if (!$PHPReader->canRead($filePath)) {
  254. $PHPReader = new \PHPExcel_Reader_CSV();
  255. if (!$PHPReader->canRead($filePath)) {
  256. $this->error(__('Unknown data format'));
  257. }
  258. }
  259. }
  260. //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
  261. $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
  262. $table = $this->model->getQuery()->getTable();
  263. $database = \think\Config::get('database.database');
  264. $fieldArr = [];
  265. $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
  266. foreach ($list as $k => $v) {
  267. if ($importHeadType == 'comment') {
  268. $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
  269. } else {
  270. $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
  271. }
  272. }
  273. $PHPExcel = $PHPReader->load($filePath); //加载文件
  274. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  275. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  276. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  277. $maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn);
  278. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  279. for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
  280. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  281. $fields[] = $val;
  282. }
  283. }
  284. $insert = [];
  285. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  286. $values = [];
  287. for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
  288. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  289. $values[] = is_null($val) ? '' : $val;
  290. }
  291. $row = [];
  292. $temp = array_combine($fields, $values);
  293. foreach ($temp as $k => $v) {
  294. if (isset($fieldArr[$k]) && $k !== '') {
  295. $row[$fieldArr[$k]] = $v;
  296. }
  297. }
  298. if ($row) {
  299. $insert[] = $row;
  300. }
  301. }
  302. if (!$insert) {
  303. $this->error(__('No rows were updated'));
  304. }
  305. try {
  306. $this->model->saveAll($insert);
  307. } catch (\think\exception\PDOException $exception) {
  308. $this->error($exception->getMessage());
  309. } catch (\Exception $e) {
  310. $this->error($e->getMessage());
  311. }
  312. $this->success();
  313. }
  314. }