Poster.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\Config;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * @icon fa fa-circle-o
  11. */
  12. class Poster extends Backend
  13. {
  14. /**
  15. * Poster模型对象
  16. * @var \app\admin\model\Poster
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Poster;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. $tableList = [];
  25. $list = \think\Db::query("SHOW TABLES");
  26. foreach ($list as $key => $row) {
  27. $tableList[reset($row)] = reset($row);
  28. }
  29. $this->view->assign("tableList", $tableList);
  30. }
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = false;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $total = $this->model
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->count();
  50. $list = $this->model
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->limit($offset, $limit)
  54. ->select();
  55. foreach ($list as $row) {
  56. $row->visible(['id','title','bg_image','weigh','status','createtime','updatetime']);
  57. }
  58. $list = collection($list)->toArray();
  59. $result = array("total" => $total, "rows" => $list);
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 获取字段列表
  66. * @internal
  67. */
  68. public function get_field_list()
  69. {
  70. $dbname = Config::get('database.database');
  71. $prefix = Config::get('database.prefix');
  72. $table = $this->request->request('table');
  73. //从数据库中获取表字段信息
  74. $sql = "SELECT * FROM `information_schema`.`columns` "
  75. . "WHERE TABLE_SCHEMA = ? AND table_name = ? "
  76. . "ORDER BY ORDINAL_POSITION";
  77. //加载主表的列
  78. $columnList = Db::query($sql, [$dbname, $table]);
  79. $fieldlist = [];
  80. foreach ($columnList as $index => $item) {
  81. $fieldlist[] = $item['COLUMN_NAME'];
  82. }
  83. $this->success("", null, ['fieldlist' => $fieldlist]);
  84. }
  85. /**
  86. * 添加
  87. */
  88. public function add()
  89. {
  90. if ($this->request->isPost()) {
  91. $params = $this->request->post("row/a");
  92. if ($params) {
  93. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  94. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  95. $params[$this->dataLimitField] = $this->auth->id;
  96. }
  97. $result = false;
  98. Db::startTrans();
  99. try {
  100. //是否采用模型验证
  101. if ($this->modelValidate) {
  102. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  103. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  104. $this->model->validateFailException(true)->validate($validate);
  105. }
  106. $result = $this->model->allowField(true)->save($params);
  107. Db::commit();
  108. } catch (ValidateException $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. } catch (PDOException $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. } catch (Exception $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. }
  118. if ($result !== false) {
  119. $this->success();
  120. } else {
  121. $this->error(__('No rows were inserted'));
  122. }
  123. }
  124. $this->error(__('Parameter %s can not be empty', ''));
  125. }
  126. return $this->view->fetch();
  127. }
  128. /**
  129. * 编辑
  130. */
  131. public function edit($ids = null)
  132. {
  133. $row = $this->model->get($ids);
  134. if (!$row) {
  135. $this->error(__('No Results were found'));
  136. }
  137. $adminIds = $this->getDataLimitAdminIds();
  138. if (is_array($adminIds)) {
  139. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  140. $this->error(__('You have no permission'));
  141. }
  142. }
  143. if ($this->request->isPost()) {
  144. $params = $this->request->post("row/a");
  145. if ($params) {
  146. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  147. $result = false;
  148. Db::startTrans();
  149. try {
  150. //是否采用模型验证
  151. if ($this->modelValidate) {
  152. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  153. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  154. $row->validateFailException(true)->validate($validate);
  155. }
  156. $result = $row->allowField(true)->save($params);
  157. Db::commit();
  158. } catch (ValidateException $e) {
  159. Db::rollback();
  160. $this->error($e->getMessage());
  161. } catch (PDOException $e) {
  162. Db::rollback();
  163. $this->error($e->getMessage());
  164. } catch (Exception $e) {
  165. Db::rollback();
  166. $this->error($e->getMessage());
  167. }
  168. if ($result !== false) {
  169. $this->success();
  170. } else {
  171. $this->error(__('No rows were updated'));
  172. }
  173. }
  174. $this->error(__('Parameter %s can not be empty', ''));
  175. }
  176. $row['data'] = json_decode(str_replace('&quot;', '\'', $row['data']), true);
  177. $this->view->assign("row", $row);
  178. return $this->view->fetch();
  179. }
  180. /**
  181. * 删除
  182. */
  183. public function del($ids = "")
  184. {
  185. if ($ids) {
  186. $pk = $this->model->getPk();
  187. $adminIds = $this->getDataLimitAdminIds();
  188. if (is_array($adminIds)) {
  189. $this->model->where($this->dataLimitField, 'in', $adminIds);
  190. }
  191. $list = $this->model->where($pk, 'in', $ids)->select();
  192. $count = 0;
  193. Db::startTrans();
  194. try {
  195. foreach ($list as $k => $v) {
  196. $count += $v->delete();
  197. rmdirs(ROOT_PATH . 'public/' . $this->model->getDirs($v['id']), true);
  198. }
  199. Db::commit();
  200. } catch (PDOException $e) {
  201. Db::rollback();
  202. $this->error($e->getMessage());
  203. } catch (Exception $e) {
  204. Db::rollback();
  205. $this->error($e->getMessage());
  206. }
  207. if ($count) {
  208. $this->success();
  209. } else {
  210. $this->error(__('No rows were deleted'));
  211. }
  212. }
  213. $this->error(__('Parameter %s can not be empty', 'ids'));
  214. }
  215. /**
  216. * 海报记录
  217. * @author Created by Xing <464401240@qq.com>
  218. */
  219. public function posterrecord()
  220. {
  221. $ids = $this->request->param('ids');
  222. //当前是否为关联查询
  223. $this->relationSearch = true;
  224. //设置过滤方法
  225. $this->request->filter(['strip_tags', 'trim']);
  226. if ($this->request->isAjax()) {
  227. $this->model = new \app\admin\model\PosterLog();
  228. $condition = $ids ? ['poster_id' => $ids] : [];
  229. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  230. $total = $this->model
  231. ->with(['user'])
  232. ->where($condition)
  233. ->where($where)
  234. ->order($sort, $order)
  235. ->count();
  236. $list = $this->model
  237. ->with(['user'])
  238. ->where($condition)
  239. ->where($where)
  240. ->order($sort, $order)
  241. ->limit($offset, $limit)
  242. ->select();
  243. foreach ($list as $row) {
  244. $row->visible(['id','image','createtime','updatetime']);
  245. $row->visible(['user']);
  246. $row->getRelation('user')->visible(['nickname']);
  247. $row['image'] = '/' . $row['image'];
  248. }
  249. $list = collection($list)->toArray();
  250. $result = array("total" => $total, "rows" => $list);
  251. return json($result);
  252. }
  253. $this->assignconfig('ids', $ids);
  254. $this->view->assign("ids", $ids);
  255. return $this->view->fetch();
  256. }
  257. /**
  258. * 清除海报数据
  259. * @author Created by Xing <464401240@qq.com>
  260. */
  261. public function clear()
  262. {
  263. $ids = $this->request->param('poster_id');
  264. $where = ['poster_id' => ['gt', 0]];
  265. if ($ids) {
  266. $where = ['poster_id' => $ids];
  267. }
  268. $posterLogModel = new \app\admin\model\PosterLog();
  269. $adminIds = $this->getDataLimitAdminIds();
  270. if (is_array($adminIds)) {
  271. $posterLogModel->where($this->dataLimitField, 'in', $adminIds);
  272. }
  273. $list = $posterLogModel->where($where)->select();
  274. $count = 0;
  275. Db::startTrans();
  276. try {
  277. foreach ($list as $k => $v) {
  278. $count += $v->delete();
  279. }
  280. Db::commit();
  281. } catch (PDOException $e) {
  282. Db::rollback();
  283. $this->error($e->getMessage());
  284. } catch (Exception $e) {
  285. Db::rollback();
  286. $this->error($e->getMessage());
  287. }
  288. rmdirs(ROOT_PATH . 'public/' . $this->model->getDirs($ids), true);
  289. if ($count) {
  290. $this->success();
  291. } else {
  292. $this->error(__('No rows were deleted'));
  293. }
  294. $this->error(__('Parameter %s can not be empty', 'ids'));
  295. }
  296. }