ConfigOption.php 6.1 KB

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