From 2037d7f549c368d7ae3b521ab955fe5054bbcce7 Mon Sep 17 00:00:00 2001 From: liapples Date: Mon, 23 Aug 2021 12:01:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=8A=A0=E5=9F=9F=E5=90=8D?= =?UTF-8?q?=E5=89=8D=E7=BC=80=E5=8F=8A=E5=8E=BB=E6=8E=89=E5=8E=9F=E6=9D=A5?= =?UTF-8?q?=E7=9A=84=E8=AE=BF=E9=97=AE=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/AgentProductController.php | 7 +++-- .../Controllers/Api/ArticleController.php | 6 +++- app/Http/Controllers/Api/IndexController.php | 29 ++++++++++++++++--- app/Http/Controllers/Api/OrderController.php | 22 ++++++++++++-- .../Controllers/Api/UserFavController.php | 20 +++++++++++++ app/Models/Advertising.php | 21 -------------- app/Models/Agent.php | 22 -------------- app/Models/AgentProductItem.php | 2 ++ app/Models/Article.php | 11 ------- app/Models/BaseModel.php | 8 ----- app/Models/Channel.php | 11 ------- app/Models/Order.php | 4 ++- app/Models/Supplier.php | 10 ------- 13 files changed, 79 insertions(+), 94 deletions(-) delete mode 100644 app/Models/Advertising.php diff --git a/app/Http/Controllers/Api/AgentProductController.php b/app/Http/Controllers/Api/AgentProductController.php index 38705c7..09b1ebf 100644 --- a/app/Http/Controllers/Api/AgentProductController.php +++ b/app/Http/Controllers/Api/AgentProductController.php @@ -25,8 +25,9 @@ class AgentProductController extends Controller $where['agent_id'] = $this->agent_id; $list = AgentProduct::list()->where($where)->simplePaginate(); + $list = $this->paginatePicAddHost($list); - return $this->success($this->paginatePicAddHost($list)); + return $this->success($list); } // 产品详情 @@ -91,7 +92,9 @@ class AgentProductController extends Controller if (!$list->isEmpty()) { $prefix = Storage::disk('public')->url(''); foreach ($list->items() as $k=>&$v) { - $v->product->pictures = array_map(fn($item) => ($prefix . $item), $v->product->pictures); + $v->product->pictures = array_map(function($item) use ($prefix) { + return strpos($item, $prefix) === false ? $prefix . $item : $item; + }, $v->product->pictures); } } return $list; diff --git a/app/Http/Controllers/Api/ArticleController.php b/app/Http/Controllers/Api/ArticleController.php index da083da..2843243 100644 --- a/app/Http/Controllers/Api/ArticleController.php +++ b/app/Http/Controllers/Api/ArticleController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\Article; +use Illuminate\Support\Facades\Storage; /** * 文章 @@ -23,7 +24,9 @@ class ArticleController extends Controller ->simplePaginate()->toArray(); $new_data = []; - foreach ($list['data'] as $k => $item) { + $prefix = Storage::disk('public')->url(''); + foreach ($list['data'] as $k => &$item) { + $item['image'] = $prefix . $item['image']; $new_key = floor($k / 4); if ($k % 4 == 0) { $new_data[$new_key] = [ @@ -47,6 +50,7 @@ class ArticleController extends Controller if (!$article) { return $this->error('文章不存在或已被删除'); } + $article->image = Storage::disk('public')->url('') . $article->image; return $this->success($article); } } diff --git a/app/Http/Controllers/Api/IndexController.php b/app/Http/Controllers/Api/IndexController.php index ec47bb7..118a776 100644 --- a/app/Http/Controllers/Api/IndexController.php +++ b/app/Http/Controllers/Api/IndexController.php @@ -20,10 +20,18 @@ class IndexController extends Controller // 首页数据大杂烩 public function index() { - # 轮播图 + $img_prefix = Storage::disk('public')->url(''); + + # 轮播图 $slide = Slide::where(['agent_id' => $this->agent_id, 'status' => 1]) ->orderBy('sort')->orderBy('id', 'DESC')->limit(10) ->get(['title', 'picture', 'type', 'url']); + foreach ($slide as &$v) { + $v->picture = $img_prefix . $v->picture; + if ($v->type == 0) { + $v->url = $v->url ? '/pages/goodsDetail/index?goods_id=' . $v->url : ''; + } + } # 公告 $notice = Notice::where('agent_id', $this->agent_id)->limit(10)->get(['id', 'title', 'updated_at']); @@ -58,6 +66,11 @@ class IndexController extends Controller ->limit(8) ->get(['id', 'name', 'icon']); } + if (!$my_channels->isEmpty()) { + foreach ($my_channels as &$v) { + $v->icon = $img_prefix . $v->icon; + } + } # 专题列表 $special = Special::query() @@ -65,9 +78,8 @@ class IndexController extends Controller ->orderBy('sort')->orderBy('id') ->limit(6)->get(['id', 'picture_ad']); if (!$special->isEmpty()) { - $prefix = Storage::disk('public')->url(''); foreach ($special as $k=>&$v) { - $v->picture_ad = $prefix . $v->picture_ad; + $v->picture_ad = $img_prefix . $v->picture_ad; } } @@ -77,8 +89,17 @@ class IndexController extends Controller ->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with ->with('product:id,title,pictures') //必须查询ID才能正常查询 ->limit(6)->get(); + if (!$hots->isEmpty()) { + foreach ($hots as &$v) { + if (!empty($v->product->pictures) && is_array($v->product->pictures)) { + $v->product->pictures = array_map(function($item) use ($img_prefix) { + return strpos($item, $img_prefix) === false ? $img_prefix . $item : $item; + }, $v->product->pictures); + } + } + } - return $this->success([ + return $this->success([ 'slide' => $slide, 'notice' => $notice, 'my_channels' => $my_channels, diff --git a/app/Http/Controllers/Api/OrderController.php b/app/Http/Controllers/Api/OrderController.php index 0b7e4b0..6aa66c5 100644 --- a/app/Http/Controllers/Api/OrderController.php +++ b/app/Http/Controllers/Api/OrderController.php @@ -47,15 +47,31 @@ class OrderController extends Controller $order_list = Order::where($where) ->with('product:id,title,pictures') - ->select('id', 'agent_product_id', 'product_id', 'price', 'num', 'status', 'created_at') + ->select('id', 'agent_product_id', 'product_id', 'picture', 'price', 'num', 'status', 'created_at') ->orderBy('id', 'DESC') ->simplePaginate(15) ->toArray(); $time = time(); $timeout_ids = []; - //10分钟内未付款订单提示付款 + $prefix = Storage::disk('public')->url(''); foreach ($order_list['data'] as &$v) { + //图片加上域名 + if (strpos($v['picture'], $prefix) === false) { + $v['picture'] = $prefix . $v['picture']; + } + + if (strpos($v['product']['picture'], $prefix) === false) { + $v['product']['picture'] = $prefix . $v['product']['picture']; + } + + if (!empty($v['product']['pictures']) && is_array($v['product']['pictures'])) { + $v['product']['pictures'] = array_map(function($item) use ($prefix) { + return strpos($item, $prefix) === false ? $prefix . $item : $item; + }, $v['product']['pictures']); + } + + //10分钟内未付款订单提示付款 if ($v['status'] == Status::UNPAID) { $minute = $time - strtotime($v['created_at']); //订单创建后10分钟内未付款则提示,否则取消订单 @@ -338,7 +354,7 @@ class OrderController extends Controller } //订单ID和核销码拼接,查询时通过订单ID和核销码来查询,这样核销码不用建索引 - $order->verify_code = $order->id . '-' . $order->verify_code; + $order->verify_code = $order->verify_code ? $order->id . '-' . $order->verify_code : ''; $order->coupon = Coupon::query() ->whereIn('id', $order->coupon_id) diff --git a/app/Http/Controllers/Api/UserFavController.php b/app/Http/Controllers/Api/UserFavController.php index d71b5e3..b900971 100644 --- a/app/Http/Controllers/Api/UserFavController.php +++ b/app/Http/Controllers/Api/UserFavController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Models\AgentProduct; use App\Models\UserFav; +use Illuminate\Support\Facades\Storage; /** * 用户收藏 @@ -24,6 +25,25 @@ class UserFavController extends Controller ->select('id', 'agent_product_id', 'product_id', 'created_at') ->orderBy('id', 'DESC') ->simplePaginate(15); + if (!$list->isEmpty()) { + $list = $list->toArray(); + $prefix = Storage::disk('public')->url(''); + if ($list['data']) { + foreach ($list['data'] as &$v) { + if (!empty($v['product']['pictures'])) { + //单图 + if (strpos($v['product']['picture'], $prefix) === false) { + $v['product']['picture'] = $prefix . $v['product']['picture']; + } + + //多图 + $v['product']['pictures'] = array_map(function ($v) use ($prefix) { + return strpos($v, $prefix) === false ? $prefix . $v : $v; + }, $v['product']['pictures']); + } + } + } + } return $this->success($list); } diff --git a/app/Models/Advertising.php b/app/Models/Advertising.php deleted file mode 100644 index 787ac3a..0000000 --- a/app/Models/Advertising.php +++ /dev/null @@ -1,21 +0,0 @@ -host . $value : ''; - } - - public function setPictureAttribute($value) - { - //修改器里面$this->host变量为空 - $this->attributes['picture'] = str_replace(env('APP_URL'), '', $value); - } -} diff --git a/app/Models/Agent.php b/app/Models/Agent.php index 979e2a1..87e1555 100644 --- a/app/Models/Agent.php +++ b/app/Models/Agent.php @@ -9,28 +9,6 @@ class Agent extends BaseModel { use HasFactory, SoftDeletes; - public function getLogoAttribute($value): string - { - return $value ? $this->host . $value : ''; - } - - public function getLicensePicAttribute($value): string - { - return $value ? $this->host . $value : ''; - } - - public function setLogoAttribute($value) - { - //修改器里面$this->host变量为空 - $this->attributes['logo'] = str_replace(env('APP_URL'), '', $value); - } - - public function setLicensePicAttribute($value) - { - //修改器里面$this->host变量为空 - $this->attributes['license_pic'] = str_replace(env('APP_URL'), '', $value); - } - public function agentInfo() { return $this->hasOne(AgentInfo::class); diff --git a/app/Models/AgentProductItem.php b/app/Models/AgentProductItem.php index 5772ef5..de45a03 100644 --- a/app/Models/AgentProductItem.php +++ b/app/Models/AgentProductItem.php @@ -7,4 +7,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; class AgentProductItem extends BaseModel { use HasFactory; + + protected $guarded = ['id']; //不允许批量赋值的字段 } diff --git a/app/Models/Article.php b/app/Models/Article.php index 25efcad..66152a6 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -7,15 +7,4 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; class Article extends BaseModel { use HasFactory; - - public function getImageAttribute($value): string - { - return $value ? $this->host . $value : ''; - } - - public function setImageAttribute($value) - { - //修改器里面$this->host变量为空 - $this->attributes['image'] = str_replace(env('APP_URL'), '', $value); - } } diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index 4de43fb..4906b1f 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -9,14 +9,6 @@ class BaseModel extends Model { use HasDateTimeFormatter; - protected $host = ''; - - public function __construct(array $attributes = []) - { - parent::__construct($attributes); - $this->host = env('APP_URL'); - } - //默认按id desc排序 protected static function booted() { diff --git a/app/Models/Channel.php b/app/Models/Channel.php index 008cc02..f23230b 100644 --- a/app/Models/Channel.php +++ b/app/Models/Channel.php @@ -26,15 +26,4 @@ class Channel extends BaseModel parent::__construct($attributes); $this->timestamps = false; } - - public function getIconAttribute($value): string - { - return $value ? $this->host . $value : ''; - } - - public function setIconAttribute($value) - { - //修改器里面$this->host变量为空 - $this->attributes['license_pic'] = str_replace(env('APP_URL'), '', $value); - } } diff --git a/app/Models/Order.php b/app/Models/Order.php index 82bedab..1d247d2 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -35,7 +35,9 @@ class Order extends BaseModel if (!empty($value['pictures']) && is_array($value['pictures'])) { $prefix = Storage::disk('public')->url(''); foreach ($value['pictures'] as &$v) { - $v = $prefix . $v; + if ($v && strpos($v, $prefix) === false) { + $v = $prefix . $v; + } } } return $value ?? []; diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php index baea81e..0fc9f08 100644 --- a/app/Models/Supplier.php +++ b/app/Models/Supplier.php @@ -9,16 +9,6 @@ class Supplier extends BaseModel { use HasFactory, SoftDeletes; - public function getLogoAttribute($value): string - { - return $value ? $this->host . $value : ''; - } - - public function getLicensePicAttribute($value): string - { - return $value ? $this->host . $value : ''; - } - //密码修改 public function setPasswordAttribute($value) {