SupplyController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use app\Lib\Pagination;
  4. use app\Lib\Utils;
  5. use App\Lib\Weidian\weidianApi;
  6. use App\Lib\Youzan\YouzanApi;
  7. use App\Model\admin\GoodModel;
  8. use App\Model\admin\SupplyModel;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Controller;
  11. use Illuminate\Support\Facades\Input;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Validation\Rules\In;
  14. use \View;
  15. use Illuminate\Support\Facades\Auth;
  16. use Illuminate\Support\Facades\Redirect;
  17. use GuzzleHttp;
  18. use Cache;
  19. class SupplyController extends Controller
  20. {
  21. public function __construct()
  22. {
  23. if (!Auth::check()) {
  24. return Redirect::to("admin-login");
  25. }
  26. }
  27. public function addAction(Request $request)
  28. {
  29. if ($request->isMethod('post')) {
  30. $validate = \Validator::make($request->input(), [
  31. 'supplyname' => "required",
  32. 'supplykey' => "required",
  33. 'merchant_id' => "required",
  34. 'apihost' => "required",
  35. ], [
  36. 'required' => ":attribute必填项不能为空",
  37. ], [
  38. 'supplyname' => '供应商名',
  39. 'supplykey' => '密钥',
  40. 'merchant_id' => '商户ID',
  41. 'apihost' => '接口主机',
  42. ]);
  43. if ($validate->fails()) {
  44. return \redirect()->back()->withErrors($validate)->withInput();
  45. }
  46. $supply = new SupplyModel();
  47. $supply->supplyname = Input::get('supplyname');
  48. $supply->supplykey = Input::get('supplykey');
  49. $supply->merchant_id = Input::get('merchant_id');
  50. $supply->apihost = Input::get('apihost');
  51. $supply->createtime = time();
  52. if ($supply->save()) {
  53. return Redirect::to('supply-list')->with('message', '操作成功!');
  54. } else {
  55. return \redirect()->back()->with('message', '添加失败!');
  56. }
  57. }
  58. return View::make("admin.supplyform")->with('message', '添加成功!');
  59. }
  60. public function listAction()
  61. {
  62. $supply = SupplyModel::paginate(20);
  63. return View::make("admin.supply", ['list' => $supply]);
  64. }
  65. public function updateAction(Request $request, $id)
  66. {
  67. $supply = SupplyModel::find($id);
  68. if ($request->isMethod('post')) {
  69. $supply->supplyname = Input::get('supplyname');
  70. $supply->supplykey = Input::get('supplykey');
  71. $supply->merchant_id = Input::get('merchant_id');
  72. $supply->apihost = Input::get('apihost');
  73. if ($supply->save()) {
  74. return Redirect::to('supply-list')->with('message', '操作成功!');
  75. } else {
  76. return \redirect()->back()->with('message', '添加失败!');
  77. }
  78. }
  79. return View::make("admin.supplyform", ['supply' => $supply]);
  80. }
  81. /*
  82. * 获取渠道列表以及对应的分类
  83. * */
  84. public function sourceAction()
  85. {
  86. $supplies = SupplyModel::all();
  87. foreach ($supplies as $s) {
  88. if ($s->type == 1) {
  89. $data = ['opcode' => 'get_category_list', 'merchant_id' => $s->merchant_id]; //获取类别
  90. ksort($data);
  91. $string = Utils::createLinkstring($data);
  92. $data['sign'] = md5($string . $s->supplykey);
  93. $data['sign_type'] = 'MD5';
  94. $s->apihost = Utils::endWith($s->apihost, '/') ? $s->apihost : $s->apihost . "/";
  95. $url = $s->apihost . 'api/service/business';
  96. $res = Utils::curlRequest($url, json_encode($data));
  97. $list = json_decode($res);
  98. Cache::put("goods_cat_cid" . $s->id, serialize($s));
  99. }
  100. }
  101. if (!isset($list->category_list)) {
  102. return isset($list->ret_msg) ?: dd($list);
  103. }
  104. $category_list = json_decode($list->category_list);
  105. return View::make("admin.supplies", ['supplies' => $supplies, 'list' => $category_list]);
  106. }
  107. public function goodsViewsAction(Request $request)
  108. {
  109. $settings = \DB::table('settings')->get();
  110. $settingArray = array();
  111. if ($settings) {
  112. foreach ($settings as $v) {
  113. $settingArray[$v->skey] = $v->svalue;
  114. }
  115. }
  116. $success = $fail = 0 ;
  117. $message = "";
  118. if ($request->isMethod('post')) {
  119. $shops = \DB::table('shop')->find(Input::get('shop'));
  120. if ($shops) {
  121. foreach (Input::get('skuid') as $key) {
  122. $stock = Input::get('sku_stock_num_show')[$key];
  123. $isImport = \DB::table('goods')->where(['shopid' => $shops->id, 'sku_id' => $key])->first(); //是否已经导入过
  124. $imgs = $skus = array();
  125. $price = Input::get('sku_price')[$key] / 100;
  126. if (Input::get('addprice') == 'add_price') {
  127. $price = $skus[0]['price'] = ceil($price + $settingArray['cf_price']);
  128. } elseif (Input::get('addprice') == 'cf_price') {
  129. $skus[0]['price'] = $price = ceil($price * $settingArray['cf_price']);
  130. }
  131. $good = new GoodModel();
  132. $good->shopid = $shops->id;
  133. $good->type = $shops->type;
  134. $good->sku_id = $key;//Input::get('skuid')[$key];
  135. $good->sku_name = Input::get('sku_name')[$key];
  136. $good->sku_stock_num_show = $stock;
  137. $good->sku_price = $price;
  138. $good->brand_name = Input::get('brand_name')[$key];
  139. $good->country_name = Input::get('country_name')[$key];
  140. $good->sku_weight = Input::get('sku_weight')[$key];
  141. $good->sku_weight_unit = Input::get('sku_weight_unit')[$key];
  142. $good->trade_id = Input::get('trade_id')[$key];
  143. $good->country_logo = Input::get('country_logo')[$key];
  144. $good->sku_detail = Input::get('sku_detail')[$key];
  145. $good->country_map = Input::get('country_map')[$key];
  146. $good->model_type = Input::get('model_type')[$key];
  147. $good->gstatus = 0;
  148. if (isset(Input::get('quality_start_date')[$key]) && !empty(Input::get('quality_start_date')[$key])) {
  149. $good->quality_start_date = Input::get('quality_start_date')[$key];
  150. $good->quality_end_date = Input::get('quality_end_date')[$key];
  151. }
  152. if (Input::get('trade_id')[$key] == 2) {
  153. $trade = ' 直邮商品';
  154. } elseif (Input::get('trade_id')[$key] == 1) {
  155. $trade = '保税商品';
  156. } elseif (Input::get('trade_id')[$key] == 3) {
  157. $trade = '完税商品';
  158. } else {
  159. $trade = '售后商品';
  160. }
  161. $item_comment = Input::get('sku_name')[$key] . "\n品牌:" . Input::get('brand_name')[$key] . "\n产地:" . Input::get('country_name')[$key] . "\n重量:"
  162. . Input::get('sku_weight')[$key] . Input::get('sku_weight_unit')[$key] . "\n贸易类型:" . $trade;
  163. if (!empty(Input::get('quality_start_date')[$key])) {
  164. $item_comment .= "\n保质期:" . Input::get('quality_start_date')[$key] . ' - ' . Input::get('quality_end_date')[$key];
  165. }
  166. if ($shops->type == 1) { //处理有赞
  167. $yzApi = YouzanApi::getInstance($shops->app_id, $shops->app_secret);
  168. //更新商品库存信息。
  169. if ($isImport && Input::get('updateStock')) {
  170. $getParams = ['num_iid' => $isImport->item_id];
  171. $productInfo = $yzApi->getProduct($getParams);
  172. if ($productInfo['code'] == 200 && isset($productInfo['response'])) { //商品存在
  173. $skusInfo = $productInfo['response']['item']['skus'];
  174. $skuParams = [
  175. 'num_iid' => $skusInfo[0]['num_iid'],
  176. 'sku_id' => $skusInfo[0]['sku_id'],
  177. 'quantity' => $stock,
  178. 'price' => $price,
  179. 'outer_id' => $key,
  180. ];
  181. $result = $yzApi->updateSkuInfos($skuParams);
  182. if ($result['code'] == 200 && isset($result['response'])) {
  183. \DB::table('goods')->where(['shopid' => $shops->id, 'item_id' => $isImport->item_id])
  184. ->update(['sku_stock_num_show' => $stock, 'gstatus' => 1, 'sku_price' => $price]);
  185. $success++;
  186. } else {
  187. $fail++;
  188. }
  189. }
  190. continue;
  191. }
  192. $params = [
  193. 'title' => Input::get('sku_name')[$key],
  194. 'price' => $price,
  195. 'post_fee' => '0',
  196. 'outer_id' => $key,
  197. 'quantity' => Input::get('sku_stock_num_show')[$key],
  198. 'delivery_template_id' => $shops->delivery_template_id ? $shops->delivery_template_id : $settingArray['yz_template'],
  199. 'is_display' => 0
  200. ];
  201. $skus_with_json = [];
  202. $skus_with_json[0]['sku_property'] = ['贸易类型' => $trade];
  203. $skus_with_json[0]['sku_price'] = $price;
  204. $skus_with_json[0]['sku_quantity'] = Input::get('sku_stock_num_show')[$key];
  205. $skus_with_json[0]['sku_outer_id'] = $key;
  206. $params['skus_with_json'] = json_encode($skus_with_json);
  207. if (!is_dir(storage_path() . '/product')) {
  208. mkdir(storage_path() . '/product');
  209. }
  210. if (!is_dir(storage_path('product/') . $key)) {
  211. mkdir(storage_path('product/') . $key);
  212. }
  213. $file = Utils::downloadImage(Input::get('sku_pic')[$key], md5(Input::get('sku_pic')[$key]), storage_path('product/' . $key));
  214. if (!$file) throw new Exception('没有商品主图');
  215. $files = [
  216. [
  217. 'url' => $file['saveDir'] . '/' . $file['fileName'],
  218. 'field' => 'images[]',
  219. ],
  220. ];
  221. $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
  222. preg_match_all($pattern, Input::get('sku_detail')[$key], $match);
  223. $params['desc'] = '<p>' . str_replace("\n", "<br/>", $item_comment) . "</p>";
  224. foreach ($match[1] as $img) {
  225. if ($shops->savePic) {
  226. $file = Utils::downloadImage($img, md5(Input::get('sku_pic')[$key]), storage_path('product/' . $key));
  227. if (!$file) continue;
  228. $files[] = ['url' => $file['saveDir'] . '/' . $file['fileName'], 'field' => 'images[]'];
  229. } else {
  230. $params['desc'] .= '<img src="' . $img . '" />';
  231. }
  232. }
  233. $params['desc'] .= '</p>';
  234. $result = $yzApi->addProduct($params, $files);
  235. if ($result['code'] == 200 && isset($result['response'])) {
  236. $good->item_id = $result['response']['item']['num_iid'];
  237. $good->sku_pic = $result['response']['item']['pic_url'];
  238. $good->save();
  239. $success++;
  240. } else {
  241. $fail++;
  242. }
  243. } else if ($shops->type == 2) { //处理微店
  244. $accessToken = weidianApi::get_accesstoken($shops->app_id, $shops->app_secret);
  245. \DB::table('shop')->where('id', $shops->id)->update(['access_token' => $accessToken]);
  246. $wdProductApi = new \ProductClientApi($accessToken);
  247. if ($isImport && Input::get('updateStock')) {
  248. $result = $wdProductApi->getProduct($isImport->item_id)->getDataAsObject();
  249. if ($result->status->status_code == 0) { //检索商品成功!
  250. $items = $result->result->skus;
  251. $skus_update_items = [];
  252. foreach ($items as $item) {
  253. $skus_update_items [] = ['item_id' => $isImport->item_id, 'item_sku_id' => $item->id, 'stock' => $stock];
  254. }
  255. $res = $wdProductApi->updateStock($skus_update_items);
  256. if ($res) {
  257. $success++;
  258. } else {
  259. $fail++;
  260. }
  261. continue;
  262. }
  263. }
  264. $imgUlr = $wdProductApi->uploadProductImage("@" . Input::get('sku_pic')[$key])->data;
  265. $imgUlr = json_decode($imgUlr);
  266. $skus[0]['img'] = $imgUlr->result;
  267. $saveImage = $imgUlr->result;
  268. $imgs[] = $imgUlr->result;
  269. $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
  270. preg_match_all($pattern, Input::get('sku_detail')[$key], $match);
  271. foreach ($match[1] as $img) {
  272. $imgUlr = $wdProductApi->uploadProductImage("@" . $img)->data;
  273. $imgUlr = json_decode($imgUlr);
  274. $imgs[] = $imgUlr->result;
  275. }
  276. if (Input::get('addprice') == 'add_price') {
  277. $price = $skus[0]['price'] = ceil($price + $settingArray['cf_price']);
  278. } elseif (Input::get('addprice') == 'cf_price') {
  279. $skus[0]['price'] = $price = ceil($price * $settingArray['cf_price']);
  280. }
  281. $skus[0]['title'] = Input::get('sku_name')[$key];
  282. $skus[0]['stock'] = Input::get('sku_stock_num_show')[$key];
  283. $skus[0]['sku_merchant_code'] = $key;
  284. $merchant_code = $key;
  285. $item_name = trim(Input::get('sku_name')[$key]);
  286. $free_delivery = 1; //是否包邮
  287. $remote_free_delivery = 0;
  288. if (Input::get('model_type') == 2 || Input::get('model_type') == 1) {
  289. $free_delivery = 0; //包邮
  290. if (Input::get('model_type') == 2) { //部分包邮
  291. $remote_free_delivery = 0;
  292. } else {
  293. $remote_free_delivery = 1; //偏远地区是否包邮:1包邮,0不包邮只有在free_delivery为1时候有效,当free_delivery为0时无论传入何值都不包邮
  294. }
  295. }
  296. //添加商品
  297. $response = $wdProductApi->addProduct($item_name, $price, [], $stock, $item_comment, $merchant_code, $imgs, $free_delivery, 2, $skus, $remote_free_delivery);
  298. if (!$response->getDataAsObject()) {
  299. Log::info("导入 " . $key . " 失败");
  300. $fail++;
  301. continue;
  302. }
  303. $add_item_id = $response->getDataAsObject()->result->item_id;
  304. $good->item_id = $add_item_id;
  305. $good->sku_pic = $saveImage;
  306. $good->save();
  307. $success++;
  308. }
  309. }
  310. $message = $success . '条操作成功,' . $fail . '条操作失败';
  311. } else {
  312. $message = '选择的店铺不存在!';
  313. }
  314. \redirect()->refresh();
  315. }
  316. $supply = SupplyModel::where('id', Input::get('sid'))->first();
  317. if ($supply) {
  318. $page = intval(Input::get('page')) ?: 1;
  319. $limit = intval(Input::get('limit')) ?: 10;
  320. $data = ['opcode' => 'get_goods_list', 'merchant_id' => $supply->merchant_id,
  321. 'category_id' => Input::get('cid'), 'page_size' => $limit, 'page_index' => $page - 1]; //获取产品列表gi
  322. ksort($data);
  323. $string = Utils::createLinkstring($data);
  324. $data['sign'] = md5($string . $supply->supplykey);
  325. $data['sign_type'] = 'MD5';
  326. $supply->apihost = Utils::endWith($supply->apihost, '/') ? $supply->apihost : $supply->apihost . "/";
  327. $url = $supply->apihost . 'api/service/business';
  328. $res = Utils::curlRequest($url, json_encode($data));
  329. $list = json_decode($res);
  330. if (Input::get('debug') == 1 || !$list) dd($list);
  331. $total = intval($list->total);
  332. $markup = "";
  333. if ($total > $limit) {
  334. $url = route("goodsview", ['sid' => Input::get('sid'), 'cid' => Input::get('cid'), 'cat_name' => Input::get('cat_name')]);
  335. $params = array(
  336. 'total_rows' => intval($total), #(必须)
  337. 'method' => 'html', #(必须)
  338. 'parameter' => $url . "&page=@",//url()->current()."?" , //'xxx.com/20-0-0-0-40-?.html', #(必须)
  339. 'now_page' => $page, #(必须)
  340. 'list_rows' => $limit, #(可选) 默认为15
  341. );
  342. $pagination = new Pagination($params);
  343. $markup = $pagination->show(2);
  344. }
  345. $shops = \DB::table('shop')->get();
  346. return View::make('admin.goodsList', ['list' => $list, 'shops' => $shops, 'supply' => $supply,
  347. 'markup' => $markup, 'settings' => $settingArray,'message'=>$message, 'cat' => urldecode(Input::get('cat_name'))]);
  348. }
  349. }
  350. public function storeGoodsAction(Request $request)
  351. {
  352. $settings = \DB::table('settings')->get();
  353. $settingArray = array();
  354. if ($settings) {
  355. foreach ($settings as $v) {
  356. $settingArray[$v->skey] = $v->svalue;
  357. }
  358. }
  359. $shops = \DB::table('shop')->find(Input::get('shop'));
  360. if ($shops) {
  361. $success = $fail = 0;
  362. foreach (Input::get('skuid') as $key) {
  363. $stock = Input::get('sku_stock_num_show')[$key];
  364. $isImport = \DB::table('goods')->where(['shopid' => $shops->id, 'sku_id' => $key])->first(); //是否已经导入过
  365. $imgs = $skus = array();
  366. $price = Input::get('sku_price')[$key] / 100;
  367. if (Input::get('addprice') == 'add_price') {
  368. $price = $skus[0]['price'] = ceil($price + $settingArray['cf_price']);
  369. } elseif (Input::get('addprice') == 'cf_price') {
  370. $skus[0]['price'] = $price = ceil($price * $settingArray['cf_price']);
  371. }
  372. $good = new GoodModel();
  373. $good->shopid = $shops->id;
  374. $good->type = $shops->type;
  375. $good->sku_id = $key;//Input::get('skuid')[$key];
  376. $good->sku_name = Input::get('sku_name')[$key];
  377. $good->sku_stock_num_show = $stock;
  378. $good->sku_price = $price;
  379. $good->brand_name = Input::get('brand_name')[$key];
  380. $good->country_name = Input::get('country_name')[$key];
  381. $good->sku_weight = Input::get('sku_weight')[$key];
  382. $good->sku_weight_unit = Input::get('sku_weight_unit')[$key];
  383. $good->trade_id = Input::get('trade_id')[$key];
  384. $good->country_logo = Input::get('country_logo')[$key];
  385. $good->sku_detail = Input::get('sku_detail')[$key];
  386. $good->country_map = Input::get('country_map')[$key];
  387. $good->model_type = Input::get('model_type')[$key];
  388. $good->gstatus = 0;
  389. $quality_start_date = Input::get('quality_start_date');
  390. if (isset($quality_start_date) && !empty($quality_start_date)) {
  391. $good->quality_start_date = Input::get('quality_start_date')[$key];
  392. $good->quality_end_date = Input::get('quality_end_date')[$key];
  393. }
  394. if (Input::get('trade_id')[$key] == 2) {
  395. $trade = ' 直邮商品';
  396. } elseif (Input::get('trade_id')[$key] == 1) {
  397. $trade = '保税商品';
  398. } elseif (Input::get('trade_id')[$key] == 3) {
  399. $trade = '完税商品';
  400. } else {
  401. $trade = '售后商品';
  402. }
  403. $item_comment = Input::get('sku_name')[$key] . "\n品牌:" . Input::get('brand_name')[$key] . "\n产地:" . Input::get('country_name')[$key] . "\n重量:"
  404. . Input::get('sku_weight')[$key] . Input::get('sku_weight_unit')[$key] . "\n贸易类型:" . $trade;
  405. if (!empty(Input::get('quality_start_date')[$key])) {
  406. $item_comment .= "\n保质期:" . Input::get('quality_start_date')[$key] . ' - ' . Input::get('quality_end_date')[$key];
  407. }
  408. if ($shops->type == 1) { //处理有赞
  409. $yzApi = YouzanApi::getInstance($shops->app_id, $shops->app_secret);
  410. //更新商品库存信息。
  411. if ($isImport && Input::get('updateStock')) {
  412. $getParams = ['num_iid' => $isImport->item_id];
  413. $productInfo = $yzApi->getProduct($getParams);
  414. if ($productInfo['code'] == 200 && isset($productInfo['response'])) { //商品存在
  415. $skusInfo = $productInfo['response']['item']['skus'];
  416. $skuParams = [
  417. 'num_iid' => $skusInfo[0]->item_id,
  418. 'sku_id' => $skusInfo[0]->sku_id,
  419. 'quantity' => $stock,
  420. 'price' => $price,
  421. 'outer_id' => $key,
  422. ];
  423. $result = $yzApi->updateSkuInfos($skuParams);
  424. if ($result['code'] == 200 && isset($result['response'])) {
  425. \DB::table('goods')->where(['shopid' => $shops->id, 'item_id' => $isImport->item_id])
  426. ->update(['sku_stock_num_show' => $stock, 'gstatus' => 1, 'sku_price' => $price]);
  427. $success++;
  428. } else {
  429. $fail++;
  430. }
  431. }
  432. continue;
  433. }
  434. $params = [
  435. 'title' => Input::get('sku_name')[$key],
  436. 'price' => $price,
  437. 'post_fee' => '0',
  438. 'outer_id' => $key,
  439. 'quantity' => Input::get('sku_stock_num_show')[$key],
  440. 'delivery_template_id' => $shops->delivery_template_id ? $shops->delivery_template_id : $settingArray['yz_template'],
  441. 'is_display' => 0
  442. ];
  443. $skus_with_json = [];
  444. $skus_with_json[0]['sku_property'] = ['贸易类型' => $trade];
  445. $skus_with_json[0]['sku_price'] = $price;
  446. $skus_with_json[0]['sku_quantity'] = Input::get('sku_stock_num_show')[$key];
  447. $skus_with_json[0]['sku_outer_id'] = $key;
  448. $params['skus_with_json'] = json_encode($skus_with_json);
  449. if (!is_dir(storage_path() . '/product')) {
  450. mkdir(storage_path() . '/product');
  451. }
  452. if (!is_dir(storage_path('product/') . $key)) {
  453. mkdir(storage_path('product/') . $key);
  454. }
  455. $file = Utils::downloadImage(Input::get('sku_pic')[$key], md5(Input::get('sku_pic')[$key]), storage_path('product/' . $key));
  456. if (!$file) throw new Exception('没有商品主图');
  457. $files = [
  458. [
  459. 'url' => $file['saveDir'] . '/' . $file['fileName'],
  460. 'field' => 'images[]',
  461. ],
  462. ];
  463. $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
  464. preg_match_all($pattern, Input::get('sku_detail')[$key], $match);
  465. $params['desc'] = '<p>' . str_replace("\n", "<br/>", $item_comment) . "</p>";
  466. foreach ($match[1] as $img) {
  467. if ($shops->savePic) {
  468. $file = Utils::downloadImage($img, md5(Input::get('sku_pic')[$key]), storage_path('product/' . $key));
  469. if (!$file) continue;
  470. $files[] = ['url' => $file['saveDir'] . '/' . $file['fileName'], 'field' => 'images[]'];
  471. } else {
  472. $params['desc'] .= '<img src="' . $img . '" />';
  473. }
  474. }
  475. $params['desc'] .= '</p>';
  476. $result = $yzApi->addProduct($params, $files);
  477. if ($result['code'] == 200 && isset($result['response'])) {
  478. $good->item_id = $result['response']['item']['num_iid'];
  479. $good->sku_pic = $result['response']['item']['pic_url'];
  480. $good->save();
  481. $success++;
  482. } else {
  483. $fail++;
  484. }
  485. } else if ($shops->type == 2) { //处理微店
  486. $accessToken = weidianApi::get_accesstoken($shops->app_id, $shops->app_secret);
  487. \DB::table('shop')->where('id', $shops->id)->update(['access_token' => $accessToken]);
  488. $wdProductApi = new \ProductClientApi($accessToken);
  489. if ($isImport && Input::get('updateStock')) {
  490. $result = $wdProductApi->getProduct($isImport->item_id)->getDataAsObject();
  491. if ($result->status->status_code == 0) { //检索商品成功!
  492. $items = $result->result->skus;
  493. $skus_update_items = [];
  494. foreach ($items as $item) {
  495. $skus_update_items [] = ['item_id' => $isImport->item_id, 'item_sku_id' => $item->id, 'stock' => $stock];
  496. }
  497. $res = $wdProductApi->updateStock($skus_update_items);
  498. if ($res) {
  499. $success++;
  500. } else {
  501. $fail++;
  502. }
  503. continue;
  504. }
  505. }
  506. $imgUlr = $wdProductApi->uploadProductImage("@" . Input::get('sku_pic')[$key])->data;
  507. $imgUlr = json_decode($imgUlr);
  508. $skus[0]['img'] = $imgUlr->result;
  509. $saveImage = $imgUlr->result;
  510. $imgs[] = $imgUlr->result;
  511. $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
  512. preg_match_all($pattern, Input::get('sku_detail')[$key], $match);
  513. foreach ($match[1] as $img) {
  514. $imgUlr = $wdProductApi->uploadProductImage("@" . $img)->data;
  515. $imgUlr = json_decode($imgUlr);
  516. $imgs[] = $imgUlr->result;
  517. }
  518. if (Input::get('addprice') == 'add_price') {
  519. $price = $skus[0]['price'] = ceil($price + $settingArray['cf_price']);
  520. } elseif (Input::get('addprice') == 'cf_price') {
  521. $skus[0]['price'] = $price = ceil($price * $settingArray['cf_price']);
  522. }
  523. $skus[0]['title'] = Input::get('sku_name')[$key];
  524. $skus[0]['stock'] = Input::get('sku_stock_num_show')[$key];
  525. $skus[0]['sku_merchant_code'] = $key;
  526. $merchant_code = $key;
  527. $item_name = trim(Input::get('sku_name')[$key]);
  528. $free_delivery = 1; //是否包邮
  529. $remote_free_delivery = 0;
  530. if (Input::get('model_type') == 2 || Input::get('model_type') == 1) {
  531. $free_delivery = 0; //包邮
  532. if (Input::get('model_type') == 2) { //部分包邮
  533. $remote_free_delivery = 0;
  534. } else {
  535. $remote_free_delivery = 1; //偏远地区是否包邮:1包邮,0不包邮只有在free_delivery为1时候有效,当free_delivery为0时无论传入何值都不包邮
  536. }
  537. }
  538. //添加商品
  539. $response = $wdProductApi->addProduct($item_name, $price, [], $stock, $item_comment, $merchant_code, $imgs, $free_delivery, 2, $skus, $remote_free_delivery);
  540. if (!$response->getDataAsObject()) {
  541. Log::info("导入 " . $key . " 失败");
  542. $fail++;
  543. continue;
  544. }
  545. $add_item_id = $response->getDataAsObject()->result->item_id;
  546. $good->item_id = $add_item_id;
  547. $good->sku_pic = $saveImage;
  548. $good->save();
  549. $success++;
  550. }
  551. }
  552. $request->session()->flash('message', $success . '条操作成功,' . $fail . '条操作失败');
  553. $request->session()->flash('do', 1);
  554. } else {
  555. $request->session()->flash('message', '选择的店铺不存在!');
  556. }
  557. Redirect::back();
  558. }
  559. }