Urlsconfig.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 听雨 < 389625819@qq.com >
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\controller\base\Permissions;
  13. use app\admin\model\Urlconfig;
  14. use think\Cache;
  15. use think\Db;
  16. class Urlsconfig extends Permissions
  17. {
  18. public function index()
  19. {
  20. $model = new Urlconfig();
  21. $urlconfig = $model->order('create_time desc')->paginate(20);
  22. $this->assign('urlconfig', $urlconfig);
  23. return $this->fetch();
  24. }
  25. public function publish()
  26. {
  27. $id = $this->request->param('id', 0, 'intval');
  28. $model = new Urlconfig();
  29. $post = $this->request->post();
  30. if ($this->request->isPost()) {
  31. $validate = new \think\Validate([
  32. ['url|需要美化的url', 'require'],
  33. ['aliases|美化后的url', 'require|regex:^[a-zA-Z_]+\S*$'],
  34. ]);
  35. if (!$validate->check($post)) {
  36. $this->error('提交失败:' . $validate->getError());
  37. }
  38. }
  39. if ($id > 0) {
  40. //是修改操作
  41. if ($this->request->isPost()) {
  42. $urlconfig = $model->where('id', $id)->find();
  43. if (empty($urlconfig)) {
  44. $this->error('id不正确');
  45. }
  46. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  47. $this->error('修改失败');
  48. } else {
  49. Cache::clear();
  50. $this->success('修改成功,并清理缓存', 'admin/urlsconfig/index');
  51. }
  52. } else {
  53. $urlconfig = $model->where('id', $id)->find();
  54. if (!empty($urlconfig)) {
  55. $this->assign('urlconfig', $urlconfig);
  56. return $this->fetch();
  57. } else {
  58. $this->error('id不正确');
  59. }
  60. }
  61. } else {
  62. //是新增操作
  63. if ($this->request->isPost()) {
  64. if (false == $model->allowField(true)->save($post)) {
  65. $this->error('添加失败');
  66. } else {
  67. Cache::clear();
  68. $this->success('添加成功,并清理缓存', 'admin/urlsconfig/index');
  69. }
  70. } else {
  71. return $this->fetch();
  72. }
  73. }
  74. }
  75. public function delete()
  76. {
  77. if ($this->request->isAjax()) {
  78. $id = $this->request->param('id', 0, 'intval');
  79. if (false == Db::name('urlconfig')->where('id', $id)->delete()) {
  80. $this->error('删除失败');
  81. } else {
  82. Cache::clear();
  83. $this->success('删除成功,并清理缓存', 'admin/urlsconfig/index');
  84. }
  85. }
  86. }
  87. public function status()
  88. {
  89. $id = $this->request->param('id', 0, 'intval');
  90. if ($id > 0) {
  91. if ($this->request->isPost()) {
  92. //是提交操作
  93. $post = $this->request->post();
  94. $status = $post['status'];
  95. if (false == Db::name('urlconfig')->where('id', $id)->update(['status' => $status])) {
  96. $this->error('设置失败');
  97. } else {
  98. Cache::clear();
  99. $this->success('设置成功,并清理缓存', 'admin/urlsconfig/index');
  100. }
  101. }
  102. } else {
  103. $this->error('id不正确');
  104. }
  105. }
  106. }