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