Article.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\common\controller\AdminController;
  4. use app\common\model\ArticleTypeModel;
  5. use app\common\model\CouponPlanModel;
  6. use app\common\model\SystemArticleModel;
  7. use app\common\model\SystemBannerModel;
  8. use app\common\model\UserModel;
  9. use think\App;
  10. use think\facade\Db;
  11. use think\Request;
  12. /**
  13. * @ControllerAnnotation(title="系统公告/文章配置")
  14. */
  15. class Article extends AdminController
  16. {
  17. use \app\admin\traits\Curd;
  18. public function __construct (App $app)
  19. {
  20. parent::__construct($app);
  21. $this->model = new SystemArticleModel();
  22. }
  23. public function index ()
  24. {
  25. if ($this->request->isAjax()) {
  26. if (input('selectFields')) {
  27. return $this->selectList();
  28. }
  29. list($page, $limit, $where) = $this->buildTableParames();
  30. $count = $this->model
  31. ->where($where)
  32. ->count();
  33. // return 11;
  34. // return json_decode('[{"price":10, "fee":1},{"price":50, "fee":0.5},{"price":100, "fee":0}]');
  35. $list = $this->model
  36. ->where($where)
  37. ->withAttr('type', function ($val, $data){
  38. return config('type.article')[$val]?config('type.article')[$val]:'未知类型';
  39. })
  40. ->page($page, $limit)
  41. ->order($this->sort)
  42. ->select();
  43. $data = [
  44. 'code' => 0,
  45. 'msg' => '',
  46. 'count' => $count,
  47. 'data' => $list,
  48. ];
  49. return json($data);
  50. }
  51. return $this->fetch();
  52. }
  53. /**
  54. * @NodeAnotation(title="修改")
  55. */
  56. public function edit ($id)
  57. {
  58. $row = $this->model->find($id);
  59. empty($row) && $this->error('数据不存在');
  60. if ($this->request->isAjax()) {
  61. $post = $this->request->post();
  62. $rule = [];
  63. $this->validate($post, $rule);
  64. try {
  65. $post['banner_desc'] = htmlspecialchars_decode($post['banner_desc']);
  66. // $post['img_pic'] = __HTTPSAVEIMGADMIN($post['img_pic']);
  67. $save = $row->save($post);
  68. } catch (\Exception $e) {
  69. $this->error('保存失败');
  70. }
  71. $save ? $this->success('保存成功') : $this->error('保存失败');
  72. }
  73. $row['img_pic'] = __HTTPGETIMGADMIN($row['img_pic']);
  74. $this->assign('row', $row);
  75. return $this->fetch();
  76. }
  77. /**
  78. * @NodeAnotation(title="添加")
  79. */
  80. public function add()
  81. {
  82. if ($this->request->isAjax()){
  83. $row = request()->post();
  84. // $row['img_pic'] = __HTTPSAVEIMGADMIN($row['img_pic']);
  85. $row['banner_desc'] = htmlspecialchars_decode($row['banner_desc']);
  86. $row['create_time'] = sr_getcurtime(time());
  87. unset($row['file']);
  88. $type_info = Db::name('article_type')->where('id', $row['type'])->find();
  89. if ($type_info){
  90. $count = $this->model->where('type', $row['type'])->count();
  91. if ($count >= $type_info['max_count']){
  92. $this->error('这种类型只能存在'.$type_info['max_count'].'种');
  93. }
  94. }
  95. $save = $this->model->insert($row);
  96. $save?$this->success('添加成功'):$this->error('添加失败');
  97. }
  98. return $this->fetch();
  99. }
  100. public function articleType()
  101. {
  102. $model = new ArticleTypeModel();
  103. $list = $model
  104. ->where('status', 1)
  105. ->field('id,name')
  106. ->select()
  107. ->toArray();
  108. $this->success('成功', $list);
  109. }
  110. }