IntegralGoods.php 9.0 KB

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