Goods.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace app\store\controller;
  3. use library\Controller;
  4. use think\Db;
  5. /**
  6. * 商品管理
  7. * Class Goods
  8. * @package app\store\controller
  9. */
  10. class Goods extends Controller
  11. {
  12. /**
  13. * 绑定数据表
  14. * @var string
  15. */
  16. protected $table = 'StoreGoods';
  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)->where(['is_integral'=>0])->like('name');
  31. $query->dateBetween('create_at')->order('sort desc , 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. reload_url();
  60. $this->title = '添加商品';
  61. // 分类
  62. $goods_cate = Db::table('store_goods_cate')
  63. ->field('id,title')
  64. ->where(['is_deleted'=>0,'status'=>1,'pid'=>0])
  65. ->order('sort desc ,id desc')
  66. ->select();
  67. array_walk($goods_cate,function (&$v){
  68. $v['children'] =Db::table('store_goods_cate')
  69. ->where(['is_deleted'=>0,'status'=>1,'pid'=>$v['id']])
  70. ->order('sort desc ,id desc')
  71. ->select();
  72. });
  73. $this->goods_cate = $goods_cate;
  74. // 规格
  75. $goods_spec = Db::table('store_goods_specifica')
  76. ->field('id,title,detail')
  77. ->where(['is_deleted'=>0])
  78. ->order('sort desc ,id desc')
  79. ->select();
  80. array_walk($goods_spec,function (&$val){
  81. // $val['detail'] = json_decode($val['detail']);
  82. });
  83. $this->all_temp = Db::table('freight_template')
  84. ->field('id,name')->where(['status'=>1,'is_deleted'=>0])
  85. ->order('sort desc ,id desc')->select();
  86. $this->goods_spec = $goods_spec;
  87. $this->_form($this->table, 'goods_no');
  88. }
  89. /**
  90. * 编辑商品
  91. * @auth true
  92. * @menu true
  93. * @throws \think\Exception
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. * @throws \think\exception\PDOException
  98. */
  99. function edit()
  100. {
  101. reload_url();
  102. $this->title = '编辑商品';
  103. // 分类
  104. $goods_cate = Db::table('store_goods_cate')
  105. ->field('id,title')
  106. ->where(['is_deleted'=>0,'status'=>1,'pid'=>0])
  107. ->order('sort desc ,id desc')
  108. ->select();
  109. array_walk($goods_cate,function (&$v){
  110. $v['children'] =Db::table('store_goods_cate')
  111. ->where(['is_deleted'=>0,'status'=>1,'pid'=>$v['id']])
  112. ->order('sort desc ,id desc')
  113. ->select();
  114. });
  115. $this->goods_cate = $goods_cate;
  116. // 规格
  117. $goods_spec = Db::table('store_goods_specifica')
  118. ->field('id,title,detail')
  119. ->where(['is_deleted'=>0])
  120. ->order('sort desc ,id desc')
  121. ->select();
  122. array_walk($goods_spec,function (&$val){
  123. // $val['detail'] = json_decode($val['detail']);
  124. });
  125. $this->all_temp = Db::table('freight_template')
  126. ->field('id,name')->where(['status'=>1,'is_deleted'=>0])
  127. ->order('sort desc ,id desc')->select();
  128. $goods_spec = array_column($goods_spec,null,'id');
  129. $this->goods_spec = $goods_spec;
  130. $this->_form($this->table, 'form');
  131. }
  132. /**
  133. * 表单数据处理
  134. * @auth true
  135. * @menu true
  136. * @param array $data
  137. */
  138. protected function _form_filter(&$data)
  139. {
  140. if($this->request->isGet()){
  141. if($this->request->action() == 'edit') {
  142. if($data['spec']) $data['spec'] = json_decode($data['spec'],true);
  143. }
  144. }
  145. // 添加或编辑商品
  146. if ($this->request->isPost()) {
  147. $spec_count = count($data['goods_no']);
  148. $goods_spec = [];
  149. $detail_list = [];
  150. if(isset($data['spec_id']) && $data['spec_id'] > 0) {
  151. $spec_info = Db::table('store_goods_specifica')->find($data['spec_id'] );
  152. $detail_list = $spec_info['detail'] ? json_decode($spec_info['detail']) : [];
  153. }
  154. $total_num = 0;
  155. for ($i= 0;$i < $spec_count;$i++) {
  156. $goods_spec[$i]['goods_no'] = $data['goods_no'][$i];
  157. $goods_spec[$i]['spec_img'] = $data['spec_img'][$i] ? $data['spec_img'][$i] :'' ;
  158. $goods_spec[$i]['store_num'] = $data['store_num'][$i] > 0 ?$data['store_num'][$i] :0 ;
  159. $goods_spec[$i]['price'] = $data['price'][$i] >0 ? $data['price'][$i] :0 ;
  160. $goods_spec[$i]['sell_money'] = $data['sell_money'][$i] >0 ? $data['sell_money'][$i] : 0;
  161. $goods_spec[$i]['seckill_money'] = $data['seckill_money'][$i] >0 ? $data['seckill_money'][$i] : 0;
  162. $goods_spec[$i]['spec_exp'] = $data['spec_exp'][$i] ? $data['spec_exp'][$i] : '';
  163. $goods_spec[$i]['spec_key'] = !empty($detail_list) ? array_search(trim($data['spec_exp'][$i]),$detail_list) : '';
  164. $total_num +=$goods_spec[$i]['store_num'] ;
  165. }
  166. $data['spec'] =json_encode($goods_spec);
  167. $data['stock'] =$total_num; // 总库存
  168. $data['floor_price'] = min( array_column($goods_spec,'sell_money') );// 最低价
  169. }
  170. }
  171. /**
  172. * @auth true
  173. * @menu true
  174. * 商品上架
  175. */
  176. public function up()
  177. {
  178. $this->_save($this->table, ['status' => '1']);
  179. }
  180. /**
  181. * @auth true
  182. * @menu true
  183. * 商品下架
  184. */
  185. public function down()
  186. {
  187. $this->_save($this->table, ['status' => '0']);
  188. }
  189. /**
  190. * @auth true
  191. * @menu true
  192. * 商品下架
  193. */
  194. public function del()
  195. {
  196. $this->_save($this->table, ['is_deleted' => '1']);
  197. }
  198. public function upload(){
  199. if(!in_array($_FILES["file"]["type"],['image/png','image/jpeg','image/jpeg'])) echo '图片类型不支持';
  200. if($_FILES["file"]["size"] > 50000000) echo '图片大小最大50M';
  201. if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  202. // $dir = env('root_path') . 'public/static/pic';
  203. $dir = $_SERVER['DOCUMENT_ROOT']. '/static/pic';
  204. /* var_dump($dir);
  205. if(!is_dir($dir)) {
  206. var_dump(mkdir($dir));
  207. }*/
  208. move_uploaded_file($_FILES["file"]["tmp_name"], $dir."/" . $_FILES["file"]["name"]);
  209. echo $dir."/" . $_FILES["file"]["name"];
  210. }
  211. }