初始化项目

This commit is contained in:
2025-01-11 14:16:20 +08:00
parent 1ed53bf65b
commit cf64159069
11206 changed files with 1446622 additions and 0 deletions

View File

@@ -0,0 +1,151 @@
<?php
namespace App\Http\Controllers\Member;
use App\Models\Cart;
use App\Models\YiiModels\Drug;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
/**
* 购物车
*/
class CartController extends MemberBaseController
{
/**
* 添加购物车
*
* @return void
*/
public function cartAdd()
{
$input = $this->toValidator([
'drug_id' => 'required|integer',
'change_qty' => 'nullable|integer',
'true_qty' => 'nullable|integer',
], [], [
'drug_id' => '商品',
'change_qty' => '数量',
'true_qty' => '数量',
], true);
if (!$input['change_qty'] && !$input['true_qty']) {
json_error('商品数量不正确');
}
/* @var Drug $drug */
$drug = Drug::with([
'drugStoreRelation' => function (Relation $relation) {
$relation->select(['drug_id', 'total_sales_volume', 'status'])
->where('store_id', $this->getStoreId());
},
'drugstoreDrug' => function (Relation $relation) {
$relation->select(['drug_id', 'price', 'stock'])
->where('drugstore_id', $this->getDrugstoreId());
},
])
->select([
'id', 'status',
])
->whereHasIn('drugStoreRelation', function (Builder $builder) {
$builder->where('store_id', $this->getStoreId());
})
->where('id', $input['drug_id'])
->first();
if (!$drug && !$drug->drugStoreRelation && !$drug->drugstoreDrug) {
json_error('商品不存在');
}
if ($drug->status != Drug::STATUS_ON || $drug->drugStoreRelation->status != Drug::STATUS_ON) {
json_error('商品已下架');
}
$cart = $this->user()->carts()->firstOrCreate([
'drug_id' => $input['drug_id'],
'store_id' => $this->getStoreId(),
]);
if ($input['change_qty']) {
if ($input['change_qty'] > $drug->drugstoreDrug->stock) {
json_error("库存不足,最多可加入{$drug->drugstoreDrug->stock}个");
}
$cart->increment('qty', $input['change_qty'], ['join_at' => now()]);
if ($cart->qty <= 0) {
$cart->delete();
json_success('购物车商品已删除');
}
} else {
if (($input['true_qty'] - $cart['qty']) > $drug->drugstoreDrug->stock) {
json_error("库存不足,最多可加入{$drug->drugstoreDrug->stock}个");
}
$cart->update([
'qty' => $input['true_qty'],
'join_at' => now(),
]);
if ($cart->qty <= 0) {
$cart->delete();
json_success('购物车商品已删除');
}
}
json_success('已加入购物车');
}
/**
* 清空购物车
*
* @return void
*/
public function clearAll()
{
Cart::where('store_id', $this->getStoreId())
->where('user_id', $this->userId())
->delete();
json_success('已清空购物车');
}
/**
* 购物车列表
*
* @return void
*/
public function cartLists()
{
$carts = Cart::with([
'drug' => function (Relation $relation) {
$relation->with([
'drugStoreRelation' => function (Relation $relation) {
$relation->select(['drug_id', 'total_sales_volume', 'status'])
->where('store_id', $this->getStoreId());
},
'drugstoreDrug' => function (Relation $relation) {
$relation->select(['drug_id', 'price', 'stock'])
->where('drugstore_id', $this->getDrugstoreId());
},
])
->select(['id', 'drug_name', 'source', 'type', 'is_otc', 'small_info', 'usage', 'specification', 'image', 'status']);
}
])
->whereHasIn('drug')
->where('store_id', $this->getStoreId())
->where('user_id', $this->userId())
->orderByDesc('join_at')
->get();
$valid = $invalid = [];
/* @var Cart $cart */
foreach ($carts as $cart) {
if (!$cart->drug || !$cart->drug->drugStoreRelation || !$cart->drug->drugstoreDrug) {
$invalid[] = $cart;
continue;
}
if ($cart->drug->status != Drug::STATUS_ON || $cart->drug->drugStoreRelation->status != Drug::STATUS_ON) {
$invalid[] = $cart;
continue;
}
$valid[] = $cart;
}
json_success([
'store_name' => $this->getStore()->name,
'valid' => $valid,
'invalid' => $invalid,
]);
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace App\Http\Controllers\Member;
use App\Models\DrugCategory;
use App\Models\DrugStoreRelation;
use App\Models\YiiModels\Drug;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
/**
* 药品
*/
class DrugController extends MemberBaseController
{
/**
* 药品分类
*
* @return void
*/
public function drugCategory()
{
json_success(DrugCategory::getCategory());
}
/**
* 商品列表(带库存)
*
* @return void
*/
public function drugLists()
{
$category_first = request('category_first');
$category_second = request('category_second');
$keyword = request('keyword');
// $this->toValidator([
// 'category_first' => 'required|integer',
// 'category_second' => 'required|integer',
// ], [], [
// 'category_first' => '一级分类',
// 'category_second' => '二级分类',
// ]);
$drugs = Drug::with([
'drugstoreDrug' => function (Relation $relation) {
$relation->select(['drug_id', 'price', 'stock'])
->where('drugstore_id', $this->getDrugstoreId());
},
])
->select([
'id', 'drug_name', 'source', 'type', 'is_otc', 'category_first', 'category_second',
'usage', 'small_info', 'specification', 'image',
])
->whereHasIn('drugStoreRelation', function (Builder $builder) {
$builder->where('status', DrugStoreRelation::STATUS_ON)
->where('store_id', $this->getStoreId());
})
->when($keyword, function (Builder $builder) use ($keyword) {
$builder->where('drug_name', 'like', "%$keyword%");
})
->where('status', Drug::STATUS_ON)
->where('type_id', [Drug::TYPE_CHINESE_DRUG, Drug::TYPE_PELLET_PATENT])
->where('is_otc', Drug::IS_OTC_FALSE)
->where('category_first', $category_first)
->where('category_second', $category_second)
->get();
json_success($drugs);
}
/**
* 商品详情
*
* @return void
*/
public function drugDetail()
{
$id = $this->getId();
/* @var Drug $drug */
$drug = Drug::with([
'drugStoreRelation' => function (Relation $relation) {
$relation->select(['store_id', 'drug_id', 'total_sales_volume', 'status'])
->with(['store:id,name'])
->where('store_id', $this->getStoreId());
},
'drugstoreDrug' => function (Relation $relation) {
$relation->select(['drugstore_id', 'drug_id', 'price', 'stock'])
->where('drugstore_id', $this->getDrugstoreId());
},
])
->select([
'id', 'drug_name', 'source', 'type', 'is_otc', 'category_first', 'category_second',
'usage', 'specification', 'image', 'small_info', 'info', 'content', 'status',
])
->whereHasIn('drugStoreRelation', function (Builder $builder) {
$builder->where('store_id', $this->getStoreId());
})
->where('id', $id)
->first();
if (!$drug || !$drug->drugStoreRelation || !$drug->drugstoreDrug) {
json_error('商品不存在');
}
if ($drug->status != Drug::STATUS_ON || $drug->drugStoreRelation->status != Drug::STATUS_ON) {
json_error('商品已下架');
}
json_success($drug);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Http\Controllers\Member;
use App\Models\ExpressFee;
/**
* 物流
*/
class ExpressController extends MemberBaseController
{
public function feeInfo()
{
$expressFeeInfo = ExpressFee::getDefaultExpressFee();
if (!$expressFeeInfo) {
json_error('运费信息错误');
}
json_success($expressFeeInfo);
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace App\Http\Controllers\Member;
use App\Http\Controllers\Controller;
use App\Models\YiiModels\Drugstore;
use App\Models\YiiModels\Store;
use App\Models\YiiModels\User;
use Illuminate\Support\Facades\Auth;
/**
* 用户端基类
*/
class MemberBaseController extends Controller
{
protected ?Store $store = null;
protected ?Drugstore $drugstore = null;
protected ?int $store_id = 0;
protected ?int $drugstore_id = 0;
/**
* @comment 用户
* @return \App\Models\YiiModels\User|\Illuminate\Contracts\Auth\Authenticatable|null
*/
public function user()
{
return Auth::guard(User::GUARD)->user();
}
/**
* @comment 用户id
* @return int
*/
public function userId(): int
{
return $this->user()->id;
}
/**
* 当前仓库ID
*
* @return int
*/
public function getDrugstoreId(): int
{
if (!$this->drugstore_id) {
$this->drugstore_id = $this->getStore()->drugstore_id;
}
return $this->drugstore_id;
}
/**
* 当前仓库
*
* @return Drugstore
*/
public function getDrugstore(): Drugstore
{
$this->drugstore = Drugstore::where('id', $this->getDrugstoreId())->first();
if (!$this->drugstore) {
json_error('药房信息获取失败');
}
return $this->drugstore;
}
/**
* 获取门店ID
*
* @return int
*/
public function getStoreId(): int
{
if (!$this->store_id) {
$this->store_id = (int)request()->input('store_id');
}
return $this->store_id;
}
/**
* 获取门店
*
* @return Store
*/
public function getStore(): Store
{
$this->store = Store::where('id', $this->getStoreId())->first();
if (!$this->store) {
json_error('门店信息获取失败');
}
return $this->store;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Controllers\Member;
use App\Models\YiiModels\User;
/**
* @deprecated fixme delete 测试接口
*/
class TestController extends MemberBaseController
{
public function testLogin()
{
if (app()->isLocal() && file_exists(base_path('./../dev.ini'))) {
/* @var User $user */
$user = User::where('mobile', request('mobile'))->first();
if ($user) {
json_success('登陆成功', ['token' => $user->token, 'user_id' => $user->id]);
}
}
json_error('错误示例');
}
}