收货地址管理
This commit is contained in:
parent
7df9e218e0
commit
1f9e6c19d3
88
app/Domains/Company/Http/Controllers/AddressController.php
Normal file
88
app/Domains/Company/Http/Controllers/AddressController.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
namespace App\Domains\Company\Http\Controllers;
|
||||
|
||||
use App\Core\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Exceptions\NotAllowedException;
|
||||
use App\Domains\Sms\Services\SmsService;
|
||||
use App\Domains\Virtual\Services\CompanyAddressService;
|
||||
|
||||
class AddressController extends Controller
|
||||
{
|
||||
protected $request;
|
||||
protected $companyAddressService;
|
||||
|
||||
/**
|
||||
* 构造函数,自动注入.
|
||||
*/
|
||||
public function __construct(Request $request, CompanyAddressService $companyAddressService)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->companyAddressService = $companyAddressService;
|
||||
$this->account = $request->user('company');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取收货地址列表
|
||||
*/
|
||||
public function list()
|
||||
{
|
||||
$res = $this->companyAddressService->getCompanyAddress($this->account->company_id);
|
||||
return res($res, '收货地址', 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加收货地址
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$attributes = $this->request->all();
|
||||
$attributes['company_id'] = $this->account->company_id;
|
||||
|
||||
$res = $this->companyAddressService->store($attributes);
|
||||
|
||||
return res($res, '添加成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改收货地址
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$attributes = $this->request->all();
|
||||
$attributes['id'] = $id;
|
||||
$attributes['company_id'] = $this->account->company_id;
|
||||
|
||||
$res = $this->companyAddressService->store($attributes);
|
||||
|
||||
return res($res, '修改成功');
|
||||
}
|
||||
|
||||
public function destroy()
|
||||
{
|
||||
$ids = $this->request->ids();
|
||||
|
||||
$res = $this->companyAddressService->getCompanyAddress($this->account->company_id)->pluck('id')->toArray();
|
||||
|
||||
foreach ($ids as $id) {
|
||||
if (!in_array($id, $res)) {
|
||||
throw new NotAllowedException('非法操作');
|
||||
}
|
||||
}
|
||||
|
||||
$this->companyAddressService->destroy($ids);
|
||||
|
||||
return res(true, '删除成功');
|
||||
}
|
||||
|
||||
public function default($id)
|
||||
{
|
||||
$node = $this->companyAddressService->setDefault($id);
|
||||
return res($node, '设置成功');
|
||||
}
|
||||
}
|
@ -15,5 +15,11 @@ $router->group(['prefix' => 'companies', 'as' => 'companies'], function ($router
|
||||
$router->post('/account/password_by_old', ['as' => 'account.passwordByOld', 'uses' => 'AccountController@password', 'middleware' => ['company_password']]);
|
||||
$router->post('/account/password_by_sms', ['as' => 'account.passwordBySms', 'uses' => 'AccountController@password', 'middleware' => ['verify_code']]);
|
||||
$router->post('/account/mobile', ['as' => 'account.mobile', 'uses' => 'AccountController@mobile', 'middleware' => ['verify_code']]);
|
||||
|
||||
$router->get('/addresses/list', ['as' => 'addresses.list', 'uses' => 'AddressController@list']);
|
||||
$router->post('/addresses/create', ['as' => 'addresses.create', 'uses' => 'AddressController@create']);
|
||||
$router->post('/addresses/update/{id}', ['as' => 'addresses.update', 'uses' => 'AddressController@update']);
|
||||
$router->post('/addresses/destroy', ['as' => 'addresses.destroy', 'uses' => 'AddressController@destroy']);
|
||||
$router->post('/addresses/default/{id}', ['as' => 'addresses.default', 'uses' => 'AddressController@default']);
|
||||
});
|
||||
});
|
||||
|
@ -16,7 +16,7 @@ class CompanyAddressRepository extends Repository
|
||||
|
||||
/**
|
||||
* 是否开启数据转化
|
||||
*
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $needTransform = false;
|
||||
@ -29,7 +29,8 @@ class CompanyAddressRepository extends Repository
|
||||
'created_at' => 'like',
|
||||
];
|
||||
|
||||
public function model() {
|
||||
public function model()
|
||||
{
|
||||
return Model::class;
|
||||
}
|
||||
|
||||
@ -57,6 +58,26 @@ class CompanyAddressRepository extends Repository
|
||||
$this->model = $this->model->whereIn('id', $conditions['id']);
|
||||
}
|
||||
|
||||
if (isset($conditions['company_id'])) {
|
||||
$this->model = $this->model->where('company_id', $conditions['company_id']);
|
||||
}
|
||||
|
||||
if (isset($conditions['contacts'])) {
|
||||
$this->model = $this->model->where('contacts', $conditions['contacts']);
|
||||
}
|
||||
|
||||
if (isset($conditions['mobile'])) {
|
||||
$this->model = $this->model->where('mobile', $conditions['mobile']);
|
||||
}
|
||||
|
||||
if (isset($conditions['area'])) {
|
||||
$this->model = $this->model->where('area', $conditions['area']);
|
||||
}
|
||||
|
||||
if (isset($conditions['address'])) {
|
||||
$this->model = $this->model->where('address', $conditions['address']);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
134
app/Domains/Virtual/Services/CompanyAddressService.php
Normal file
134
app/Domains/Virtual/Services/CompanyAddressService.php
Normal file
@ -0,0 +1,134 @@
|
||||
<?php
|
||||
namespace App\Domains\Virtual\Services;
|
||||
|
||||
use App\Core\Service;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Exceptions\NotExistException;
|
||||
use App\Models\Virtual\CompanyAddress;
|
||||
use App\Exceptions\NotAllowedException;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Domains\Virtual\Repositories\CompanyAddressRepository;
|
||||
|
||||
class CompanyAddressService extends Service
|
||||
{
|
||||
protected $companyAddressRepository;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(CompanyAddressRepository $companyAddressRepository)
|
||||
{
|
||||
$this->companyAddressRepository = $companyAddressRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取企业收货地址
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return Collection
|
||||
*/
|
||||
public function getCompanyAddress($companyId)
|
||||
{
|
||||
$res = $this->companyAddressRepository->withConditions(['company_id' => $companyId])->get();
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认收货地址
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return CompanyAddress
|
||||
*/
|
||||
public function setDefault($id)
|
||||
{
|
||||
if (!$node = $this->companyAddressRepository->find($id)) {
|
||||
throw new NotExistException('地址不存在或已删除');
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($node) {
|
||||
$this->companyAddressRepository->where(['company_id' => $node->company_id])->update(['default' => 0]);
|
||||
$this->companyAddressRepository->setModel($node)->update(['default' => 1]);
|
||||
});
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储收货地址
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return CompanyAddress
|
||||
*/
|
||||
public function store(array $attributes = [])
|
||||
{
|
||||
$attributes = array_only($attributes, array_merge(app(CompanyAddress::class)->getFillable()));
|
||||
|
||||
$rule = [
|
||||
'company_id' => ['required', 'exists:virtual_companies,id'],
|
||||
'contacts' => ['required', 'display_length:2,32'],
|
||||
'mobile' => ['required', 'cn_phone'],
|
||||
'area' => ['required', 'max:255'],
|
||||
'address' => ['required', 'max:255'],
|
||||
];
|
||||
|
||||
$message = [
|
||||
'company_id.required' => '请输入企业ID',
|
||||
'company_id.exists' => '企业不存在或已删除',
|
||||
'contacts.required' => '联系人不能为空',
|
||||
'contacts.display_length' => '联系人名称长度不合法',
|
||||
'mobile.required' => '手机号不能为空',
|
||||
'mobile.cn_phone' => '手机号不合法',
|
||||
'area.required' => '请选择区域',
|
||||
'address.required' => '请输入详细地址',
|
||||
];
|
||||
|
||||
Validator::validate($attributes, $rule, $message);
|
||||
|
||||
if (!$attributes['id']) {
|
||||
if ($this->companyAddressRepository->withConditions($attributes)->count()) {
|
||||
throw new NotAllowedException('地址已存在,请勿重复添加');
|
||||
}
|
||||
|
||||
$count = $this->companyAddressRepository->withConditions(['company_id' => $attributes['company_id']])->count();
|
||||
|
||||
if ($count >= 10) {
|
||||
throw new NotAllowedException('最多添加10个地址');
|
||||
}
|
||||
|
||||
if ($count == 0) {
|
||||
$attributes['default'] = 1;
|
||||
}
|
||||
|
||||
$node = $this->companyAddressRepository->create($attributes);
|
||||
}
|
||||
|
||||
if ($attributes['id']) {
|
||||
if (!$node = $this->companyAddressRepository->find($attributes['id'])) {
|
||||
throw new NotExistException('地址不存在或已删除');
|
||||
}
|
||||
|
||||
$this->companyAddressRepository->setModel($node)->update($attributes);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($ids)
|
||||
{
|
||||
$ids = is_array($ids) ? $ids : [$ids];
|
||||
|
||||
$this->companyAddressRepository->destroy($ids);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -3,8 +3,13 @@
|
||||
namespace App\Models\Virtual;
|
||||
|
||||
use App\Core\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class CompanyAddress extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $table = 'virtual_company_addresses';
|
||||
|
||||
protected $fillable = ['id', 'company_id' , 'contacts', 'mobile', 'area', 'address', 'default', 'status'];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user