弹出框
需求
有一个需求是设置商户的旗舰店, 商户必须只有一个旗舰店,保存的时候要给个弹框告诉操作者,之前的旗舰店是哪家,要改为哪家。
于是乎就出现了这篇文章。
控制器中的请求是这么写的,重点是js部分
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$form = new Form(new MerchantStore);
$form->hidden('store_id');
$form->text('name', __('门店名称'))->required()->autofocus();
$this->showMerchantName($form,'归属商户');
$form->text('tel', __('门店电话'))->required();
$form->text('business_hours', __('营业时间'))->required();
$form->distpicker(['province_id', 'city_id', 'district_id'],'区域')->required();
$form->text('address', __('详细地址'))->required();
$form->latlong('latitude', 'longitude', '经纬度')->default(['lat' => 31.235929, 'lng' => 121.48054]);
$form->multipleImage('banner_imgs','顶部banner图')->move('images/merchant/store/banner_imgs/')->uniqueName()->removable();
$form->textarea('introduced', __('门店介绍'));
$form->multipleImage('introduced_imgs','门店介绍图片')->move('images/merchant/store/introduced_imgs/')->uniqueName()->removable();;
$form->radio('is_flagship', __('旗舰店'))->options([ 0 => '否',1 => '是'])->required();
// Admin::script('console.log("hello world");');
// swal()
Admin::script(
<<<EOT
//$(".form-horizontal").find("button:reset").hide();
$(".form-horizontal").find("button:submit").attr("type","button");
$(".form-horizontal").find("button:button").on("click", function(){
var store_id = $(".form-horizontal").find("input[name='store_id']").val();
var is_flagship = $(".form-horizontal").find("input[name='is_flagship']:checked").val();
$.get("/admin/ajax/getFlagship",{q:store_id}, function(data){
if(data && is_flagship == 1)
{
swal({
title: data ,
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "确认",
showLoaderOnConfirm: true,
cancelButtonText: "取消",
preConfirm: function() {
$(".form-horizontal").submit();
}
});
}else
{
$(".form-horizontal").submit();
}
});
});
EOT
);
//保存后回调
$form->saved(function (Form $form) {
if($form->is_flagship == 1)
{
MerchantStore::setFlagship($form->store_id,$form->merchant_id);
}
});
return $this->checkIsMy($form);
}
}
设置ajax
1. 设置路由
$router->get('/ajax/getFlagship', 'CommonController@getFlagship'); //获取旗舰店信息
2. 设置CommonController里的getFlagship方法
//获取旗舰店信息
public function getFlagship(Request $request)
{
$id = $request->get('q');
$txt = '';
if (!empty($id))
{
$txt = MerchantStore::getFlagshipMsg($id);
}
return $txt;
}
3. 模型类 MerchantStore
//后台修改旗舰店需要返回一个提示。
public static function getFlagshipMsg($id)
{
$current = self::where([ 'store_id' => $id])->first();
if (!empty($current->merchant_id))
{
$flagship = self::where(['merchant_id'=>$current->merchant_id,'is_flagship'=>1])
->where('store_id', '<>', $id)
->value('name');
if (!empty($flagship))
{
return $txt = '你已设置了【'.$flagship.'】为旗舰店,是否确定修改为【'.$current->name.'】';
}
}
return '';
}
//修改为旗舰店
public static function setFlagship($store_id,$merchant_id)
{
if (empty($store_id))
{
return false;
}
if (!empty($merchant_id))
{
self::where(['merchant_id'=>$merchant_id])->update(['is_flagship'=>0]);
}
return self::where('store_id',$store_id)->update(['is_flagship'=>1]);
}