Live.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if($data['end_at'] < date('Y-m-d H:i:s')) $data['status'] = 3;
  113. if($data['end_at'] > date('Y-m-d H:i:s') && $data['start_at'] < date('Y-m-d H:i:s') ) $data['status'] = 1;
  114. if($data['start_at'] > date('Y-m-d H:i:s')) $data['status'] = 2;
  115. }
  116. }
  117. /**
  118. * 表单结果处理
  119. * @param boolean $result
  120. */
  121. protected function _form_result($result)
  122. {
  123. $live_info = Db::table('store_live')->find($result);
  124. $live_goods = json_decode($live_info['detail'],true);
  125. Db::table('store_live_goods')->where(['live_id'=>$result])->delete();
  126. $int_data = [];
  127. foreach ($live_goods as $v) {
  128. $int_data[]=[
  129. 'live_id' => $result ,
  130. 'goods_id' => $v['goods_id'] ,
  131. 'st_at' => $v['gl_st'] ,
  132. 'end_at' => $v['gl_end'] ,
  133. 'live_status' => $live_info['status'] ,
  134. 'create_at' => date('Y-m-d H:i:s') ,
  135. ];
  136. }
  137. Db::table('store_live_goods')->insertAll($int_data);
  138. }
  139. /**
  140. * @auth true
  141. * @menu true
  142. * 商品上架
  143. */
  144. public function up()
  145. {
  146. $this->_save($this->table, ['status' => '1']);
  147. }
  148. /**
  149. * @auth true
  150. * @menu true
  151. * 商品下架
  152. */
  153. public function down()
  154. {
  155. $this->_save($this->table, ['status' => '0']);
  156. }
  157. /**
  158. * @auth true
  159. * @menu true
  160. * 商品下架
  161. */
  162. public function del()
  163. {
  164. $this->_save($this->table, ['is_deleted' => '1']);
  165. }
  166. public function upload(){
  167. if(!in_array($_FILES["file"]["type"],['image/png','image/jpeg','image/jpeg'])) echo '图片类型不支持';
  168. if($_FILES["file"]["size"] > 50000000) echo '图片大小最大50M';
  169. if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  170. // $dir = env('root_path') . 'public/static/pic';
  171. $dir = $_SERVER['DOCUMENT_ROOT']. '/static/pic';
  172. /* var_dump($dir);
  173. if(!is_dir($dir)) {
  174. var_dump(mkdir($dir));
  175. }*/
  176. move_uploaded_file($_FILES["file"]["tmp_name"], $dir."/" . $_FILES["file"]["name"]);
  177. echo $dir."/" . $_FILES["file"]["name"];
  178. }
  179. }