Commit c96ce830 by 宁成龙

完善新增,更新客户地址

parent 239a35e3
<?php
namespace App\Admin\Actions\UserAddress;
use App\Admin\Forms\User\AssignUserHandle;
use App\Admin\Forms\UserAddress\SaveUserAddressHandle;
use App\Admin\Forms\User\EditUserHandle;
use App\Models\Order as OrderModel;
use App\Models\User;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\BatchAction;
use Dcat\Admin\Widgets\Modal;
use Illuminate\Http\Request;
use Dcat\Admin\Grid\RowAction;
class UserAddressEditAction extends RowAction
{
protected $action;
protected $htmlClasses = ['btn btn-sm btn-primary'];
// 注意action的构造方法参数一定要给默认值
public function __construct($title = null, $action = 1)
{
$this->title = $title;
$this->action = $action;
}
public function render()
{
$form = SaveUserAddressHandle::make(['address_id' => $this->getRow()['address_id']]);
$buttonName = trans('user.labels.handle');
return Modal::make()->lg()->title($this->title)->body($form->payload(['address_id' => $this->getRow()['address_id']]))
->onLoad
($this->getModalScript())->button('<button style="margin-right: 5px" class="btn btn-sm btn-primary">
<i class="feather icon-check-circle"></i><span class="d-none d-sm-inline" style="margin-left: 5px">' .
$buttonName . '</span>
</button>');
}
protected function getModalScript()
{
return <<<JS
var key = {$this->getRow()['address_id']}
$('#address_id').val(key);
JS;
}
public function html()
{
return parent::html(); // TODO: Change the autogenerated stub
}
// 设置请求参数
public function parameters()
{
return [
'action' => $this->action,
'address_id' => $this->getKey()
];
}
}
......@@ -6,6 +6,7 @@ use App\Models\Cms\CmsUser;
use App\Models\Country;
use App\Models\Inquiry;
use App\Models\User;
use App\Models\UserAddress;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Form\Row;
use Dcat\Admin\Traits\LazyWidget;
......@@ -25,10 +26,43 @@ class SaveUserAddressHandle extends Form implements LazyRenderable
public function handle(array $input)
{
try {
$userId = $this->payload['user_id'];
$address = 0;
User::updateById($id, $input);
$addressId = $this->payload['address_id'] ?? 0;
if ($addressId) {
$address = UserAddress::getInfoByAddressId($addressId);
if (empty($address)) {
return $this->response()->error("地址不存在");
}
$input = array_filter($input, function ($value) {
return $value !== null;
});
if ($input['is_default'] == 1) {
UserAddress::updateByUserId($address['user_id'], ['is_default' => 0]);
}
UserAddress::updateById($addressId, $input);
} else {
$userId = $this->payload['user_id'] ?? 0;
if ($input['is_default'] == 1) {
UserAddress::updateByUserId($userId, ['is_default' => 0]);
}
$addressData = [
"user_id" => $userId,
"first_name" => $input["first_name"] ?? "",
"last_name" => $input["last_name"] ?? "",
"address_type" => $input["address_type"] ?? "",
"company_name" => $input["company_name"] ?? "",
"email" => $input["email"] ?? "",
"phone" => $input["phone"] ?? "",
"country" => $input["country"] ?? "",
"province" => $input["province"] ?? "",
"city" => $input["city"] ?? "",
"post_code" => $input["post_code"] ?? "",
"detail_address" => $input["detail_address"] ?? "",
"is_default" => $input["is_default"] ?? "",
"create_time" => time(),
"update_time" => time(),
];
UserAddress::insertData($addressData);
}
return $this
->response()
->success(trans('admin.update_succeeded'))
......@@ -61,7 +95,7 @@ class SaveUserAddressHandle extends Form implements LazyRenderable
});
$this->row(function (Row $row) {
$row->width(3)->select('country')->options(Country::getCountryMap())->required();
$row->width(3)->text('province');
$row->width(3)->text('province')->default('');
$row->width(3)->text('city')->required();
$row->width(2)->text('post_code')->required();
});
......@@ -73,4 +107,18 @@ class SaveUserAddressHandle extends Form implements LazyRenderable
});
}
/**
* The data of the form.
*
* @return array
*/
public function default()
{
$userInfo = UserAddress::getInfoByAddressId($this->payload['address_id'] ?? 0);
if (!$userInfo) {
return [];
}
return $userInfo;
}
}
......@@ -7,6 +7,7 @@ use App\Admin\Actions\User\UserAssignAction;
use App\Admin\Actions\User\UserStatusAction;
use App\Admin\Actions\User\UserTransferAction;
use App\Admin\Actions\UserAddress\UserAddressCreateAction;
use App\Admin\Actions\UserAddress\UserAddressEditAction;
use App\Admin\Repositories\Inquiry;
use App\Admin\Repositories\User;
use App\Admin\Service\UserAddressService;
......@@ -48,8 +49,14 @@ class UserAddress extends \Dcat\Admin\Support\LazyRenderable
$deleteAction = new UserAddressDeleteAction();
$deleteAction->setRow($actions->row);
$deleteAction->setKey($actions->getKey());
$actions->append($deleteAction);
$editAction = new UserAddressEditAction();
$editAction->setKey($actions->row);
$editAction->setRow($actions->row);
$actions->append($editAction);
});
});
}
......
......@@ -28,6 +28,7 @@ use Illuminate\Database\Eloquent\Model;
class UserAddress extends Model
{
use HasDateTimeFormatter;
public $timestamps = false;
protected $table = 'user_address';
......@@ -46,4 +47,20 @@ class UserAddress extends Model
return self::where('address_id', $addressId)->delete();
}
public static function updateById($id, $update)
{
return self::where("address_id", $id)->update($update);
}
public static function updateByUserId($userId, $update)
{
return self::where("user_id", $userId)->update($update);
}
public static function getInfoByAddressId($id)
{
$res = self::where('address_id', $id)->first();
return ($res) ? $res->toArray() : [];
}
}
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