Live.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace app\store\controller;
  3. use library\Controller;
  4. use think\Db;
  5. /**
  6. * 商品管理
  7. * Class Live
  8. * @package app\store\controller
  9. */
  10. class Live extends Controller
  11. {
  12. /**
  13. * 绑定数据表
  14. * @var string
  15. */
  16. protected $table = 'StoreLive';
  17. /**
  18. * 直播列表
  19. * @auth true
  20. * @menu true
  21. * @throws \think\Exception
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. * @throws \think\exception\PDOException
  26. */
  27. public function index()
  28. {
  29. $this->title = '直播列表';
  30. $query = $this->_query($this->table)->where('is_deleted',0)->like('name');
  31. $query->dateBetween('create_at')->order('id desc')->page();
  32. }
  33. /**
  34. * 数据列表处理
  35. * @auth true
  36. * @menu true
  37. * @param array $data
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @throws \think\exception\DbException
  41. */
  42. protected function _index_page_filter(&$data)
  43. {
  44. foreach ($data as $k=>&$v){
  45. }
  46. }
  47. /**
  48. * 添加商品
  49. * @auth true
  50. * @menu true
  51. * @throws \think\Exception
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. * @throws \think\exception\DbException
  55. * @throws \think\exception\PDOException
  56. */
  57. public function add()
  58. {
  59. $this->title = '添加直播';
  60. $all_goods = Db::table('store_goods')
  61. ->field('id,name,cover')
  62. ->where(['is_integral'=>0,'is_deleted'=>0,'status'=>1])
  63. ->order('sort desc ,id desc')
  64. ->select();
  65. $this->all_goods = $all_goods;
  66. $this->_form($this->table, 'add');
  67. }
  68. /**
  69. * 编辑商品
  70. * @auth true
  71. * @menu true
  72. * @throws \think\Exception
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. * @throws \think\exception\PDOException
  77. */
  78. function edit()
  79. {
  80. $this->title = '编辑直播';
  81. $all_goods = Db::table('store_goods')
  82. ->field('id,name,cover')
  83. ->where(['is_integral'=>0,'is_deleted'=>0,'status'=>1])
  84. ->order('sort desc ,id desc')
  85. ->select();
  86. $this->all_goods = $all_goods;
  87. $this->_form($this->table, 'form');
  88. }
  89. /**
  90. * 表单数据处理
  91. * @auth true
  92. * @menu true
  93. * @param array $data
  94. */
  95. protected function _form_filter(&$data)
  96. {
  97. if($this->request->isGet()){
  98. if($this->request->action() == 'edit') {
  99. if($data['detail']) $data['detail'] = json_decode($data['detail'],true);
  100. }
  101. }
  102. // 添加或编辑商品
  103. if ($this->request->isPost()) {
  104. $detail_count = count($data['gl_st']);
  105. $live_goods = [];
  106. for ($i= 0;$i < $detail_count;$i++) {
  107. $live_goods[$i]['gl_st'] = $data['gl_st'][$i];
  108. $live_goods[$i]['gl_end'] = $data['gl_end'][$i];
  109. $live_goods[$i]['goods_id'] = $data['goods_id'][$i];
  110. }
  111. $data['detail'] = json_encode($live_goods);
  112. }
  113. }
  114. /**
  115. * 表单结果处理
  116. * @param boolean $result
  117. */
  118. protected function _form_result($result)
  119. {
  120. $live_info = Db::table('store_live')->find($result);
  121. $live_goods = json_decode($live_info['detail'],true);
  122. Db::table('store_live_goods')->where(['live_id'=>$result])->delete();
  123. $int_data = [];
  124. foreach ($live_goods as $v) {
  125. $int_data[]=[
  126. 'live_id' => $result ,
  127. 'goods_id' => $v['goods_id'] ,
  128. 'st_at' => $v['gl_st'] ,
  129. 'end_at' => $v['gl_end'] ,
  130. 'live_status' => $live_info['status'] ,
  131. 'create_at' => date('Y-m-d H:i:s') ,
  132. ];
  133. }
  134. Db::table('store_live_goods')->insertAll($int_data);
  135. }
  136. /**
  137. * @auth true
  138. * @menu true
  139. * 商品上架
  140. */
  141. public function up()
  142. {
  143. $this->_save($this->table, ['status' => '1']);
  144. }
  145. /**
  146. * @auth true
  147. * @menu true
  148. * 商品下架
  149. */
  150. public function down()
  151. {
  152. $this->_save($this->table, ['status' => '0']);
  153. }
  154. /**
  155. * @auth true
  156. * @menu true
  157. * 商品下架
  158. */
  159. public function del()
  160. {
  161. $this->_save($this->table, ['is_deleted' => '1']);
  162. }
  163. public function upload(){
  164. if(!in_array($_FILES["file"]["type"],['image/png','image/jpeg','image/jpeg'])) echo '图片类型不支持';
  165. if($_FILES["file"]["size"] > 50000000) echo '图片大小最大50M';
  166. if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  167. // $dir = env('root_path') . 'public/static/pic';
  168. $dir = $_SERVER['DOCUMENT_ROOT']. '/static/pic';
  169. /* var_dump($dir);
  170. if(!is_dir($dir)) {
  171. var_dump(mkdir($dir));
  172. }*/
  173. move_uploaded_file($_FILES["file"]["tmp_name"], $dir."/" . $_FILES["file"]["name"]);
  174. echo $dir."/" . $_FILES["file"]["name"];
  175. }
  176. }