Taobaocsv.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Goods;
  9. use app\models\GoodsPic;
  10. use yii\base\Model;
  11. require \Yii::$app->basePath . '/librarys/phpexcel/PHPExcel.php';
  12. class Taobaocsv extends Model
  13. {
  14. public $store_id;
  15. public $excel;
  16. public $zip;
  17. public $mch_id;
  18. public function rules()
  19. {
  20. return [
  21. [['excel'], 'file', 'extensions' => ['excel']],
  22. [['zip'], 'file', 'extensions' => ['zip']],
  23. ];
  24. }
  25. public function search()
  26. {
  27. if (!$this->validate()) {
  28. return $this->getErrorResponse();
  29. }
  30. set_time_limit(0);
  31. $filename = $_FILES['excel']['name'];
  32. $tmpname = $_FILES['excel']['tmp_name'];
  33. $path = \Yii::$app->basePath . '/web/temp/';
  34. if(!is_dir($path)){
  35. mkdir($path);
  36. }
  37. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  38. if (($ext != 'xlsx') && ($ext != 'xls')) {
  39. return [
  40. 'code' => 1,
  41. 'msg' => '请上传excel文件'
  42. ];
  43. }
  44. $file = time() . $this->store_id . '.' . $ext;
  45. $uploadfile = $path . $file;
  46. $result = move_uploaded_file($tmpname, $uploadfile);
  47. // 加载zip
  48. $imagePath = \Yii::$app->basePath . '/web/uploads/image/tbi/' . date('Y') . '/' . date('m') . '/';
  49. try {
  50. $this->get_zip_originalsize($_FILES['zip']['tmp_name'], $imagePath);
  51. } catch (\Exception $e) {
  52. unlink($uploadfile);
  53. return [
  54. 'code' => 1,
  55. 'msg' => $e->getMessage()
  56. ];
  57. }
  58. // 读取Excel文件
  59. $reader = \PHPExcel_IOFactory::createReader(($ext == 'xls' ? 'Excel5' : 'Excel2007'));
  60. $excel = $reader->load($uploadfile);
  61. $sheet = $excel->getActiveSheet();
  62. $highestRow = $sheet->getHighestRow();
  63. $highestColumn = $sheet->getHighestColumn();
  64. $highestColumnCount = \PHPExcel_Cell::columnIndexFromString($highestColumn);
  65. $row = 1;
  66. $colIndex = [];
  67. $arr = [];
  68. while ($row <= $highestRow) {
  69. $rowValue = array();
  70. $col = 0;
  71. while ($col < $highestColumnCount) {
  72. $rowValue[] = (string)$sheet->getCellByColumnAndRow($col, $row)->getValue();
  73. ++$col;
  74. }
  75. if(count($rowValue) == 0){
  76. unlink($uploadfile);
  77. return [
  78. 'code' => 1,
  79. 'msg' => '上传文件内容不符合规范'
  80. ];
  81. }else{
  82. if($row == 1){
  83. }else if($row == 2){
  84. $colIndex = array_flip($rowValue);
  85. }else if($row == 3){
  86. }else{
  87. $newItem = [
  88. 'title' => $rowValue[$colIndex['title']],
  89. 'price' => $rowValue[$colIndex['price']],
  90. 'num' => $rowValue[$colIndex['num']],
  91. 'description' => $rowValue[$colIndex['description']],
  92. ];
  93. $picContents = $rowValue[$colIndex['picture']];
  94. $allpics = explode(';', $picContents);
  95. $pics = array();
  96. $optionpics = array();
  97. foreach ($allpics as $imgurl) {
  98. if (empty($imgurl)) {
  99. continue;
  100. }
  101. $picDetail = explode('|', $imgurl);
  102. $picDetail = explode(':', $picDetail[0]);
  103. $imgRootUrl = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/uploads/image/tbi/' . date('Y') . '/' . date('m') . '/' . $picDetail[0] . '.png');
  104. $imgurl = $imagePath . $picDetail[0] . '.png';
  105. if (@fopen($imgurl, 'r')) {
  106. if ($picDetail[1] == 1) {
  107. $pics[] = $imgRootUrl;
  108. }
  109. if ($picDetail[1] == 2) {
  110. $optionpics[$picDetail[0]] = $imgRootUrl;
  111. }
  112. }
  113. }
  114. $newItem['pics'] = $pics;
  115. $res = $this->save($newItem);
  116. if($res){
  117. $arr[] = $res;
  118. }
  119. }
  120. }
  121. ++$row;
  122. }
  123. $count = count($arr);
  124. unlink($uploadfile);
  125. return [
  126. 'code' => 0,
  127. 'msg' => "共导入{$count}条数据"
  128. ];
  129. }
  130. // 获取字旁文件的内容
  131. private function get_zip_originalsize($filename, $path)
  132. {
  133. if (!file_exists($filename)) {
  134. throw new \Exception('文件不存在', 1);
  135. }
  136. $filename = iconv('utf-8', 'gb2312', $filename);
  137. $path = iconv('utf-8', 'gb2312', $path);
  138. $resource = zip_open($filename);
  139. while ($dir_resource = zip_read($resource)) {
  140. if (zip_entry_open($resource, $dir_resource)) {
  141. $file_name = $path . zip_entry_name($dir_resource);
  142. $file_path = substr($file_name, 0, strrpos($file_name, '/'));
  143. if (!is_dir($file_path)) {
  144. mkdir($file_path, 511, true);
  145. }
  146. if (!is_dir($file_name)) {
  147. $file_size = zip_entry_filesize($dir_resource);
  148. if ($file_size < 1024 * 1024 * 10) {
  149. $file_content = zip_entry_read($dir_resource, $file_size);
  150. $ext = strrchr($file_name, '.');
  151. if ($ext == '.png') {
  152. file_put_contents($file_name, $file_content);
  153. } else {
  154. if ($ext == '.tbi') {
  155. $file_name = substr($file_name, 0, strlen($file_name) - 4);
  156. file_put_contents($file_name . '.png', $file_content);
  157. }
  158. }
  159. }
  160. }
  161. zip_entry_close($dir_resource);
  162. }
  163. }
  164. zip_close($resource);
  165. }
  166. private function save($list = [])
  167. {
  168. if(count($list) == 0){
  169. return false;
  170. }
  171. $goods = new Goods();
  172. $goods->name = $list['title'];
  173. $goods->store_id = $this->store_id;
  174. $goods->price = $list['price'];
  175. $goods->original_price = $list['price'];
  176. $goods->goods_num = $list['num'];
  177. $goods->use_attr = 0;
  178. $goods->detail = $list['description'];
  179. $goods->cover_pic = count($list['pics']) >= 1 ? $list['pics'][0] : '';
  180. if($this->mch_id){
  181. $goods->mch_id = $this->mch_id;
  182. }else{
  183. $goods->mch_id = 0;
  184. }
  185. list($default_attr, $default_attr_group) = Goods::getDefaultAttr($this->store_id);
  186. $attr = [
  187. [
  188. 'attr_list' => [
  189. [
  190. 'attr_group_name' => $default_attr_group->attr_group_name,
  191. 'attr_id' => $default_attr->id,
  192. 'attr_name' => $default_attr->attr_name,
  193. ],
  194. ],
  195. 'num' => intval($goods->goods_num) ? intval($goods->goods_num) : 0,
  196. 'price' => $goods->price,
  197. 'no' => ''
  198. ],
  199. ];
  200. $goods->attr = \Yii::$app->serializer->encode($attr);
  201. if($goods->save()){
  202. //商品图片保存
  203. foreach ($list['pics'] as $pic_url) {
  204. $goods_pic = new GoodsPic();
  205. $goods_pic->goods_id = $goods->id;
  206. $goods_pic->pic_url = $pic_url;
  207. $goods_pic->is_delete = 0;
  208. $goods_pic->save();
  209. }
  210. return true;
  211. }else{
  212. return false;
  213. }
  214. }
  215. }