pretreat_tmdata.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 15/11/6
  6. * Time: 下午9:52
  7. */
  8. class pretreat_tmdata
  9. {
  10. private $tm_goods;
  11. private $tm_spec;
  12. private $tm_props;
  13. private $tm_prop_values;
  14. private $goods_class;
  15. private $cls_tree;
  16. public function __construct()
  17. {
  18. $this->tm_goods = Model('tm_goods');
  19. $this->tm_spec = Model('tm_spec');
  20. $this->tm_props = Model('tm_props');
  21. $this->tm_prop_values = Model('tm_prop_values');
  22. $this->goods_class = Model('goods_class');
  23. $this->cls_tree = new class_tree();
  24. }
  25. public function proc()
  26. {
  27. $this->tm_spec->where(array('num_iid > 0'))->delete(); //->where(array('imported' => 0))
  28. $results = $this->tm_goods->field('body,title,num_iid')->limit(false)->select();
  29. foreach ($results as $result) {
  30. $num_iid = $result['num_iid'];
  31. $body = $result['body'];
  32. $response = json_decode($body, true);
  33. $item = $response['item_seller_get_response']['item'];
  34. $cid = $item['cid'];
  35. if(is_excids($cid)) continue;
  36. $this->parse_tm_product($item);
  37. //下载图片,并确认图片数量,用于验证后面商品是否成功导入
  38. $picnum = 0;
  39. $this->parse_tm_pic($item,$picnum);
  40. $this->tm_goods->where(array('num_iid' => $num_iid))->update(array('picnum' => $picnum));
  41. }
  42. }
  43. private function parse_propnames($propnames)
  44. {
  45. $spec_vals = preg_split("/[;]+/", $propnames);
  46. $ret = array();
  47. foreach($spec_vals as $sv)
  48. {
  49. $data = preg_split("/[:]+/", $sv);
  50. if(!empty($data)) {
  51. $tmpid = $data[0];
  52. $tmpvid = $data[1];
  53. $tmpname = $data[2];
  54. $tmvname = $data[3];
  55. $ret[$tmpid]['name'] = $tmpname;
  56. $ret[$tmpid]['val'][$tmpvid] = $tmvname;
  57. }
  58. }
  59. return $ret;
  60. }
  61. //根据cid 和 pid 取到规格名称。
  62. private function get_tm_props_name($cid,$pid)
  63. {
  64. $item = $this->tm_props->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
  65. if(empty($item)) {
  66. return NULL;
  67. } else {
  68. return $item[0]['name'];
  69. }
  70. }
  71. private function get_prop_value_name($cid,$pid,$vid)
  72. {
  73. $items = $this->tm_prop_values->where(array('cid' => $cid,'pid' => $pid,'vid' => $vid))->limit(false)->select();
  74. if(empty($items)) {
  75. return NULL;
  76. } else {
  77. return $items[0]['name'];
  78. }
  79. }
  80. private function add_props($cid,$prposname)
  81. {
  82. $prop_names = $this->parse_propnames($prposname);
  83. foreach($prop_names as $key => $arval)
  84. {
  85. $pid = $key;
  86. $pname = $this->get_tm_props_name($cid,$pid);
  87. if(empty($pname)) {
  88. $pname = $arval['name'];
  89. }
  90. foreach($arval['val'] as $vid => $vname)
  91. {
  92. $name = $this->get_prop_value_name($cid,$pid,$vid);
  93. if(empty($name)) {
  94. $this->tm_prop_values->insert(array('cid' => $cid,'vid' => $vid, 'pid' => $pid, 'name' => $vname));
  95. }
  96. }
  97. }
  98. }
  99. private function get_class_name($cid)
  100. {
  101. $ret = $this->goods_class->field('gc_name')->where(array('gc_id' => $cid))->find();
  102. return $ret['gc_name'];
  103. }
  104. private function download($url)
  105. {
  106. static $pngext = array('png');
  107. $info = pathinfo($url);
  108. $name = md5($url) . '.' . $info['extension'];
  109. $path = BASE_DATA_PATH . '/Download';
  110. $path = $path . '/' . $name;
  111. if(!file_exists($path)) {
  112. $pic_url = $info['dirname'] . '/' . urlencode($info['basename']);
  113. exec("wget -O $path $pic_url");
  114. }
  115. if(file_exists($path))
  116. {
  117. $fh = fopen($path, "rb");
  118. $head = fread($fh, 8);
  119. fclose($fh);
  120. if(empty($head)) {
  121. unlink($path);
  122. Log::record("{$path} file is empty.",Log::ERR);
  123. return false;
  124. }
  125. if(!in_array($info['extension'],$pngext))
  126. {
  127. $arr = unpack("C4", $head);
  128. if($arr[1] == 137 && $arr[2] == 80 && $arr[3] == 78 && $arr[4] == 71) {
  129. Log::record("{$path} is a png file",Log::ERR);
  130. }
  131. }
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. }
  137. private function parse_tm_pic($item,&$npics)
  138. {
  139. $pics = array();
  140. $main_pic = $item['pic_url'];
  141. if(!empty($main_pic)) {
  142. array_push($pics,$main_pic);
  143. }
  144. $item_imgs = $item['item_imgs'];
  145. $item_img = $item_imgs['item_img'];
  146. foreach ($item_img as $img) {
  147. $url = $img['url'];
  148. array_push($pics,$url);
  149. }
  150. $prop_imgs = $item['prop_imgs']['prop_img'];
  151. if(!empty($prop_imgs))
  152. {
  153. foreach($prop_imgs as $img) {
  154. $url = $img['url'];
  155. array_push($pics,$url);
  156. }
  157. }
  158. $arpic = array();
  159. foreach($pics as $pic)
  160. {
  161. $name = md5($pic);
  162. if(array_key_exists($name,$arpic) == false)
  163. {
  164. if($this->download($pic)) {
  165. $arpic[$name] = 1;
  166. } else {
  167. Log::record("download pic {$pic}.",Log::ERR);
  168. }
  169. } else {
  170. //$arpic[$name] += 1;
  171. }
  172. }
  173. foreach($arpic as $key => $val) {
  174. $npics += $val;
  175. }
  176. }
  177. private function parse_tm_product($item)
  178. {
  179. $cid = $item['cid'];
  180. $num_iid = $item['num_iid'];
  181. $title = $item['title'];
  182. $outer_id = $item['outer_id'];
  183. $sku_id = $item['sku_id'];
  184. $this->add_props($cid,$item['props_name']);
  185. $skus = $item['skus'];
  186. if(empty($skus))
  187. {
  188. $val = array();
  189. $val['cid'] = $cid;
  190. $val['num_iid'] = $num_iid;
  191. $val['sku_id'] = $sku_id;
  192. $val['outer_id'] = $outer_id;
  193. $val['title'] = $title;
  194. $val['class_name'] = $this->get_class_name($cid);
  195. $val['detail_url'] = $item['detail_url'];
  196. $this->tm_spec->insert($val);
  197. }
  198. else
  199. {
  200. foreach ($skus as $skuex)
  201. {
  202. foreach ($skuex as $sku)
  203. {
  204. $val = array();
  205. $val['cid'] = $cid;
  206. $val['num_iid'] = $num_iid;
  207. $val['props_val_name'] = $sku['properties_name'];
  208. $val['sku_id'] = empty($sku['sku_id']) ? $sku_id : $sku['sku_id'];
  209. $val['outer_id'] = empty($sku['outer_id']) ? $item['outer_id'] : $sku['outer_id'];
  210. //$props = $this->parse_props($sku['properties']);
  211. $prop_names = $this->parse_propnames($sku['properties_name']);
  212. foreach($prop_names as $key => $arval)
  213. {
  214. $pid = $key;
  215. $pname = $this->get_tm_props_name($cid,$pid);
  216. foreach($arval['val'] as $vid => $vname)
  217. {
  218. $name = $this->get_prop_value_name($cid,$pid,$vid);
  219. if(empty($name)) {
  220. $this->tm_prop_values->insert(array('cid' => $cid,'vid' => $vid, 'pid' => $pid, 'name' => $vname));
  221. }
  222. }
  223. }
  224. $val['sp_name'] = $pname;
  225. $val['props'] = $pid;
  226. $val['props_val'] = $vid;
  227. $val['title'] = $title;
  228. $val['class_name'] = $this->get_class_name($cid);
  229. $val['detail_url'] = $item['detail_url'];
  230. $this->tm_spec->insert($val);
  231. }
  232. }
  233. }
  234. }
  235. }