Backend.php 12 KB

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