shop-php/app/controller/admin/ProductLibrary.php
2023-11-01 20:17:32 +08:00

194 lines
8.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\controller\admin;
use crmeb\basic\BaseController;
use think\facade\Db;
use think\facade\Log;
use crmeb\services\UploadService;
class ProductLibrary extends BaseController
{
public function lst()
{
[$page, $limit] = $this->getPage();
$param = $this->request->param();
$where=['is_del'=>0];
$list = Db::name('product_library')->where($where)->page($page)->limit($limit)->order('id desc')->select();
$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'];
$res = Db::name('product_library')->where('id', $param['id'])->update($data);
if ($res) {
return app('json')->success('修改成功');
} else {
return app('json')->fail('修改失败');
}
}
public function del()
{
$param = $this->request->param();
$res = Db::name('product_library')->where('id', $param['id'])->update(['is_del' => 1]);
if ($res) {
return app('json')->success('删除成功');
} else {
return app('json')->fail('删除失败');
}
}
public function caiji($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;
try{
$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']!=''){
$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']));
}else{
$data['image'] = 'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230130/00ebcfdf75684f5494c0193075055d1.png';
}
$res = Db::name('product_library')->insert($data);
if($res){
return app('json')->success('添加成功');
}else{
throw new \think\exception\ValidateException('添加失败');
}
} else {
if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
throw new \think\exception\ValidateException("参数错误");
} elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
throw new \think\exception\ValidateException("AppCode错误");
} elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
throw new \think\exception\ValidateException("请求的 Method、Path 或者环境错误");
} elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
throw new \think\exception\ValidateException("服务未被授权或URL和Path不正确");
} elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
throw new \think\exception\ValidateException("套餐包次数用完");
} elseif ($httpCode == 403 && strpos($header, "Api Market Subscription quota exhausted") !== false) {
throw new \think\exception\ValidateException("套餐包次数用完,请续购套餐");
} elseif ($httpCode == 500) {
throw new \think\exception\ValidateException("API网关错误");
} elseif ($httpCode == 0) {
throw new \think\exception\ValidateException("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']);
throw new \think\exception\ValidateException("参数名错误 或 其他错误");
}
}
}catch(\Exception $e){
throw new \think\exception\ValidateException($e->getMessage());
}
}
}