Commit 30085249 by 宁成龙

完善新增客户页面

parent 90333144
...@@ -16,6 +16,8 @@ use Dcat\Admin\Layout\Content; ...@@ -16,6 +16,8 @@ use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row; use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show; use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController; use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\Response;
class UserController extends AdminController class UserController extends AdminController
{ {
...@@ -32,6 +34,7 @@ class UserController extends AdminController ...@@ -32,6 +34,7 @@ class UserController extends AdminController
$grid->disableRefreshButton(); $grid->disableRefreshButton();
$grid->disableBatchDelete(); $grid->disableBatchDelete();
// $grid->disableCreateButton(); // $grid->disableCreateButton();
$grid->model()->orderBy("id", "desc");
UserService::userListListField($grid); UserService::userListListField($grid);
UserService::userListTool($grid); UserService::userListTool($grid);
UserService::userListActions($grid); UserService::userListActions($grid);
...@@ -62,12 +65,64 @@ class UserController extends AdminController ...@@ -62,12 +65,64 @@ class UserController extends AdminController
*/ */
public function create(Content $content) public function create(Content $content)
{ {
return $content return $content->body(CreateUser::make()->render());
->header('Dashboard')
->description('Description...')
->body(CreateUser::make()->render());
} }
/**
* Store a newly created resource in storage.
*
* @return mixed
*/
public function store()
{
try {
$data = request()->all();
$params = [
'company_name' => $data['company_name'],
'user_sn' => $data['user_sn'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'phone' => $data['phone'],
'email' => $data['email'],
'account_properties' => $data['account_properties'],
'remark' => $data['remark'],
'user_address_list' => $data['user_address_list'] ?? [],
];
$validator = Validator::make($params, [
'company_name' => 'required',
'first_name' => 'required',
'last_name' => 'required',
'phone' => 'required',
'email' => 'required',
'account_properties' => 'required',
// 'user_address_list' => 'required',
// 'user_address_list.first_name' => 'required',
// 'user_address_list.last_name' => 'required',
// 'user_address_list.email' => 'required',
// 'user_address_list.phone' => 'required',
// 'user_address_list.country' => 'required',
// 'user_address_list.city' => 'required',
// 'user_address_list.post_code' => 'required',
// 'user_address_list.detail_address' => 'required',
// 'user_address_list.is_default' => 'required',
]);
if ($validator->fails()) {
$error = $validator->errors()->first();
throw new \Exception($error);
}
UserService::createUserAndAddress($params);
} catch (\Throwable $throwable) {
return $this->form()->response()
->error(trans('admin.save_failed'))
->withExceptionIf($throwable->getMessage(), $throwable);
}
$url = admin_url("/users/list");
return $this->form()->response()->success('操作成功')->redirect($url)->refresh();
}
/** /**
* Make a form builder. * Make a form builder.
* *
......
...@@ -27,7 +27,7 @@ class CreateUser extends \Dcat\Admin\Support\LazyRenderable ...@@ -27,7 +27,7 @@ class CreateUser extends \Dcat\Admin\Support\LazyRenderable
$form->disableCreatingCheck(); $form->disableCreatingCheck();
$form->row(function (Form\Row $form) { $form->row(function (Form\Row $form) {
$form->width(4)->text('company_name')->required(); $form->width(4)->text('company_name')->required();
$form->width(4)->text('user_sn')->required(); $form->width(4)->text('user_sn');
}); });
$form->row(function (Form\Row $form) { $form->row(function (Form\Row $form) {
$form->width(4)->text('first_name')->required(); $form->width(4)->text('first_name')->required();
...@@ -35,15 +35,15 @@ class CreateUser extends \Dcat\Admin\Support\LazyRenderable ...@@ -35,15 +35,15 @@ class CreateUser extends \Dcat\Admin\Support\LazyRenderable
}); });
$form->row(function (Form\Row $form) { $form->row(function (Form\Row $form) {
$form->width(4)->text('phone')->required(); $form->width(4)->text('phone')->required();
$form->width(4)->text('email')->required(); $form->width(4)->email('email')->required();
}); });
$form->row(function (Form\Row $form) { $form->row(function (Form\Row $form) {
$form->width(4)->select("reg_source")->options(admin_trans('user.options.reg_source'))->required(); $form->width(4)->select("account_properties")->options(admin_trans('user.options.account_properties'))->required();
$form->width(4)->text('remark')->required(); $form->width(4)->text('remark');
}); });
$form->row(function (Form\Row $form) { $form->row(function (Form\Row $form) {
$form->array('column_name', function ($table) { $form->array('user_address_list', function ($table) {
$table->row(function (Form\Row $table) { $table->row(function (Form\Row $table) {
$table->width(3)->text('first_name')->required(); $table->width(3)->text('first_name')->required();
$table->width(3)->text('last_name')->required(); $table->width(3)->text('last_name')->required();
...@@ -51,15 +51,15 @@ class CreateUser extends \Dcat\Admin\Support\LazyRenderable ...@@ -51,15 +51,15 @@ class CreateUser extends \Dcat\Admin\Support\LazyRenderable
$table->row(function (Form\Row $table) { $table->row(function (Form\Row $table) {
$table->width(3)->select('address_type')->options(admin_trans('user-address.options.address_type')) $table->width(3)->select('address_type')->options(admin_trans('user-address.options.address_type'))
->required(); ->required();
$table->width(3)->text('company_name')->required(); $table->width(3)->text('company_name');
}); });
$table->row(function (Form\Row $table) { $table->row(function (Form\Row $table) {
$table->width(3)->text('email')->required(); $table->width(3)->email('email')->required();
$table->width(3)->text('phone')->required(); $table->width(3)->text('phone')->required();
}); });
$table->row(function (Form\Row $table) { $table->row(function (Form\Row $table) {
$table->width(3)->select('country')->options(Country::getCountryMap())->required(); $table->width(3)->select('country')->options(Country::getCountryMap())->required();
$table->width(3)->text('province')->required(); $table->width(3)->text('province');
$table->width(3)->text('city')->required(); $table->width(3)->text('city')->required();
$table->width(2)->text('post_code')->required(); $table->width(2)->text('post_code')->required();
}); });
......
...@@ -9,10 +9,16 @@ use App\Admin\Actions\User\UserStatusAction; ...@@ -9,10 +9,16 @@ use App\Admin\Actions\User\UserStatusAction;
use App\Admin\Actions\User\UserTransferAction; use App\Admin\Actions\User\UserTransferAction;
use App\Models\Cms\CmsUser; use App\Models\Cms\CmsUser;
use App\Models\User; use App\Models\User;
use App\Models\UserAddress;
use Dcat\Admin\Grid; use Dcat\Admin\Grid;
use Dcat\Admin\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\Response;
class UserService class UserService
{ {
public static function userListFilter(Grid $grid) public static function userListFilter(Grid $grid)
{ {
$grid->filter(function ($filter) { $grid->filter(function ($filter) {
...@@ -36,7 +42,7 @@ class UserService ...@@ -36,7 +42,7 @@ class UserService
public static function userListListField(Grid $grid) public static function userListListField(Grid $grid)
{ {
$grid->column('company_name'); $grid->column('company_name');
$grid->column('user_sn')->link(function ($user_sn)use($grid) { $grid->column('user_sn')->link(function ($user_sn) use ($grid) {
return admin_url("/users/{$this->id}"); return admin_url("/users/{$this->id}");
}); });
$grid->column('name'); $grid->column('name');
...@@ -50,8 +56,6 @@ class UserService ...@@ -50,8 +56,6 @@ class UserService
$grid->column('status')->using(admin_trans('user.options.status')); $grid->column('status')->using(admin_trans('user.options.status'));
} }
public static function userListTool(Grid $grid) public static function userListTool(Grid $grid)
{ {
$grid->tools([ $grid->tools([
...@@ -73,7 +77,7 @@ class UserService ...@@ -73,7 +77,7 @@ class UserService
// $rowArray = $actions->row->toArray(); // $rowArray = $actions->row->toArray();
// 获取当前行主键值 // 获取当前行主键值
$id = $actions->getKey(); $id = $actions->getKey();
$actions->append('<a href="'.admin_url("/users/{$id}").'" class="btn btn-primary btn-sm $actions->append('<a href="' . admin_url("/users/{$id}") . '" class="btn btn-primary btn-sm
btn-mini">detail</a> '); btn-mini">detail</a> ');
//状态按钮 //状态按钮
$actions->append(new UserStatusAction()); $actions->append(new UserStatusAction());
...@@ -87,4 +91,70 @@ class UserService ...@@ -87,4 +91,70 @@ class UserService
]; ];
User::updateById($id, $update); User::updateById($id, $update);
} }
public static function createUserAndAddress($params)
{
try {
DB::beginTransaction();
$userData = [
"company_name" => $params["company_name"],
"user_sn" => $params["user_sn"],
"first_name" => $params["first_name"],
"last_name" => $params["last_name"],
"phone" => $params["phone"],
"email" => $params["email"],
"reg_source" => User::REG_SOURCE_MANUAL,
"remark" => $params["remark"],
"password" => "1234567",
"status" => User::STATUS_NORMAL,
"sale_id" => request()->user['userId'] ?? "1000",
"sale_name" => request()->user['name'] ?? "admin",
"created_time" => time(),
"update_time" => time(),
];
$userId = User::insertData($userData);
foreach ($params['user_address_list'] as $address) {
$validator = Validator::make($address, [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required',
'phone' => 'required',
'country' => 'required',
'city' => 'required',
'post_code' => 'required',
'detail_address' => 'required',
'is_default' => 'required',
]);
if ($validator->fails()) {
$error = $validator->errors()->first();
throw new \Exception("客户地址错误:" . $error);
}
$addressData = [
"user_id" => $userId,
"first_name" => $address["first_name"]??"",
"last_name" => $address["last_name"]??"",
"address_type" => $address["address_type"]??"",
"company_name" => $address["company_name"]??"",
"email" => $address["email"]??"",
"phone" => $address["phone"]??"",
"country" => $address["country"]??"",
"province" => $address["province"]??"",
"city" => $address["city"]??"",
"post_code" => $address["post_code"]??"",
"detail_address" => $address["detail_address"]??"",
"is_default" => $address["is_default"]??"",
"create_time" => time(),
"update_time" => time(),
];
UserAddress::insertData($addressData);
}
DB::commit();
} catch (\Throwable $throwable) {
DB::rollBack();
throw $throwable;
}
}
} }
...@@ -34,6 +34,12 @@ class User extends Model ...@@ -34,6 +34,12 @@ class User extends Model
const STATUS_NORMAL = 1; const STATUS_NORMAL = 1;
const STATUS_DISABLE = -1; const STATUS_DISABLE = -1;
const REG_SOURCE_WEB = 1;
const REG_SOURCE_MANUAL = 2;
const ACCOUNT_PROPERTIES_PERSONAL = 1;
const ACCOUNT_PROPERTIES_ENTERPRISE = 2;
public static function insertData($data) public static function insertData($data)
{ {
return self::insertGetId($data); return self::insertGetId($data);
......
...@@ -31,6 +31,11 @@ class UserAddress extends Model ...@@ -31,6 +31,11 @@ class UserAddress extends Model
protected $table = 'user_address'; protected $table = 'user_address';
public static function insertData($data)
{
return self::insertGetId($data);
}
public static function getListByUserId($userId) public static function getListByUserId($userId)
{ {
$res = self::where('user_id', $userId)->get(); $res = self::where('user_id', $userId)->get();
......
...@@ -36,5 +36,9 @@ return [ ...@@ -36,5 +36,9 @@ return [
"1"=>"网站", "1"=>"网站",
"2"=>"人工新增" "2"=>"人工新增"
], ],
"account_properties"=>[
"1"=>"个人",
"2"=>"企业"
],
], ],
]; ];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment