diff --git a/app/Domains/Company/Http/Controllers/AddressController.php b/app/Domains/Company/Http/Controllers/AddressController.php new file mode 100644 index 00000000..aa7aeee5 --- /dev/null +++ b/app/Domains/Company/Http/Controllers/AddressController.php @@ -0,0 +1,88 @@ +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, '设置成功'); + } +} diff --git a/app/Domains/Company/Routes/api.php b/app/Domains/Company/Routes/api.php index c7557566..8fb800c3 100644 --- a/app/Domains/Company/Routes/api.php +++ b/app/Domains/Company/Routes/api.php @@ -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']); }); }); diff --git a/app/Domains/Virtual/Repositories/CompanyAddressRepository.php b/app/Domains/Virtual/Repositories/CompanyAddressRepository.php index 0ae84dd7..74072d92 100644 --- a/app/Domains/Virtual/Repositories/CompanyAddressRepository.php +++ b/app/Domains/Virtual/Repositories/CompanyAddressRepository.php @@ -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; } -} \ No newline at end of file +} diff --git a/app/Domains/Virtual/Services/CompanyAddressService.php b/app/Domains/Virtual/Services/CompanyAddressService.php new file mode 100644 index 00000000..dd91fd12 --- /dev/null +++ b/app/Domains/Virtual/Services/CompanyAddressService.php @@ -0,0 +1,134 @@ +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; + } +} diff --git a/app/Models/Virtual/CompanyAddress.php b/app/Models/Virtual/CompanyAddress.php index 2b386cdf..7e1a06d3 100644 --- a/app/Models/Virtual/CompanyAddress.php +++ b/app/Models/Virtual/CompanyAddress.php @@ -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']; }