作者 Your Name

Merge branch 'dev' of http://47.244.231.31:8099/zhl/globalso-v6 into dev

... ... @@ -25,7 +25,7 @@ class AttrController extends BaseController
$map[] = ['title', 'like', "%{$this->param['search']}%"];
}
$sort = ['id' => 'desc'];
$data = $logic->getList($map, $sort, ['id', 'title', 'remark', 'value_num']);
$data = $logic->getList($map, $sort, ['id', 'title', 'attrs']);
return $this->success($data);
}
... ... @@ -36,7 +36,7 @@ class AttrController extends BaseController
'id.required' => 'ID不能为空'
]);
$data = $logic->getInfo($this->param['id']);
return $this->success(Arr::twoKeepKeys($data, ['id', 'title', 'remark', 'values']));
return $this->success(Arr::twoKeepKeys($data, ['id', 'title', 'attrs']));
}
public function save(AttrRequest $request, AttrLogic $logic)
... ...
... ... @@ -28,7 +28,7 @@ class KeywordController extends BaseController
$sort = ['id' => 'desc'];
$data = $logic->getList($map, $sort, ['id', 'title', 'seo_title', 'seo_keywords', 'seo_description', 'status', 'created_at']);
foreach ($data['list'] as &$v){
$v['product_num'] = $logic->getProductNum($v['title']);
$v['product_num'] = $logic->getProductNum($v['id']);
$v['tdk'] = boolval($v['seo_title']) * boolval($v['seo_keywords']) * boolval($v['seo_description']);
//todo 获取域名 拼接链接
$v['url'] = $v['route'];
... ...
... ... @@ -9,6 +9,7 @@ use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Product\ProductLogic;
use App\Http\Requests\Bside\Product\ProductRequest;
use App\Models\Product\CategoryRelated;
use App\Models\Product\KeywordRelated;
use App\Rules\Ids;
use Illuminate\Http\Request;
... ... @@ -34,6 +35,10 @@ class ProductController extends BaseController
$ids = CategoryRelated::where('cate_id', $this->param['category_id'])->pluck('product_id')->toArray();
$map[] = ['id', 'in', $ids];
}
if(!empty($this->param['keyword_id'])){
$ids = KeywordRelated::where('keyword_id', $this->param['keyword_id'])->pluck('product_id')->toArray();
$map[] = ['id', 'in', $ids];
}
if(isset($this->param['status'])){
$map[] = ['status', $this->param['status']];
}
... ...
... ... @@ -26,6 +26,8 @@ class ImageController
];
public $path = '';
public $config = '';
public $thr_path = '';
public $request = '';
... ... @@ -33,7 +35,8 @@ class ImageController
public function __construct(Request $request)
{
$this->request = $request;
$this->path = config('filesystems.disks')['upload']['root'].config('upload');
$this->config = config('filesystems.disks.upload');
$this->path = $this->config['root'];
}
public function index($hash = '', $w = 0 ,$h = 0){
... ... @@ -105,25 +108,30 @@ class ImageController
*/
public function single($files){
$hash = hash_file('md5', $files->getPathname());
//查看文件是否存在
$imageModel = new ImageModel();
$image_hash = $imageModel->read(['hash'=>$hash]);
if($image_hash !== false){
return $hash;
}
$url = $this->path;
$filename = date('ymdHis').rand(10000,99999);
$res = $this->request->file('image')->move($url,$filename);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
$imageModel = new ImageModel();
$data = [
'path' => $url.$filename,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $hash.$filename,
'hash' => $hash,
'type'=>$files->getClientOriginalExtension(),
];
$rs = $imageModel->add($data);
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
return $hash.$filename;
return $hash;
}
/**
* 生成缩略图缓存
... ... @@ -151,7 +159,13 @@ class ImageController
$save_data = [];
$data = [];
foreach ($files as $file) {
$imageModel = new ImageModel();
$hash = hash_file('md5', $file->getPathname());
$image_hash = $imageModel->read(['hash'=>$hash]);
if($image_hash !== false){
$data[] = $hash;
continue;
}
$url = $this->path;
$filename = date('ymdHis').rand(10000,99999);
$res = $file->move($url,$filename);
... ... @@ -162,12 +176,11 @@ class ImageController
'path' => $url.$filename,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $hash.$filename,
'hash' => $hash,
'type'=>$files->getClientOriginalExtension(),
];
$data[] = $hash.$filename;
$data[] = $hash;
}
$imageModel = new ImageModel();
$imageModel->insert($save_data);
return $data;
}
... ...
... ... @@ -5,7 +5,7 @@ namespace App\Http\Logic\Bside;
use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Helper\Common;
use App\Http\Controllers\ImageLogic;
use App\Http\Controllers\ImageController;
use App\Http\Logic\Logic;
use App\Models\Image as ImageModel;
use Illuminate\Http\Request;
... ... @@ -99,8 +99,9 @@ class BaseLogic extends Logic
* @method
*/
public function upload(){
$imageLogic = new ImageLogic();
return $imageLogic->upload();
$image = new ImageController();
$hash = $image->upload();
return $hash;
}
/**
... ...
... ... @@ -36,10 +36,11 @@ class InquiryLogic extends BaseLogic
public function save($param)
{
//todo 根据域名 判断 project_id
$param['ip_info'] = Arr::s2a($param['ip_info']);
$param['ip'] = $param['ip_info']['ip'] ?? '';
$param['ip_country'] = $param['ip_info']['country'] ?? '';
return parent::save($param);
Logic::save($param);
}
}
... ...
... ... @@ -96,15 +96,6 @@ class NewsLogic extends BaseLogic
try {
//上传图片
if(isset($this->param['image']) && is_file($this->param['image'])){
//查看当前用户是否已有头像
if($info !== false && !empty($info['image'])){
//TODO::删除资源
$imageModel = new Image();
$image_info = $imageModel->read(['hash'=>$info['image']],['id','path']);
shell_exec('rm -rf '.$image_info['path'] . ' ./../uploads/images/cache_'. $info['image'] . '*');
//清空数据库
$imageModel->del(['hash'=>$info['image']]);
}
$this->param['image'] = $this->upload();
}
//设置路由
... ...
... ... @@ -4,8 +4,6 @@ namespace App\Http\Logic\Bside\Product;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Product\Attr;
use App\Models\Product\AttrValue;
use Illuminate\Support\Facades\DB;
/**
* Class AttrLogic
... ... @@ -22,47 +20,5 @@ class AttrLogic extends BaseLogic
$this->model = new Attr();
}
public function getInfo($id){
$info = parent::getCacheInfo($id);
if(!$info){
$this->fail('数据不存在或者已经删除');
}
$info->values;
return $this->success($info->toArray());
}
public function save($param){
$param['values'] = array_unique($param['values']);
DB::beginTransaction();
try {
//删除之前的参数值
if(!empty($param['id'])){
AttrValue::where('attr_id', $param['id'])->delete();
}
//保存参数名称
$data = $param;
unset($data['values']);
$data['value_num'] = count($param['values']);
$res = parent::save($data);
$attr_id = $res['id'];
//保存参数值
$values = [];
foreach ($param['values'] as $value){
$values[] = [
'attr_id' => $attr_id,
'value' => $value
];
}
AttrValue::insert($values);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
errorLog('产品参数保存失败', $param, $e);
$this->fail('保存失败');
}
return $this->success();
}
}
... ...
... ... @@ -65,7 +65,6 @@ class CategoryLogic extends BaseLogic
*/
public function getProductNum($cate_id){
$cate_ids = $this->model->getChildIdsArr($cate_id);
$product_ids = CategoryRelated::whereIn('cate_id', $cate_ids)->pluck('id');
return Product::whereIn('id', $product_ids)->count();
return CategoryRelated::whereIn('cate_id', $cate_ids)->count();
}
}
... ...
... ... @@ -4,7 +4,7 @@ namespace App\Http\Logic\Bside\Product;
use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Product\Product;
use App\Models\Product\KeywordRelated;
use App\Models\RouteMap;
use App\Models\Product\Keyword;
use Illuminate\Support\Facades\DB;
... ... @@ -47,6 +47,16 @@ class KeywordLogic extends BaseLogic
parent::delete($ids);
foreach ($ids as $id){
$info = $this->getCacheInfo($id);
if(!$info){
continue;
}
//是否有对应商品
if(KeywordRelated::where('keyword_id', $id)->count()){
$this->fail("关键词{$info['title']}存在产品,不能删除");
}
//删除路由映射
RouteMap::delRoute(RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
}
... ... @@ -60,7 +70,7 @@ class KeywordLogic extends BaseLogic
return $this->success();
}
public function getProductNum($title){
return Product::whereRaw("find_in_set('{$title}',`keywords`)")->count();
public function getProductNum($keyword_id){
return KeywordRelated::where('keyword_id', $keyword_id)->count();
}
}
... ...
... ... @@ -6,6 +6,7 @@ use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Http\Logic\Bside\User\UserLogic;
use App\Models\Product\CategoryRelated;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\RouteMap;
use Illuminate\Support\Facades\DB;
... ... @@ -62,6 +63,8 @@ class ProductLogic extends BaseLogic
$res = parent::save($data);
//关联分类
CategoryRelated::saveRelated($res['id'], $data['category_id']);
//关联关键词
KeywordRelated::saveRelated($res['id'], $data['keyword_id']);
//路由映射
RouteMap::setRoute($param['route'], RouteMap::SOURCE_PRODUCT, $res['id'], $this->user['project_id']);
DB::commit();
... ... @@ -86,6 +89,9 @@ class ProductLogic extends BaseLogic
//删除分类关联
CategoryRelated::where('product_id', $id)->delete();
//删除关键词关联
KeywordRelated::where('product_id', $id)->delete();
}
DB::commit();
... ...
... ... @@ -61,8 +61,6 @@ class WebSettingServiceLogic extends BaseLogic
$info = $image->read(['hash'=>$this->param['hash']]);
if($info !== false){
$image->del(['id'=>$info['id']]);
//删除资源文件
shell_exec('rm -rf '.$info['path']);
}
DB::commit();
}catch (\Exception $e){
... ...
... ... @@ -70,18 +70,9 @@ class UserLogic extends BaseLogic
try {
//上传图片
if(isset($this->param['image']) && is_file($this->param['image'])){
//查看当前用户是否已有头像
$info = $this->model->read(['id'=>$this->param['id']],['id','image']);
if($info !== false && !empty($info['image'])){
//TODO::删除资源
$imageModel = new Image();
$image_info = $imageModel->read(['hash'=>$info['image']],['id','path']);
shell_exec('rm -rf '.$image_info['path'] .'./../uploads/images/cache_'. $info['image'] . '*');
$imageModel->del(['hash'=>$info['image']]);
}
$this->param['image'] = $this->upload();
$this->model->edits($this->param);
}
$this->model->edits($this->param);
}catch (\exception $e){
$this->fail('参数错误或其他服务器原因,编辑失败');
}
... ...
<?php
namespace App\Http\Controllers;
use App\Enums\Common\Code;
use App\Models\Image as ImageModel;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
class ImageLogic
{
public $path = '';
public $request = '';
public function __construct(Request $request)
{
$this->request = $request;
$this->path = config('filesystems.disks')['upload']['root'].config('upload');
}
/**
* 图片上传
*/
public function upload() {
$this->request->validate([
'image'=>['required'],
],[
'image.required'=>'图片必须填写',
]);
$files = $this->request->file('image');
if (empty($files)) {
$this->response('没有上传的文件!', 400);
}
$type = $this->request->post('type', 'single');
if ($type == 'multi') {
return $this->multi($files);
} else {
return $this->single($files);
}
}
/**
* @name :上传图片
* @return void
* @author :liyuhang
* @method
*/
public function single($files){
$hash = hash_file('md5', $files->getPathname());
$url = $this->path;
$filename = date('ymdHis').rand(10000,99999);
$res = $this->request->file('image')->move($url,$filename);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
$imageModel = new ImageModel();
$data = [
'path' => $url.$filename,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $hash.$filename,
'type'=>$files->getClientOriginalExtension(),
];
$rs = $imageModel->add($data);
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
return $hash.$filename;
}
/**
* @name :(删除资源及对应表数据)del
* @author :lyh
* @method :post
* @time :2023/5/4 14:57
*/
public function del($hash){
DB::beginTransaction();
$imageModel = new ImageModel();
try {
if(is_array($hash)){
foreach ($hash as $k => $v){
$this->del($v);
}
}else{
$info = $imageModel->read(['hash'=>$hash]);
if($info !== false){
shell_exec('sudo rm -rf '.$info['path']. ' '.$this->path . $info['hash'] .'*');
}
}
$imageModel->del(['hash'=>['in',$hash]]);
DB::commit();
}catch (\Exception $e){
DB::rollBack();
$this->response('error',Code::USER_ERROR);
}
}
/**
* 生成缩略图缓存
* @param type $info
* @param type $w
* @param type $h
* @return string
*/
private function cacheImage($info, $w, $h) {
$path = $info['path'];
$filename = $this->path . $info['hash'] . $w . '_' . $h;
Image::make($path)->resize($w, $h)->save($filename);
return $filename;
}
/**
* 多图片上传
* @param type $files file对象集合
* @return type
*/
private function multi($files) {
if (!is_array($files)) {
$files = [$files];
}
$save_data = [];
$data = [];
foreach ($files as $file) {
$hash = hash_file('md5', $file->getPathname());
$url = $this->path;
$filename = date('ymdHis').rand(10000,99999);
$res = $file->move($url,$filename);
if ($res === false) {
return $this->response($file->getError(), Code::USER_ERROR);
}
$save_data[] = [
'path' => $url.$filename,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $hash.$filename,
'type'=>$files->getClientOriginalExtension(),
];
$data[] = $hash.$filename;
}
$imageModel = new ImageModel();
$imageModel->insert($save_data);
return $data;
}
//下载
public function download($filename){
$path = Storage::path($filename);
return response()->download($path,time().rand(1,100000));
}
/**
* @name 统一返回参数
* @return JsonResponse
* @author :liyuhang
* @method
*/
public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
{
$code = Code::fromValue($code);
$result = [
'msg' => $msg == ' ' ? $code->description : $msg,
'code' => $code->value,
'data' => $data,
];
$this->header['Content-Type'] = $type;
$response = response($result,$result_code,$this->header);
throw new HttpResponseException($response);
}
}
... ... @@ -31,8 +31,16 @@ class AttrRequest extends FormRequest
{
return [
'title' => 'required|max:50',
'remark' => 'max:200',
'values' => 'required|array'
'attrs' => ['required', 'array', function ($attribute, $value, $fail) {
foreach ($value as $v) {
if (empty($v['key'])) {
$fail('属性名不能为空');
}
if (empty($v['value'])) {
$fail('属性值不能为空');
}
}
}]
];
}
... ... @@ -41,9 +49,8 @@ class AttrRequest extends FormRequest
return [
'title.required' => '请输入参数名称',
'title.max' => '参数名称不能超过50个字符',
'remark.max' => '备注不能超过200个字符',
'values.required' => '请添加参数值',
'values.array' => '参数值格式异常',
'attrs.required' => '请添加属性参数',
'attrs.array' => '属性参数格式异常',
];
}
... ...
... ... @@ -2,6 +2,7 @@
namespace App\Models\Product;
use App\Helper\Arr;
use App\Models\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
... ... @@ -9,12 +10,24 @@ class Attr extends Base
{
use SoftDeletes;
protected $appends = ['attr_num'];
//设置关联表名
protected $table = 'gl_product_attr';
public function values()
public function setAttrsAttribute($value)
{
$this->attributes['attrs'] = Arr::a2s($value);
}
public function getAttrsAttribute($value)
{
return Arr::s2a($value);
}
public function getAttrNumAttribute()
{
return $this->hasMany(AttrValue::class, 'attr_id', 'id')->orderBy('id');
return count($this->attrs);
}
}
... ...
<?php
namespace App\Models\Product;
use App\Models\Base;
class AttrValue extends Base
{
//设置关联表名
protected $table = 'gl_product_attr_value';
}
<?php
namespace App\Models\Product;
use App\Helper\Arr;
use App\Models\Base;
class KeywordRelated extends Base
{
//设置关联表名
protected $table = 'gl_product_keyword_related';
const CREATED_AT = null;
const UPDATED_AT = null;
/**
* 关联产品关键词
* @param $product_id
* @param $keyword_ids
* @author zbj
* @date 2023/5/4
*/
public static function saveRelated($product_id, $keyword_ids)
{
if(!is_array($keyword_ids)){
$keyword_ids = array_filter(Arr::splitFilterToArray($keyword_ids), 'intval');
}
//先删除
self::where('product_id', $product_id)->delete();
//批量保存
$data = [];
foreach ($keyword_ids as $keyword_id){
$data[] = [
'product_id' => $product_id,
'keyword_id' => $keyword_id
];
}
self::insert($data);
}
}
... ...