Goods.php 6.9 KB

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