Live.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. $ch_goods = Db::table('store_live_goods')->where(['live_id'=>$result])->select();
  127. $ck_ids = array_column($ch_goods,'goods_id');
  128. $new_ids = array_column($live_goods,'goods_id');
  129. $int_data = [];
  130. foreach ($live_goods as $v) {
  131. // 需要新加
  132. if(!in_array($v['goods_id'],$ck_ids)) {
  133. $int_data[]=[
  134. 'live_id' => $result ,
  135. 'goods_id' => $v['goods_id'] ,
  136. 'st_at' => $v['gl_st'] ,
  137. 'end_at' => $v['gl_end'] ,
  138. 'live_status' => $live_info['status'] ,
  139. 'create_at' => date('Y-m-d H:i:s') ,
  140. ];
  141. }else if(in_array($v['goods_id'],$ck_ids)) {// 更新
  142. $up_data = [
  143. 'st_at' => $v['gl_st'] ,
  144. 'end_at' => $v['gl_end'] ,
  145. 'live_status' => $live_info['status'] ,
  146. 'create_at' => date('Y-m-d H:i:s') ,
  147. ];
  148. Db::table('store_live_goods')->where(['live_id'=>$result,'goods_id'=>$v['goods_id']])->update($up_data);
  149. }
  150. }
  151. if(!empty($int_data)) Db::table('store_live_goods')->insertAll($int_data);
  152. // 删掉多余的
  153. foreach ($ck_ids as $cv) {
  154. if(!in_array($cv,$new_ids)) Db::table('store_live_goods')->where(['live_id'=>$result,'goods_id'=>$cv])->delete();
  155. }*/
  156. }
  157. /**
  158. * @auth true
  159. * @menu true
  160. * 商品上架
  161. */
  162. public function up()
  163. {
  164. $this->_save($this->table, ['status' => '1']);
  165. }
  166. /**
  167. * @auth true
  168. * @menu true
  169. * 商品下架
  170. */
  171. public function down()
  172. {
  173. $this->_save($this->table, ['status' => '0']);
  174. }
  175. /**
  176. * @auth true
  177. * @menu true
  178. * 商品下架
  179. */
  180. public function del()
  181. {
  182. $this->_save($this->table, ['is_deleted' => '1']);
  183. }
  184. public function upload(){
  185. if(!in_array($_FILES["file"]["type"],['image/png','image/jpeg','image/jpeg'])) echo '图片类型不支持';
  186. if($_FILES["file"]["size"] > 50000000) echo '图片大小最大50M';
  187. if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  188. // $dir = env('root_path') . 'public/static/pic';
  189. $dir = $_SERVER['DOCUMENT_ROOT']. '/static/pic';
  190. /* var_dump($dir);
  191. if(!is_dir($dir)) {
  192. var_dump(mkdir($dir));
  193. }*/
  194. move_uploaded_file($_FILES["file"]["tmp_name"], $dir."/" . $_FILES["file"]["name"]);
  195. echo $dir."/" . $_FILES["file"]["name"];
  196. }
  197. }