TaobaoCsvForm.php 9.5 KB

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