Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Curd.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | Trait Curd
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\library\traits;
  15. use Exception;
  16. use think\Db;
  17. use think\exception\PDOException;
  18. use think\exception\ValidateException;
  19. trait Curd
  20. {
  21. /**
  22. * 排除前台提交过来的字段
  23. * @param $params
  24. * @return array
  25. */
  26. protected function preExcludeFields($params)
  27. {
  28. if (is_array($this->excludeFields)) {
  29. foreach ($this->excludeFields as $field) {
  30. if (key_exists($field, $params)) {
  31. unset($params[$field]);
  32. }
  33. }
  34. } else {
  35. if (key_exists($this->excludeFields, $params)) {
  36. unset($params[$this->excludeFields]);
  37. }
  38. }
  39. return $params;
  40. }
  41. /**
  42. * 查看
  43. */
  44. public function index()
  45. {
  46. if ($this->request->isAjax()) {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  52. $count = $this->modelClass
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->count();
  56. $data = $this->modelClass
  57. ->where($where)
  58. ->order($sort, $order)
  59. ->page($page, $limit)
  60. ->select();
  61. $result = ["code" => 0, 'count' => $count, 'data' => $data];
  62. return json($result);
  63. }
  64. return $this->fetch();
  65. }
  66. /**
  67. * 回收站
  68. */
  69. public function recyclebin()
  70. {
  71. if ($this->request->isAjax()) {
  72. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  73. $count = $this->modelClass
  74. ->onlyTrashed()
  75. ->where($where)
  76. ->order($sort, $order)
  77. ->count();
  78. $data = $this->modelClass
  79. ->onlyTrashed()
  80. ->where($where)
  81. ->order($sort, $order)
  82. ->page($page, $limit)
  83. ->select();
  84. $result = ["code" => 0, 'count' => $count, 'data' => $data];
  85. return json($result);
  86. }
  87. return $this->fetch();
  88. }
  89. /**
  90. * 添加
  91. */
  92. public function add()
  93. {
  94. if ($this->request->isPost()) {
  95. $params = $this->request->post("row/a");
  96. if ($params) {
  97. $params = $this->preExcludeFields($params);
  98. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  99. $params[$this->dataLimitField] = $this->auth->id;
  100. }
  101. $result = false;
  102. Db::startTrans();
  103. try {
  104. //是否采用模型验证
  105. if ($this->modelValidate) {
  106. $name = str_replace("\\model\\", "\\validate\\", get_class($this->modelClass));
  107. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  108. $this->validateFailException(true)->validate($params, $validate);
  109. }
  110. $result = $this->modelClass->allowField(true)->save($params);
  111. Db::commit();
  112. } catch (ValidateException | PDOException | Exception $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. }
  116. if ($result !== false) {
  117. $this->success('新增成功');
  118. } else {
  119. $this->error('未插入任何行');
  120. }
  121. }
  122. $this->error('参数不能为空');
  123. }
  124. return $this->fetch();
  125. }
  126. /**
  127. * 编辑
  128. */
  129. public function edit()
  130. {
  131. $id = $this->request->param('id/d', 0);
  132. $row = $this->modelClass->get($id);
  133. if (!$row) {
  134. $this->error('记录未找到');
  135. }
  136. $adminIds = $this->getDataLimitAdminIds();
  137. if (is_array($adminIds)) {
  138. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  139. $this->error('你没有权限访问');
  140. }
  141. }
  142. if ($this->request->isPost()) {
  143. $params = $this->request->post("row/a");
  144. if ($params) {
  145. $params = $this->preExcludeFields($params);
  146. $result = false;
  147. Db::startTrans();
  148. try {
  149. //是否采用模型验证
  150. if ($this->modelValidate) {
  151. $name = str_replace("\\model\\", "\\validate\\", get_class($this->modelClass));
  152. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  153. $this->validateFailException(true)->validate($params, $validate);
  154. }
  155. $result = $row->allowField(true)->save($params);
  156. Db::commit();
  157. } catch (ValidateException | PDOException | Exception $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. if ($result !== false) {
  162. $this->success('修改成功');
  163. } else {
  164. $this->error('未更新任何行');
  165. }
  166. }
  167. $this->error('参数不能为空');
  168. }
  169. $this->view->assign("data", $row);
  170. return $this->fetch();
  171. }
  172. /**
  173. * 删除
  174. */
  175. public function del()
  176. {
  177. if (false === $this->request->isPost()) {
  178. $this->error('未知参数');
  179. }
  180. $ids = $this->request->param('id/a', null);
  181. if (empty($ids)) {
  182. $this->error('参数错误!');
  183. }
  184. if (!is_array($ids)) {
  185. $ids = [0 => $ids];
  186. }
  187. $pk = $this->modelClass->getPk();
  188. $adminIds = $this->getDataLimitAdminIds();
  189. $where = [];
  190. if (is_array($adminIds)) {
  191. $where[] = [$this->dataLimitField, 'in', $adminIds];
  192. }
  193. $where[] = [$pk, 'in', $ids];
  194. $list = $this->modelClass->where($where)->select();
  195. $count = 0;
  196. Db::startTrans();
  197. try {
  198. foreach ($list as $k => $v) {
  199. $count += $v->delete();
  200. }
  201. Db::commit();
  202. } catch (PDOException | Exception $e) {
  203. Db::rollback();
  204. $this->error($e->getMessage());
  205. }
  206. if ($count) {
  207. $this->success("操作成功!");
  208. }
  209. $this->error('没有数据删除!');
  210. }
  211. /**
  212. * 真实删除
  213. */
  214. public function destroy()
  215. {
  216. if (false === $this->request->isPost()) {
  217. $this->error('未知参数');
  218. }
  219. $ids = $this->request->param('id/a', null);
  220. if ($ids && !is_array($ids)) {
  221. $ids = [0 => $ids];
  222. }
  223. $pk = $this->modelClass->getPk();
  224. $adminIds = $this->getDataLimitAdminIds();
  225. $where = [];
  226. if (is_array($adminIds)) {
  227. $where[] = [$this->dataLimitField, 'in', $adminIds];
  228. }
  229. if ($ids) {
  230. $where[] = [$pk, 'in', $ids];
  231. }
  232. $count = 0;
  233. Db::startTrans();
  234. try {
  235. $list = $this->modelClass->onlyTrashed()->where($where)->select();
  236. foreach ($list as $item) {
  237. $count += $item->delete(true);
  238. }
  239. Db::commit();
  240. } catch (PDOException | Exception $e) {
  241. Db::rollback();
  242. $this->error($e->getMessage());
  243. }
  244. if ($count) {
  245. $this->success("操作成功!");
  246. }
  247. $this->error('没有数据删除!');
  248. }
  249. /**
  250. * 还原
  251. */
  252. public function restore()
  253. {
  254. if (false === $this->request->isPost()) {
  255. $this->error('未知参数');
  256. }
  257. $ids = $this->request->param('id/a', null);
  258. if ($ids && !is_array($ids)) {
  259. $ids = [0 => $ids];
  260. }
  261. $pk = $this->modelClass->getPk();
  262. $adminIds = $this->getDataLimitAdminIds();
  263. $where = [];
  264. if (is_array($adminIds)) {
  265. $where[] = [$this->dataLimitField, 'in', $adminIds];
  266. }
  267. if ($ids) {
  268. $where[] = [$pk, 'in', $ids];
  269. }
  270. $count = 0;
  271. Db::startTrans();
  272. try {
  273. $list = $this->modelClass->onlyTrashed()->where($where)->select();
  274. foreach ($list as $item) {
  275. $count += $item->restore();
  276. }
  277. Db::commit();
  278. } catch (PDOException | Exception $e) {
  279. Db::rollback();
  280. $this->error($e->getMessage());
  281. }
  282. if ($count) {
  283. $this->success("操作成功!");
  284. }
  285. $this->error('未更新任何行');
  286. }
  287. /**
  288. * 批量更新
  289. */
  290. public function multi()
  291. {
  292. if (false === $this->request->isPost()) {
  293. $this->error('未知参数');
  294. }
  295. $ids = $this->request->param('id/a', null);
  296. if (empty($ids)) {
  297. $this->error('参数错误!');
  298. }
  299. if (!is_array($ids)) {
  300. $ids = [0 => $ids];
  301. }
  302. $value = $this->request->param('value/d', 0);
  303. if ($this->request->has('param')) {
  304. $param = $this->request->param('param/s');
  305. $param = $this->auth->isAdministrator() ? $param : (in_array($param, (is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields))) ? $param : '');
  306. if ($param) {
  307. $pk = $this->modelClass->getPk();
  308. $adminIds = $this->getDataLimitAdminIds();
  309. $where = [];
  310. if (is_array($adminIds)) {
  311. $where[] = [$this->dataLimitField, 'in', $adminIds];
  312. }
  313. $where[] = [$pk, 'in', $ids];
  314. $count = 0;
  315. Db::startTrans();
  316. try {
  317. $list = $this->modelClass->where($where)->select();
  318. foreach ($list as $item) {
  319. $item->{$param} = $value;
  320. $count += $item->save();
  321. }
  322. Db::commit();
  323. } catch (Exception $e) {
  324. Db::rollback();
  325. $this->error($e->getMessage());
  326. }
  327. if ($count) {
  328. $this->success("操作成功!");
  329. }
  330. $this->error('未更新任何行');
  331. } else {
  332. $this->error('你没有权限操作!');
  333. }
  334. }
  335. $this->error('Param参数不能为空');
  336. }
  337. }