OneDatum.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\Nutrition\controller;
  3. use app\common\model\datum_idIntro;
  4. use app\common\model\datum_idUrl;
  5. use app\common\model\DatumIntro;
  6. use app\common\model\DatumUrl;
  7. use library\Controller;
  8. use library\tools\Data;
  9. use think\Db;
  10. use app\common\model\DatumCate;
  11. /**
  12. * 资料管理
  13. * Class OneDatum
  14. * @package app\Nutrition\controller
  15. */
  16. class OneDatum extends Controller
  17. {
  18. /**
  19. * 绑定数据表
  20. * @var string
  21. */
  22. protected $table = 'DatumIntro';
  23. /**
  24. * 列表
  25. * @auth true
  26. * @menu true
  27. * @throws \think\Exception
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. * @throws \think\exception\DbException
  31. * @throws \think\exception\PDOException
  32. */
  33. public function index()
  34. {
  35. $this->title = '资料列表';
  36. $datum_cate = DatumCate::field('id,title')->select()->toArray();
  37. $this->datum_cate = array_column($datum_cate,null,'id');
  38. $sel_where = [];
  39. $sel_where[] = ['is_deleted','=',0];
  40. $sel_where[] = ['type','=',1];
  41. if($title = $this->request->get('title')) $sel_where[] = ['title','like','%'.$title.'%'];
  42. $query = $this->_query($this->table);
  43. $query->where($sel_where)->order('status desc ,is_top desc ,sort desc,id desc')->page();
  44. }
  45. /**
  46. * 数据列表处理
  47. * @auth true
  48. * @menu true
  49. * @param array $data
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @throws \think\exception\DbException
  53. */
  54. protected function _index_page_filter(&$data)
  55. {
  56. foreach ($data as &$v)
  57. {
  58. $v['second_id'] = DatumUrl::where('datum_id',$v['id'])->where('is_deleted',0)->value('id');
  59. }
  60. }
  61. /**
  62. * 添加
  63. * @auth true
  64. * @menu true
  65. * @throws \think\Exception
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. * @throws \think\exception\PDOException
  70. */
  71. public function add()
  72. {
  73. $this->title = '添加资料';
  74. $this->type = input('get.type',1);
  75. $this->_form($this->table, 'form');
  76. }
  77. /**
  78. * 编辑
  79. * @auth true
  80. * @menu true
  81. * @throws \think\Exception
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. * @throws \think\exception\PDOException
  86. */
  87. public function edit()
  88. {
  89. $this->title = '编辑资料';
  90. $this->type = input('get.type',1);
  91. $this->_form($this->table, 'form') ;
  92. }
  93. /**
  94. * 禁用
  95. * @auth true
  96. * @menu true
  97. * @throws \think\Exception
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. * @throws \think\exception\DbException
  101. * @throws \think\exception\PDOException
  102. */
  103. public function forbidden()
  104. {
  105. $this->_save($this->table, ['status' => '0']);
  106. }
  107. /**
  108. * 启用
  109. * @auth true
  110. * @menu true
  111. * @throws \think\Exception
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. * @throws \think\exception\DbException
  115. * @throws \think\exception\PDOException
  116. */
  117. public function enable()
  118. {
  119. $this->_save($this->table, ['status' => 1]);
  120. }
  121. /**
  122. * 删除资料
  123. * @auth true
  124. * @menu true
  125. * @throws \think\Exception
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. * @throws \think\exception\DbException
  129. * @throws \think\exception\PDOException
  130. */
  131. public function del()
  132. {
  133. $this->_save($this->table, ['is_deleted' => 1]);
  134. }
  135. /**
  136. * 表单数据处理
  137. * @auth true
  138. * @menu true
  139. * @param array $data
  140. */
  141. protected function _form_filter(&$data)
  142. {
  143. if($this->request->isGet()){
  144. $this->r = input('get.r',0);
  145. }
  146. if($this->request->isPost()) {
  147. }
  148. }
  149. protected function _form_result($result){
  150. if($this->request->isPost() && in_array($this->request->action(),['add','edit'])) {
  151. $datum_info = DatumIntro::where('id',$result)->find()->toArray();
  152. $datum_url = [
  153. 'datum_id' => $result,
  154. 'url' => $datum_info['datum_url'],
  155. 'sort' => $datum_info['sort'],
  156. 'is_deleted' => 0,
  157. 'is_vip' => $datum_info['is_vip'],
  158. 'source' => 1,
  159. 'title' => $datum_info['title'],
  160. ];
  161. Data::save('DatumUrl',$datum_url,'datum_id',['datum_id' => $result]);
  162. }
  163. }
  164. }