IntegralGoods.php 8.5 KB

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