From 6ecdc00dec395196c09f42e1487b4c84599fef67 Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Thu, 19 May 2022 10:06:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Controllers/UserVipController.php | 2 +- .../Http/Controllers/ArticleController.php | 59 +----- .../Controllers/ArticleController.php.bak | 187 ++++++++++++++++++ .../Api/Http/Controllers/VipController.php | 4 +- app/Endpoint/Api/routes.php | 24 ++- app/Models/ArticleCategory.php | 10 + app/Models/UserVip.php | 10 + app/Models/Vip.php | 8 + 8 files changed, 241 insertions(+), 63 deletions(-) create mode 100644 app/Endpoint/Api/Http/Controllers/ArticleController.php.bak diff --git a/app/Admin/Controllers/UserVipController.php b/app/Admin/Controllers/UserVipController.php index 33e3b1fb..4364adab 100644 --- a/app/Admin/Controllers/UserVipController.php +++ b/app/Admin/Controllers/UserVipController.php @@ -19,7 +19,7 @@ class UserVipController extends AdminController { return Grid::make(UserVip::with(['user', 'vip']), function (Grid $grid) { - $grid->model()->where('status', 1); + $grid->model()->where('status', UserVip::STATUS_SUCCESS); $grid->column('user.phone'); $grid->column('vip.name'); diff --git a/app/Endpoint/Api/Http/Controllers/ArticleController.php b/app/Endpoint/Api/Http/Controllers/ArticleController.php index 675a6df2..8088050f 100644 --- a/app/Endpoint/Api/Http/Controllers/ArticleController.php +++ b/app/Endpoint/Api/Http/Controllers/ArticleController.php @@ -34,25 +34,11 @@ class ArticleController extends Controller ]); } - /** - * - * - * @param \Illuminate\Http\Request $request - * @return void - */ public function index(Request $request) { - $cate = (string) $request->query('cate'); + $categoryId = (string) $request->query('cate'); $key = (string) $request->query('key'); - $categoryId = Arr::get([ - 'help'=> app_settings('app.article_help'), - 'agreement'=> app_settings('app.article_agreement'), - 'health'=> app_settings('app.article_health'), - ], $cate); - $query = Article::query()->with(['likesInfo'=>function ($q) use ($request) { - $user_id = $request->user()?->id; - return $q->where('user_id', $user_id ?? 0); - }])->where('is_show', 1); + $query = Article::where('is_show', 1); if ($categoryId) { $query->where('category_id', $categoryId); } @@ -68,46 +54,19 @@ class ArticleController extends Controller return ArticleSimpleResource::collection($list); } - public function healthCategory(Request $request) + public function category() { - $key = (string) $request->query('key'); - $categoryId = app_settings('app.article_health'); - $query = ArticleCategory::query()->where('parent_id', $categoryId)->where('is_show', 1); + $list = ArticleCategory::whereNull('parent_id')->show()->sort()->get(); - if ($key) { - $query->where('name', 'like', '%'.$key.'%'); - } - - $query->orderBy('is_recommend', 'desc'); - $query->orderBy('sort', 'desc'); - $query->orderBy('created_at', 'desc'); - - $categories = $query->get(); - - return ArticleCategoryResource::collection($categories); + return ArticleCategoryResource::collection($list); } - public function articleList($id, Request $request) + public function categoryTree() { - $key = (string) $request->query('key'); - $categoryId = $id; - $query = Article::query()->with(['likesInfo'=>function ($q) use ($request) { - $user_id = $request->user()?->id; - return $q->where('user_id', $user_id ?? 0); - }])->where('is_show', 1); - if ($categoryId) { - $query->where('category_id', $categoryId); - } - if ($key) { - $query->where('title', 'like', '%'.$key.'%'); - } + $nodes = ArticleCategory::select(['id', 'cover', 'name', 'parent_id', 'sort'])->get(); + $list = (new ArticleCategory())->toTree($nodes->all()); - $query->orderBy('is_recommend', 'desc'); - $query->orderBy('sort', 'desc'); - $query->orderBy('created_at', 'desc'); - - $list = $query->simplePaginate(PaginatorHelper::resolvePerPage('per_page', 20, 50)); - return ArticleSimpleResource::collection($list); + return $list; } /** diff --git a/app/Endpoint/Api/Http/Controllers/ArticleController.php.bak b/app/Endpoint/Api/Http/Controllers/ArticleController.php.bak new file mode 100644 index 00000000..675a6df2 --- /dev/null +++ b/app/Endpoint/Api/Http/Controllers/ArticleController.php.bak @@ -0,0 +1,187 @@ +json([ + 'about_us'=>app_settings('app.article_about_us', ''), + 'user_promotion_agreement'=> app_settings('app.article_user_promotion_agreement', ''), + 'user_hide_agreement' => app_settings('app.article_user_hide_agreement', ''), + ]); + } + + /** + * + * + * @param \Illuminate\Http\Request $request + * @return void + */ + public function index(Request $request) + { + $cate = (string) $request->query('cate'); + $key = (string) $request->query('key'); + $categoryId = Arr::get([ + 'help'=> app_settings('app.article_help'), + 'agreement'=> app_settings('app.article_agreement'), + 'health'=> app_settings('app.article_health'), + ], $cate); + $query = Article::query()->with(['likesInfo'=>function ($q) use ($request) { + $user_id = $request->user()?->id; + return $q->where('user_id', $user_id ?? 0); + }])->where('is_show', 1); + if ($categoryId) { + $query->where('category_id', $categoryId); + } + if ($key) { + $query->where('title', 'like', '%'.$key.'%'); + } + + $query->orderBy('is_recommend', 'desc'); + $query->orderBy('sort', 'desc'); + $query->orderBy('created_at', 'desc'); + + $list = $query->simplePaginate(PaginatorHelper::resolvePerPage('per_page', 20, 50)); + return ArticleSimpleResource::collection($list); + } + + public function healthCategory(Request $request) + { + $key = (string) $request->query('key'); + $categoryId = app_settings('app.article_health'); + $query = ArticleCategory::query()->where('parent_id', $categoryId)->where('is_show', 1); + + if ($key) { + $query->where('name', 'like', '%'.$key.'%'); + } + + $query->orderBy('is_recommend', 'desc'); + $query->orderBy('sort', 'desc'); + $query->orderBy('created_at', 'desc'); + + $categories = $query->get(); + + return ArticleCategoryResource::collection($categories); + } + + public function articleList($id, Request $request) + { + $key = (string) $request->query('key'); + $categoryId = $id; + $query = Article::query()->with(['likesInfo'=>function ($q) use ($request) { + $user_id = $request->user()?->id; + return $q->where('user_id', $user_id ?? 0); + }])->where('is_show', 1); + if ($categoryId) { + $query->where('category_id', $categoryId); + } + if ($key) { + $query->where('title', 'like', '%'.$key.'%'); + } + + $query->orderBy('is_recommend', 'desc'); + $query->orderBy('sort', 'desc'); + $query->orderBy('created_at', 'desc'); + + $list = $query->simplePaginate(PaginatorHelper::resolvePerPage('per_page', 20, 50)); + return ArticleSimpleResource::collection($list); + } + + /** + * 文章详情 + * + * @param [type] $id + * @return void + */ + public function show($id, Request $request) + { + $article = Article::with(['likesInfo'=>function ($q) use ($request) { + return $q->where('user_id', $request->user()->id); + }])->where('is_show', 1)->findOrFail($id); + return ArticleResource::make($article); + } + + /** + * 文章点赞 + * + * @param [type] $id + * @param Request $request + * @return void + */ + public function like($id, Request $request) + { + $article = Article::where('is_show', 1)->findOrFail($id); + if (ArticleLikesLog::where('user_id', $request->user()->id)->where('article_id', $article->id)->exists()) { + throw new BizException('您已点赞过这篇文章'); + } + try { + DB::beginTransaction(); + ArticleLikesLog::create([ + 'article_id' => $article->id, + 'user_id' => $request->user()->id, + ]); + $article->increment('likes'); + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + throw new BizException('系统繁忙,请稍后再试'); + } + return response()->noContent(); + } + + /** + * 阅读加积分接口 + * + * @param [type] $id + * @return void + */ + public function read($id, Request $request, PointsService $pointsService) + { + $article = Article::where('is_show', 1)->findOrFail($id); + if ($article->points > 0) { + if (ArticlePointsLog::where('user_id', $request->user()->id)->whereDate('created_at', now())->exists()) { + throw new BizException('您今天已拿过积分了,请明天再来'); + } + try { + DB::beginTransaction(); + ArticlePointsLog::create([ + 'user_id' => $request->user()->id, + 'article_id' => $article->id, + ]); + $pointsService->sendPoints(PointsLog::TYPE_READ, $request->user(), $article->points, '阅读文章送积分'); + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + throw new BizException('系统繁忙,请稍后再试'); + } + } + return response()->json([ + 'points'=>$article->points, + ]); + } +} diff --git a/app/Endpoint/Api/Http/Controllers/VipController.php b/app/Endpoint/Api/Http/Controllers/VipController.php index f0871501..a31655eb 100644 --- a/app/Endpoint/Api/Http/Controllers/VipController.php +++ b/app/Endpoint/Api/Http/Controllers/VipController.php @@ -3,7 +3,7 @@ namespace App\Endpoint\Api\Http\Controllers; use App\Endpoint\Api\Http\Resources\{VipResource, UserVipResource}; -use App\Models\Vip; +use App\Models\{Vip, UserVip}; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use App\Exceptions\BizException; @@ -43,7 +43,7 @@ class VipController extends Controller { $user = $request->user(); - $query = $user->vips()->where('status', 1); + $query = $user->vips()->where('status', UserVip::STATUS_SUCCESS); $list = $query->paginate($request->input('per_page')); diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index 98a68940..2503706e 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -73,13 +73,8 @@ Route::group([ Route::get('product/products/{product}', [ProductSkuController::class, 'show']); //查看搜索热词 Route::get('product/search-hot-keys', [ProductSkuController::class, 'searchHotKeys']); - //文章列表 - Route::get('articles', [ArticleController::class, 'index']); - Route::get('articles/health-category', [ArticleController::class, 'healthCategory']); - Route::get('articles/by-category/{category}', [ArticleController::class, 'articleList']); //指定文章配置 - Route::get('article-config', [ArticleController::class, 'config']); //获取最新app版本信息 Route::get('app-version', [AppVersionController::class, 'index']); //绑定用户端cid @@ -170,11 +165,6 @@ Route::group([ // 我的优惠券 Route::get('coupons', [UserCouponController::class, 'index']); - //文章 - Route::get('articles/{article}', [ArticleController::class, 'show']); - Route::post('articles/{article}/read', [ArticleController::class, 'read']); - Route::post('articles/{article}/like', [ArticleController::class, 'like']); - //签到 Route::get('click', [ClickController::class, 'index']); Route::post('click', [ClickController::class, 'click']); @@ -224,11 +214,25 @@ Route::group([ }); }); + // 门店提货 Route::get('store/{id}/products', [StoreController::class, 'products']); Route::apiResource('store', StoreController::class)->only(['index', 'show']); + // 门店下单 Route::group(['middleware' => ['auth:api', \App\Endpoint\Api\Http\Middleware\CheckUserStatus::class]], function () { Route::post('order-pre', [App\Endpoint\Api\Http\Controllers\Order\OrderPreController::class, 'store']); Route::post('order-pre/order', [App\Endpoint\Api\Http\Controllers\Order\OrderPreController::class, 'storeOrder']); }); + + // 文章 + Route::get('articles/category', [ArticleController::class, 'category']); + Route::get('articles/category-tree', [ArticleController::class, 'categoryTree']); + /** 以前的路由 */ + Route::get('article-config', [ArticleController::class, 'config']); + Route::get('articles', [ArticleController::class, 'index']); + Route::group(['middleware' => ['auth:api', \App\Endpoint\Api\Http\Middleware\CheckUserStatus::class]], function () { + Route::get('articles/{article}', [ArticleController::class, 'show']); + Route::post('articles/{article}/read', [ArticleController::class, 'read']); + Route::post('articles/{article}/like', [ArticleController::class, 'like']); + }); }); diff --git a/app/Models/ArticleCategory.php b/app/Models/ArticleCategory.php index b4ae547c..3219191f 100644 --- a/app/Models/ArticleCategory.php +++ b/app/Models/ArticleCategory.php @@ -44,4 +44,14 @@ class ArticleCategory extends Model } }); } + + public function scopeShow($q) + { + return $q->where('is_show', 1); + } + + public function scopeSort($q) + { + return $q->orderBy('is_recommend', 'desc')->orderBy('sort', 'desc')->orderBy('created_at', 'desc'); + } } diff --git a/app/Models/UserVip.php b/app/Models/UserVip.php index a9a5919f..b43f5aaa 100644 --- a/app/Models/UserVip.php +++ b/app/Models/UserVip.php @@ -9,6 +9,10 @@ class UserVip extends Model { use HasFactory; + const STATUS_UNPAID = 0; + const STATUS_SUCCESS = 1; + const STATUS_PROCESSING = 2; + protected $fillable = ['user_id', 'vip_id', 'name', 'price', 'times', 'gift', 'status', 'success_time']; protected $casts = [ @@ -16,6 +20,12 @@ class UserVip extends Model 'gift' => 'json' ]; + public static $statusMap = [ + self::STATUS_UNPAID => '待付款', + self::STATUS_SUCCESS => '已付款', + self::STATUS_PROCESSING => '处理中' + ]; + public function user() { return $this->belongsTo(User::class, 'user_id'); diff --git a/app/Models/Vip.php b/app/Models/Vip.php index 5eb6a4dd..76538466 100644 --- a/app/Models/Vip.php +++ b/app/Models/Vip.php @@ -16,6 +16,9 @@ class Vip extends Model const TIME_MONTH = 'month'; const TIME_DAY = 'day'; + const STATUS_ENABLE = 1; + const STATUS_DISABLE = 0; + protected $fillable = ['name', 'times', 'sort', 'status', 'gift', 'price', 'description']; protected $casts = [ @@ -29,6 +32,11 @@ class Vip extends Model self::TIME_DAY => '天', ]; + public static $statusMap = [ + self::STATUS_ENABLE => '可用', + self::STATUS_DISABLE => '不可用' + ]; + protected static function booted() { static::addGlobalScope('sort', function (Builder $builder) {