Backend.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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' : true) : $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. }
  85. }
  86. $this->error(__('Parameter %s can not be empty', ''));
  87. }
  88. return $this->view->fetch();
  89. }
  90. /**
  91. * 编辑
  92. */
  93. public function edit($ids = NULL)
  94. {
  95. $row = $this->model->get($ids);
  96. if (!$row)
  97. $this->error(__('No Results were found'));
  98. $adminIds = $this->getDataLimitAdminIds();
  99. if (is_array($adminIds)) {
  100. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  101. $this->error(__('You have no permission'));
  102. }
  103. }
  104. if ($this->request->isPost()) {
  105. $params = $this->request->post("row/a");
  106. if ($params) {
  107. try {
  108. //是否采用模型验证
  109. if ($this->modelValidate) {
  110. $name = basename(str_replace('\\', '/', get_class($this->model)));
  111. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  112. $row->validate($validate);
  113. }
  114. $result = $row->allowField(true)->save($params);
  115. if ($result !== false) {
  116. $this->success();
  117. } else {
  118. $this->error($row->getError());
  119. }
  120. } catch (\think\exception\PDOException $e) {
  121. $this->error($e->getMessage());
  122. }
  123. }
  124. $this->error(__('Parameter %s can not be empty', ''));
  125. }
  126. $this->view->assign("row", $row);
  127. return $this->view->fetch();
  128. }
  129. /**
  130. * 删除
  131. */
  132. public function del($ids = "")
  133. {
  134. if ($ids) {
  135. $pk = $this->model->getPk();
  136. $adminIds = $this->getDataLimitAdminIds();
  137. if (is_array($adminIds)) {
  138. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  139. }
  140. $list = $this->model->where($pk, 'in', $ids)->select();
  141. $count = 0;
  142. foreach ($list as $k => $v) {
  143. $count += $v->delete();
  144. }
  145. if ($count) {
  146. $this->success();
  147. } else {
  148. $this->error(__('No rows were deleted'));
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', 'ids'));
  152. }
  153. /**
  154. * 真实删除
  155. */
  156. public function destroy($ids = "")
  157. {
  158. $pk = $this->model->getPk();
  159. $adminIds = $this->getDataLimitAdminIds();
  160. if (is_array($adminIds)) {
  161. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  162. }
  163. if ($ids) {
  164. $this->model->where($pk, 'in', $ids);
  165. }
  166. $count = 0;
  167. $list = $this->model->onlyTrashed()->select();
  168. foreach ($list as $k => $v) {
  169. $count += $v->delete(true);
  170. }
  171. if ($count) {
  172. $this->success();
  173. } else {
  174. $this->error(__('No rows were deleted'));
  175. }
  176. $this->error(__('Parameter %s can not be empty', 'ids'));
  177. }
  178. /**
  179. * 还原
  180. */
  181. public function restore($ids = "")
  182. {
  183. $pk = $this->model->getPk();
  184. $adminIds = $this->getDataLimitAdminIds();
  185. if (is_array($adminIds)) {
  186. $this->model->where($this->dataLimitField, 'in', $adminIds);
  187. }
  188. if ($ids) {
  189. $this->model->where($pk, 'in', $ids);
  190. }
  191. $count = 0;
  192. $list = $this->model->onlyTrashed()->select();
  193. foreach ($list as $index => $item) {
  194. $count += $item->restore();
  195. }
  196. if ($count) {
  197. $this->success();
  198. }
  199. $this->error(__('No rows were updated'));
  200. }
  201. /**
  202. * 批量更新
  203. */
  204. public function multi($ids = "")
  205. {
  206. $ids = $ids ? $ids : $this->request->param("ids");
  207. if ($ids) {
  208. if ($this->request->has('params')) {
  209. parse_str($this->request->post("params"), $values);
  210. $values = array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  211. if ($values) {
  212. $adminIds = $this->getDataLimitAdminIds();
  213. if (is_array($adminIds)) {
  214. $this->model->where($this->dataLimitField, 'in', $adminIds);
  215. }
  216. $count = 0;
  217. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  218. foreach ($list as $index => $item) {
  219. $count += $item->allowField(true)->isUpdate(true)->save($values);
  220. }
  221. if ($count) {
  222. $this->success();
  223. } else {
  224. $this->error(__('No rows were updated'));
  225. }
  226. } else {
  227. $this->error(__('You have no permission'));
  228. }
  229. }
  230. }
  231. $this->error(__('Parameter %s can not be empty', 'ids'));
  232. }
  233. /**
  234. * 导入
  235. */
  236. protected function import()
  237. {
  238. $file = $this->request->request('file');
  239. if (!$file) {
  240. $this->error(__('Parameter %s can not be empty', 'file'));
  241. }
  242. $filePath = ROOT_PATH . DS . 'public' . DS . $file;
  243. if (!is_file($filePath)) {
  244. $this->error(__('No results were found'));
  245. }
  246. $PHPReader = new \PHPExcel_Reader_Excel2007();
  247. if (!$PHPReader->canRead($filePath)) {
  248. $PHPReader = new \PHPExcel_Reader_Excel5();
  249. if (!$PHPReader->canRead($filePath)) {
  250. $PHPReader = new \PHPExcel_Reader_CSV();
  251. if (!$PHPReader->canRead($filePath)) {
  252. $this->error(__('Unknown data format'));
  253. }
  254. }
  255. }
  256. //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
  257. $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
  258. $table = $this->model->getQuery()->getTable();
  259. $database = \think\Config::get('database.database');
  260. $fieldArr = [];
  261. $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
  262. foreach ($list as $k => $v) {
  263. if ($importHeadType == 'comment') {
  264. $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
  265. } else {
  266. $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
  267. }
  268. }
  269. $PHPExcel = $PHPReader->load($filePath); //加载文件
  270. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  271. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  272. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  273. $maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn);
  274. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  275. for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
  276. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  277. $fields[] = $val;
  278. }
  279. }
  280. $insert = [];
  281. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  282. $values = [];
  283. for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
  284. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  285. $values[] = is_null($val) ? '' : $val;
  286. }
  287. $row = [];
  288. $temp = array_combine($fields, $values);
  289. foreach ($temp as $k => $v) {
  290. if (isset($fieldArr[$k]) && $k !== '') {
  291. $row[$fieldArr[$k]] = $v;
  292. }
  293. }
  294. if ($row) {
  295. $insert[] = $row;
  296. }
  297. }
  298. if (!$insert) {
  299. $this->error(__('No rows were updated'));
  300. }
  301. try {
  302. $this->model->saveAll($insert);
  303. } catch (\think\exception\PDOException $exception) {
  304. $this->error($exception->getMessage());
  305. }
  306. $this->success();
  307. }
  308. }