正在显示
9 个修改的文件
包含
252 行增加
和
12 行删除
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Http\Controllers\Bside\Product; | ||
| 4 | + | ||
| 5 | +use App\Helper\Arr; | ||
| 6 | +use App\Http\Controllers\Bside\BaseController; | ||
| 7 | +use App\Http\Logic\Bside\Product\AttrLogic; | ||
| 8 | +use App\Http\Requests\Bside\product\AttrRequest; | ||
| 9 | +use App\Rules\Ids; | ||
| 10 | +use Illuminate\Http\Request; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * Class AttrController | ||
| 14 | + * @package App\Http\Controllers\Bside | ||
| 15 | + * @author zbj | ||
| 16 | + * @date 2023/4/15 | ||
| 17 | + */ | ||
| 18 | +class AttrController extends BaseController | ||
| 19 | +{ | ||
| 20 | + | ||
| 21 | + public function index(AttrLogic $logic) | ||
| 22 | + { | ||
| 23 | + $map = []; | ||
| 24 | + if(!empty($this->param['search'])){ | ||
| 25 | + $map[] = ['title', 'like', "%{$this->param['search']}%"]; | ||
| 26 | + } | ||
| 27 | + $sort = ['id' => 'desc']; | ||
| 28 | + $data = $logic->getList($map, $sort, ['id', 'title', 'remark', 'value_num']); | ||
| 29 | + return $this->success($data); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + public function info(Request $request, AttrLogic $logic){ | ||
| 33 | + $request->validate([ | ||
| 34 | + 'id'=>'required' | ||
| 35 | + ],[ | ||
| 36 | + 'id.required' => 'ID不能为空' | ||
| 37 | + ]); | ||
| 38 | + $data = $logic->getInfo($this->param['id']); | ||
| 39 | + return $this->success(Arr::twoKeepKeys($data, ['id', 'title', 'remark', 'values'])); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + public function save(AttrRequest $request, AttrLogic $logic) | ||
| 43 | + { | ||
| 44 | + $data = $logic->save($this->param); | ||
| 45 | + return $this->success($data); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public function delete(Request $request, AttrLogic $logic) | ||
| 49 | + { | ||
| 50 | + $request->validate([ | ||
| 51 | + 'ids'=>['required', new Ids()] | ||
| 52 | + ],[ | ||
| 53 | + 'ids.required' => 'ID不能为空' | ||
| 54 | + ]); | ||
| 55 | + | ||
| 56 | + $data = $logic->delete($this->param['ids']); | ||
| 57 | + return $this->success($data); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | +} |
| @@ -87,6 +87,17 @@ class BaseLogic | @@ -87,6 +87,17 @@ class BaseLogic | ||
| 87 | */ | 87 | */ |
| 88 | public function getInfo($id) | 88 | public function getInfo($id) |
| 89 | { | 89 | { |
| 90 | + $info = $this->getCacheInfo($id); | ||
| 91 | + return $this->success($info->toArray()); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * @param $id | ||
| 96 | + * @return mixed | ||
| 97 | + * @author zbj | ||
| 98 | + * @date 2023/4/15 | ||
| 99 | + */ | ||
| 100 | + public function getCacheInfo($id){ | ||
| 90 | if($this->is_cache){ | 101 | if($this->is_cache){ |
| 91 | $info = Cache::get($this->getInfoCacheKey($id)); | 102 | $info = Cache::get($this->getInfoCacheKey($id)); |
| 92 | if (!$info) { | 103 | if (!$info) { |
| @@ -98,7 +109,7 @@ class BaseLogic | @@ -98,7 +109,7 @@ class BaseLogic | ||
| 98 | }else{ | 109 | }else{ |
| 99 | $info = $this->model->find($id); | 110 | $info = $this->model->find($id); |
| 100 | } | 111 | } |
| 101 | - return $this->success($info->toArray()); | 112 | + return $info; |
| 102 | } | 113 | } |
| 103 | 114 | ||
| 104 | /** | 115 | /** |
| @@ -110,23 +121,25 @@ class BaseLogic | @@ -110,23 +121,25 @@ class BaseLogic | ||
| 110 | * @date 2023/4/13 | 121 | * @date 2023/4/13 |
| 111 | */ | 122 | */ |
| 112 | public function save($param){ | 123 | public function save($param){ |
| 113 | - if(empty($param['id'])){ | ||
| 114 | - $param['created_at'] = $param['updated_at'] = date('Y-m-d H:i:s'); | ||
| 115 | - $res = $this->model->insert($param); | ||
| 116 | - }else{ | ||
| 117 | - $info = $this->getInfo($param['id']); | ||
| 118 | - if(!$info){ | 124 | + if(!empty($param['id'])){ |
| 125 | + $this->model = $this->getCacheInfo($param['id']); | ||
| 126 | + if(!$this->model){ | ||
| 119 | $this->fail('数据不存在或者已经删除'); | 127 | $this->fail('数据不存在或者已经删除'); |
| 120 | } | 128 | } |
| 121 | - $param['updated_at'] = date('Y-m-d H:i:s'); | ||
| 122 | - $res = $this->model->where('id', $param['id'])->update($param); | ||
| 123 | } | 129 | } |
| 130 | + | ||
| 131 | + foreach ($param as $name => $value){ | ||
| 132 | + $this->model[$name] = $value; | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + $res = $this->model->save(); | ||
| 136 | + | ||
| 124 | if($res){ | 137 | if($res){ |
| 125 | //清缓存 | 138 | //清缓存 |
| 126 | if($this->is_cache && !empty($param['id'])){ | 139 | if($this->is_cache && !empty($param['id'])){ |
| 127 | Cache::forget($this->getInfoCacheKey($param['id'])); | 140 | Cache::forget($this->getInfoCacheKey($param['id'])); |
| 128 | } | 141 | } |
| 129 | - return $this->success(); | 142 | + return $this->success(['id' => $this->model->id]); //返回保存的数据id |
| 130 | }else{ | 143 | }else{ |
| 131 | $this->fail('保存失败'); | 144 | $this->fail('保存失败'); |
| 132 | } | 145 | } |
app/Http/Logic/Bside/Product/AttrLogic.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Http\Logic\Bside\Product; | ||
| 4 | + | ||
| 5 | +use App\Helper\Arr; | ||
| 6 | +use App\Http\Logic\Bside\BaseLogic; | ||
| 7 | +use App\Models\Product\Attr; | ||
| 8 | +use App\Models\Product\AttrValue; | ||
| 9 | +use Illuminate\Support\Facades\DB; | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * Class AttrLogic | ||
| 13 | + * @package App\Http\Logic\Bside\Product | ||
| 14 | + * @author zbj | ||
| 15 | + * @date 2023/4/15 | ||
| 16 | + */ | ||
| 17 | +class AttrLogic extends BaseLogic | ||
| 18 | +{ | ||
| 19 | + public function __construct() | ||
| 20 | + { | ||
| 21 | + parent::__construct(); | ||
| 22 | + | ||
| 23 | + $this->model = new Attr(); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + public function getInfo($id){ | ||
| 27 | + $info = parent::getCacheInfo($id); | ||
| 28 | + $info->values; | ||
| 29 | + return $this->success($info->toArray()); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + public function save($param){ | ||
| 33 | + $param['values'] = array_unique($param['values']); | ||
| 34 | + DB::beginTransaction(); | ||
| 35 | + try { | ||
| 36 | + //删除之前的参数值 | ||
| 37 | + if(!empty($param['id'])){ | ||
| 38 | + AttrValue::where('attr_id', $param['id'])->delete(); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + //保存参数名称 | ||
| 42 | + $data = $param; | ||
| 43 | + unset($data['values']); | ||
| 44 | + $data['value_num'] = count($param['values']); | ||
| 45 | + $res = parent::save($data); | ||
| 46 | + $attr_id = $res['id']; | ||
| 47 | + | ||
| 48 | + //保存参数值 | ||
| 49 | + $values = []; | ||
| 50 | + foreach ($param['values'] as $value){ | ||
| 51 | + $values[] = [ | ||
| 52 | + 'attr_id' => $attr_id, | ||
| 53 | + 'value' => $value | ||
| 54 | + ]; | ||
| 55 | + } | ||
| 56 | + AttrValue::insert($values); | ||
| 57 | + | ||
| 58 | + DB::commit(); | ||
| 59 | + }catch (\Exception $e){ | ||
| 60 | + DB::rollBack(); | ||
| 61 | + $this->fail('保存失败'); | ||
| 62 | + } | ||
| 63 | + return $this->success(); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + public function delete($ids){ | ||
| 67 | + $ids= array_filter(Arr::splitFilterToArray($ids), 'intval'); | ||
| 68 | + foreach ($ids as $id){ | ||
| 69 | + $info = $this->getCacheInfo($id); | ||
| 70 | + if(!$info){ | ||
| 71 | + continue; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + //todo 是否有关联商品 | ||
| 75 | + } | ||
| 76 | + return parent::delete($ids); | ||
| 77 | + } | ||
| 78 | +} |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Http\Requests\Bside\product; | ||
| 4 | + | ||
| 5 | +use Illuminate\Foundation\Http\FormRequest; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * Class AttrRequest | ||
| 9 | + * @package App\Http\Requests\Bside\product | ||
| 10 | + * @author zbj | ||
| 11 | + * @date 2023/4/15 | ||
| 12 | + */ | ||
| 13 | +class AttrRequest extends FormRequest | ||
| 14 | +{ | ||
| 15 | + /** | ||
| 16 | + * Determine if the user is authorized to make this request. | ||
| 17 | + * | ||
| 18 | + * @return bool | ||
| 19 | + */ | ||
| 20 | + public function authorize() | ||
| 21 | + { | ||
| 22 | + return true; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * Get the validation rules that apply to the request. | ||
| 27 | + * | ||
| 28 | + * @return array | ||
| 29 | + */ | ||
| 30 | + public function rules() | ||
| 31 | + { | ||
| 32 | + return [ | ||
| 33 | + 'title' => 'required|max:30', | ||
| 34 | + 'remark' => 'max:200', | ||
| 35 | + 'values' => 'required|array' | ||
| 36 | + ]; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + public function messages() | ||
| 40 | + { | ||
| 41 | + return [ | ||
| 42 | + 'title.required' => '请输入参数名称', | ||
| 43 | + 'title.max' => '参数名称不能超过30个字符', | ||
| 44 | + 'remark.max' => '备注不能超过200个字符', | ||
| 45 | + 'values.required' => '请添加参数值', | ||
| 46 | + 'values.array' => '参数值格式异常', | ||
| 47 | + ]; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | +} |
app/Models/Product/Attr.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Models\Product; | ||
| 4 | + | ||
| 5 | +use App\Models\Base; | ||
| 6 | +use Illuminate\Database\Eloquent\SoftDeletes; | ||
| 7 | + | ||
| 8 | +class Attr extends Base | ||
| 9 | +{ | ||
| 10 | + use SoftDeletes; | ||
| 11 | + | ||
| 12 | + //设置关联表名 | ||
| 13 | + protected $table = 'gl_product_attr'; | ||
| 14 | + | ||
| 15 | + public function values() | ||
| 16 | + { | ||
| 17 | + return $this->hasMany(AttrValue::class, 'attr_id', 'id')->orderBy('id'); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | +} |
app/Models/Product/AttrValue.php
0 → 100644
| @@ -46,5 +46,11 @@ Route::group([], function () { | @@ -46,5 +46,11 @@ Route::group([], function () { | ||
| 46 | Route::get('keyword/info', [\App\Http\Controllers\Bside\Product\KeywordController::class, 'info'])->name('product_keyword_info'); | 46 | Route::get('keyword/info', [\App\Http\Controllers\Bside\Product\KeywordController::class, 'info'])->name('product_keyword_info'); |
| 47 | Route::post('keyword/save', [\App\Http\Controllers\Bside\Product\KeywordController::class, 'save'])->name('product_keyword_save'); | 47 | Route::post('keyword/save', [\App\Http\Controllers\Bside\Product\KeywordController::class, 'save'])->name('product_keyword_save'); |
| 48 | Route::any('keyword/delete', [\App\Http\Controllers\Bside\Product\KeywordController::class, 'delete'])->name('product_keyword_delete'); | 48 | Route::any('keyword/delete', [\App\Http\Controllers\Bside\Product\KeywordController::class, 'delete'])->name('product_keyword_delete'); |
| 49 | + | ||
| 50 | + //产品参数 | ||
| 51 | + Route::get('attr', [\App\Http\Controllers\Bside\Product\AttrController::class, 'index'])->name('product_attr'); | ||
| 52 | + Route::get('attr/info', [\App\Http\Controllers\Bside\Product\AttrController::class, 'info'])->name('product_attr_info'); | ||
| 53 | + Route::post('attr/save', [\App\Http\Controllers\Bside\Product\AttrController::class, 'save'])->name('product_attr_save'); | ||
| 54 | + Route::any('attr/delete', [\App\Http\Controllers\Bside\Product\AttrController::class, 'delete'])->name('product_attr_delete'); | ||
| 49 | }); | 55 | }); |
| 50 | }); | 56 | }); |
-
请 注册 或 登录 后发表评论