This commit is contained in:
mkm 2023-11-01 14:11:50 +08:00
parent b6c5a92f66
commit be635affa2
2 changed files with 95 additions and 3 deletions

View File

@ -4,6 +4,7 @@ namespace app\controller\admin;
use crmeb\basic\BaseController;
use think\facade\Db;
use think\facade\Log;
class ProductLibrary extends BaseController
{
@ -63,7 +64,7 @@ class ProductLibrary extends BaseController
$data['image'] = $param['image'];
$data['slider_image'] = $param['slider_image'];
$data['images'] = $param['images'];
$res = Db::name('product_library')->where('id',$param['id'])->update($data);
$res = Db::name('product_library')->where('id', $param['id'])->update($data);
if ($res) {
return app('json')->success('修改成功');
} else {
@ -74,7 +75,7 @@ class ProductLibrary extends BaseController
public function del()
{
$param = $this->request->param();
$res = Db::name('product_library')->where('id',$param['id'])->update(['is_del'=>1]);
$res = Db::name('product_library')->where('id', $param['id'])->update(['is_del' => 1]);
if ($res) {
return app('json')->success('删除成功');
} else {
@ -84,6 +85,96 @@ class ProductLibrary extends BaseController
public function caiji()
{
}
$code = $this->request->param('code');
$url_host=$this->request->host();
if ($code=='') {
return app('json')->fail('编码不能为空');
}
$host = "https://codequery.market.alicloudapi.com";
$path = "/querybarcode";
$method = "GET";
$appcode = "7d2824a6331244918c66830cf33d2c4e"; //开通服务后 买家中心-查看AppCode
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "code=".$code;
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$" . $host, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$out_put = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
list($header, $body) = explode("\r\n\r\n", $out_put, 2);
if ($httpCode == 200) {
$param=json_decode($body,true)['result'];
$data['store_name'] = $param['goodsName'].' '.$param['spec'];
$data['store_info'] = '';
$data['keyword'] = '';
$data['bar_code'] = $param['code'];
$data['is_used'] = 1;
$data['status'] = 1;
$data['cate_id'] = 0;
$data['unit_name'] = '';
$data['price'] = 0;
$data['cost'] = 0;
$data['ot_price'] = 0;
$data['stock'] =9999999;
$data['create_time'] = date('Y-m-d H:i:s');
$data['images'] = '';
$data['slider_image'] = '';
if($param['img']!=''){
$filename = basename($param['img']); // 获取文件名
$destination = public_path('uploads').'img/' .date('Y-m-d').'/'. $filename; // 目标路径
$data['image']= $url_host.'/uploads/img/'.date('Y-m-d').'/'. $filename;
file_put_contents($destination, file_get_contents($param['img']));
}else{
$data['image'] = '';
}
$res = Db::name('product_library')->insert($data);
if($res){
return app('json')->success('添加成功');
}else{
return app('json')->fail('添加失败');
}
} else {
if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
return app('json')->fail("参数错误");
} elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
return app('json')->fail("AppCode错误");
} elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
return app('json')->fail("请求的 Method、Path 或者环境错误");
} elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
return app('json')->fail("服务未被授权或URL和Path不正确");
} elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
return app('json')->fail("套餐包次数用完");
} elseif ($httpCode == 403 && strpos($header, "Api Market Subscription quota exhausted") !== false) {
return app('json')->fail("套餐包次数用完,请续购套餐");
} elseif ($httpCode == 500) {
return app('json')->fail("API网关错误");
} elseif ($httpCode == 0) {
return app('json')->fail("URL错误");
} else {
$headers = explode("\r\n", $header);
$headList = array();
foreach ($headers as $head) {
$value = explode(':', $head);
$headList[$value[0]] = $value[1];
}
Log::error('商品采集错误:'.$headList['x-ca-error-message']);
return app('json')->fail("参数名错误 或 其他错误");
}
}
}
}

View File

@ -19,6 +19,7 @@ Route::group(function () {
Route::group('library', function () {
Route::get('product_list', '/lst');
Route::get('caiji', '/caiji');
Route::post('add', '/add');
Route::post('edit', '/edit');
Route::post('del', '/del');