IntegralGoods.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. array_map(function ($v)use (&$lev_data){
  95. $lev_data[$v['id']] = $v['name'];
  96. },$lev_set);
  97. $this->lev_set =$lev_data;
  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->all_temp = Db::table('freight_template')
  123. ->field('id,name')->where(['status'=>1,'is_deleted'=>0])
  124. ->order('sort desc ,id desc')->select();
  125. $this->_form($this->table, 'form');
  126. }
  127. /**
  128. * 表单数据处理
  129. * @auth true
  130. * @menu true
  131. * @param array $data
  132. */
  133. protected function _form_filter(&$data)
  134. {
  135. if($this->request->isGet()){
  136. if($this->request->action() == 'edit') {
  137. if($data['spec']) $data['spec'] = json_decode($data['spec'],true);
  138. }
  139. }
  140. // 添加或编辑商品
  141. if ($this->request->isPost()) {
  142. $spec_count = count($data['goods_no']);
  143. $goods_spec = [];
  144. $detail_list = [];
  145. if(isset($data['spec_id']) && $data['spec_id'] > 0) {
  146. $spec_info = Db::table('store_goods_specifica')->find($data['spec_id'] );
  147. $detail_list = $spec_info['detail'] ? json_decode($spec_info['detail']) : [];
  148. }
  149. $total_num = 0;
  150. for ($i= 0;$i < $spec_count;$i++) {
  151. $goods_spec[$i]['goods_no'] = $data['goods_no'][$i];
  152. $goods_spec[$i]['spec_img'] = $data['cover'];
  153. $goods_spec[$i]['store_num'] = $data['store_num'][$i] > 0 ?$data['store_num'][$i] :0 ;
  154. $goods_spec[$i]['price'] = $data['price'][$i] >0 ? $data['price'][$i] :0; ['price'][$i] >0 ? $data['price'][$i] :0 ;
  155. $goods_spec[$i]['integral'] = $data['integral'][$i] >0 ? $data['integral'][$i] :0; ['price'][$i] >0 ? $data['price'][$i] :0 ;
  156. $goods_spec[$i]['sell_money'] = 0;
  157. $goods_spec[$i]['seckill_money'] = 0;
  158. $goods_spec[$i]['spec_exp'] = '';
  159. $goods_spec[$i]['spec_key'] = '';
  160. $total_num +=$goods_spec[$i]['store_num'] ;
  161. }
  162. $data['spec'] =json_encode($goods_spec);
  163. $data['stock'] =$total_num; // 总库存
  164. $data['floor_price'] = min( array_column($goods_spec,'sell_money') );// 最低价
  165. $data['integral'] = $data['integral'][0];
  166. $data['is_integral'] = 1;
  167. }
  168. }
  169. /**
  170. * @auth true
  171. * @menu true
  172. * 商品上架
  173. */
  174. public function up()
  175. {
  176. $this->_save($this->table, ['status' => '1']);
  177. }
  178. /**
  179. * @auth true
  180. * @menu true
  181. * 商品下架
  182. */
  183. public function down()
  184. {
  185. $this->_save($this->table, ['status' => '0']);
  186. }
  187. /**
  188. * @auth true
  189. * @menu true
  190. * 商品下架
  191. */
  192. public function del()
  193. {
  194. $this->_save($this->table, ['is_deleted' => '1']);
  195. }
  196. public function upload(){
  197. if(!in_array($_FILES["file"]["type"],['image/png','image/jpeg','image/jpeg'])) echo '图片类型不支持';
  198. if($_FILES["file"]["size"] > 50000000) echo '图片大小最大50M';
  199. if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  200. // $dir = env('root_path') . 'public/static/pic';
  201. $dir = $_SERVER['DOCUMENT_ROOT']. '/static/pic';
  202. /* var_dump($dir);
  203. if(!is_dir($dir)) {
  204. var_dump(mkdir($dir));
  205. }*/
  206. move_uploaded_file($_FILES["file"]["tmp_name"], $dir."/" . $_FILES["file"]["name"]);
  207. echo $dir."/" . $_FILES["file"]["name"];
  208. }
  209. }