Backend.php 12 KB

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