Goods.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace app\admin\controller\life;
  3. use app\admin\controller\BaseController;
  4. use app\admin\server\life\GoodsServer;
  5. use PhpOffice\PhpSpreadsheet\IOFactory;
  6. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  7. use app\admin\model\LifeGoods;
  8. class Goods extends BaseController
  9. {
  10. /**
  11. * Notes:获取健康超市商品列表
  12. * @return \support\Response
  13. * User: ZQ
  14. * Date: 2022/10/09
  15. */
  16. public function getGoodsList()
  17. {
  18. [$page, $limit] = $this->getPage();
  19. $keywords = $this->request->get('keywords');
  20. $result = GoodsServer::getGoodsList($page, $limit, $keywords);
  21. return json_success($result, '成功');
  22. }
  23. /**
  24. * Notes:获取所有角色
  25. * @return \support\Response
  26. * User: ZQ
  27. * Date: 2022/9/19
  28. */
  29. public function getGoodsAll()
  30. {
  31. $result = GoodsServer::getGoodsAll();
  32. return json_success($result, '成功');
  33. }
  34. /**
  35. * Notes:修改健康超市商品
  36. * @return \support\Response
  37. * User: ZQ
  38. * Date: 2022/10/09
  39. */
  40. public function updateGoods()
  41. {
  42. $goods_id = $this->request->post('goods_id');
  43. $goods_name = $this->request->post('goods_name','');
  44. $goods_img = $this->request->post('goods_img','');
  45. $goods_category_id = $this->request->post('goods_category_id','');
  46. $goods_shop_id = $this->request->post('goods_shop_id');
  47. $goods_is_new = $this->request->post('goods_is_new');
  48. $goods_price = $this->request->post('goods_price');
  49. $goods_oldprice = $this->request->post('goods_oldprice',1);
  50. $goods_content = $this->request->post('goods_content');
  51. $goods_sell_num = $this->request->post('goods_sell_num');
  52. $goods_postage_id = $this->request->post('goods_postage_id');
  53. $goods_status = $this->request->post('goods_status');
  54. $goods_weight = $this->request->post('goods_weight');
  55. $goods_sort = $this->request->post('goods_sort');
  56. $goods_slide = $this->request->post('goods_slide','');
  57. $goods_pack_price = $this->request->post('goods_pack_price','');
  58. $food_attr = $this->request->post('goods_attr','');
  59. $food_varieties = $this->request->post('goods_varieties','');
  60. $food_packaging = $this->request->post('goods_packaging','');
  61. $food_storage = $this->request->post('goods_storage','');
  62. $goods_delivery_type = $this->request->post('goods_delivery_type','');
  63. $goods_inventory = $this->request->post('goods_inventory',0);
  64. $admin_id = $this->request->admin_id;
  65. $this->validateCheck('life\GoodsValidate', ['goods_id' => $goods_id, 'goods_name' => $goods_name, 'goods_img' => $goods_img, 'goods_category_id' => $goods_category_id, 'goods_shop_id' => $goods_shop_id, 'goods_is_new' => $goods_is_new, 'goods_price' => $goods_price, 'goods_content' => $goods_content, 'goods_status' => $goods_status, 'goods_postage_id' => $goods_postage_id], 'update');
  66. $result = GoodsServer::updateGoods($goods_id,$goods_name,$goods_img,$goods_category_id,$goods_shop_id,$goods_is_new,$goods_price,$goods_oldprice,$goods_content,$goods_sell_num,$goods_postage_id,$goods_status,$goods_weight,$goods_sort,$goods_slide,$goods_pack_price,$food_attr,$food_varieties,$food_packaging,$food_storage,$goods_delivery_type,$goods_inventory,$admin_id);
  67. return json_success($result, '修改成功');
  68. }
  69. /**
  70. * Notes:删除商品
  71. * @return \support\Response
  72. * User: ZQ
  73. * Date: 2022/9/13
  74. */
  75. public function delGoods()
  76. {
  77. $goods_id = $this->request->get('goods_id');
  78. $admin_id = $this->request->admin_id;
  79. $this->validateCheck('life\GoodsValidate', ['goods_id' => $goods_id], 'info');
  80. $result = GoodsServer::delGoods($goods_id,$admin_id);
  81. if ($result){
  82. return json_success($result, '删除成功');
  83. }else{
  84. throw new \Exception('删除失败!');
  85. }
  86. }
  87. /**
  88. * Notes:添加商品
  89. * @return \support\Response
  90. * User: ZQ
  91. * Date: 2022/9/13
  92. */
  93. public function addGoods()
  94. {
  95. $goods_name = $this->request->post('goods_name','');
  96. $goods_img = $this->request->post('goods_img');
  97. $goods_category_id = $this->request->post('goods_category_id');
  98. $goods_shop_id = $this->request->post('goods_shop_id');
  99. $goods_is_new = $this->request->post('goods_is_new');
  100. $goods_price = $this->request->post('goods_price');
  101. $goods_oldprice = $this->request->post('goods_oldprice',1);
  102. $goods_content = $this->request->post('goods_content');
  103. $goods_sell_num = $this->request->post('goods_sell_num');
  104. $goods_postage_id = $this->request->post('goods_postage_id');
  105. $goods_status = $this->request->post('goods_status');
  106. $goods_weight = $this->request->post('goods_weight');
  107. $goods_sort = $this->request->post('goods_sort');
  108. $goods_slide = $this->request->post('goods_slide','');
  109. $goods_pack_price = $this->request->post('goods_pack_price','');
  110. $food_attr = $this->request->post('goods_attr','');
  111. $food_varieties = $this->request->post('goods_varieties','');
  112. $food_packaging = $this->request->post('goods_packaging','');
  113. $food_storage = $this->request->post('goods_storage','');
  114. $goods_delivery_type = $this->request->post('goods_delivery_type','');
  115. $goods_inventory = $this->request->post('goods_inventory',0);
  116. $admin_id = $this->request->admin_id;
  117. $this->validateCheck('life\GoodsValidate', ['goods_name' => $goods_name, 'goods_img' => $goods_img, 'goods_category_id' => $goods_category_id, 'goods_shop_id' => $goods_shop_id, 'goods_is_new' => $goods_is_new, 'goods_price' => $goods_price, 'goods_content' => $goods_content, 'goods_status' => $goods_status, 'goods_postage_id' => $goods_postage_id], 'create');
  118. $result = GoodsServer::insertGoods($goods_name,$goods_img,$goods_category_id,$goods_shop_id,$goods_is_new,$goods_price,$goods_oldprice,$goods_content,$goods_sell_num,$goods_postage_id,$goods_status,$goods_weight,$goods_sort,$goods_slide,$goods_pack_price,$food_attr,$food_varieties,$food_packaging,$food_storage,$goods_delivery_type,$goods_inventory,$admin_id);
  119. return json_success($result, '添加成功');
  120. }
  121. /**
  122. * Notes:查询商品详情
  123. * @return \support\Response
  124. * User: ZQ
  125. * Date: 2022/9/13
  126. */
  127. public function goodsInfo()
  128. {
  129. $goods_id = $this->request->get('goods_id');
  130. $this->validateCheck('life\GoodsValidate', ['goods_id' => $goods_id], 'info');
  131. $result = GoodsServer::goodsInfo($goods_id);
  132. return json_success($result, '成功');
  133. }
  134. /**
  135. * Notes:商品上架下架
  136. * @return \support\Response
  137. * User: ZQ
  138. * Date: 2022/10/09
  139. */
  140. public function updateStatus()
  141. {
  142. $goods_id = $this->request->get('goods_id');
  143. $goods_status = $this->request->get('goods_status');
  144. $this->validateCheck('life\GoodsValidate', ['goods_id' => $goods_id], 'info');
  145. $result = GoodsServer::updateStatus($goods_id, $goods_status);
  146. return json_success($result, '修改成功');
  147. }
  148. /**
  149. * Notes:批量导入商品
  150. * @return \support\Response
  151. * User: Ycp
  152. * Date: 2023/8/23
  153. */
  154. public function goodsImport()
  155. {
  156. $imageFilePath = public_path().'/uploads/imgs/'; //图片本地存储的路径
  157. if (!file_exists($imageFilePath)) { //如果目录不存在则递归创建
  158. mkdir($imageFilePath, 0777, true);
  159. }
  160. try {
  161. $file = $this->request->get('excel_address');//文件地址;
  162. $objRead = IOFactory::createReader('Xlsx');
  163. $objSpreadsheet = $objRead->load($file);
  164. $objWorksheet = $objSpreadsheet->getSheet(0);
  165. $data = $objWorksheet->toArray();
  166. $img_data = [];// 图片数组
  167. foreach ($objWorksheet->getDrawingCollection() as $drawing) {
  168. list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
  169. $imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999);
  170. switch ($drawing->getExtension()) {
  171. case 'jpg':
  172. case 'jpeg':
  173. $imageFileName .= '.jpg';
  174. $source = imagecreatefromjpeg($drawing->getPath());
  175. imagejpeg($source, $imageFilePath . $imageFileName);
  176. break;
  177. case 'gif':
  178. $imageFileName .= '.gif';
  179. $source = imagecreatefromgif($drawing->getPath());
  180. imagegif($source, $imageFilePath . $imageFileName);
  181. break;
  182. case 'png':
  183. $imageFileName .= '.png';
  184. $source = imagecreatefrompng($drawing->getPath());
  185. imagepng($source, $imageFilePath . $imageFileName);
  186. break;
  187. }
  188. $img_data[$startRow-1][$startColumn] = '/uploads/imgs/' . $imageFileName;
  189. }
  190. // 合并表格数据
  191. if(!empty($img_data)){
  192. foreach ($img_data as $key => $value){
  193. //$data[$key][2] 是导入表格图片的哪一行
  194. $data[$key][2] = $value['C'];
  195. }
  196. }
  197. unset($data[0]);
  198. $config = config('plugin.tinywan.storage.app.storage');
  199. foreach ($data as &$value) {
  200. $params['goods_name'] = $value[1];
  201. $params['goods_img'] = $config[$config['default']]['domain'].$value[2];
  202. $params['goods_slide'] = $config[$config['default']]['domain'].$value[2];
  203. if($value[16] == "健康检测"){
  204. $params['goods_category_id'] = 252;
  205. }elseif($value[16] == "运动康复"){
  206. $params['goods_category_id'] = 253;
  207. }elseif($value[16] == "健康食品"){
  208. $params['goods_category_id'] = 254;
  209. }elseif($value[16] == "适老产品"){
  210. $params['goods_category_id'] = 256;
  211. }elseif($value[16] == "健康产品"){
  212. $params['goods_category_id'] = 289;
  213. }elseif($value[16] == "活动专区"){
  214. $params['goods_category_id'] = 300;
  215. }elseif($value[16] == "理疗设备"){
  216. $params['goods_category_id'] = 301;
  217. }elseif($value[16] == "医疗设备"){
  218. $params['goods_category_id'] = 302;
  219. }
  220. $params['goods_shop_id'] = 1;
  221. $params['goods_price'] = $value[14];
  222. $params['goods_pack_price'] = 0;
  223. $params['goods_postage_id'] = 11;
  224. $params['goods_storage'] = $value['8'];
  225. $params['goods_packaging'] = '';
  226. $params['goods_attr'] = $value[4];
  227. $params['goods_varieties'] = $value[3];
  228. $params['goods_sort'] = $value[17];
  229. $params['goods_create_time'] = time();
  230. $params['goods_content'] = "<p>产品参数:".$value['6'].PHP_EOL."</p><p>产品功能:".$value['7'].PHP_EOL."</p><p>发货地:".$value['8'].PHP_EOL."</p><p>发货时间:".$value['9'].PHP_EOL."</p><p>质保期:".$value['11'].PHP_EOL."</p><p>退换货规则:".$value['12'].PHP_EOL."</p><p>主要卖点:".$value['13']."</p>";
  231. $result = LifeGoods::insertGetId($params);
  232. if (!empty($result)){
  233. $msg = '管理员:' . $this->request->admin_id . '在:' . date("Y-m-d H:i:s", time()) . '批量添加健康超市商品-编号: ' . $result;
  234. plog('life-goods-create', '悦活-健康超市-批量添加商品', $msg);
  235. }
  236. }
  237. return json_success($result, '导入成功');
  238. } catch (\Exception $e) {
  239. throw $e;
  240. }
  241. }
  242. }