企业管理
This commit is contained in:
parent
5cb52908a7
commit
80a6542fdd
80
app/Domains/Virtual/Http/Controllers/CompanyController.php
Normal file
80
app/Domains/Virtual/Http/Controllers/CompanyController.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
namespace App\Domains\Virtual\Http\Controllers;
|
||||
|
||||
use App\Core\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Domains\Virtual\Services\CompanyService;
|
||||
|
||||
class CompanyController extends Controller
|
||||
{
|
||||
protected $request;
|
||||
protected $companyService;
|
||||
|
||||
/**
|
||||
* 构造函数,自动注入.
|
||||
*/
|
||||
public function __construct(Request $request, CompanyService $companyService)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->companyService = $companyService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$conditions = [];
|
||||
$conditions['limit'] = $this->request->get('limit', 20);
|
||||
|
||||
$accounts = $this->companyService->index($conditions);
|
||||
|
||||
return res($accounts, '企业列表', 201);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$attributes = $this->request->all();
|
||||
|
||||
$account = $this->companyService->store($attributes);
|
||||
|
||||
return res($account, '创建成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update($id)
|
||||
{
|
||||
$attributes = $this->request->all();
|
||||
$attributes['id'] = $id;
|
||||
|
||||
$account = $this->companyService->store($attributes);
|
||||
|
||||
return res($account, '修改成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy()
|
||||
{
|
||||
$ids = $this->request->ids();
|
||||
|
||||
$this->companyService->destroy($ids);
|
||||
|
||||
return res(true, '删除成功');
|
||||
}
|
||||
}
|
@ -6,11 +6,17 @@ $router->group(['prefix' => 'virtuals', 'as' => 'virtuals', 'middleware' => ['ad
|
||||
// The controllers live in Domains/Virtual/Http/Controllers
|
||||
$router->get('/', ['as' => 'index', 'uses' => 'VirtualController@index']);
|
||||
|
||||
// The controllers live in Domains/Account/Http/Controllers
|
||||
$router->get('company/account/index', ['as' => 'index', 'uses' => 'CompanyAccountController@index']);
|
||||
$router->post('company/account/create', ['as' => 'create', 'uses' => 'CompanyAccountController@create']);
|
||||
$router->post('company/account/update/{id}', ['as' => 'update', 'uses' => 'CompanyAccountController@update']);
|
||||
$router->post('company/account/destroy', ['as' => 'destroy', 'uses' => 'CompanyAccountController@destroy']);
|
||||
// 企业管理
|
||||
$router->get('company/index', ['as' => 'index', 'uses' => 'CompanyController@index']);
|
||||
$router->post('company/create', ['as' => 'create', 'uses' => 'CompanyController@create']);
|
||||
$router->post('company/update/{id}', ['as' => 'update', 'uses' => 'CompanyController@update']);
|
||||
$router->post('company/destroy', ['as' => 'destroy', 'uses' => 'CompanyController@destroy']);
|
||||
|
||||
// 企业账号管理
|
||||
$router->get('company/account/index', ['as' => 'company.account.index', 'uses' => 'CompanyAccountController@index']);
|
||||
$router->post('company/account/create', ['as' => 'company.account.create', 'uses' => 'CompanyAccountController@create']);
|
||||
$router->post('company/account/update/{id}', ['as' => 'company.account.update', 'uses' => 'CompanyAccountController@update']);
|
||||
$router->post('company/account/destroy', ['as' => 'company.account.destroy', 'uses' => 'CompanyAccountController@destroy']);
|
||||
|
||||
/**
|
||||
* 需要认证的接口
|
||||
|
99
app/Domains/Virtual/Services/CompanyService.php
Normal file
99
app/Domains/Virtual/Services/CompanyService.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
namespace App\Domains\Virtual\Services;
|
||||
|
||||
use App\Core\Service;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\Virtual\Company;
|
||||
use Illuminate\Validation\Rule;
|
||||
use App\Exceptions\NotExistException;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Domains\Virtual\Repositories\CompanyRepository;
|
||||
|
||||
class CompanyService extends Service
|
||||
{
|
||||
protected $companyRepository;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(CompanyRepository $companyRepository)
|
||||
{
|
||||
$this->companyRepository = $companyRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业列表
|
||||
*
|
||||
* @param array $conditions
|
||||
* @return mixed
|
||||
*/
|
||||
public function index(array $conditions = [])
|
||||
{
|
||||
$limit = $conditions['limit'] ?? 20;
|
||||
|
||||
$companies = $this->companyRepository->withConditions($conditions)->applyConditions()->paginate($limit);
|
||||
|
||||
return $companies;
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储企业
|
||||
*
|
||||
* @param array $attributes
|
||||
* @param Company $parent
|
||||
* @return Company
|
||||
*/
|
||||
public function store(array $attributes = [])
|
||||
{
|
||||
$attributes = array_only($attributes, array_merge(app(Company::class)->getFillable()));
|
||||
|
||||
$rule = [
|
||||
'name' => ['required', 'display_length:2,32', Rule::unique($this->companyRepository->getTable(), 'name')->ignore($attributes['id'])],
|
||||
'contacts' => ['string', 'display_length:2,32'],
|
||||
'mobile' => ['string', 'cn_phone'],
|
||||
'address' => ['string'],
|
||||
];
|
||||
|
||||
$message = [
|
||||
'name.required' => '请输入企业名称',
|
||||
'name.display_length' => '企业名称长度不合法',
|
||||
'name.unique' => '企业名称已经被其他用户所使用',
|
||||
'contacts.display_length' => '联系人长度不合法',
|
||||
'mobile.cn_phone' => '手机号不合法',
|
||||
];
|
||||
|
||||
Validator::validate($attributes, $rule, $message);
|
||||
|
||||
if (!$attributes['id']) {
|
||||
$node = $this->companyRepository->create($attributes);
|
||||
}
|
||||
|
||||
if ($attributes['id']) {
|
||||
$node = $this->companyRepository->find($attributes['id']);
|
||||
|
||||
if (!$node) {
|
||||
throw new NotExistException('企业不存在或已删除');
|
||||
}
|
||||
|
||||
$this->companyRepository->setModel($node)->update($attributes);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($ids)
|
||||
{
|
||||
$ids = is_array($ids) ? $ids : [$ids];
|
||||
|
||||
$this->companyRepository->destroy($ids);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -8,8 +8,8 @@ use App\Models\CompanyBase;
|
||||
class Company extends CompanyBase
|
||||
{
|
||||
protected $table = 'virtual_companies';
|
||||
|
||||
public $incrementing = false;
|
||||
|
||||
protected $fillable = ['id', 'name' , 'contacts', 'mobile', 'address', 'remark'];
|
||||
|
||||
public function cards()
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user