shop-php/app/controller/admin/ProductLibrary.php

194 lines
8.1 KiB
PHP
Raw Normal View History

2023-11-01 11:57:51 +08:00
<?php
namespace app\controller\admin;
use crmeb\basic\BaseController;
use think\facade\Db;
2023-11-01 14:11:50 +08:00
use think\facade\Log;
2023-11-01 20:17:32 +08:00
use crmeb\services\UploadService;
2023-11-01 11:57:51 +08:00
class ProductLibrary extends BaseController
{
public function lst()
{
[$page, $limit] = $this->getPage();
$param = $this->request->param();
2023-11-01 18:37:12 +08:00
$where=['is_del'=>0];
$list = Db::name('product_library')->where($where)->page($page)->limit($limit)->order('id desc')->select();
2023-11-01 11:57:51 +08:00
$count = Db::name('product_library')->count();
return app('json')->success(['list' => $list, 'count' => $count]);
}
public function add()
{
$param = $this->request->param();
$data['store_name'] = $param['store_name'];
$data['store_info'] = $param['store_info'];
$data['keyword'] = $param['keyword'];
$data['bar_code'] = $param['bar_code'];
$data['is_used'] = 1;
$data['status'] = 1;
$data['cate_id'] = $param['cate_id'];
$data['unit_name'] = $param['unit_name'];
$data['price'] = $param['price'];
$data['cost'] = $param['cost'];
$data['ot_price'] = $param['ot_price'];
$data['stock'] = $param['stock'];
$data['create_time'] = date('Y-m-d H:i:s');
$data['image'] = $param['image'];
$data['slider_image'] = $param['slider_image'];
$data['images'] = $param['images'];
$res = Db::name('product_library')->insert($data);
if ($res) {
return app('json')->success('添加成功');
} else {
return app('json')->fail('添加失败');
}
}
public function edit()
{
$param = $this->request->param();
$data['store_name'] = $param['store_name'];
$data['store_info'] = $param['store_info'];
$data['keyword'] = $param['keyword'];
$data['bar_code'] = $param['bar_code'];
$data['is_used'] = 1;
$data['status'] = 1;
$data['cate_id'] = $param['cate_id'];
$data['unit_name'] = $param['unit_name'];
$data['price'] = $param['price'];
$data['cost'] = $param['cost'];
$data['ot_price'] = $param['ot_price'];
$data['stock'] = $param['stock'];
$data['create_time'] = date('Y-m-d H:i:s');
$data['image'] = $param['image'];
$data['slider_image'] = $param['slider_image'];
$data['images'] = $param['images'];
2023-11-01 14:11:50 +08:00
$res = Db::name('product_library')->where('id', $param['id'])->update($data);
2023-11-01 11:57:51 +08:00
if ($res) {
return app('json')->success('修改成功');
} else {
return app('json')->fail('修改失败');
}
}
public function del()
{
$param = $this->request->param();
2023-11-01 14:11:50 +08:00
$res = Db::name('product_library')->where('id', $param['id'])->update(['is_del' => 1]);
2023-11-01 11:57:51 +08:00
if ($res) {
return app('json')->success('删除成功');
} else {
return app('json')->fail('删除失败');
}
}
2023-11-01 15:03:33 +08:00
public function caiji($code='')
2023-11-01 11:57:51 +08:00
{
2023-11-01 14:11:50 +08:00
$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;
2023-11-01 15:03:33 +08:00
try{
2023-11-01 14:11:50 +08:00
$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);
2023-11-01 11:57:51 +08:00
2023-11-01 14:11:50 +08:00
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']!=''){
2023-11-01 20:17:32 +08:00
$upload = UploadService::create();
$dir = 'def/' . date('Y-m-d');
$oss = $upload->to($dir)->stream(file_get_contents($param['img']));
$data['image'] = $oss->filePath;
// $filename = basename($param['img']); // 获取文件名
// $destination = public_path('uploads').'img/' .date('Y-m-d').'/'. $filename; // 目标路径
// if(!is_dir(public_path('uploads').'img/' .date('Y-m-d'))){ //需要先判断文件夹是否存
// mkdir(public_path('uploads').'img/' .date('Y-m-d'),0777,true);
// }
// $data['image']='https://'.$url_host.'/uploads/img/'.date('Y-m-d').'/'. $filename;
// file_put_contents($destination, file_get_contents($param['img']));
2023-11-01 14:11:50 +08:00
}else{
2023-11-01 15:03:33 +08:00
$data['image'] = 'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230130/00ebcfdf75684f5494c0193075055d1.png';
2023-11-01 14:11:50 +08:00
}
$res = Db::name('product_library')->insert($data);
if($res){
return app('json')->success('添加成功');
}else{
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException('添加失败');
2023-11-01 14:11:50 +08:00
}
} else {
if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException("参数错误");
2023-11-01 14:11:50 +08:00
} elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException("AppCode错误");
2023-11-01 14:11:50 +08:00
} elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException("请求的 Method、Path 或者环境错误");
2023-11-01 14:11:50 +08:00
} elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException("服务未被授权或URL和Path不正确");
2023-11-01 14:11:50 +08:00
} elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException("套餐包次数用完");
2023-11-01 14:11:50 +08:00
} elseif ($httpCode == 403 && strpos($header, "Api Market Subscription quota exhausted") !== false) {
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException("套餐包次数用完,请续购套餐");
2023-11-01 14:11:50 +08:00
} elseif ($httpCode == 500) {
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException("API网关错误");
2023-11-01 14:11:50 +08:00
} elseif ($httpCode == 0) {
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException("URL错误");
2023-11-01 14:11:50 +08:00
} 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']);
2023-11-01 15:03:33 +08:00
throw new \think\exception\ValidateException("参数名错误 或 其他错误");
2023-11-01 14:11:50 +08:00
}
}
2023-11-01 15:03:33 +08:00
}catch(\Exception $e){
throw new \think\exception\ValidateException($e->getMessage());
}
2023-11-01 14:11:50 +08:00
}
2023-11-01 11:57:51 +08:00
}