Browse Source

图片加域名前缀及去掉原来的访问器

dev
李可松 4 years ago
parent
commit
2037d7f549
  1. 7
      app/Http/Controllers/Api/AgentProductController.php
  2. 6
      app/Http/Controllers/Api/ArticleController.php
  3. 29
      app/Http/Controllers/Api/IndexController.php
  4. 22
      app/Http/Controllers/Api/OrderController.php
  5. 20
      app/Http/Controllers/Api/UserFavController.php
  6. 21
      app/Models/Advertising.php
  7. 22
      app/Models/Agent.php
  8. 2
      app/Models/AgentProductItem.php
  9. 11
      app/Models/Article.php
  10. 8
      app/Models/BaseModel.php
  11. 11
      app/Models/Channel.php
  12. 4
      app/Models/Order.php
  13. 10
      app/Models/Supplier.php

7
app/Http/Controllers/Api/AgentProductController.php

@ -25,8 +25,9 @@ class AgentProductController extends Controller
$where['agent_id'] = $this->agent_id; $where['agent_id'] = $this->agent_id;
$list = AgentProduct::list()->where($where)->simplePaginate(); $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()) { if (!$list->isEmpty()) {
$prefix = Storage::disk('public')->url(''); $prefix = Storage::disk('public')->url('');
foreach ($list->items() as $k=>&$v) { 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; return $list;

6
app/Http/Controllers/Api/ArticleController.php

@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\Article; use App\Models\Article;
use Illuminate\Support\Facades\Storage;
/** /**
* 文章 * 文章
@ -23,7 +24,9 @@ class ArticleController extends Controller
->simplePaginate()->toArray(); ->simplePaginate()->toArray();
$new_data = []; $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); $new_key = floor($k / 4);
if ($k % 4 == 0) { if ($k % 4 == 0) {
$new_data[$new_key] = [ $new_data[$new_key] = [
@ -47,6 +50,7 @@ class ArticleController extends Controller
if (!$article) { if (!$article) {
return $this->error('文章不存在或已被删除'); return $this->error('文章不存在或已被删除');
} }
$article->image = Storage::disk('public')->url('') . $article->image;
return $this->success($article); return $this->success($article);
} }
} }

29
app/Http/Controllers/Api/IndexController.php

@ -20,10 +20,18 @@ class IndexController extends Controller
// 首页数据大杂烩 // 首页数据大杂烩
public function index() public function index()
{ {
# 轮播图
$img_prefix = Storage::disk('public')->url('');
# 轮播图
$slide = Slide::where(['agent_id' => $this->agent_id, 'status' => 1]) $slide = Slide::where(['agent_id' => $this->agent_id, 'status' => 1])
->orderBy('sort')->orderBy('id', 'DESC')->limit(10) ->orderBy('sort')->orderBy('id', 'DESC')->limit(10)
->get(['title', 'picture', 'type', 'url']); ->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']); $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) ->limit(8)
->get(['id', 'name', 'icon']); ->get(['id', 'name', 'icon']);
} }
if (!$my_channels->isEmpty()) {
foreach ($my_channels as &$v) {
$v->icon = $img_prefix . $v->icon;
}
}
# 专题列表 # 专题列表
$special = Special::query() $special = Special::query()
@ -65,9 +78,8 @@ class IndexController extends Controller
->orderBy('sort')->orderBy('id') ->orderBy('sort')->orderBy('id')
->limit(6)->get(['id', 'picture_ad']); ->limit(6)->get(['id', 'picture_ad']);
if (!$special->isEmpty()) { if (!$special->isEmpty()) {
$prefix = Storage::disk('public')->url('');
foreach ($special as $k=>&$v) { 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 ->select('id', 'sale', 'product_id', 'price', 'original_price') //必须查询product_id才能with
->with('product:id,title,pictures') //必须查询ID才能正常查询 ->with('product:id,title,pictures') //必须查询ID才能正常查询
->limit(6)->get(); ->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, 'slide' => $slide,
'notice' => $notice, 'notice' => $notice,
'my_channels' => $my_channels, 'my_channels' => $my_channels,

22
app/Http/Controllers/Api/OrderController.php

@ -47,15 +47,31 @@ class OrderController extends Controller
$order_list = Order::where($where) $order_list = Order::where($where)
->with('product:id,title,pictures') ->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') ->orderBy('id', 'DESC')
->simplePaginate(15) ->simplePaginate(15)
->toArray(); ->toArray();
$time = time(); $time = time();
$timeout_ids = []; $timeout_ids = [];
//10分钟内未付款订单提示付款
$prefix = Storage::disk('public')->url('');
foreach ($order_list['data'] as &$v) { 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) { if ($v['status'] == Status::UNPAID) {
$minute = $time - strtotime($v['created_at']); $minute = $time - strtotime($v['created_at']);
//订单创建后10分钟内未付款则提示,否则取消订单 //订单创建后10分钟内未付款则提示,否则取消订单
@ -338,7 +354,7 @@ class OrderController extends Controller
} }
//订单ID和核销码拼接,查询时通过订单ID和核销码来查询,这样核销码不用建索引 //订单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() $order->coupon = Coupon::query()
->whereIn('id', $order->coupon_id) ->whereIn('id', $order->coupon_id)

20
app/Http/Controllers/Api/UserFavController.php

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Models\AgentProduct; use App\Models\AgentProduct;
use App\Models\UserFav; 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') ->select('id', 'agent_product_id', 'product_id', 'created_at')
->orderBy('id', 'DESC') ->orderBy('id', 'DESC')
->simplePaginate(15); ->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); return $this->success($list);
} }

21
app/Models/Advertising.php

@ -1,21 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Advertising extends BaseModel
{
use HasFactory;
public function getPictureAttribute($value)
{
return $value ? $this->host . $value : '';
}
public function setPictureAttribute($value)
{
//修改器里面$this->host变量为空
$this->attributes['picture'] = str_replace(env('APP_URL'), '', $value);
}
}

22
app/Models/Agent.php

@ -9,28 +9,6 @@ class Agent extends BaseModel
{ {
use HasFactory, SoftDeletes; 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() public function agentInfo()
{ {
return $this->hasOne(AgentInfo::class); return $this->hasOne(AgentInfo::class);

2
app/Models/AgentProductItem.php

@ -7,4 +7,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
class AgentProductItem extends BaseModel class AgentProductItem extends BaseModel
{ {
use HasFactory; use HasFactory;
protected $guarded = ['id']; //不允许批量赋值的字段
} }

11
app/Models/Article.php

@ -7,15 +7,4 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
class Article extends BaseModel class Article extends BaseModel
{ {
use HasFactory; 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);
}
} }

8
app/Models/BaseModel.php

@ -9,14 +9,6 @@ class BaseModel extends Model
{ {
use HasDateTimeFormatter; use HasDateTimeFormatter;
protected $host = '';
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->host = env('APP_URL');
}
//默认按id desc排序 //默认按id desc排序
protected static function booted() protected static function booted()
{ {

11
app/Models/Channel.php

@ -26,15 +26,4 @@ class Channel extends BaseModel
parent::__construct($attributes); parent::__construct($attributes);
$this->timestamps = false; $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);
}
} }

4
app/Models/Order.php

@ -35,7 +35,9 @@ class Order extends BaseModel
if (!empty($value['pictures']) && is_array($value['pictures'])) { if (!empty($value['pictures']) && is_array($value['pictures'])) {
$prefix = Storage::disk('public')->url(''); $prefix = Storage::disk('public')->url('');
foreach ($value['pictures'] as &$v) { foreach ($value['pictures'] as &$v) {
$v = $prefix . $v;
if ($v && strpos($v, $prefix) === false) {
$v = $prefix . $v;
}
} }
} }
return $value ?? []; return $value ?? [];

10
app/Models/Supplier.php

@ -9,16 +9,6 @@ class Supplier extends BaseModel
{ {
use HasFactory, SoftDeletes; 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) public function setPasswordAttribute($value)
{ {

Loading…
Cancel
Save