Live.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace app\store\controller;
  3. use library\Controller;
  4. use library\helper\PageHelper;
  5. use think\Db;
  6. /**
  7. * 直播管理
  8. * Class Live
  9. * @package app\store\controller
  10. */
  11. class Live extends Controller
  12. {
  13. /**
  14. * 绑定数据表
  15. * @var string
  16. */
  17. protected $table = 'StoreLive';
  18. public function initialize(){
  19. $date_str = date("Y-m-d H:i:s");
  20. // 将活动改为进行中状态
  21. Db::table('store_live')->where([['start_at','< time',$date_str],['status','=',2]])->update(['status'=>1]);
  22. }
  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. $query = $this->_query($this->table)->where('is_deleted',0)->like('name');
  37. $query->dateBetween('create_at')->order('id desc')->page();
  38. }
  39. /**
  40. * 数据列表处理
  41. * @auth true
  42. * @menu true
  43. * @param array $data
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @throws \think\exception\DbException
  47. */
  48. protected function _index_page_filter(&$data)
  49. {
  50. }
  51. /**
  52. * 添加直播
  53. * @auth true
  54. * @menu true
  55. * @throws \think\Exception
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. * @throws \think\exception\PDOException
  60. */
  61. public function add()
  62. {
  63. $this->title = '添加直播';
  64. $this->_form($this->table, 'form');
  65. }
  66. /**
  67. * 编辑直播
  68. * @auth true
  69. * @menu true
  70. * @throws \think\Exception
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @throws \think\exception\DbException
  74. * @throws \think\exception\PDOException
  75. */
  76. function edit()
  77. {
  78. $this->title = '编辑直播';
  79. $this->_form($this->table, 'form');
  80. }
  81. /**
  82. * 表单数据处理
  83. * @auth true
  84. * @menu true
  85. * @param array $data
  86. */
  87. protected function _form_filter(&$data)
  88. {
  89. if($this->request->isGet()){
  90. if($this->request->action() == 'edit') {
  91. if($data['detail']) $data['detail'] = json_decode($data['detail'],true);
  92. }
  93. }
  94. // 添加或编辑直播
  95. if ($this->request->isPost()) {
  96. }
  97. }
  98. /**
  99. * 表单结果处理
  100. * @param boolean $result
  101. */
  102. protected function _form_result($result)
  103. {
  104. }
  105. /**
  106. * @auth true
  107. * @menu true
  108. * 直播上架
  109. */
  110. public function up()
  111. {
  112. $this->_save($this->table, ['status' => '1']);
  113. }
  114. /**
  115. * @auth true
  116. * @menu true
  117. * 直播下架
  118. */
  119. public function down()
  120. {
  121. $this->_save($this->table, ['status' => '0']);
  122. }
  123. /**
  124. * @auth true
  125. * @menu true
  126. * 直播下架
  127. */
  128. public function del()
  129. {
  130. $this->_save($this->table, ['is_deleted' => '1']);
  131. }
  132. /**
  133. * @auth true
  134. * @menu true
  135. * 删除直播商品
  136. */
  137. public function del_goods()
  138. {
  139. $id = input('id');
  140. Db::table('store_live_goods')->where('id',$id)->delete();
  141. return json_encode(['info'=>'删除成功']);
  142. }
  143. /**
  144. * @auth true
  145. * @menu true
  146. * 直播下架
  147. */
  148. public function close()
  149. {
  150. $this->_save($this->table, ['status' => '3','end_at'=>date('Y-m-d H:i:s')]);
  151. }
  152. public function upload(){
  153. if(!in_array($_FILES["file"]["type"],['image/png','image/jpeg','image/jpeg'])) echo '图片类型不支持';
  154. if($_FILES["file"]["size"] > 50000000) echo '图片大小最大50M';
  155. if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  156. // $dir = env('root_path') . 'public/static/pic';
  157. $dir = $_SERVER['DOCUMENT_ROOT']. '/static/pic';
  158. /* var_dump($dir);
  159. if(!is_dir($dir)) {
  160. var_dump(mkdir($dir));
  161. }*/
  162. move_uploaded_file($_FILES["file"]["tmp_name"], $dir."/" . $_FILES["file"]["name"]);
  163. echo $dir."/" . $_FILES["file"]["name"];
  164. }
  165. /**
  166. * @auth true
  167. * @menu true
  168. * 直播商品
  169. */
  170. public function live_goods()
  171. {
  172. $lid = input('id');
  173. $cate_id = input('cate_id',0);
  174. $search_name = input('search_name','');
  175. $live_info = Db::table('store_live')->find($lid);
  176. $this->title = $live_info['name'];
  177. $goods_cate = Db::table('store_live_cate')
  178. ->field('id,title')
  179. ->where('status',1)
  180. ->where('is_deleted',0)
  181. ->order('sort desc , id desc')
  182. ->select();
  183. $this->goods_cate = array_column($goods_cate,null,'id');
  184. $this->lid = $lid;
  185. $where = [];
  186. $where[]= ['l.live_id','=',$lid];
  187. if($search_name) $where[] = ['g.name','like',"%".$search_name."%"];
  188. if($cate_id) $where[] = ['l.cate_id','=',$cate_id];
  189. $this->assign('cate_id',$cate_id);
  190. $list = $this->_query('StoreLiveGoods')
  191. ->alias('l')
  192. ->field('l.*,name as goods_name,cover,title')
  193. ->where($where)
  194. ->join('live_goods_list g','l.goods_id = g.id','LEFT')
  195. ->join('store_live_cate c','c.id = l.cate_id','LEFT')
  196. ->order('l.sort desc ,l.id desc')->page();
  197. }
  198. /**
  199. * @auth true
  200. * @menu true
  201. * 已添加直播商品列表
  202. */
  203. public function add_goods()
  204. {
  205. $lid = input('id');
  206. $cate_id = input('cate_id',0);
  207. $live_info = Db::table('store_live')->find($lid);
  208. $this->title = $live_info['name'];
  209. $goods_cate = Db::table('store_live_cate')
  210. ->field('id,title')
  211. ->where('status',1)
  212. ->where('is_deleted',0)
  213. ->order('sort desc , id desc')
  214. ->select();
  215. $this->goods_cate = array_column($goods_cate,null,'id');
  216. $this->lid = $lid;
  217. $sel_goods = Db::table('store_live_goods')->field('goods_id')->where(['live_id'=>$lid])->select();
  218. $sel_ids = array_column($sel_goods,'goods_id');
  219. $where = [];
  220. $where[]= ['is_deleted','=',0];
  221. $where[]= ['status','=',1];
  222. if(!empty($sel_ids)) $where[] = ['id','not in',$sel_ids];
  223. if($cate_id) $where[] = ['cate_id','=',$cate_id];
  224. $this->assign('cate_id',$cate_id);
  225. $list = $this->_query('LiveGoodsList')
  226. ->where($where)
  227. ->order('sort desc ,id desc')->page();
  228. return $this->fetch('add_goods');
  229. }
  230. /**
  231. * @auth true
  232. * @menu true
  233. * 添加直播商品
  234. */
  235. public function ajax_add()
  236. {
  237. $sel_goods = input('post.ids');
  238. $live_id = input('post.live_id');
  239. $good_arr = Db::table('live_goods_list')
  240. ->field('id,cate_id,sort')
  241. ->where(['id'=>$sel_goods])
  242. ->select();
  243. $cal_data = array_column($good_arr,null,'id');
  244. $int_data = [];
  245. foreach ($sel_goods as $goods_id){
  246. $int_data[]=[
  247. 'live_id' => $live_id,
  248. 'goods_id' => $goods_id,
  249. 'cate_id' => $cal_data[$goods_id]['cate_id'],
  250. 'sort' => $cal_data[$goods_id]['sort'],
  251. 'create_at' => date("Y-m-d H:i:s")
  252. ];
  253. }
  254. Db::table('store_live_goods')->insertAll($int_data);
  255. return json_encode(['code'=>200]);
  256. }
  257. }