| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\ShareLevel;
- use yii\base\Model;
- use yii\helpers\Json;
- class CopyForm extends Model
- {
- public $url;
- public $level = 2;
- private $taobaoApi = 'https://api03.6bqb.com/taobao/detail?apikey=3749F9A9867E7B463FF92F2029CB5663';
- private $tmallApi = 'https://api03.6bqb.com/tmall/detail?apikey=3749F9A9867E7B463FF92F2029CB5663';
- //private $jdApi = 'https://api03.6bqb.com/jd/detail?apikey=3749F9A9867E7B463FF92F2029CB5663';
- private $t1688Api = 'https://api-gw.onebound.cn/1688/item_get/?api_name=item_get&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326';
- private $jdApi = 'https://api-gw.onebound.cn/jd/item_get?api_name=item_get&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326';
- public function rules()
- {
- return [
- [['url'], 'trim'],
- [['url'], 'string']
- ];
- }
- public function copy()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- try {
- if (strpos($this->url, 'item.jd')) {
- $id = $this->str_substr('item.jd.com/', '.html', $this->url);
- $id = $this->pregSubstr('/item.jd.[a-z]+\//', '/.html/', $this->url);
- if (empty($id[0])) {
- return [
- 'code' => 1,
- 'msg' => '链接错误,请检查链接'
- ];
- }
- return $this->j_copy($id[0]);
- }
- //淘宝连接检查URL的合法性
- $arr = explode('?', $this->url);
- if (count($arr) < 2) {
- return [
- 'code' => 1,
- 'msg' => '链接错误,请检查链接'
- ];
- }
- if (strpos($arr[0], 'taobao')) {
- $id = $this->pregSubstr('/(\?id=|&id=)/', '/&/', $this->url);
- if (empty($id[0])) {
- return [
- 'code' => 1,
- 'msg' => '链接错误,请检查链接'
- ];
- }
- $data = $this->taobao($id[0]);
-
- return $data;
- }
-
- if (strpos($arr[0], 'tmall')) {
- $id = $this->pregSubstr('/(\?id=|&id=)/', '/&/', $this->url);
- if (empty($id[0])) {
- return [
- 'code' => 1,
- 'msg' => '链接错误,请检查链接'
- ];
- }
- $data = $this->tmall($id[0]);
-
- return $data;
- }
- if (strpos($arr[0], '1688')) {
- $id = $this->pregSubstr('/(\/offer\/)/', '/\.html/', $this->url);
- if (empty($id[0])) {
- return [
- 'code' => 1,
- 'msg' => '链接错误,请检查链接'
- ];
- }
- return $this->t1688($id[0]);
- }
-
- return [
- 'code' => 1,
- 'msg' => '链接错误,请检查链接'
- ];
- } catch (\Exception $e) {
- \Yii::error([__METHOD__, $e->getTrace()]);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- private function taobao($id)
- {
- $res = http_get($this->taobaoApi . '&itemid=' . $id);
- \Yii::error([__METHOD__, $res, $id, $this->taobaoApi]);
- if ($res->getStatusCode() != 200) {
- return [
- 'code' => 1,
- 'msg' => '请求出错',
- ];
- }
- $res = json_decode((string)$res->getBody(), true);
- \Yii::error([__METHOD__, $res, $id, $this->taobaoApi]);
- if (!empty($res['message']) || $res['retcode'] != '0000') {
- return [
- 'code' => 1,
- 'msg' => !empty($res['message']) ? $res['message'] : $res['data'],
- ];
- }
- // 售价
- $price = null;
- if (!empty($res['data']['item']['priceRange'])) {
- $priceEx = explode('-', $res['data']['item']['priceRange']);
- $price = $priceEx[0];
- }
- // 市场价
- $marketPrice = null;
- if (!empty($res['data']['item']['marketPriceRange'])) {
- $marketrPriceEx = explode('-', $res['data']['item']['marketPriceRange']);
- $marketPrice = $marketrPriceEx[0];
- }
- // 总库存
- $quantity = 0;
- if (!empty($res['data']['item']['sku'][0])) {
- $quantity = $res['data']['item']['sku'][0]['quantity'];
- }
- // 详情
- $desc = null;
- if (!empty($res['data']['item']['desc'])) {
- $desc = $res['data']['item']['desc'];
- } else {
- $desc = '<p>';
- if (!empty($res['data']['item']['descImgs'])) {
- foreach ($res['data']['item']['descImgs'] as $key => $value) {
- $desc .= '<img src="' . $value . '">';
- }
- }
- $desc .= '</p>';
- }
- // 视频
- $video = null;
- if (!empty($res['data']['item']['videos'])) {
- $video = $res['data']['item']['videos'][0]['url'];
- }
- //虚拟销量
- $vagueSellCount = 0;
- if (!empty($res['data']['item']['vagueSellCount'])) {
- $vagueSellCount = (int)$res['data']['item']['vagueSellCount'];
- }
- $images = $res['data']['item']['images'];
- foreach ($images as &$item) {
- $index = strstr($item, 'http');
- if (!$index) {
- $item = 'https://' . $item;
- }
- $item = \str_replace('http://', 'https://', $item);
- $item = \str_replace('https:////', 'https://', $item);
- }
- $data = [
- 'title' => $res['data']['item']['title'],
- 'pics' => $images,
- 'price' => $price,
- 'marketPrice' => $marketPrice,
- 'vagueSellCount' => $vagueSellCount,
- 'quantity' => $quantity,
- 'desc' => $desc,
- 'video' => $video,
- 'attr' => $this->exeAttr($res['data']['item']['sku']),
- 'attrGroup' => $this->exeAttrGroup($res['data']['item']['props']),
- ];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- ];
- }
- private function tmall($id)
- {
- $res = http_get($this->tmallApi . '&itemid=' . $id);
- if ($res->getStatusCode() != 200) {
- return [
- 'code' => 1,
- 'msg' => '请求出错',
- ];
- }
- $res = json_decode((string)$res->getBody(), true);
- if (!empty($res['message']) || $res['retcode'] != '0000') {
- return [
- 'code' => 1,
- 'msg' => !empty($res['message']) ? $res['message'] : $res['data'],
- ];
- }
- // 售价
- $price = null;
- if (!empty($res['data']['item']['priceRange'])) {
- $priceEx = explode('-', $res['data']['item']['priceRange']);
- $price = $priceEx[0];
- }
- // 市场价
- $marketPrice = null;
- if (!empty($res['data']['item']['marketPriceRange'])) {
- $marketrPriceEx = explode('-', $res['data']['item']['marketPriceRange']);
- $marketPrice = $marketrPriceEx[0];
- }
- // 总库存
- $quantity = 0;
- if (!empty($res['data']['item']['sku'][0])) {
- $quantity = $res['data']['item']['sku'][0]['quantity'];
- }
- // 详情
- $desc = null;
- if (!empty($res['data']['item']['desc'])) {
- $desc = $res['data']['item']['desc'];
- } else {
- $desc = '<p>';
- if ($res['data']['item']['descImgs']) {
- foreach ($res['data']['item']['descImgs'] as $key => $value) {
- $desc .= '<img src="' . $value . '">';
- }
- }
- $desc .= '</p>';
- }
- // 视频
- $video = null;
- if (!empty($res['data']['item']['videos'])) {
- $video = $res['data']['item']['videos'][0]['url'];
- }
- //虚拟销量
- $vagueSellCount = 0;
- if (!empty($res['data']['item']['vagueSellCount'])) {
- $vagueSellCount = (int)$res['data']['item']['vagueSellCount'];
- }
- $images = $res['data']['item']['images'];
- foreach ($images as &$item) {
- $index = strstr($item, 'http');
- if (!$index) {
- $item = 'https://' . $item;
- }
- $item = \str_replace('http://', 'https://', $item);
- $item = \str_replace('https:////', 'https://', $item);
- }
- $data = [
- 'title' => $res['data']['item']['title'],
- 'pics' => $images,
- 'price' => $price,
- 'marketPrice' => $marketPrice,
- 'vagueSellCount' => $vagueSellCount,
- 'quantity' => $quantity,
- 'desc' => $desc,
- 'video' => $video,
- 'attr' => $this->exeAttr($res['data']['item']['sku']),
- 'attrGroup' => $this->exeAttrGroup($res['data']['item']['props']),
- ];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- ];
- }
- private function t1688($id) {
- $res = http_get($this->t1688Api . '&num_iid=' . $id);
- if ($res->getStatusCode() != 200) {
- return [
- 'code' => 1,
- 'msg' => '请求出错',
- ];
- }
- $res = json_decode((string)$res->getBody(), true);
- if (!empty($res['message']) || $res['error_code'] != '0000') {
- return [
- 'code' => 1,
- 'msg' => !empty($res['message']) ? $res['message'] : $res['data'],
- ];
- }
- $title = $res['item']['title'];
- $price = $res['item']['price'];
- $marketPrice = $res['item']['orginal_price'];
- $quantity = $res['item']['num'];
- // 详情
- if (!empty($res['item']['desc'])) {
- $desc = $res['item']['desc'];
- } else {
- $desc = '<p>';
- foreach ($res['item']['item_imgs'] as $key => $value) {
- $desc .= '<img src="' . $value['url'] . '">';
- }
- $desc .= '</p>';
- }
- // 视频
- $video = null;
- if (!empty($res['item']['video'])) {
- $video = $res['item']['video'][0]['url'] ?? $res['item']['video'];
- }
- //虚拟销量
- $vagueSellCount = 0;
- // if (!empty($res['data']['item']['vagueSellCount'])) {
- // $vagueSellCount = (int)$res['data']['item']['vagueSellCount'];
- // }
- $images = array_map(function($item){
- $index = strstr($item['url'], 'http');
- if (!$index) {
- $item['url'] = 'https://' . $item['url'];
- }
- $url = \str_replace('http://', 'https://', $item['url']);
- //兼容本来有两个斜杠问题
- return \str_replace('https:////', 'https://', $url);
- }, $res['item']['item_imgs']);
- //兼容商城规格组数据格式
- //兼容商品规格
- $prop_imgs_arr = array();
- if ($res['item']['prop_imgs']['prop_img']) {
- foreach ($res['item']['prop_imgs']['prop_img'] as $prop_img) {
- $prop_imgs_arr[$prop_img['propPath']] = $prop_img['url'];
- }
- }
- //兼容商城规格组数据格式
- $attr_group_list_array = array();
- if ($res['item']['props_list']) {
- foreach ($res['item']['props_list'] as $index => $item) {
- $prop_group = explode(':', $index);
- $prop_item = explode(':', $item);
- $attr_group_list_array[$prop_group[0]]['attr_group_name'] = $prop_item[0];
- $attr_group_list_array[$prop_group[0]]['attr_list'][$prop_group[1]]['attr_name'] = $prop_item[1];
- }
- }
- //兼容商城规格数据格式
- $attr_list_array = array();
- if ($res['item']['skus']['sku']) {
- foreach ($res['item']['skus']['sku'] as $index => $item) {
- $attr = [
- 'num' => $item['quantity'],
- 'price' => $item['price'],
- 'cost_price' => $item['price'],
- 'no' => '',
- 'pic' => $res['item']['prop_imgs']['prop_img'][$index]['url'] ?: $res['item']['pic_url'],
- 'share_commission_first' => '',
- 'wholesale_price' => $item['price'],
- 'original_price' => $item['price'],
- 'weight' => 0,
- 'share_commission_second' => '',
- 'share_commission_third' => '',
- ];
- $properties = explode(';', $item['properties_name']);
- $attrs = [];
- foreach ($properties as $properties_item) {
- $properties_item = explode(":", $properties_item);
- unset($properties_item[0], $properties_item[1]);
- $properties_item = array_values($properties_item);
- $attrs[] = [
- 'attr_name' => $properties_item[1],
- 'attr_group_name' => $properties_item[0],
- ];
- }
- $attr['attr_list'] = $attrs;
- $attr_list_array[] = $attr;
- }
- }
- $data = [
- 'title' => $title,
- 'pics' => $images,
- 'price' => $price,
- 'marketPrice' => $marketPrice,
- 'vagueSellCount' => $vagueSellCount,
- 'quantity' => $quantity,
- 'desc' => $desc,
- 'video' => $video,
- 'attr' => $attr_list_array,
- 'attrGroup' => $attr_group_list_array,
- ];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- ];
- }
- /**
- * 处理规格
- */
- protected function exeAttr($sku)
- {
- unset($sku[0]);
- $data = [];
- foreach ($sku as $key => $value) {
- $attr = [
- 'num' => $value['quantity'],
- 'price' => $value['price'],
- 'cost_price' => $value['price'],
- 'wholesale_price' => $value['price'],
- 'original_price' => $value['price'],
- 'weight' => 0,
- 'no' => '',
- 'pic' => isset($value['image']) && !empty($value['image']) ? $value['image'] : '',
- 'share_commission_first' => '',
- 'share_commission_second' => '',
- 'share_commission_third' => '',
- ];
- $attrEx = explode(';', $value['skuName']);
- $attrs = [];
- foreach ($attrEx as $k => $v) {
- $attrItemEx = explode('--', $v);
- $attrs[] = [
- 'attr_name' => $attrItemEx[1],
- 'attr_group_name' => $attrItemEx[0],
- ];
- }
- $attr['attr_list'] = $attrs;
- $data[] = $attr;
- }
- return $data;
- }
- // 处理规格组
- protected function exeAttrGroup($props)
- {
- $data = [];
- if (is_array($props) && !empty($props)) {
- foreach ($props as $key => $value) {
- $group = [
- 'attr_group_name' => $value['name'],
- ];
- $groupValues = [];
- foreach ($value['values'] as $k => $v) {
- $groupValues[] = [
- 'attr_name' => $v['name'],
- ];
- }
- $group['attr_list'] = $groupValues;
- $data[] = $group;
- }
- }
- return $data;
- }
- public function copy1()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- if (strpos($this->url, 'item.jd')) {
- $id = $this->preg_substr('/item.jd.[a-z]+\//', '/.html/', $this->url);
- $data = $this->j_copy($id[0]);
- if ($data['code'] == 1) {
- return $data;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data
- ];
- }
- //淘宝连接检查URL的合法性
- $arr = explode('?', $this->url);
- if (count($arr) < 2) {
- return [
- 'code' => 1,
- 'msg' => '链接错误,请检查链接'
- ];
- }
- if (strpos($arr[0], 'taobao') || strpos($arr[0], 'tmall')) {
- $id = $this->preg_substr('/(\?id=|&id=)/', '/&/', $this->url);
- $data = $this->t_copy($id[0]);
- if ($data['code'] == 1) {
- return $data;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '链接错误,请检查链接'
- ];
- }
- public function t_copy($num_iid){
- //图文详情
- //商品基本信息
- $title = '';
- $goods_price_sale = '';
- $goods_price = '';
- $sale_count = '';
- $quantity = '';
- $attr_group_list = [];
- $checked_attr_list = [];
- $picsPath = [];
- $detail_info = "";
- try {
- // $detail_api = "http://api.onebound.cn/taobao/api_call.php?api_name=item_get&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326";
- // $html = $this->getHTTPS($detail_api);
- // $detail_json = json_decode($html, true);
- // \Yii::error($detail_json);
- // // 转化商品详情的编码
- // if ($detail_json['item']['desc']) {
- // $detail_info = $detail_json['item']['desc'];
- // } else {
- //
- // }
- //临时处理办法,兼容淘宝详情采集为空的问题
- $detail_api = "https://api-gw.onebound.cn/taobao/item_get/?key=tel18538854912&&num_iid={$num_iid}&is_promotion=1&&lang=zh-CN&secret=20200326";
- $html = $this->getHTTPS($detail_api);
- $detail_json = json_decode($html, true);
- $detail_info = $detail_json['item']['desc'];
- $coding = mb_detect_encoding($detail_info, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5', 'ISO-8859-1'));
- $detail_info = mb_convert_encoding($detail_info, 'utf-8', $coding);
- $title = $detail_json['item']['title'];
- $goods_price_sale = $detail_json['item']['price'];
- $goods_price = $detail_json['item']['orginal_price'];
- $sale_count = $detail_json['item']['sales'];
- $video = $detail_json['item']['video'];
- $pic_url = $detail_json['item']['pic_url'];
- $quantity = "";
- //兼容商品图片的数组格式
- $picsPath_array=array();
- if (!empty($detail_json['item']['item_imgs'])) {
- foreach($detail_json['item']['item_imgs'] as $key => $value){
- if (strpos($value['url'],'http') === false) {
- $value['url'] = 'http:'. trim($value['url']);
- }
- $picsPath_array[$key]=$value['url'];
- }
- }
- $picsPath = $picsPath_array;
- //兼容商品规格
- $prop_imgs_arr = array();
- if ($detail_json['item']['prop_imgs']) {
- $prop_imgs = json_decode(json_encode($detail_json['item']['prop_imgs']),true);
- if ($prop_imgs['prop_img']) {
- foreach($prop_imgs['prop_img'] as $prop_img){
- $prop_imgs_arr[$prop_img['properties']] = $prop_img['url'];
- }
- }
- }
- $prop_array=array();
- foreach ($detail_json['item']['props_list'] as $dk=>$val) {
- $dks = explode(':',$dk);
- $vals = explode(':',$val);
- $prop_img = !empty($prop_imgs_arr[$dks[0].':'.$dks[1]]) ? $prop_imgs_arr[$dks[0] . ':' . $dks[1]] : '';
- $prop_array[$dks[0]][$dks[1]]=array(
- 'prop_key' => $dks[0],
- 'prop_val' => $dks[1],
- 'name' => $vals[0],
- 'value' => $vals[1],
- 'pic_url' => $prop_img,
- );
- }
- $mypropids = array_keys($prop_array);
- $mypropids = implode('|', $mypropids);
- //兼容商城规格组数据格式
- $attr_group_list_array = array();
- if (!empty($prop_array)) {
- $i = 0;
- foreach ($prop_array as $key=>$value) {
- $name = current($value);
- $attr_group_list_array[$i] = [
- 'attr_group_name' => $name['name']
- ];
- $j = 0;
- foreach ($value as $key2 => $value) {
- $attr_group_list_array[$i]['attr_list'][$j]['attr_name']=$value['value'];
- $j++;
- }
- $i++;
- }
- }
- $attr_group_list = $attr_group_list_array;
- //兼容货品\库存\价格数据格式
- $sku = $detail_json['item']['skus']['sku'];
- $checked_attr_list = array();
- foreach ($sku as $key1 => $value1) {
- $attr_list = explode(';',$value1['properties_name']);
- foreach ($attr_list as $key2=>$value2) {
- $value2_arr = explode(':',$value2);
- $attr_name = $value2_arr[3];
- $attr_group_name = $value2_arr[2];
- $checked_attr_list[$key1]['attr_list'][$key2] = array("attr_id"=>null,'attr_name' => $attr_name, 'attr_group_name' => $attr_group_name);
- }
- $checked_attr_list[$key1]['price'] = $value1['price'];
- $checked_attr_list[$key1]['num'] = $value1['quantity'];
- }
- } catch (\Exception $e) {}
- return [
- 'title' => $title,//标题
- 'pic_url' => $pic_url,
- 'picsPath' => $picsPath,//缩略图
- 'price' => $goods_price,//原价
- 'sale_price' => $goods_price_sale,//售价
- 'sale_count' => $sale_count,//销量
- 'quantity' => $quantity,//库存
- 'detail_info' => $detail_info,//图文详情
- 'video' => $video,//图文详情
- 'attr_group_list' => Json::encode($attr_group_list),
- 'checked_attr_list' => Json::encode($checked_attr_list),
- ];
- }
- private function j_copy_detail($num_iid, $sku_id)
- {
- $html = $this->getHTTPS("https://api-gw.onebound.cn/jd/item_get_desc?api_name=item_get_desc&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326&main_sku_id={$sku_id}");
- $detail_json = json_decode($html, true);
- $detail_info = $detail_json['item']['desc'];
- return $detail_info;
- }
- /**
- * @param $id
- * @return array
- * 京东抓取
- */
- public function j_copy($num_iid)
- {
- //图文详情
- //商品基本信息
- $title = '';
- $goods_price_sale = '';
- $goods_price = '';
- $sale_count = '';
- $quantity = '';
- $attr_group_list = [];
- $checked_attr_list = [];
- $picsPath = [];
- $detail_info = "";
- try {
- $html = $this->getHTTPS("https://api-gw.onebound.cn/jd/item_get?api_name=item_get&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326");
- //$html = http_get("https://api-gw.onebound.cn/jd/item_get?api_name=item_get&num_iid={$num_iid}&is_promotion=1&lang=zh-CN&key=tel18538854912&secret=20200326");
- $detail_json = json_decode($html, true);
- //转化商品详情的编码
- $detail_info = $detail_json['item']['desc'];
- if (empty($detail_info)) {
- $detail_info = $this->j_copy_detail($num_iid, $detail_json['item']['main_sku_id']);
- }
- // $coding = mb_detect_encoding($detail_info, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5', 'ISO-8859-1'));
- // $detail_info = mb_convert_encoding($detail_info, 'utf-8', $coding);
- $detail_info = str_replace('data-lazyload', 'src', $detail_info);
- $end_index = stripos($detail_info, '</style>');
- if ($end_index !== false) {
- $detail_style_info = substr($detail_info, 0, ($end_index + 8));
- $arr = $this->setDetail($detail_style_info);
- $img_str = '';
- $detail_info = substr($end_index, ($end_index + 8));
- foreach ($arr as $item) {
- $img_str .= '<img src="'. $item .'">';
- }
- $detail_info = $img_str .$detail_info;
- }
- $title =$detail_json['item']['title'];
- $goods_price_sale = $detail_json['item']['price'];
- $goods_price = $detail_json['item']['orginal_price'];
- $sale_count = $detail_json['item']['sales'];
- $video=$detail_json['item']['video'];
- $quantity = "";
- //兼容商品图片的数组格式
- $picsPath_array=array();
- if(!empty($detail_json['item']['item_imgs'])){
- foreach($detail_json['item']['item_imgs'] as $key=>$value){
- $index = strstr($value['url'], 'http');
- if (!$index) {
- $value['url'] = 'https://' . $value['url'];
- }
- $picsPath_array[$key]= \str_replace('http://', 'https://', $value['url']);
- //兼容本来有两个斜杠问题
- $picsPath_array[$key]= \str_replace('https:////', 'https://', $picsPath_array[$key]);
- }
- }
- $picsPath = $picsPath_array;
- //兼容商品规格
- $prop_imgs_arr = array();
- if($detail_json['item']['prop_imgs']){
- $prop_imgs = json_decode(json_encode($detail_json['item']['prop_imgs']),true);
- if($prop_imgs['prop_img']){
- foreach($prop_imgs['prop_img'] as $prop_img){
- $prop_imgs_arr[$prop_img['properties']] = $prop_img['url'];
- }
- }
- }
- $prop_array=array();
- foreach($detail_json['item']['props_list'] as $dk=>$val){
- $dks=explode(':',$dk);
- $vals=explode(':',$val);
- $prop_img = !empty($prop_imgs_arr[$dks[0].':'.$dks[1]])?$prop_imgs_arr[$dks[0].':'.$dks[1]]:'';
- $prop_array[$dks[0]][$dks[1]]=array(
- 'prop_key'=>$dks[0],
- 'prop_val'=>$dks[1],
- 'name'=>$vals[0],
- 'value'=>$vals[1],
- 'pic_url'=>$prop_img,
- );
- }
- $mypropids= array_keys($prop_array);
- $mypropids= implode('|',$mypropids);
- //兼容商城规格组数据格式
- $attr_group_list_array=array();
- if(!empty($prop_array)){
- $i=0;
- foreach($prop_array as $key=>$value){
- $name=current($value);
- $attr_group_list_array[$i] = [
- 'attr_group_name' => $name['name']
- ];
- $j=0;
- foreach($value as $key2=>$value){
- $attr_group_list_array[$i]['attr_list'][$j]['attr_name']=$value['value'];
- $j++;
- }
- $i++;
- }
- }
- $attr_group_list =$attr_group_list_array;
- //兼容货品\库存\价格数据格式
- $sku = $detail_json['item']['skus']['sku'];
- $checked_attr_list=array();
- $share_level = ShareLevel::find()->where(['store_id' => get_store_id(), 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON])
- ->select('level id, name')->orderBy('level ASC')->asArray()->all();
- $share_level = array_merge([[
- 'id' => 0,
- 'name' => '默认等级'
- ]], $share_level);
- foreach($sku as $key1=>$value1){
- $attr_list=explode(';',$value1['properties_name']);
- foreach($attr_list as $key2=>$value2){
- $value2_arr=explode(':',$value2);
- $attr_name=$value2_arr[3];
- $attr_group_name=$value2_arr[2];
- $checked_attr_list[$key1]['attr_list'][$key2]=array("attr_id"=>null,'attr_name'=>$attr_name,'attr_group_name'=>$attr_group_name);
- }
- $checked_attr_list[$key1]['price']=$value1['price'];
- $checked_attr_list[$key1]['cost_price']=$value1['price'];
- $checked_attr_list[$key1]['num']=$value1['quantity'];
- $checked_attr_list[$key1]['wholesale_price'] = $value1['price'];
- $checked_attr_list[$key1]['original_price'] = $value1['price'];
- $checked_attr_list[$key1]['weight'] = 0;
- $checked_attr_list[$key1]['share_commission_new_first'] = null;
- $checked_attr_list[$key1]['share_commission_new_second'] = null;
- $checked_attr_list[$key1]['share_commission_new_third'] = null;
- foreach ($share_level as $share_level_index => $share_level_) {
- $checked_attr_list[$key1]['share_commission_new_first'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
- $checked_attr_list[$key1]['share_commission_new_first'][$share_level_index]['value'] = 0;
- $checked_attr_list[$key1]['share_commission_new_second'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
- $checked_attr_list[$key1]['share_commission_new_second'][$share_level_index]['value'] = 0;
- $checked_attr_list[$key1]['share_commission_new_third'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id'];
- $checked_attr_list[$key1]['share_commission_new_third'][$share_level_index]['value'] = 0;
- }
- //$checked_attr_list[$key1]['pic']=$value1['quantity'];
- }
- } catch (\Exception $e) {}
- return [
- 'code' => 0,
- 'data' => [
- 'title' => $title,//标题
- 'pics' => $picsPath,//缩略图
- 'price' => $goods_price,//原价
- 'sale_price' => $goods_price_sale,//售价
- 'sale_count' => $sale_count,//销量
- 'quantity' => $quantity, //库存
- 'desc' => $detail_info,//图文详情
- 'video' => $video,//图文详情
- 'attrGroup' => $attr_group_list,
- 'attr' => $checked_attr_list,
- ]
- ];
- }
- public function setDetail($detail_info, $arr = [], $index = 0) {
- $start = stripos($detail_info, 'background-image:url(');
- $end = stripos($detail_info, '.jpg');
- if ($end !== false && $start !== false) {
- $arr[$index] = substr($detail_info, ($start), (($end + 4) - $start));
- $arr[$index] = substr($arr[$index], 21);
- if (!stripos($arr[$index], 'http') && !stripos($arr[$index], 'https')) {
- $arr[$index] = 'https:' . $arr[$index];
- }
- $new_detail_info = substr($detail_info, ($end + 4));
- if (stripos($new_detail_info, 'background-image:url(')) {
- ++$index;
- return $this->setDetail($new_detail_info, $arr, $index);
- }
- }
- return $arr;
- }
- /**
- * @param $list //规格列表
- * @param $level //规格列表下标
- * @param array $attr_list
- * @param string $n
- * @param array $new_list
- * @return array
- * 规格列表数组重组
- */
- public function get_attr_list($list, $level, $attr_list = array(), $n = '', $new_list = array())
- {
- foreach ($list[$level]['values'] as $key => $item) {
- $attr_list_new = $attr_list;
- $n_n = $n;
- $a = [];
- $n_n .= $list[$level]['propId'] . ':' . $item['valueId'];
- $a['attr_group_name'] = $list[$level]['propName'];
- $a['attr_id'] = null;
- $a['attr_name'] = $item['name'];
- array_push($attr_list_new, $a);
- if ($level < count($list) - 1 && $level < $this->level) {
- $new_list = $this->get_attr_list($list, $level + 1, $attr_list_new, $n_n . ';', $new_list);
- } else {
- $new_list_new['attr_list'] = $attr_list_new;
- $new_list_new['dataId'] = $n_n;
- $new_list[] = $new_list_new;
- }
- }
- return $new_list;
- }
- /**
- * @param $list //规格列表
- * @param $level //规格列表下标
- * @param array $attr_list
- * @param string $n
- * @param array $new_list
- * @return array
- * 规格列表数组重组 for jd by tianxin100
- */
- public function get_attr_list_for_jd($list, $level, $attr_list = array(), $n = '', $new_list = array())
- {
- foreach ($list[$level]['attr_list'] as $key => $item) {
- $attr_list_new = $attr_list;
- //$n_n = $n;
- $a = [];
- // $n_n .= $list[$level]['propId'] . ':' . $item['valueId'];
- $a['attr_group_name'] = $list[$level]['attr_group_name'];
- $a['attr_id'] = null;
- $a['attr_name'] = $item['attr_name'];
- array_push($attr_list_new, $a);
- if ($level < count($list) - 1 && $level < $this->level) {
- $new_list[] = $this->get_attr_list_for_jd($list, $level + 1, $attr_list_new, $new_list);
- } else {
- $new_list_new['attr_list'] = $attr_list_new;
- //$new_list_new['dataId'] = $n_n;
- $new_list[] = $new_list_new;
- }
- }
- return $new_list;
- }
- /**
- * @param $start
- * @param $end
- * @param $str
- * @return array
- * 正则截取函数
- */
- public function pregSubstr($start, $end, $str) // 正则截取函数
- {
- try {
- $temp = preg_split($start, $str);
- } catch (\Exception $e) {
- var_dump($str);
- exit();
- }
- $result = [];
- foreach ($temp as $index => $value) {
- if ($index == 0) {
- continue;
- }
- $content = preg_split($end, $value);
- array_push($result, $content[0]);
- }
- return $result;
- }
- /**
- * @param $start
- * @param $end
- * @param $str
- * @return array
- * 字符串截取函数
- */
- function str_substr($start, $end, $str) // 字符串截取函数
- {
- $temp = explode($start, $str);
- try {
- $result = [];
- foreach ($temp as $index => $value) {
- if ($index == 0) {
- continue;
- }
- $content = explode($end, $value);
- array_push($result, $content[0]);
- }
- return $result;
- } catch (\Exception $e) {
- var_dump($temp);
- return [];
- }
- }
- /**
- * @param $url
- * @return mixed
- * curl访问https
- */
- public function getHTTPS($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_HEADER, false);
- if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
- }
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_REFERER, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- /**
- * @param $url
- * @return mixed
- * curl访问http
- */
- public function normal_curl($url)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($curl);
- //错误提示
- if (curl_exec($curl) === false) {
- die(curl_error($curl));
- }
- // 检查是否有错误发生
- if (curl_errno($curl)) {
- echo 'Curl error: ' . curl_error($curl);
- }
- curl_close($curl);
- return $data;
- }
- //判断数组是几维数组
- public function arrayLevel($arr){
- $al = array(0);
- function aL($arr,&$al,$level=0){
- if(is_array($arr)){
- $level++;
- $al[] = $level;
- foreach($arr as $v){
- aL($v,$al,$level);
- }
- }
- }
- aL($arr,$al);
- return max($al);
- }
- }
|