ConfigOption.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\model\ConfigOption as optionModel;
  11. use think\Db;
  12. use think\Log;
  13. class ConfigOption extends Permissions
  14. {
  15. public function index()
  16. {
  17. $pid = $this->request->param('pid', 0, 'intval');
  18. if ($this->request->isAjax()) {
  19. $post = $this->request->param();
  20. $where = [
  21. 'pid' => $pid,
  22. ];
  23. if (isset($post['keywords']) and !empty($post['keywords'])) {
  24. $where['name'] = ['like', '%' . $post['keywords'] . '%'];
  25. }
  26. $model = new optionModel();
  27. $count = $model->where($where)->count();
  28. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('sort desc')->select();
  29. foreach ($data as $k => $v) {
  30. $v['cate_name'] = $v->config['name'];
  31. $v['thumb_url'] = geturl($v['image']);
  32. $v['group_type'] = $v->config['type'];
  33. $data[$k] = $v;
  34. }
  35. return array('code' => 0, 'count' => $count, 'data' => $data);
  36. } else {
  37. $this->assign('config', \app\common\model\Config::get($pid));
  38. return $this->fetch();
  39. }
  40. }
  41. public function publish()
  42. {
  43. $id = $this->request->param('id', 0, 'intval');
  44. $pid = $this->request->param('pid', 0, 'intval');
  45. $this->assign('pid', $pid);
  46. $model = new optionModel();
  47. $post = $this->request->post();
  48. if ($this->request->isPost()) {
  49. $validate = new \think\Validate([
  50. ['name', 'require', '标题不能为空'],
  51. ['pid', 'require', '请选择分类'],
  52. ]);
  53. if (!$validate->check($post)) {
  54. $this->error('提交失败:' . $validate->getError());
  55. }
  56. } else {
  57. $this->assign('config', \app\common\model\Config::get($pid));
  58. }
  59. if ($id > 0) {
  60. //是修改操作
  61. $link = $model->where('id', $id)->find();
  62. if (empty($link)) {
  63. $this->error('id不正确');
  64. }
  65. if ($this->request->isPost()) {
  66. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  67. $this->error('修改失败');
  68. } else {
  69. $this->success('修改成功', 'index', ['pid' => $pid]);
  70. }
  71. } else {
  72. $this->assign('link', $link);
  73. return $this->fetch();
  74. }
  75. } else {
  76. //是新增操作
  77. if ($this->request->isPost()) {
  78. if ($model->where('pid', $pid)->count() == 0) {
  79. $config = \app\common\model\Config::get($pid);
  80. if (!$config) {
  81. $this->error('pid异常');
  82. }
  83. if ($config->type == \app\common\model\Config::TYPE_RADIO) {
  84. $post['single_status'] = 1;
  85. }
  86. }
  87. if (false == $model->allowField(true)->save($post)) {
  88. $this->error('添加失败');
  89. } else {
  90. $this->success('添加成功', 'index', ['pid' => $pid]);
  91. }
  92. } else {
  93. return $this->fetch();
  94. }
  95. }
  96. }
  97. public function delete()
  98. {
  99. if ($this->request->isAjax()) {
  100. $id = $this->request->param('id', 0, 'intval');
  101. if (false == Db::name('config_option')->where('id', $id)->delete()) {
  102. $this->error('删除失败');
  103. } else {
  104. $this->success('删除成功', 'index');
  105. }
  106. }
  107. }
  108. public function deletes()
  109. {
  110. if ($this->request->isAjax()) {
  111. $post = $this->request->param();
  112. $ids = $post['ids'];
  113. $model = new optionModel();
  114. if ($model->where('id', 'in', $ids)->delete()) {
  115. Log::log("批量删除链接:" . implode(',', $ids));
  116. $this->success('删除成功');
  117. }
  118. }
  119. }
  120. //排序
  121. public function sort()
  122. {
  123. if ($this->request->isPost() && $this->request->has('ids')) {
  124. $post = $this->request->post();
  125. $i = 0;
  126. foreach ($post['ids'] as $k => $id) {
  127. $sort = Db::name('config_option')->where('id', $id)->value('sort');
  128. $newsort = $post['sorts'][$k]??$sort;
  129. if ($sort != $newsort) {
  130. if (false == Db::name('config_option')->where('id', $id)->update(['sort' => $newsort])) {
  131. $this->error('更新失败');
  132. } else {
  133. $i++;
  134. }
  135. }
  136. }
  137. $this->success('成功更新' . $i . '个数据', 'index');
  138. } else {
  139. $this->error('无数据更新', 'index');
  140. }
  141. }
  142. public function status()
  143. {
  144. if ($this->request->isPost()) {
  145. $post = $this->request->post();
  146. if (false == Db::name('config_option')->where('id', $post['id'])->update(['status' => $post['status']])) {
  147. $this->error('设置失败');
  148. } else {
  149. $this->success('设置成功', 'index');
  150. }
  151. }
  152. }
  153. //单选状态
  154. public function single_status()
  155. {
  156. if ($this->request->isPost()) {
  157. $post = $this->request->post();
  158. Db::name('config_option')->where('pid', $post['pid'])->update(['single_status' => 0]);
  159. if (false == Db::name('config_option')->where('id', $post['id'])->update(['single_status' => $post['status']])) {
  160. $this->error('设置失败');
  161. } else {
  162. $this->success('设置成功', 'index');
  163. }
  164. }
  165. }
  166. }