UserArticle.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace app\Nutrition\controller;
  3. use app\common\model\ArticleCate;
  4. use app\common\model\ArticleIntro;
  5. use app\common\model\User;
  6. use app\common\model\UserMessage;
  7. use app\common\model\VideoIntro;
  8. use app\common\model\VideoUrl;
  9. use library\Controller;
  10. use library\tools\Data;
  11. use think\Db;
  12. use app\common\model\VideoCate as VCM;
  13. /**
  14. * 图文管理
  15. * Class UserArticle
  16. * @package app\Nutrition\controller
  17. */
  18. class UserArticle extends Controller
  19. {
  20. /**
  21. * 绑定数据表
  22. * @var string
  23. */
  24. protected $table = 'UserArticle';
  25. /**
  26. * 列表
  27. * @auth true
  28. * @menu true
  29. * @throws \think\Exception
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. * @throws \think\exception\DbException
  33. * @throws \think\exception\PDOException
  34. */
  35. public function index()
  36. {
  37. $this->title = '图文列表';
  38. $video_cate = VCM::field('id,title')->select()->toArray();
  39. $this->video_cate = array_column($video_cate,null,'id');
  40. $sel_where = [];
  41. $sel_where[] = ['v.is_deleted','=',0];
  42. if($title = $this->request->get('title')) $sel_where[] = ['v.title','like','%'.$title.'%'];
  43. if($phone = $this->request->get('phone')) $sel_where[] = ['m.phone','like','%'.$phone.'%'];
  44. $this->status = $this->request->get('status',-1);
  45. if( $this->status >= 0 ) $sel_where[] = ['v.status','=', $this->status ];
  46. $query = $this->_query($this->table)->field('v.*,m.phone,m.name user_name, m.headimg')->alias('v')
  47. ->leftJoin('StoreMember m','m.id = v.user_id');
  48. $query->where($sel_where)->order('v.status desc ,v.id desc')->page();
  49. }
  50. /**
  51. * 数据列表处理
  52. * @auth true
  53. * @menu true
  54. * @param array $data
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. * @throws \think\exception\DbException
  58. */
  59. protected function _index_page_filter(&$data)
  60. {
  61. }
  62. /**
  63. * 编辑
  64. * @auth true
  65. * @menu true
  66. * @throws \think\Exception
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @throws \think\exception\DbException
  70. * @throws \think\exception\PDOException
  71. */
  72. public function edit()
  73. {
  74. $this->title = '编辑图文';
  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 forbidden()
  88. {
  89. $this->_save($this->table, ['status' => '0']);
  90. }
  91. /**
  92. * 启用
  93. * @auth true
  94. * @menu true
  95. * @throws \think\Exception
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @throws \think\exception\DbException
  99. * @throws \think\exception\PDOException
  100. */
  101. public function enable()
  102. {
  103. $this->_save($this->table, ['status' => 1]);
  104. }
  105. /**
  106. * 删除图文
  107. * @auth true
  108. * @menu true
  109. * @throws \think\Exception
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. * @throws \think\exception\PDOException
  114. */
  115. public function del()
  116. {
  117. $this->_save($this->table, ['is_deleted' => 1]);
  118. }
  119. /**
  120. * 表单数据处理
  121. * @auth true
  122. * @menu true
  123. * @param array $data
  124. */
  125. protected function _form_filter(&$data)
  126. {
  127. if($this->request->isGet() && in_array($this->request->action(),['add','edit'])){
  128. $all_cate = VCM::where(['is_deleted'=>0])->order('sort desc ,id desc')->select();
  129. $this->cate_tree = make_tree($all_cate);
  130. }
  131. if($this->request->isPost() && in_array($this->request->action(),['add','edit'])) {
  132. if($data['status'] > 0 ) UserMessage::sendUserMessage($data['user_id'],'article',2,$data['status']-1,0,$data['id']);
  133. }
  134. }
  135. protected function _form_result($result){
  136. }
  137. /**
  138. * 图文添加到系列
  139. * @auth true
  140. * @throws \think\Exception
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. * @throws \think\exception\DbException
  144. * @throws \think\exception\PDOException
  145. */
  146. public function series()
  147. {
  148. if ($this->request->isGet()) {
  149. $id = $this->request->get('id');
  150. $has_series = \app\common\model\ArticleItem::where('rel_id',$id)->column('article_id');
  151. $user_article = \app\common\model\UserArticle::where(['id' => $id])->find()->toArray();
  152. $series_list = ArticleIntro::where(['is_deleted'=>0,'type'=>2])->where('id','not in',$has_series)->column('title','id');
  153. $this->fetch('', ['vo' => $user_article,'series_list'=>$series_list]);
  154. } else {
  155. $id = input('post.id');
  156. $series_id = input('post.series_id');
  157. if(!$series_id) $this->error('请选择系列');
  158. $is_vip = input('post.is_vip');
  159. $user_article = \app\common\model\UserArticle::where(['id' => $id])->find()->toArray();
  160. $user_phone = User::where('id',$user_article['user_id'])->value('phone');
  161. \app\common\model\ArticleItem::create([
  162. 'article_id'=>$series_id,
  163. 'cover'=>explode('|',$user_article['images'])[0],
  164. 'images'=>$user_article['images'],
  165. 'sort'=>0,
  166. 'is_vip'=>$is_vip,
  167. 'source'=>2,
  168. 'rel_id'=>$id,
  169. 'content'=>$user_article['desc'],
  170. 'title'=>$user_article['title'],
  171. 'user_id' => $user_article['user_id'],
  172. 'phone' => $user_phone,
  173. 'release_time' => date("Y-m-d H:i:s"),
  174. ]);
  175. ArticleIntro::where('id',$series_id)->setInc('item_num');
  176. $this->success('添加成功!');
  177. }
  178. }
  179. public function new_article(){
  180. if($this->request->isGet())
  181. {
  182. $user_article = \app\common\model\UserArticle::where('id',input('id'))->find()->toArray();
  183. $all_cate = ArticleCate::where(['is_deleted'=>0])->order('sort desc ,id desc')->select();
  184. $cate_tree = make_tree($all_cate);
  185. $this->cate_tree = $cate_tree;
  186. $this->fetch('',['vo'=>$user_article,'cate_tree '=>$cate_tree]);
  187. }else{
  188. $user_article_id= input('user_article_id');
  189. $user_article = \app\common\model\UserArticle::where('id',$user_article_id)->find()->toArray();
  190. $user_phone = User::where('id',$user_article['user_id'])->value('phone');
  191. $new_article = [
  192. 'title' => $user_article['title'],
  193. 'content' => $user_article['desc'],
  194. 'cover' => explode('|',$user_article['images'])[0],
  195. 'images' => $user_article['images'],
  196. 'first_classify' => input('first_classify'),
  197. 'second_classify' => input('second_classify'),
  198. 'type' => 1,
  199. 'is_over' => 1,
  200. 'label' => $user_article['label'],
  201. 'is_vip' => input('is_vip'),
  202. ];
  203. $result = ArticleIntro::create($new_article);
  204. $article_item = [
  205. 'article_id' => $result->id,
  206. 'cover' => $new_article['cover'],
  207. 'images' => $new_article['images'],
  208. 'content' => $new_article['content'],
  209. 'is_vip' => $new_article['is_vip'],
  210. 'source' => 2,
  211. 'title' => $new_article['title'],
  212. 'rel_id' => $user_article_id,
  213. 'user_id' => $user_article['user_id'],
  214. 'phone' => $user_phone,
  215. 'release_time' => date("Y-m-d H:i:s"),
  216. ];
  217. Data::save('ArticleItem',$article_item,'article_id',['article_id' => $result->id]);
  218. $this->success('创建成功');
  219. }
  220. }
  221. }